Added Multi Threading to # 3. Run your Agent (Claude)
Browse files117 unmodified lines118119120121122123124125126127128129130131132133134135136137138139117 unmodified linesreturn f"An unexpected error occurred fetching questions: {e}", None# 3. Run your Agentresults_log = []answers_payload = []print(f"Running agent on {len(questions_data)} questions...")for item in questions_data:task_id = item.get("task_id")question_text = item.get("question")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(question_text)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:print(f"Error running agent on task {task_id}: {e}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})if not answers_payload:print("Agent did not produce any answers to submit.")117 unmodified lines118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166117 unmodified linesreturn f"An unexpected error occurred fetching questions: {e}", None# 3. Run your Agentfrom concurrent.futures import ThreadPoolExecutor, as_completedresults_log = []answers_payload = []print(f"Running agent on {len(questions_data)} questions...")def process_question(item):task_id = item.get("task_id")question_text = item.get("question")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")return None, None, Nonetry:submitted_answer = agent(question_text)return task_id, question_text, submitted_answerexcept Exception as e:print(f"Error running agent on task {task_id}: {e}")return task_id, question_text, f"AGENT ERROR: {e}"with ThreadPoolExecutor(max_workers=5) as executor:futures = {executor.submit(process_question, item): item for item in questions_data}for future in as_completed(futures):task_id, question_text, submitted_answer = future.result()if task_id is None:continueanswers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})# results_log = []# answers_payload = []# print(f"Running agent on {len(questions_data)} questions...")# for item in questions_data:# task_id = item.get("task_id")# question_text = item.get("question")# if not task_id or question_text is None:# print(f"Skipping item with missing task_id or question: {item}")# continue# try:# submitted_answer = agent(question_text)# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})# except Exception as e:# print(f"Error running agent on task {task_id}: {e}")# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})if not answers_payload:print("Agent did not produce any answers to submit.")
app.py+33-6