A ChatGPT based emulation of the therapist Doctor Kernel from the book "A digital Affair" by Neora Shem Shaul
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

doctor.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from langchain.chat_models import ChatOpenAI
  2. #from langchain.llms import OpenAI
  3. from langchain.chains import ConversationChain
  4. from langchain.memory import ConversationBufferWindowMemory
  5. from langchain.prompts.prompt import PromptTemplate
  6. from langchain.schema import messages_from_dict, messages_to_dict
  7. from time import sleep
  8. import sys
  9. import os
  10. def slowprint(s):
  11. for c in s+'\n':
  12. sys.stdout.write(c)
  13. sys.stdout.flush()
  14. sleep(0.01)
  15. MODEL_NAME = "gpt-3.5-turbo-16k-0613"
  16. # Read prompt template
  17. template = open("prompt-template.txt").read().strip()
  18. PROMPT = PromptTemplate(input_variables=["history", "input"], template=template)
  19. memory = ConversationBufferWindowMemory(ai_prefix="DoctorK", human_prefix="JJ", k=64)
  20. for line in open("book-chats.txt").readlines():
  21. v=[s.strip() for s in line.split(":")]
  22. if len(v)!=2:
  23. continue
  24. if v[0].lower()=="doc":
  25. memory.chat_memory.add_ai_message(v[1])
  26. else:
  27. memory.chat_memory.add_user_message(v[1])
  28. conversation = ConversationChain(
  29. prompt=PROMPT,
  30. llm=ChatOpenAI(model_name=MODEL_NAME),
  31. verbose=False,
  32. memory=memory
  33. )
  34. sys.stderr.write(chr(27) + "[2J") # Clear screen ;)
  35. sys.stderr.flush()
  36. slowprint("""=====================================================================
  37. Interactive Talk-Program loaded and started.
  38. Users:
  39. * [Doctor Kernel]
  40. * [JJ] <- you
  41. =====================================================================""")
  42. slowprint("Doctor Kernel: {}".format(conversation.predict(input="Hello again, doctor. Please remind me what we were talking about last time.")))
  43. while True:
  44. prompt = ""
  45. while not prompt:
  46. try:
  47. prompt = input("> ").strip()
  48. except EOFError:
  49. slowprint("""
  50. =====================================================================
  51. End of Talk
  52. =====================================================================""")
  53. sys.exit(0)
  54. slowprint("\nJJ: {}".format(prompt))
  55. slowprint("Doctor Kernel: {}".format(conversation.predict(input=prompt)))