Spaces:
Running
Running
File size: 58,591 Bytes
8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 6b699cc 8d1dee0 |
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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 |
import json
import os
import queue
import random
import time
import traceback
from abc import ABC, abstractmethod
from multiprocessing import Process, Queue
import openai
import pandas as pd
import requests
from requests.exceptions import RequestException, Timeout, ConnectionError as RequestsConnectionError
# Set OpenAI API key
# openai.api_key = ""
# Set Chutes AI API key (commented out)
# Use multiprocessing to implement real timeout mechanism
def _timeout_target(queue, func, args, kwargs):
"""multiprocessing target function, must be defined at module level to be pickled"""
try:
result = func(*args, **kwargs)
queue.put(('success', result))
except Exception as e:
tb = traceback.format_exc()
print(f"Exception in subprocess:\n{tb}")
queue.put(('error', f"{type(e).__name__}: {str(e)}\n{tb}"))
def call_with_timeout(func, args, kwargs, timeout_seconds=60):
"""use multiprocessing to implement API call timeout, can force terminate"""
queue = Queue()
process = Process(target=_timeout_target, args=(queue, func, args, kwargs))
process.start()
process.join(timeout_seconds)
if process.is_alive():
# force terminate process
process.terminate()
process.join()
print(
f"API call timed out after {timeout_seconds} seconds and process was terminated")
return {"error": f"API call timed out after {timeout_seconds} seconds"}
try:
result_type, result = queue.get_nowait()
if result_type == 'success':
return result
else:
return {"error": result}
except queue.Empty:
return {"error": "Process completed but no result returned"}
# ======================
# 1. Configuration (Prompt + Schema)
# ======================
# ---- Agent 1 Prompt ----
AGENT_1_PROMPT_TEMPLATE = """\
Please help me construct one item as stimuli for a psycholinguistic experiment based on the description:
Experimental stimuli design: {experiment_design}
Existing stimuli (DO NOT repeat any of these): {previous_stimuli}
Previously rejected stimuli with validation feedback (learn from these failures and avoid similar issues):
{rejected_stimuli}
CRITICAL REQUIREMENTS:
1. Generate a COMPLETELY NEW and UNIQUE stimulus that is DIFFERENT from ALL existing stimuli above.
2. Do NOT repeat or slightly modify any existing stimulus - create something entirely original.
3. Avoid any content that overlaps with existing or rejected stimuli.
4. Learn from the rejected stimuli above - understand why they failed validation and avoid making similar mistakes.
{generation_requirements}
Please return in JSON format.
"""
# ---- Agent 2 Prompt ----
AGENT_2_PROMPT_TEMPLATE = """\
Please verify the following NEW STIMULUS with utmost precision, ensuring they meet the Experimental stimuli design and following strict criteria.
NEW STIMULUS: {new_stimulus};
Experimental stimuli design: {experiment_design}
Please return in JSON format.
"""
# ---- Agent 3 Prompt ----
AGENT_3_PROMPT_TEMPLATE = """\
Please rate the following STIMULUS based on the Experimental stimuli design provided for a psychological experiment:
STIMULUS: {valid_stimulus}
Experimental stimuli design: {experiment_design}
SCORING REQUIREMENTS:
{scoring_requirements}
Please return in JSON format including the score for each dimension within the specified ranges.
"""
# ---- Agent 1 Stimulus Schema ----
AGENT_1_PROPERTIES = {}
# ---- Agent 2 Validation Result Schema ----
AGENT_2_PROPERTIES = {}
# ---- Agent 3 Scoring Result Schema ----
AGENT_3_PROPERTIES = {}
# ======================
# 2. Abstract Model Client Interface
# ======================
class ModelClient(ABC):
"""Abstract base class for model clients"""
@abstractmethod
def generate_completion(self, prompt, properties, params=None):
"""Generate a completion with JSON schema response format"""
pass
@abstractmethod
def get_default_params(self):
"""Get default parameters for this model"""
pass
# ======================
# 3. Concrete Model Client Implementations
# ======================
class OpenAIClient(ModelClient):
"""OpenAI GPT model client"""
def __init__(self, api_key=None):
self.api_key = api_key
if api_key:
openai.api_key = api_key
print("OpenAI API key configured successfully")
else:
print("Warning: No OpenAI API key provided!")
def _api_call(self, prompt, properties, params, api_key):
"""API call function, will be called by multiprocessing"""
# set API key in subprocess
openai.api_key = api_key
return openai.ChatCompletion.create(
model=params["model"],
messages=[{"role": "user", "content": prompt}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "response_schema",
"schema": {
"type": "object",
"properties": properties,
"required": list(properties.keys()),
"additionalProperties": False
}
}
}
)
def generate_completion(self, prompt, properties, params=None):
"""Generate completion using OpenAI API"""
if params is None:
params = self.get_default_params()
# retry mechanism
for attempt in range(3):
try:
response = call_with_timeout(
self._api_call, (prompt, properties, params, self.api_key), {}, 60)
if isinstance(response, dict) and "error" in response:
print(f"OpenAI API timeout attempt {attempt + 1}/3")
if attempt == 2: # last attempt
return {"error": "API timeout after 3 attempts"}
time.sleep(2 ** attempt) # exponential backoff
continue
return json.loads(response['choices'][0]['message']['content'])
except json.JSONDecodeError as e:
print(f"Failed to parse OpenAI JSON response: {e}")
return {"error": f"Failed to parse response: {str(e)}"}
except (openai.error.APIError, openai.error.RateLimitError) as e:
print(f"OpenAI API error attempt {attempt + 1}/3: {e}")
if attempt == 2:
return {"error": f"OpenAI API error after 3 attempts: {str(e)}"}
time.sleep(2 ** attempt)
except openai.error.AuthenticationError as e:
print(f"OpenAI authentication error: {e}")
return {"error": f"Authentication failed: {str(e)}"}
except openai.error.InvalidRequestError as e:
print(f"OpenAI invalid request: {e}")
return {"error": f"Invalid request: {str(e)}"}
def get_default_params(self):
return {"model": "gpt-4o"}
# class HuggingFaceClient(ModelClient):
# """Hugging Face model client"""
# def __init__(self, api_key):
# self.api_key = api_key
# def _api_call(self, messages, response_format, params):
# """API call function that will be called by multiprocessing"""
# client = InferenceClient(
# params["model"],
# token=self.api_key,
# headers={"x-use-cache": "false"}
# )
# return client.chat_completion(
# messages=messages,
# response_format=response_format,
# max_tokens=params.get("max_tokens", 1000),
# temperature=params.get("temperature", 0.7)
# )
# def generate_completion(self, prompt, properties, params=None):
# """Generate completion using Hugging Face API"""
# if params is None:
# params = self.get_default_params()
# response_format = {
# "type": "json_schema",
# "json_schema": {
# "name": "response_schema",
# "schema": {
# "type": "object",
# "properties": properties,
# "required": list(properties.keys()),
# "additionalProperties": False
# }
# }
# }
# messages = [{"role": "user", "content": prompt}]
# # Retry mechanism
# for attempt in range(3):
# try:
# response = call_with_timeout(
# self._api_call, (messages, response_format, params), {}, 60)
# if isinstance(response, dict) and "error" in response:
# print(f"HuggingFace API timeout attempt {attempt + 1}/3")
# if attempt == 2:
# return {"error": "API timeout after 3 attempts"}
# time.sleep(2 ** attempt)
# continue
# content = response.choices[0].message.content
# return json.loads(content)
# except (json.JSONDecodeError, AttributeError, IndexError) as e:
# print(f"Failed to parse HuggingFace JSON response: {e}")
# return {"error": "Failed to parse response"}
# except Exception as e:
# print(f"HuggingFace API error attempt {attempt + 1}/3: {e}")
# if attempt == 2:
# return {"error": f"API error after 3 attempts: {str(e)}"}
# time.sleep(2 ** attempt)
# def get_default_params(self):
# return {
# "model": "meta-llama/Llama-3.3-70B-Instruct",
# }
class CustomModelClient(ModelClient):
"""Custom model client for user-defined APIs"""
def __init__(self, api_url, api_key, model_name):
self.api_url = api_url
self.api_key = api_key
self.model_name = model_name
def _api_call(self, request_data, headers):
"""API call function, will be called by multiprocessing"""
try:
response = requests.post(
self.api_url,
headers=headers,
json=request_data,
timeout=60 # timeout for requests
)
response.raise_for_status()
return response.json()
except Timeout:
raise Timeout(
f"Request to {self.api_url} timed out after 60 seconds")
except RequestsConnectionError as e:
raise RequestsConnectionError(
f"Failed to connect to {self.api_url}: {str(e)}")
except RequestException as e:
raise RequestException(f"Request failed: {str(e)}")
def generate_completion(self, prompt, properties, params=None):
is_deepseek = self.api_url.strip().startswith("https://api.deepseek.com")
if is_deepseek:
rand_stamp = int(time.time())
# Generate field list
field_list = ', '.join([f'"{k}"' for k in properties.keys()])
# Determine agent type
# If starts with "Please verify the following NEW STIMULUS ", then return at the end of prompt, each field can only return boolean value
if prompt.strip().startswith("Please verify the following NEW STIMULUS"):
prompt = prompt.rstrip() + \
f"\nPlease return in strict JSON format, fields must include: {field_list}, requirements for each field are as follows: {properties}, each field can only return boolean values (True/False)"
elif prompt.strip().startswith("Please rate the following STIMULUS"):
prompt = prompt.rstrip() + \
f"\nPlease return in strict JSON format, fields must include: {field_list}, requirements for each field are as follows: {properties}, each field can only return numbers"
else:
prompt = prompt.rstrip() + \
f"\nPlease return in strict JSON format, fields must include: {field_list}, requirements for each field are as follows: {properties}"
request_data = {
"model": self.model_name,
"messages": [
{"role": "system", "content": f"RAND:{rand_stamp}"},
{"role": "user", "content": prompt}
],
"stream": False,
"response_format": {"type": "json_object"}
}
else:
# build base request
request_data = {
"model": self.model_name,
"messages": [{"role": "user", "content": prompt}],
"stream": False,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "response_schema",
"schema": {
"type": "object",
"properties": properties,
"required": list(properties.keys()),
"additionalProperties": False
}
}
}
}
if params is not None:
request_data.update(params)
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
# retry mechanism
for attempt in range(3):
try:
print("Sending request to Custom API with:",
json.dumps(request_data, indent=2))
result = call_with_timeout(
self._api_call, (request_data, headers), {}, 600)
if isinstance(result, dict) and "error" in result:
print(f"Custom API timeout attempt {attempt + 1}/3")
if attempt == 2:
return {"error": "API timeout after 3 attempts"}
time.sleep(2 ** attempt)
continue
print("Response from Custom API:",
json.dumps(result, indent=2))
content = result["choices"][0]["message"]["content"]
return json.loads(content)
except json.JSONDecodeError as e:
print(
f"Custom API JSON parsing error attempt {attempt + 1}/3: {e}")
if attempt == 2:
return {"error": f"API JSON parsing error after 3 attempts: {str(e)}"}
time.sleep(2 ** attempt)
except KeyError as e:
print(
f"Custom API response missing expected key attempt {attempt + 1}/3: {e}")
if attempt == 2:
return {"error": f"API response missing expected key after 3 attempts: {str(e)}"}
time.sleep(2 ** attempt)
except (Timeout, RequestsConnectionError) as e:
print(
f"Custom API connection error attempt {attempt + 1}/3: {e}")
if attempt == 2:
return {"error": f"API connection error after 3 attempts: {str(e)}"}
time.sleep(2 ** attempt)
except RequestException as e:
print(f"Custom API request error attempt {attempt + 1}/3: {e}")
if attempt == 2:
return {"error": f"API request error after 3 attempts: {str(e)}"}
time.sleep(2 ** attempt)
def get_default_params(self):
return {
}
# ======================
# 4. Model Client Factory
# ======================
def create_model_client(model_choice, settings=None):
"""Factory function to create appropriate model client"""
if model_choice == 'GPT-4o':
api_key = settings.get('api_key') if settings else None
return OpenAIClient(api_key)
elif model_choice == 'custom':
if not settings:
raise ValueError("Settings required for custom model")
return CustomModelClient(
api_url=settings.get('apiUrl'),
api_key=settings.get('api_key'),
model_name=settings.get('modelName')
)
# elif model_choice == 'HuggingFace':
# api_key = settings.get('api_key')
# return HuggingFaceClient(api_key)
else:
raise ValueError(f"Unsupported model choice: {model_choice}")
# ======================
# 5. Unified Agent Functions
# ======================
def check_stimulus_repetition(new_stimulus_dict, previous_stimuli_list):
"""
If the value of any key (dimension) in new_stimulus_dict is exactly the same as the corresponding value in any stimulus in previous_stimuli_list, it is considered a repetition.
"""
for existing_stimulus in previous_stimuli_list:
for key, new_value in new_stimulus_dict.items():
# If the key exists in existing_stimulus and the values are the same, it is considered a repetition
if key in existing_stimulus:
try:
existing_val = str(existing_stimulus[key]).lower()
new_val = str(new_value).lower()
if existing_val == new_val:
return True
except (AttributeError, TypeError):
# Skip comparison if values can't be converted to string
continue
return False
def agent_1_generate_stimulus(
model_client,
experiment_design,
previous_stimuli,
properties,
rejected_stimuli=None,
prompt_template=AGENT_1_PROMPT_TEMPLATE,
params=None,
stop_event=None):
"""
Agent 1: Generate new stimulus using the provided model client
"""
if stop_event and stop_event.is_set():
print("Generation stopped by user in agent_1_generate_stimulus.")
return {"stimulus": "STOPPED"}
# Use fixed generation_requirements
generation_requirements = "5. Follow the same JSON format as the existing stimuli."
if rejected_stimuli is None:
rejected_stimuli = []
prompt = prompt_template.format(
experiment_design=experiment_design,
previous_stimuli=previous_stimuli,
rejected_stimuli=rejected_stimuli,
generation_requirements=generation_requirements
)
try:
result = model_client.generate_completion(prompt, properties, params)
# Check stop event again
if stop_event and stop_event.is_set():
print(
"Generation stopped by user after API call in agent_1_generate_stimulus.")
return {"stimulus": "STOPPED"}
if "error" in result:
return {"stimulus": "ERROR/ERROR"}
return result
except (json.JSONDecodeError, KeyError, TypeError) as e:
print(f"Error parsing response in agent_1_generate_stimulus: {e}")
return {"stimulus": "ERROR/ERROR"}
except (RequestException, Timeout) as e:
print(f"Network error in agent_1_generate_stimulus: {e}")
return {"stimulus": "ERROR/ERROR"}
def agent_2_validate_stimulus(
model_client,
new_stimulus,
experiment_design,
properties,
prompt_template=AGENT_2_PROMPT_TEMPLATE,
stop_event=None):
"""
Agent 2: Validate experimental stimulus using the provided model client
"""
if stop_event and stop_event.is_set():
print("Generation stopped by user in agent_2_validate_stimulus.")
return {"error": "Stopped by user"}
prompt = prompt_template.format(
experiment_design=experiment_design,
new_stimulus=new_stimulus
)
try:
# use temperature=0 parameter, get model-specific default params and override temperature
fixed_params = model_client.get_default_params()
fixed_params["temperature"] = 0
result = model_client.generate_completion(
prompt, properties, fixed_params)
print("Agent 2 Output:", result)
# Check stop event again
if stop_event and stop_event.is_set():
print(
"Generation stopped by user after API call in agent_2_validate_stimulus.")
return {"error": "Stopped by user"}
if "error" in result:
print(f"Agent 2 API error: {result}")
return {"error": f"Failed to validate stimulus: {result.get('error', 'Unknown error')}"}
return result
except (json.JSONDecodeError, KeyError, TypeError) as e:
print(f"Error parsing validation response: {e}")
return {"error": f"Failed to parse validation response: {str(e)}"}
except (RequestException, Timeout) as e:
print(f"Network error in validation: {e}")
return {"error": f"Network error during validation: {str(e)}"}
def agent_2_validate_stimulus_individual(
model_client,
new_stimulus,
experiment_design,
properties,
prompt_template=AGENT_2_PROMPT_TEMPLATE,
stop_event=None,
websocket_callback=None):
"""
Agent 2: Validate experimental stimulus by checking each criterion individually
"""
if stop_event and stop_event.is_set():
print("Generation stopped by user in agent_2_validate_stimulus_individual.")
return {"error": "Stopped by user"}
validation_results = {}
# Create individual prompt template for each criterion
individual_prompt_template = """\
Please verify the following NEW STIMULUS with utmost precision for the specific criterion mentioned below.
NEW STIMULUS: {new_stimulus}
Experimental stimuli design: {experiment_design}
SPECIFIC CRITERION TO VALIDATE:
Property: {property_name}
Description: {property_description}
Please return in JSON format with only one field: "{property_name}" (boolean: true if criterion is met, false otherwise).
"""
try:
total_criteria = len(properties)
current_criterion = 0
for property_name, property_description in properties.items():
current_criterion += 1
if stop_event and stop_event.is_set():
print(
f"Generation stopped by user while validating {property_name}.")
return {"error": "Stopped by user"}
if websocket_callback:
websocket_callback(
"validator", f"Validating criterion {current_criterion}/{total_criteria}: {property_name}")
# Create prompt for individual criterion
prompt = individual_prompt_template.format(
new_stimulus=new_stimulus,
experiment_design=experiment_design,
property_name=property_name,
property_description=property_description
)
# Create properties dict with single criterion
single_property = {property_name: property_description}
# Get model-specific default params and override temperature
fixed_params = model_client.get_default_params()
fixed_params["temperature"] = 0
result = model_client.generate_completion(
prompt, single_property, fixed_params)
print(f"Agent 2 Individual Validation - {property_name}: {result}")
if "error" in result:
print(
f"Agent 2 Individual API error for {property_name}: {result}")
if websocket_callback:
websocket_callback(
"validator", f"Error validating criterion {property_name}: {result.get('error', 'Unknown error')}")
return {"error": f"Failed to validate criterion {property_name}: {result.get('error', 'Unknown error')}"}
# Extract the validation result for this criterion
if property_name in result:
validation_results[property_name] = result[property_name]
status = "PASSED" if result[property_name] else "FAILED"
if websocket_callback:
websocket_callback(
"validator", f"Criterion {property_name}: {status}")
# Early stop: if any criterion fails, immediately reject
if not result[property_name]:
if websocket_callback:
websocket_callback(
"validator", f"Early rejection: Criterion {property_name} failed. Stopping validation.")
print(
f"Agent 2 Individual Validation - Early stop: {property_name} failed")
return validation_results
else:
print(
f"Warning: {property_name} not found in result, assuming False")
validation_results[property_name] = False
if websocket_callback:
websocket_callback(
"validator", f"Criterion {property_name}: FAILED (parsing error)")
websocket_callback(
"validator", f"Early rejection: Criterion {property_name} failed. Stopping validation.")
print(
f"Agent 2 Individual Validation - Early stop: {property_name} failed (parsing error)")
return validation_results
print("Agent 2 Individual Validation - All Results:", validation_results)
if websocket_callback:
websocket_callback(
"validator", "All criteria passed successfully!")
return validation_results
except (json.JSONDecodeError, KeyError, TypeError) as e:
print(f"Error parsing individual validation response: {e}")
return {"error": f"Failed to parse validation response: {str(e)}"}
except (RequestException, Timeout) as e:
print(f"Network error in individual validation: {e}")
return {"error": f"Network error during validation: {str(e)}"}
def generate_scoring_requirements(properties):
"""
Generate scoring requirements text from properties dictionary
"""
if not properties:
return "No specific scoring requirements provided."
requirements = []
for aspect_name, aspect_details in properties.items():
min_score = aspect_details.get('minimum', 0)
max_score = aspect_details.get('maximum', 10)
description = aspect_details.get('description', aspect_name)
requirements.append(
f"- {aspect_name}: {description} (Score range: {min_score} to {max_score})")
return "\n".join(requirements)
def agent_3_score_stimulus(
model_client,
valid_stimulus,
experiment_design,
properties,
prompt_template=AGENT_3_PROMPT_TEMPLATE,
stop_event=None):
"""
Agent 3: Score experimental stimulus using the provided model client
"""
if stop_event and stop_event.is_set():
print("Generation stopped by user after API call in agent_3_score_stimulus.")
return {field: 0 for field in properties.keys()} if properties else {}
# Generate scoring requirements text
scoring_requirements = generate_scoring_requirements(properties)
prompt = prompt_template.format(
experiment_design=experiment_design,
valid_stimulus=valid_stimulus,
scoring_requirements=scoring_requirements
)
try:
# use temperature=0 parameter, get model-specific default params and override temperature
fixed_params = model_client.get_default_params()
fixed_params["temperature"] = 0
result = model_client.generate_completion(
prompt, properties, fixed_params)
if stop_event and stop_event.is_set():
print("Generation stopped by user after API call in agent_3_score_stimulus.")
return {field: 0 for field in properties.keys()} if properties else {}
if "error" in result:
print(f"Agent 3 API error: {result}")
return {field: 0 for field in properties.keys()}
return result
except (json.JSONDecodeError, KeyError, TypeError) as e:
print(f"Error parsing scoring response: {e}")
return {field: 0 for field in properties.keys()}
except (RequestException, Timeout) as e:
print(f"Network error in scoring: {e}")
return {field: 0 for field in properties.keys()}
def agent_3_score_stimulus_individual(
model_client,
valid_stimulus,
experiment_design,
properties,
prompt_template=AGENT_3_PROMPT_TEMPLATE,
stop_event=None,
websocket_callback=None):
"""
Agent 3: Score experimental stimulus by evaluating each aspect individually
"""
if stop_event and stop_event.is_set():
print("Generation stopped by user in agent_3_score_stimulus_individual.")
return {field: 0 for field in properties.keys()} if properties else {}
scoring_results = {}
# Create individual prompt template for each aspect
individual_prompt_template = """\
Please rate the following STIMULUS based on the specific aspect mentioned below for a psychological experiment:
STIMULUS: {valid_stimulus}
Experimental stimuli design: {experiment_design}
SPECIFIC ASPECT TO SCORE:
- Aspect Name: {aspect_name}
- Description: {aspect_description}
- Minimum Score: {min_score}
- Maximum Score: {max_score}
- Score Range: You must provide an integer score between {min_score} and {max_score} (inclusive)
SCORING INSTRUCTIONS:
Rate this stimulus on the "{aspect_name}" dimension based on the provided description. Your score should reflect how well the stimulus meets this criterion, with {min_score} being the lowest possible score and {max_score} being the highest possible score.
Please return in JSON format with only one field: "{aspect_name}" (integer score within the specified range {min_score}-{max_score}).
"""
try:
total_aspects = len(properties)
current_aspect = 0
for aspect_name, aspect_details in properties.items():
current_aspect += 1
if stop_event and stop_event.is_set():
print(
f"Generation stopped by user while scoring {aspect_name}.")
return {field: 0 for field in properties.keys()}
if websocket_callback:
websocket_callback(
"scorer", f"Evaluating aspect {current_aspect}/{total_aspects}: {aspect_name}")
# Extract min and max scores from aspect details
min_score = aspect_details.get('minimum', 0)
max_score = aspect_details.get('maximum', 10)
description = aspect_details.get('description', aspect_name)
# Create prompt for individual aspect
prompt = individual_prompt_template.format(
valid_stimulus=valid_stimulus,
experiment_design=experiment_design,
aspect_name=aspect_name,
aspect_description=description,
min_score=min_score,
max_score=max_score
)
# Create properties dict with single aspect (include all details for JSON schema)
single_aspect = {aspect_name: {
'type': 'integer',
'description': description,
'minimum': min_score,
'maximum': max_score
}}
# Get model-specific default params and override temperature
fixed_params = model_client.get_default_params()
fixed_params["temperature"] = 0
result = model_client.generate_completion(
prompt, single_aspect, fixed_params)
print(f"Agent 3 Individual Scoring - {aspect_name}: {result}")
if "error" in result:
print(
f"Agent 3 Individual API error for {aspect_name}: {result}")
if websocket_callback:
websocket_callback(
"scorer", f"Error scoring aspect {aspect_name}: {result.get('error', 'Unknown error')}")
scoring_results[aspect_name] = 0
continue
# Extract the scoring result for this aspect
if aspect_name in result:
score = result[aspect_name]
# Ensure score is within valid range
if isinstance(score, (int, float)):
score = max(min_score, min(max_score, int(score)))
scoring_results[aspect_name] = score
if websocket_callback:
websocket_callback(
"scorer", f"Aspect {aspect_name}: {score}/{max_score}")
else:
print(
f"Warning: Invalid score for {aspect_name}, assuming 0")
scoring_results[aspect_name] = 0
if websocket_callback:
websocket_callback(
"scorer", f"Aspect {aspect_name}: 0/{max_score} (invalid response)")
else:
print(
f"Warning: {aspect_name} not found in result, assuming 0")
scoring_results[aspect_name] = 0
if websocket_callback:
websocket_callback(
"scorer", f"Aspect {aspect_name}: 0/{max_score} (parsing error)")
print("Agent 3 Individual Scoring - All Results:", scoring_results)
if websocket_callback:
total_score = sum(scoring_results.values())
max_possible = sum(aspect_details.get('maximum', 10)
for aspect_details in properties.values())
websocket_callback(
"scorer", f"Individual scoring completed! Total: {total_score}/{max_possible}")
return scoring_results
except (json.JSONDecodeError, KeyError, TypeError) as e:
print(f"Error parsing individual scoring response: {e}")
return {field: 0 for field in properties.keys()}
except (RequestException, Timeout) as e:
print(f"Network error in individual scoring: {e}")
return {field: 0 for field in properties.keys()}
# ======================
# 6. Main Flow Function
# ======================
def generate_stimuli(settings):
stop_event = settings['stop_event']
current_iteration = settings['current_iteration']
total_iterations = settings['total_iterations']
experiment_design = settings['experiment_design']
previous_stimuli = settings['previous_stimuli'] if settings['previous_stimuli'] else [
]
model_choice = settings.get('model_choice', 'GPT-4o')
ablation = settings.get('ablation', {
"use_agent_2": True,
"use_agent_3": True
})
repetition_count = 0
validation_fails = 0
# Get custom parameters for custom model
custom_params = settings.get('params', None)
# Get session_update_callback function and websocket_callback function
session_update_callback = settings.get('session_update_callback')
websocket_callback = settings.get('websocket_callback')
# Ensure progress value is correctly initialized
with current_iteration.get_lock(), total_iterations.get_lock():
current_iteration.value = 0
total_iterations.value = settings['iteration']
# Immediately send correct initial progress
if session_update_callback:
session_update_callback()
# Check stop event at each critical point
def check_stop(message="Generation stopped by user."):
if stop_event.is_set():
print(message)
if websocket_callback:
websocket_callback("all", message)
return True
return False
# Helper function to create partial result when error or stop occurs
def create_partial_result(record_list, message, is_error=True):
nonlocal total_iterations
if len(record_list) > 0:
df = pd.DataFrame(record_list)
session_id = settings.get('session_id', 'default')
timestamp = int(time.time())
unique_id = ''.join(random.choice('0123456789abcdef')
for _ in range(6))
suffix = "_partial" if is_error else "_stopped"
suggested_filename = f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}{suffix}.csv"
df['generation_timestamp'] = timestamp
df['batch_id'] = unique_id
df['total_iterations'] = total_iterations.value
df['stopped_by_user'] = not is_error
df['error_occurred'] = is_error
df['message'] = message
df['completed_iterations'] = len(record_list)
os.makedirs("outputs", exist_ok=True)
suggested_filename = os.path.join("outputs", suggested_filename)
return df, suggested_filename
return None, None
# Helper function to check stop and return partial data if available
def check_stop_and_return(message="Generation stopped by user."):
if stop_event.is_set():
print(message)
if websocket_callback:
websocket_callback("all", message)
return True, create_partial_result(record_list, message, is_error=False)
return False, (None, None)
# Immediately check if stopped
if check_stop("Generation stopped before starting."):
return None, None
record_list = []
rejected_stimuli_memory = []
agent_1_properties = settings.get('agent_1_properties', {})
print("Agent 1 Properties:", agent_1_properties)
if websocket_callback:
websocket_callback(
"setup", f"Agent 1 Properties: {agent_1_properties}")
if check_stop():
return None, None
agent_2_properties = settings.get('agent_2_properties', {})
print("Agent 2 Properties:", agent_2_properties)
if websocket_callback:
websocket_callback(
"setup", f"Agent 2 Properties: {agent_2_properties}")
if check_stop():
return None, None
agent_3_properties = settings.get('agent_3_properties', {})
print("Agent 3 Properties:", agent_3_properties)
if websocket_callback:
websocket_callback(
"setup", f"Agent 3 Properties: {agent_3_properties}")
if check_stop():
return None, None
# Create model client using factory
try:
model_client = create_model_client(model_choice, settings)
print(f"Using model: {model_choice}")
if websocket_callback:
websocket_callback("setup", f"Using model: {model_choice}")
except Exception as e:
error_msg = f"Failed to create model client: {str(e)}"
print(error_msg)
if websocket_callback:
websocket_callback("setup", error_msg)
return None, None
if check_stop():
return None, None
# Create a function specifically for updating progress
def update_progress(completed_iterations):
if check_stop():
return
with current_iteration.get_lock(), total_iterations.get_lock():
current_value = min(completed_iterations, total_iterations.value)
if current_value > current_iteration.value:
current_iteration.value = current_value
if session_update_callback:
session_update_callback()
# Get actual total iterations
total_iter_value = total_iterations.value
for iteration_num in range(total_iter_value):
stopped, partial_result = check_stop_and_return()
if stopped:
return partial_result
round_message = f"=== No. {iteration_num + 1} Round ==="
print(round_message)
if websocket_callback:
websocket_callback("all", round_message)
# Step 1: Generate stimulus
current_retry_count = 0 # Retry counter for this iteration
while True:
stopped, partial_result = check_stop_and_return()
if stopped:
return partial_result
try:
stimuli = agent_1_generate_stimulus(
model_client=model_client,
experiment_design=experiment_design,
previous_stimuli=previous_stimuli,
properties=agent_1_properties,
rejected_stimuli=rejected_stimuli_memory,
prompt_template=AGENT_1_PROMPT_TEMPLATE,
params=custom_params,
stop_event=stop_event
)
if isinstance(stimuli, dict) and stimuli.get('stimulus') == 'STOPPED':
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Generator'.")
if stopped:
return partial_result
# Skip validation if Agent 1 returned an error
if isinstance(stimuli, dict) and stimuli.get('stimulus') == 'ERROR/ERROR':
print("Agent 1 returned ERROR, regenerating...")
if websocket_callback:
websocket_callback(
"generator", "Generator returned ERROR, regenerating...")
continue
print("Agent 1 Output:", stimuli)
if websocket_callback:
websocket_callback(
"generator", f"Generator's Output: {json.dumps(stimuli, indent=2)}")
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Generator'.")
if stopped:
return partial_result
# Step 1.5: Check if stimulus already exists
if check_stimulus_repetition(stimuli, previous_stimuli):
repetition_count += 1
current_retry_count += 1
# Add retry limit to avoid infinite loops (but never accept duplicates)
max_repetition_retries = 50
if current_retry_count > max_repetition_retries:
error_msg = f"Failed to generate unique stimulus after {max_repetition_retries} attempts. Consider adjusting experiment design or reducing target count."
print(error_msg)
if websocket_callback:
websocket_callback("generator", error_msg)
# Return partial results instead of raising exception
return create_partial_result(record_list, error_msg)
if ablation["use_agent_2"]:
print("Detected repeated stimulus, regenerating...")
if websocket_callback:
websocket_callback(
"generator", "Detected repeated stimulus, regenerating...")
continue
else:
print(
"Ablation: Skipping Agent 2 (Repetition Check)")
if websocket_callback:
websocket_callback(
"generator", "Ablation: Skipping Agent 2 (Repetition Check)")
stopped, partial_result = check_stop_and_return()
if stopped:
return partial_result
# Step 2: Validate stimulus
# Check if individual validation is enabled
individual_validation = settings.get(
'agent_2_individual_validation', False)
if individual_validation:
if websocket_callback:
websocket_callback(
"validator", f"Using individual validation mode - checking {len(agent_2_properties)} criteria...")
validation_result = agent_2_validate_stimulus_individual(
model_client=model_client,
new_stimulus=stimuli,
experiment_design=experiment_design,
properties=agent_2_properties,
stop_event=stop_event,
websocket_callback=websocket_callback
)
else:
if websocket_callback:
websocket_callback(
"validator", "Using batch validation mode...")
validation_result = agent_2_validate_stimulus(
model_client=model_client,
new_stimulus=stimuli,
experiment_design=experiment_design,
properties=agent_2_properties,
prompt_template=AGENT_2_PROMPT_TEMPLATE,
stop_event=stop_event
)
if isinstance(validation_result, dict) and validation_result.get('error') == 'Stopped by user':
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Validator'.")
if stopped:
return partial_result
print("Agent 2 Output:", validation_result)
if websocket_callback:
websocket_callback(
"validator", f"Validator's Output: {json.dumps(validation_result, indent=2)}")
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Validator'.")
if stopped:
return partial_result
# Check if there was an error first
if 'error' in validation_result:
print(f"Validation error: {validation_result['error']}")
if websocket_callback:
websocket_callback(
"validator", f"Validation error: {validation_result['error']}")
continue # Skip to next iteration
# Check validation fields
failed_fields = [
key for key, value in validation_result.items() if not value]
if failed_fields:
# Some fields failed validation
validation_fails += 1
current_retry_count += 1
# Add to rejected memory (only if it's a valid stimulus, not an error)
is_error_stimulus = (
isinstance(stimuli, dict) and
stimuli.get('stimulus') in ['ERROR/ERROR', 'STOPPED']
)
if not is_error_stimulus:
rejected_item = {
"stimulus": stimuli,
"validation_result": validation_result,
"failed_fields": failed_fields
}
rejected_stimuli_memory.append(rejected_item)
# Limit memory size to prevent unbounded growth
MAX_REJECTED_MEMORY = 20
if len(rejected_stimuli_memory) > MAX_REJECTED_MEMORY:
rejected_stimuli_memory = rejected_stimuli_memory[-MAX_REJECTED_MEMORY:]
print(
f"Failed validation for fields: {failed_fields}, regenerating...")
if websocket_callback:
websocket_callback(
"validator", f"Failed validation for fields: {failed_fields}, regenerating...")
# Check retry limit to avoid infinite loops
max_retries = 50
if current_retry_count > max_retries:
error_msg = f"Failed to generate valid stimulus after {max_retries} attempts. Consider adjusting validation criteria."
print(error_msg)
if websocket_callback:
websocket_callback("validator", error_msg)
# Return partial results instead of raising exception
return create_partial_result(record_list, error_msg)
if ablation["use_agent_2"]:
continue # Regenerate
else:
print("Ablation: Skipping Agent 2 (Validation)")
if websocket_callback:
websocket_callback(
"validator", "Ablation: Skipping Agent 2 (Validation)")
update_progress(iteration_num + 1)
break
else:
# All validations passed
print("All validations passed, proceeding to next step...")
if websocket_callback:
websocket_callback(
"validator", "All validations passed, proceeding to next step...")
update_progress(iteration_num + 1)
break
except Exception as e:
error_msg = f"Error in generation/validation step: {str(e)}"
print(error_msg)
if websocket_callback:
websocket_callback("all", error_msg)
if len(record_list) > 0:
df = pd.DataFrame(record_list)
session_id = settings.get('session_id', 'default')
timestamp = int(time.time())
unique_id = ''.join(random.choice(
'0123456789abcdef') for _ in range(6))
suggested_filename = f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}_error.csv"
df['generation_timestamp'] = timestamp
df['batch_id'] = unique_id
df['total_iterations'] = total_iter_value
df['error_occurred'] = True
df['error_message'] = str(e)
os.makedirs("outputs", exist_ok=True)
suggested_filename = os.path.join(
"outputs", f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}.csv")
return df, suggested_filename
else:
raise e
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Validator'.")
if stopped:
return partial_result
try:
stopped, partial_result = check_stop_and_return(
"Generation stopped before Scorer.")
if stopped:
return partial_result
# Step 3: Score
if ablation["use_agent_3"]:
# Check if individual scoring is enabled
individual_scoring = settings.get(
'agent_3_individual_scoring', False)
if individual_scoring:
if websocket_callback:
websocket_callback(
"scorer", f"Using individual scoring mode - evaluating {len(agent_3_properties)} aspects...")
scores = agent_3_score_stimulus_individual(
model_client=model_client,
valid_stimulus=stimuli,
experiment_design=experiment_design,
properties=agent_3_properties,
stop_event=stop_event,
websocket_callback=websocket_callback
)
else:
if websocket_callback:
websocket_callback(
"scorer", "Using batch scoring mode...")
scores = agent_3_score_stimulus(
model_client=model_client,
valid_stimulus=stimuli,
experiment_design=experiment_design,
properties=agent_3_properties,
prompt_template=AGENT_3_PROMPT_TEMPLATE,
stop_event=stop_event
)
if isinstance(scores, dict) and all(v == 0 for v in scores.values()):
if stop_event.is_set():
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Scorer'.")
if stopped:
return partial_result
print("Agent 3 Output:", scores)
if websocket_callback:
websocket_callback(
"scorer", f"Scorer's Output: {json.dumps(scores, indent=2)}")
stopped, partial_result = check_stop_and_return(
"Generation stopped after 'Scorer'.")
if stopped:
return partial_result
else:
print("Ablation: Skipping Agent 3 (Scoring)")
if websocket_callback:
websocket_callback("scorer", "Ablation: Skipping Agent 3")
# Save results
record = {
"stimulus_id": iteration_num + 1,
"stimulus_content": stimuli,
"repetition_count": repetition_count,
"validation_fails": validation_fails,
"validation_failure_reasons": validation_result
}
if ablation["use_agent_3"]:
record.update(scores or {})
record_list.append(record)
# Update previous_stimuli
previous_stimuli.append(stimuli)
# If some records have been generated, create intermediate results
if (iteration_num + 1) % 5 == 0 or iteration_num + 1 == total_iter_value:
temp_df = pd.DataFrame(record_list)
session_id = settings.get('session_id', 'default')
timestamp = int(time.time())
unique_id = ''.join(random.choice('0123456789abcdef')
for _ in range(6))
suggested_filename = f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}.csv"
temp_df['generation_timestamp'] = timestamp
temp_df['batch_id'] = unique_id
temp_df['total_iterations'] = total_iter_value
if check_stop():
return temp_df, suggested_filename
if iteration_num + 1 == total_iter_value:
update_progress(total_iter_value)
return temp_df, suggested_filename
except Exception as e:
error_msg = f"Error in scoring step: {str(e)}"
print(error_msg)
if websocket_callback:
websocket_callback("all", error_msg)
if len(record_list) > 0:
df = pd.DataFrame(record_list)
session_id = settings.get('session_id', 'default')
timestamp = int(time.time())
unique_id = ''.join(random.choice('0123456789abcdef')
for _ in range(6))
suggested_filename = f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}_error.csv"
df['generation_timestamp'] = timestamp
df['batch_id'] = unique_id
df['total_iterations'] = total_iter_value
df['error_occurred'] = True
df['error_message'] = str(e)
return df, suggested_filename
else:
raise e
# Check again if stopped at final step
if check_stop("Generation stopped at final step."):
if len(record_list) > 0:
df = pd.DataFrame(record_list)
session_id = settings.get('session_id', 'default')
timestamp = int(time.time())
unique_id = ''.join(random.choice('0123456789abcdef')
for _ in range(6))
suggested_filename = f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}.csv"
df['generation_timestamp'] = timestamp
df['batch_id'] = unique_id
df['total_iterations'] = total_iter_value
df['error_occurred'] = False
df['error_message'] = ""
completion_msg = f"Data generation completed for session {session_id}"
print(completion_msg)
if websocket_callback:
websocket_callback("all", completion_msg)
return df, suggested_filename
return None, None
# Only generate DataFrame and return results after all iterations
if len(record_list) > 0:
update_progress(total_iter_value)
df = pd.DataFrame(record_list)
session_id = settings.get('session_id', 'default')
timestamp = int(time.time())
unique_id = ''.join(random.choice('0123456789abcdef')
for _ in range(6))
suggested_filename = f"experiment_stimuli_results_{session_id}_{timestamp}_{unique_id}.csv"
df['generation_timestamp'] = timestamp
df['batch_id'] = unique_id
df['total_iterations'] = total_iter_value
df['error_occurred'] = False
df['error_message'] = ""
completion_msg = f"Data generation completed for session {session_id}"
print(completion_msg)
if websocket_callback:
websocket_callback("all", completion_msg)
return df, suggested_filename
else:
print("No records generated.")
if websocket_callback:
websocket_callback("all", "No records generated.")
return None, None
# ======================
# 7. Legacy Support Function (maintain backward compatibility)
# ======================
def custom_model_inference_handler(session_id, prompt, model, api_url, api_key, params=None):
"""Legacy function for backward compatibility"""
try:
client = CustomModelClient(api_url, api_key, model)
result = client.generate_completion(prompt, {}, params)
if "error" in result:
return {'error': result["error"]}, 500
return {'response': json.dumps(result)}, 200
except Exception as e:
return {'error': f'Unexpected error: {str(e)}'}, 500
|