A ChatGPT based emulation of the therapist Doctor Kernel from the book "A digital Affair" by Neora Shem Shaul
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

doctor.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. logfile.write('\ufeff') # Claim our rightful UTF-8 by land sea and web
  47. except:
  48. slowprint("Couldn't open log file '{}'!!!\n".format(sys.argv[1]))
  49. slowprint("""=====================================================================
  50. Interactive Talk-Program loaded and started.
  51. Users:
  52. * [Doctor Kernel]
  53. * [JJ] <- you
  54. Hit <Enter> to exit
  55. =====================================================================""", logfile)
  56. slowprint("Doctor Kernel: {}\n".format(
  57. conversation.predict(input="Hello again, doctor. Please remind me what we were talking about during our previous sessions.")), logfile)
  58. while True:
  59. try:
  60. prompt = input("JJ: > ").strip()
  61. except EOFError:
  62. prompt = ""
  63. except KeyboardInterrupt: # Might not be caught inside a .sh...
  64. prompt = ""
  65. if not prompt:
  66. slowprint("""
  67. =====================================================================
  68. End of Talk
  69. =====================================================================""",
  70. logfile)
  71. sys.exit(0)
  72. if logfile:
  73. logfile.write("JJ: {}\n\n".format(prompt))
  74. print('') # Skip a line
  75. slowprint("Doctor Kernel: {}\n".format(conversation.predict(input=prompt)), logfile)