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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. logfile = None
  11. def slowprint(s, logfile=None):
  12. for c in s+'\n':
  13. sys.stdout.write(c)
  14. sys.stdout.flush()
  15. sleep(0.01)
  16. if logfile:
  17. logfile.write(s+'\n')
  18. MODEL_NAME = "gpt-3.5-turbo-16k-0613"
  19. # Read prompt template
  20. template = open("prompt-template.txt").read().strip()
  21. PROMPT = PromptTemplate(input_variables=["history", "input"], template=template)
  22. memory = ConversationBufferWindowMemory(ai_prefix="DoctorK", human_prefix="JJ", k=64)
  23. for line in open("book-chats.txt").readlines():
  24. v=[s.strip() for s in line.split(":")]
  25. if len(v)!=2:
  26. continue
  27. if v[0].lower()=="doc":
  28. memory.chat_memory.add_ai_message(v[1])
  29. else:
  30. memory.chat_memory.add_user_message(v[1])
  31. conversation = ConversationChain(
  32. prompt=PROMPT,
  33. llm=ChatOpenAI(model_name=MODEL_NAME),
  34. verbose=False,
  35. memory=memory
  36. )
  37. sys.stderr.write("{esc}[2J{esc}[H".format(esc=chr(27))) # Clear screen ;)
  38. sys.stderr.flush()
  39. if len(sys.argv)>1:
  40. try:
  41. logfile = open(sys.argv[1], "w")
  42. except:
  43. slowprint("Couldn't open log file '{}'!!!\n".format(sys.argv[1]))
  44. slowprint("""=====================================================================
  45. Interactive Talk-Program loaded and started.
  46. Users:
  47. * [Doctor Kernel]
  48. * [JJ] <- you
  49. Hit <Enter> to exit
  50. =====================================================================""", logfile)
  51. slowprint("Doctor Kernel: {}\n".format(
  52. conversation.predict(input="Hello again, doctor. Please remind me what we were talking about last time.")), logfile)
  53. while True:
  54. try:
  55. prompt = input("JJ: > ").strip()
  56. except EOFError:
  57. prompt = ""
  58. if not prompt:
  59. slowprint("""
  60. =====================================================================
  61. End of Talk
  62. =====================================================================""")
  63. sys.exit(0)
  64. if logfile:
  65. logfile.write("JJ: {}\n\n".format(prompt))
  66. print('') # Skip a line
  67. slowprint("Doctor Kernel: {}\n".format(conversation.predict(input=prompt)), logfile)