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.

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