A ChatGPT based emulation of the therapist Doctor Kernel from the book "A digital Affair" by Neora Shem Shaul
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doctor.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. Hit <Enter> to exit
  42. =====================================================================""")
  43. slowprint("Doctor Kernel: {}".format(conversation.predict(input="Hello again, doctor. Please remind me what we were talking about last time.")))
  44. while True:
  45. try:
  46. prompt = input("> ").strip()
  47. except EOFError:
  48. prompt = ""
  49. if not prompt:
  50. slowprint("""
  51. =====================================================================
  52. End of Talk
  53. =====================================================================""")
  54. sys.exit(0)
  55. slowprint("\nJJ: {}".format(prompt))
  56. slowprint("Doctor Kernel: {}".format(conversation.predict(input=prompt)))