forked from daveshap/Medical_Intake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
116 lines (87 loc) · 3.96 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import re
import openai
from time import time, sleep
from halo import Halo
import textwrap
import yaml
### file operations
def save_file(filepath, content):
with open(filepath, 'w', encoding='utf-8') as outfile:
outfile.write(content)
def open_file(filepath):
with open(filepath, 'r', encoding='utf-8', errors='ignore') as infile:
return infile.read()
### API functions
def chatbot(conversation, model="gpt-4-0613", temperature=0, max_tokens=2000):
max_retry = 7
retry = 0
while True:
try:
spinner = Halo(text='Thinking...', spinner='dots')
spinner.start()
response = openai.ChatCompletion.create(model=model, messages=conversation, temperature=temperature, max_tokens=max_tokens)
text = response['choices'][0]['message']['content']
spinner.stop()
return text, response['usage']['total_tokens']
except Exception as oops:
print(f'\n\nError communicating with OpenAI: "{oops}"')
exit(5)
def chat_print(text):
formatted_lines = [textwrap.fill(line, width=120, initial_indent=' ', subsequent_indent=' ') for line in text.split('\n')]
formatted_text = '\n'.join(formatted_lines)
print('\n\n\nCHATBOT:\n\n%s' % formatted_text)
if __name__ == '__main__':
openai.api_key = open_file('key_openai.txt').strip()
conversation = list()
conversation.append({'role': 'system', 'content': open_file('system_01_intake.md')})
user_messages = list()
all_messages = list()
print('Describe your symptoms to the intake bot. Type DONE when done.')
## INTAKE PORTION
while True:
# get user input
text = input('\n\nPATIENT: ').strip()
if text == 'DONE':
break
user_messages.append(text)
all_messages.append('PATIENT: %s' % text)
conversation.append({'role': 'user', 'content': text})
response, tokens = chatbot(conversation)
conversation.append({'role': 'assistant', 'content': response})
all_messages.append('INTAKE: %s' % response)
print('\n\nINTAKE: %s' % response)
## CHARTING NOTES
print('\n\nGenerating Intake Notes')
conversation = list()
conversation.append({'role': 'system', 'content': open_file('system_02_prepare_notes.md')})
text_block = '\n\n'.join(all_messages)
chat_log = '<<BEGIN PATIENT INTAKE CHAT>>\n\n%s\n\n<<END PATIENT INTAKE CHAT>>' % text_block
save_file('logs/log_%s_chat.txt' % time(), chat_log)
conversation.append({'role': 'user', 'content': chat_log})
notes, tokens = chatbot(conversation)
print('\n\nNotes version of conversation:\n\n%s' % notes)
save_file('logs/log_%s_notes.txt' % time(), notes)
## GENERATING REPORT
print('\n\nGenerating Hypothesis Report')
conversation = list()
conversation.append({'role': 'system', 'content': open_file('system_03_diagnosis.md')})
conversation.append({'role': 'user', 'content': notes})
report, tokens = chatbot(conversation)
save_file('logs/log_%s_diagnosis.txt' % time(), report)
print('\n\nHypothesis Report:\n\n%s' % report)
## CLINICAL EVALUATION
print('\n\nPreparing for Clinical Evaluation')
conversation = list()
conversation.append({'role': 'system', 'content': open_file('system_04_clinical.md')})
conversation.append({'role': 'user', 'content': notes})
clinical, tokens = chatbot(conversation)
save_file('logs/log_%s_clinical.txt' % time(), clinical)
print('\n\nClinical Evaluation:\n\n%s' % clinical)
## REFERRALS & TESTS
print('\n\nGenerating Referrals and Tests')
conversation = list()
conversation.append({'role': 'system', 'content': open_file('system_05_referrals.md')})
conversation.append({'role': 'user', 'content': notes})
referrals, tokens = chatbot(conversation)
save_file('logs/log_%s_referrals.txt' % time(), referrals)
print('\n\nReferrals and Tests:\n\n%s' % referrals)