Spaces:
Runtime error
Runtime error
Update Chatbot_new.py
Browse files- Chatbot_new.py +95 -18
Chatbot_new.py
CHANGED
|
@@ -375,8 +375,8 @@ def generate_internal_external_options(chat_history, vignette, user_state):
|
|
| 375 |
|
| 376 |
Present the options in the following format exactly:
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
|
| 381 |
Do not include any additional text or explanation.
|
| 382 |
|
|
@@ -396,10 +396,88 @@ def generate_internal_external_options(chat_history, vignette, user_state):
|
|
| 396 |
return response.choices[0].message.content.strip()
|
| 397 |
|
| 398 |
# Function to generate stable-unstable and global-specific options
|
|
|
|
| 399 |
def generate_stable_unstable_options(chat_history, vignette, previous_choice, user_state):
|
| 400 |
"""
|
| 401 |
-
Generates exactly
|
| 402 |
-
Ensures only lines starting with "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
"""
|
| 404 |
|
| 405 |
# Determine the attribution type based on the user's previous choice
|
|
@@ -411,15 +489,13 @@ def generate_stable_unstable_options(chat_history, vignette, previous_choice, us
|
|
| 411 |
attribution_type = 'Internal' # Default to Internal if unexpected input
|
| 412 |
|
| 413 |
prompt = f"""
|
| 414 |
-
Generate exactly
|
| 415 |
-
1)
|
| 416 |
-
2)
|
| 417 |
-
3) Unstable & Global
|
| 418 |
-
4) Unstable & Specific
|
| 419 |
|
| 420 |
Instructions:
|
| 421 |
-
- Do not label them as
|
| 422 |
-
- Start each line with "
|
| 423 |
- Keep the language simple, pragmatic, and relevant to the scenario:
|
| 424 |
"{vignette}"
|
| 425 |
- Each statement should be concise, neutral, and easy to understand, without extra commentary.
|
|
@@ -453,15 +529,15 @@ Instructions:
|
|
| 453 |
# Split the response into lines
|
| 454 |
lines = content.split('\n')
|
| 455 |
|
| 456 |
-
# Filter lines that start with "
|
| 457 |
-
options = [line.strip() for line in lines if line.strip().startswith(("
|
| 458 |
|
| 459 |
-
# If more than
|
| 460 |
-
if len(options) >
|
| 461 |
-
options = options[:
|
| 462 |
|
| 463 |
-
# If fewer than
|
| 464 |
-
while len(options) <
|
| 465 |
options.append("Option missing due to system error.")
|
| 466 |
|
| 467 |
# Join the options into a single string separated by newlines
|
|
@@ -474,6 +550,7 @@ Instructions:
|
|
| 474 |
return "Error generating options. Please try again."
|
| 475 |
|
| 476 |
|
|
|
|
| 477 |
# First reveal
|
| 478 |
def generate_first_reveal(vignette, user_state):
|
| 479 |
prompt = f"""
|
|
|
|
| 375 |
|
| 376 |
Present the options in the following format exactly:
|
| 377 |
|
| 378 |
+
A: [Specific internal reason]
|
| 379 |
+
B: [Specific external reason]
|
| 380 |
|
| 381 |
Do not include any additional text or explanation.
|
| 382 |
|
|
|
|
| 396 |
return response.choices[0].message.content.strip()
|
| 397 |
|
| 398 |
# Function to generate stable-unstable and global-specific options
|
| 399 |
+
|
| 400 |
def generate_stable_unstable_options(chat_history, vignette, previous_choice, user_state):
|
| 401 |
"""
|
| 402 |
+
Generates exactly two short statements explaining what might have led to a situation.
|
| 403 |
+
Ensures only lines starting with "A:" or "B:" are returned.
|
| 404 |
+
"""
|
| 405 |
+
|
| 406 |
+
# Determine the attribution type based on the user's previous choice
|
| 407 |
+
if previous_choice == '1':
|
| 408 |
+
attribution_type = 'Internal'
|
| 409 |
+
elif previous_choice == '2':
|
| 410 |
+
attribution_type = 'External'
|
| 411 |
+
else:
|
| 412 |
+
attribution_type = 'Internal' # Default to Internal if unexpected input
|
| 413 |
+
|
| 414 |
+
prompt = f"""
|
| 415 |
+
Generate exactly two short statements explaining what might have led to this situation, each on its own line, with no additional comments or explanations. They should reflect a {attribution_type.lower()} cause and cover:
|
| 416 |
+
1) A stable explanation of what might have led to this.
|
| 417 |
+
2) A more temporary/unstable explanation.
|
| 418 |
+
|
| 419 |
+
Instructions:
|
| 420 |
+
- Do not label them as stable/unstable/etc. in the text itself.
|
| 421 |
+
- Start each line with "A:" or "B:" only.
|
| 422 |
+
- Keep the language simple, pragmatic, and relevant to the scenario:
|
| 423 |
+
"{vignette}"
|
| 424 |
+
- Each statement should be concise, neutral, and easy to understand, without extra commentary.
|
| 425 |
+
- Do not include disclaimers, personal info, or text beyond the four statements.
|
| 426 |
+
"""
|
| 427 |
+
|
| 428 |
+
messages = [
|
| 429 |
+
{"role": "system", "content": "You are a strict output assistant. Produce only the specified lines."},
|
| 430 |
+
{"role": "user", "content": prompt},
|
| 431 |
+
]
|
| 432 |
+
|
| 433 |
+
# Append formatted chat history if necessary
|
| 434 |
+
formatted_history = []
|
| 435 |
+
for user_msg, assistant_msg in chat_history:
|
| 436 |
+
if user_msg:
|
| 437 |
+
formatted_history.append({"role": "user", "content": user_msg})
|
| 438 |
+
if assistant_msg:
|
| 439 |
+
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
| 440 |
+
messages += formatted_history
|
| 441 |
+
|
| 442 |
+
try:
|
| 443 |
+
response = openai.chat.completions.create(
|
| 444 |
+
model="gpt-4o-mini",
|
| 445 |
+
messages=messages,
|
| 446 |
+
temperature=0.3,
|
| 447 |
+
max_tokens=300
|
| 448 |
+
)
|
| 449 |
+
|
| 450 |
+
content = response.choices[0].message.content.strip()
|
| 451 |
+
|
| 452 |
+
# Split the response into lines
|
| 453 |
+
lines = content.split('\n')
|
| 454 |
+
|
| 455 |
+
# Filter lines that start with "A:" or "B:"
|
| 456 |
+
options = [line.strip() for line in lines if line.strip().startswith(("A:", "B:"))]
|
| 457 |
+
|
| 458 |
+
# If more than 2 options are present, trim the list to the first two
|
| 459 |
+
if len(options) > 2:
|
| 460 |
+
options = options[:2]
|
| 461 |
+
|
| 462 |
+
# If fewer than 2 options are present, handle accordingly (e.g., add placeholders)
|
| 463 |
+
while len(options) < 2:
|
| 464 |
+
options.append("Option missing due to system error.")
|
| 465 |
+
|
| 466 |
+
# Join the options into a single string separated by newlines
|
| 467 |
+
options_text = "\n".join(options)
|
| 468 |
+
|
| 469 |
+
return options_text
|
| 470 |
+
|
| 471 |
+
except Exception as e:
|
| 472 |
+
print(f"Error generating options: {e}")
|
| 473 |
+
return "Error generating options. Please try again."
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
def generate_global_specific_options(chat_history, vignette, previous_choice, user_state):
|
| 478 |
+
"""
|
| 479 |
+
Generates exactly two short statements explaining what might have led to a situation.
|
| 480 |
+
Ensures only lines starting with "A:" or "B:" are returned.
|
| 481 |
"""
|
| 482 |
|
| 483 |
# Determine the attribution type based on the user's previous choice
|
|
|
|
| 489 |
attribution_type = 'Internal' # Default to Internal if unexpected input
|
| 490 |
|
| 491 |
prompt = f"""
|
| 492 |
+
Generate exactly two short statements explaining what might have led to this situation, each on its own line, with no additional comments or explanations. They should reflect a {attribution_type.lower()} cause and cover:
|
| 493 |
+
1) A broader/global explanation that applies widely.
|
| 494 |
+
2) A more situation-specific explanation.
|
|
|
|
|
|
|
| 495 |
|
| 496 |
Instructions:
|
| 497 |
+
- Do not label them as global/specific/etc. in the text itself.
|
| 498 |
+
- Start each line with "A:" or "B:" only.
|
| 499 |
- Keep the language simple, pragmatic, and relevant to the scenario:
|
| 500 |
"{vignette}"
|
| 501 |
- Each statement should be concise, neutral, and easy to understand, without extra commentary.
|
|
|
|
| 529 |
# Split the response into lines
|
| 530 |
lines = content.split('\n')
|
| 531 |
|
| 532 |
+
# Filter lines that start with "A:" or "B:"
|
| 533 |
+
options = [line.strip() for line in lines if line.strip().startswith(("A:", "B:"))]
|
| 534 |
|
| 535 |
+
# If more than 2 options are present, trim the list to the first two
|
| 536 |
+
if len(options) > 2:
|
| 537 |
+
options = options[:2]
|
| 538 |
|
| 539 |
+
# If fewer than 2 options are present, handle accordingly (e.g., add placeholders)
|
| 540 |
+
while len(options) < 2:
|
| 541 |
options.append("Option missing due to system error.")
|
| 542 |
|
| 543 |
# Join the options into a single string separated by newlines
|
|
|
|
| 550 |
return "Error generating options. Please try again."
|
| 551 |
|
| 552 |
|
| 553 |
+
|
| 554 |
# First reveal
|
| 555 |
def generate_first_reveal(vignette, user_state):
|
| 556 |
prompt = f"""
|