Humanlearning commited on
Commit
a4c9a4a
·
1 Parent(s): 6387e77

+ error handling

Browse files
Files changed (3) hide show
  1. agents.py +9 -1
  2. app.py +14 -11
  3. system_prompt.txt +5 -1
agents.py CHANGED
@@ -12,6 +12,7 @@ from opentelemetry import trace
12
  from opentelemetry.sdk.trace import TracerProvider
13
  from langfuse import get_client
14
  from rich.pretty import pprint
 
15
 
16
 
17
 
@@ -96,7 +97,14 @@ class LlamaIndexAgent:
96
  # Optionally set trace attributes
97
  span.update_trace(user_id="user_123", input={"query": query})
98
 
99
- response = await self.alfred.run(query)
 
 
 
 
 
 
 
100
 
101
  # Optionally set trace output
102
  span.update_trace(output={"response": str(response)})
 
12
  from opentelemetry.sdk.trace import TracerProvider
13
  from langfuse import get_client
14
  from rich.pretty import pprint
15
+ import aiohttp
16
 
17
 
18
 
 
97
  # Optionally set trace attributes
98
  span.update_trace(user_id="user_123", input={"query": query})
99
 
100
+ try:
101
+ response = await self.alfred.run(query)
102
+ except aiohttp.client_exceptions.ClientConnectionError as e:
103
+ span.update_trace(output={"response": f"Connection error: {e}"})
104
+ return f"AGENT ERROR: Connection error to LLM API: {e}"
105
+ except Exception as e:
106
+ span.update_trace(output={"response": f"General error: {e}"})
107
+ return f"AGENT ERROR: {e}"
108
 
109
  # Optionally set trace output
110
  span.update_trace(output={"response": str(response)})
app.py CHANGED
@@ -5,6 +5,7 @@ import inspect
5
  import pandas as pd
6
  from agents import LlamaIndexAgent
7
  import asyncio
 
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
@@ -58,18 +59,20 @@ async def generate_answers(profile: gr.OAuthProfile | None, progress=gr.Progress
58
  cached_questions = questions_data
59
  total = len(questions_data)
60
  progress(0, desc="Starting answer generation...")
 
61
  async def answer_one(item):
62
- task_id = item.get("task_id")
63
- question_text = item.get("question")
64
- if not task_id or question_text is None:
65
- print(f"Skipping item with missing task_id or question: {item}")
66
- return {"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED"}, None
67
- try:
68
- submitted_answer = await agent.aquery(question_text)
69
- return {"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}, {"task_id": task_id, "submitted_answer": submitted_answer}
70
- except Exception as e:
71
- print(f"Error running agent on task {task_id}: {e}")
72
- return {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}, None
 
73
  tasks = [answer_one(item) for item in questions_data]
74
  results_log = []
75
  answers_payload = []
 
5
  import pandas as pd
6
  from agents import LlamaIndexAgent
7
  import asyncio
8
+ import aiohttp
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
 
59
  cached_questions = questions_data
60
  total = len(questions_data)
61
  progress(0, desc="Starting answer generation...")
62
+ semaphore = asyncio.Semaphore(3) # Limit concurrency to 3
63
  async def answer_one(item):
64
+ async with semaphore:
65
+ task_id = item.get("task_id")
66
+ question_text = item.get("question")
67
+ if not task_id or question_text is None:
68
+ print(f"Skipping item with missing task_id or question: {item}")
69
+ return {"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED"}, None
70
+ try:
71
+ submitted_answer = await agent.aquery(question_text)
72
+ return {"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}, {"task_id": task_id, "submitted_answer": submitted_answer}
73
+ except Exception as e:
74
+ print(f"Error running agent on task {task_id}: {e}")
75
+ return {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}, None
76
  tasks = [answer_one(item) for item in questions_data]
77
  results_log = []
78
  answers_payload = []
system_prompt.txt CHANGED
@@ -1 +1,5 @@
1
- give single word answer and reply.
 
 
 
 
 
1
+ You are a helpful assistant tasked with answering questions using a set of tools.
2
+ Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
3
+ FINAL ANSWER: [YOUR FINAL ANSWER].
4
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
5
+ Your answer should only start with "FINAL ANSWER: ", then follows with the answer.