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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. logfile)
  64. sys.exit(0)
  65. if logfile:
  66. logfile.write("JJ: {}\n\n".format(prompt))
  67. print('') # Skip a line
  68. slowprint("Doctor Kernel: {}\n".format(conversation.predict(input=prompt)), logfile)