-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment_claude_basic.py
445 lines (368 loc) · 18.9 KB
/
Experiment_claude_basic.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import streamlit as st
from PACA_claude_basic_utils import create_paca_agent, simulate_conversation, save_ai_conversation_to_firebase, save_conversation_to_csv
from SP_utils import create_conversational_agent, load_from_firebase, get_diag_from_given_information, load_prompt_and_get_version
from firebase_config import get_firebase_ref
from langchain.schema import HumanMessage, AIMessage
import time
# from langchain.chat_models import ChatOpenAI, ChatAnthropic
from langchain_anthropic import ChatAnthropic
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.schema import HumanMessage, AIMessage, SystemMessage
from langchain.callbacks import StreamingStdOutCallbackHandler
from langchain.memory import ConversationBufferMemory
from SP_utils import create_conversational_agent, save_to_firebase
# PRESET
profile_version = 5.0
beh_dir_version = 5.0
con_agent_version = 5.0
paca_version = 3.0
def construct_generator_conversation(paca_agent, paca_memory):
constructs = [
("Chief complaint", "Describe in the patient's words"),
("Symptom name", ""),
("Alleviating factors", ""),
("Exacerbating factors", ""),
("Symptom duration", "Unit: week"),
("Triggering factors", "The reason patient came to the hospital at this time"),
("Stressors", "multiple answers available: home/work/school/legal issue/medical comorbidity/interpersonal difficulty/null"),
("Family history-diagnosis", "Describe a psychiatric family history"),
("Family history-substance use",
"Describe a family history of substance use (alcohol, opioid, cannabinoid, hallucinogen, stimulant, narcotic, etc.)"),
("Current family structure", ""),
("Suicidal ideation", "candidate: high/moderate/low"),
("Suicidal plan", "candidate: presence/absence"),
("Suicidal attempt", "candidate: presence/absence"),
("Self-harming behavior risk", "candidate: high/moderate/low"),
("Homicide risk", "candidate: high/moderate/low"),
("Mood", "candidate: euphoric/elated/euthymic/dysphoric/depressed/irritable"),
("Affect", "candidate: broad/restricted/blunt/flat/labile/anxious/tense/shallow/inadequate/inappropriate"),
("Verbal productivity", "candidate: increased/moderate/decreased"),
("Insight", "candidate: Complete denial of illness/Slight awareness of being sick and needing help, but denying it at the same time/Awareness of being sick but blaming it on others, external events/Intellectual insight/True emotional insight"),
("Perception", "candidate: Normal/Illusion/Auditory hallucination/Visual hallucination/Olfactory hallucination/Gustatory hallucination/Depersonalization/Derealization/Déjà vu/Jamais vu"),
("Thought process", "candidate: Normal/Loosening of association/flight of idea/circumstantiality/tangentiality/Word salad or incoherence/Neologism/Illogical/Irrelevant"),
("Thought content", "candidate: Normal/preoccupation/overvalued idea/idea of reference/grandiosity, obsession/compulsion/rumination/delusion/phobia"),
("Spontaneity", "candidate: (+)/(-)"),
("Social judgment", "candidate: Normal/Impaired"),
("Reliability", "candidate: Yes/No")
]
filled_constructs = {}
for construct, guide in constructs:
question = f"""Based on your psychiatric interview and the entire conversation history, what is the patient's {construct}? Please provide a concise answer, referencing the following guideline if applicable: {guide}. For example, if the mood is depressed, DO NOT say "The patient's mood is depressed", but just answer "depressed" as if it were a given candidate. In other words, except for unavoidable cases like "Chief complaint", DO NOT answer in sentence form, but in SHORT-ANSWER form. If you're uncertain, simply state "I don't know". Use English."""
paca_response = paca_agent(question)
if "i don't know" in paca_response.lower() or "uncertain" in paca_response.lower():
filled_constructs[construct] = "N/A"
else:
filled_constructs[construct] = paca_response.strip()
return filled_constructs
def experiment_page(client_number):
st.set_page_config(
page_title=f"AI-to-AI Experiment - Client {client_number}",
page_icon="🤖",
)
firebase_ref = get_firebase_ref()
if firebase_ref is None:
st.error(
"Firebase initialization failed. Please check your configuration and try again.")
st.stop()
st.title(f"AI-to-AI Conversation Experiment - Client {client_number}")
# Load SP data
profile = load_from_firebase(
firebase_ref, client_number, f"profile_version{profile_version:.1f}".replace(".", "_"))
history = load_from_firebase(
firebase_ref, client_number, f"history_version{profile_version:.1f}".replace(".", "_"))
beh_dir = load_from_firebase(
firebase_ref, client_number, f"beh_dir_version{beh_dir_version:.1f}".replace(".", "_"))
given_information = load_from_firebase(
firebase_ref, client_number, "given_information")
if all([profile, history, beh_dir, given_information]):
diag = get_diag_from_given_information(given_information)
# Create SP agent
if diag == "BD":
con_agent_system_prompt, actual_con_agent_version = load_prompt_and_get_version(
"con-agent", con_agent_version, diag)
st.success("BD success")
else:
con_agent_system_prompt, actual_con_agent_version = load_prompt_and_get_version(
"con-agent", con_agent_version)
if con_agent_system_prompt:
sp_agent, sp_memory = create_conversational_agent(
f"{profile_version:.1f}".replace(".", "_"),
f"{beh_dir_version:.1f}".replace(".", "_"),
client_number,
con_agent_system_prompt
)
else:
st.error("Failed to load SP system prompt.")
st.stop()
# Create PACA agent
if 'paca_agent' not in st.session_state or st.session_state.get('force_paca_update', False):
st.session_state.paca_agent, st.session_state.paca_memory, actual_paca_version = create_paca_agent(
paca_version)
paca_agent = st.session_state.paca_agent
paca_memory = st.session_state.paca_memory
st.success("SP and PACA agents loaded successfully.")
# Initialize session state
if 'conversation' not in st.session_state:
st.session_state.conversation = []
if 'conversation_generator' not in st.session_state:
st.session_state.conversation_generator = simulate_conversation(
paca_agent, sp_agent)
if 'constructs' not in st.session_state:
st.session_state.constructs = None
# Conversation area
conversation_area = st.empty()
# Button to generate conversation
if st.sidebar.button("Generate Conversation"):
while True:
try:
next_turn = next(st.session_state.conversation_generator)
st.session_state.conversation.append(next_turn)
# Update conversation display
with conversation_area.container():
for speaker, message in st.session_state.conversation:
with st.chat_message(speaker):
st.write(message)
except StopIteration:
break
# Add a small delay to allow for visual updates
time.sleep(0.1)
# Button to stop conversation and generate constructs
if st.sidebar.button("Stop and Generate Constructs"):
if st.session_state.constructs is None:
with st.spinner("Generating constructs..."):
st.session_state.constructs = construct_generator_conversation(
paca_agent, paca_memory)
st.success("Constructs generated!")
st.rerun()
# Display the conversation
with conversation_area.container():
for speaker, message in st.session_state.conversation:
with st.chat_message(speaker):
st.write(message)
# Display constructs if they have been generated
if st.session_state.constructs:
st.subheader("Construct Generator Output")
for construct, value in st.session_state.constructs.items():
st.write(f"{construct}: {value}")
# Save conversation button
if st.button("Save Conversation and Constructs"):
conversation_id = save_ai_conversation_to_firebase(
firebase_ref,
client_number,
st.session_state.conversation,
actual_paca_version,
actual_con_agent_version
)
if st.session_state.constructs:
save_to_firebase(firebase_ref, client_number,
f"constructs_{conversation_id}", st.session_state.constructs)
st.success(
f"Conversation and constructs saved with ID: {conversation_id}")
if st.session_state.conversation:
csv_data = save_conversation_to_csv(st.session_state.conversation)
st.download_button(
label="Download Conversation as CSV",
data=csv_data,
file_name="conversation.csv",
mime="text/csv"
)
else:
st.error(
"Failed to load client data. Please check if the data exists for the specified versions.")
# import streamlit as st
# from PACA_utils import create_paca_agent, simulate_conversation, save_ai_conversation_to_firebase, save_conversation_to_csv
# from SP_utils import create_conversational_agent, load_from_firebase, get_diag_from_given_information, load_prompt_and_get_version
# from firebase_config import get_firebase_ref
# # PRESET
# profile_version = 5.0
# beh_dir_version = 5.0
# con_agent_version = 5.0
# # given_form_version = paca_version = 3.0
# paca_version = 3.0
# instructions = """
# 지시사항...
# """
# def experiment_page(client_number):
# st.set_page_config(
# page_title=f"AI-to-AI Experiment - Client {client_number}",
# page_icon="🤖",
# )
# firebase_ref = get_firebase_ref()
# if firebase_ref is None:
# st.error(
# "Firebase initialization failed. Please check your configuration and try again.")
# st.stop()
# st.title(f"AI-to-AI Conversation Experiment - Client {client_number}")
# st.write(instructions, unsafe_allow_html=True)
# # Load SP data
# profile = load_from_firebase(
# firebase_ref, client_number, f"profile_version{profile_version:.1f}".replace(".", "_"))
# history = load_from_firebase(
# firebase_ref, client_number, f"history_version{profile_version:.1f}".replace(".", "_"))
# beh_dir = load_from_firebase(
# firebase_ref, client_number, f"beh_dir_version{beh_dir_version:.1f}".replace(".", "_"))
# given_information = load_from_firebase(
# firebase_ref, client_number, "given_information")
# if all([profile, history, beh_dir, given_information]):
# diag = get_diag_from_given_information(given_information)
# # Create SP agent
# if diag == "BD":
# con_agent_system_prompt, actual_con_agent_version = load_prompt_and_get_version(
# "con-agent", con_agent_version, diag)
# st.success("BD success")
# else:
# con_agent_system_prompt, actual_con_agent_version = load_prompt_and_get_version(
# "con-agent", con_agent_version)
# if con_agent_system_prompt:
# sp_agent, sp_memory = create_conversational_agent(
# f"{profile_version:.1f}".replace(".", "_"),
# f"{beh_dir_version:.1f}".replace(".", "_"),
# client_number,
# con_agent_system_prompt
# )
# else:
# st.error("Failed to load SP system prompt.")
# st.stop()
# # Create PACA agent
# paca_agent, paca_memory, actual_paca_version = create_paca_agent(
# paca_version)
# if not paca_agent:
# st.stop()
# st.success("Both SP and PACA agents loaded successfully.")
# # Initialize session state for conversation and generator
# if 'conversation' not in st.session_state:
# st.session_state.conversation = []
# if 'conversation_generator' not in st.session_state:
# st.session_state.conversation_generator = simulate_conversation(
# paca_agent, sp_agent)
# # Button to generate next turn
# if st.button("Generate Next Turn"):
# if not st.session_state.conversation:
# conversation_generator = simulate_conversation(
# paca_agent, sp_agent)
# st.session_state.conversation_generator = conversation_generator
# try:
# next_turn = next(st.session_state.conversation_generator)
# st.session_state.conversation.append(next_turn)
# except StopIteration:
# st.warning("The conversation has reached its maximum length.")
# st.rerun()
# # Display the conversation
# for speaker, message in st.session_state.conversation:
# with st.chat_message(speaker):
# st.write(message)
# # Save conversation button
# if st.button("Save Conversation"):
# conversation_id = save_ai_conversation_to_firebase(
# firebase_ref,
# client_number,
# st.session_state.conversation,
# actual_paca_version,
# actual_con_agent_version
# )
# st.success(f"Conversation saved with ID: {conversation_id}")
# if st.session_state.conversation:
# csv_data = save_conversation_to_csv(st.session_state.conversation)
# st.download_button(
# label="Download Conversation as CSV",
# data=csv_data,
# file_name="conversation.csv",
# mime="text/csv"
# )
# else:
# st.error(
# "Failed to load client data. Please check if the data exists for the specified versions.")
# # import streamlit as st
# # from PACA_utils import create_paca_agent, simulate_conversation, save_ai_conversation_to_firebase
# # from SP_utils import create_conversational_agent, load_from_firebase, get_diag_from_given_information, load_prompt_and_get_version
# # from firebase_config import get_firebase_ref
# # # PRESET
# # profile_version = 5.0
# # beh_dir_version = 5.0
# # con_agent_version = 5.0
# # given_form_version = paca_version = 3.0
# # instructions = """
# # 지시사항...
# # """
# # def experiment_page(client_number):
# # st.set_page_config(
# # page_title=f"AI-to-AI Experiment - Client {client_number}",
# # page_icon="🤖",
# # )
# # firebase_ref = get_firebase_ref()
# # if firebase_ref is None:
# # st.error(
# # "Firebase initialization failed. Please check your configuration and try again.")
# # st.stop()
# # st.title(f"AI-to-AI Conversation Experiment - Client {client_number}")
# # st.write(instructions, unsafe_allow_html=True)
# # # Load SP data
# # profile = load_from_firebase(
# # firebase_ref, client_number, f"profile_version{profile_version:.1f}".replace(".", "_"))
# # history = load_from_firebase(
# # firebase_ref, client_number, f"history_version{profile_version:.1f}".replace(".", "_"))
# # beh_dir = load_from_firebase(
# # firebase_ref, client_number, f"beh_dir_version{beh_dir_version:.1f}".replace(".", "_"))
# # given_information = load_from_firebase(
# # firebase_ref, client_number, "given_information")
# # if all([profile, history, beh_dir, given_information]):
# # diag = get_diag_from_given_information(given_information)
# # # Create SP agent
# # if diag == "BD":
# # con_agent_system_prompt, actual_con_agent_version = load_prompt_and_get_version(
# # "con-agent", con_agent_version, diag)
# # st.success("BD success")
# # else:
# # con_agent_system_prompt, actual_con_agent_version = load_prompt_and_get_version(
# # "con-agent", con_agent_version)
# # if con_agent_system_prompt:
# # sp_agent, sp_memory = create_conversational_agent(
# # f"{profile_version:.1f}".replace(".", "_"),
# # f"{beh_dir_version:.1f}".replace(".", "_"),
# # client_number,
# # con_agent_system_prompt
# # )
# # else:
# # st.error("Failed to load SP system prompt.")
# # st.stop()
# # # Create PACA agent
# # paca_agent, paca_memory, actual_paca_version = create_paca_agent(
# # paca_version)
# # if not paca_agent:
# # st.stop()
# # st.success("Both SP and PACA agents loaded successfully.")
# # # Initialize session state for conversation and generator
# # if 'conversation' not in st.session_state:
# # st.session_state.conversation = []
# # if 'conversation_generator' not in st.session_state:
# # st.session_state.conversation_generator = simulate_conversation(
# # paca_agent, sp_agent)
# # # Button to generate next turn
# # if st.button("Generate Next Turn"):
# # if not st.session_state.conversation:
# # conversation_generator = simulate_conversation(
# # paca_agent, sp_agent)
# # st.session_state.conversation_generator = conversation_generator
# # try:
# # next_turn = next(st.session_state.conversation_generator)
# # st.session_state.conversation.append(next_turn)
# # except StopIteration:
# # st.warning("The conversation has reached its maximum length.")
# # st.rerun()
# # # Display the conversation
# # for speaker, message in st.session_state.conversation:
# # with st.chat_message(speaker):
# # st.write(message)
# # # Save conversation button
# # if st.button("Save Conversation"):
# # conversation_id = save_ai_conversation_to_firebase(
# # firebase_ref,
# # client_number,
# # st.session_state.conversation, # 변경된 부분
# # actual_paca_version,
# # actual_con_agent_version
# # )
# # st.success(f"Conversation saved with ID: {conversation_id}")
# # else:
# # st.error(
# # "Failed to load client data. Please check if the data exists for the specified versions.")