Humanlearning commited on
Commit
33426c9
·
1 Parent(s): a4c9a4a

+ robust error handling

Browse files
Files changed (1) hide show
  1. agents.py +23 -1
agents.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import random
3
  import asyncio
 
4
  from dotenv import load_dotenv
5
  from llama_index.core.agent.workflow import AgentWorkflow
6
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
@@ -13,6 +14,7 @@ from opentelemetry.sdk.trace import TracerProvider
13
  from langfuse import get_client
14
  from rich.pretty import pprint
15
  import aiohttp
 
16
 
17
 
18
 
@@ -86,6 +88,17 @@ class LlamaIndexAgent:
86
  )
87
 
88
 
 
 
 
 
 
 
 
 
 
 
 
89
  async def run_query(self, query: str):
90
  # Instrument LlamaIndex with OpenTelemetry
91
  self.instrumentor.instrument()
@@ -101,7 +114,16 @@ class LlamaIndexAgent:
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}"
 
1
  import os
2
  import random
3
  import asyncio
4
+ import ssl
5
  from dotenv import load_dotenv
6
  from llama_index.core.agent.workflow import AgentWorkflow
7
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
 
14
  from langfuse import get_client
15
  from rich.pretty import pprint
16
  import aiohttp
17
+ from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
18
 
19
 
20
 
 
88
  )
89
 
90
 
91
+ @retry(
92
+ stop=stop_after_attempt(3),
93
+ wait=wait_exponential(multiplier=1, min=4, max=10),
94
+ retry=retry_if_exception_type((
95
+ aiohttp.client_exceptions.ClientConnectionError,
96
+ aiohttp.client_exceptions.ClientOSError,
97
+ ssl.SSLError,
98
+ KeyError,
99
+ ConnectionError
100
+ ))
101
+ )
102
  async def run_query(self, query: str):
103
  # Instrument LlamaIndex with OpenTelemetry
104
  self.instrumentor.instrument()
 
114
  response = await self.alfred.run(query)
115
  except aiohttp.client_exceptions.ClientConnectionError as e:
116
  span.update_trace(output={"response": f"Connection error: {e}"})
117
+ raise # Re-raise for retry logic
118
+ except aiohttp.client_exceptions.ClientOSError as e:
119
+ span.update_trace(output={"response": f"Client OS error: {e}"})
120
+ raise # Re-raise for retry logic
121
+ except ssl.SSLError as e:
122
+ span.update_trace(output={"response": f"SSL error: {e}"})
123
+ raise # Re-raise for retry logic
124
+ except (KeyError, ConnectionError) as e:
125
+ span.update_trace(output={"response": f"Session/Connection error: {e}"})
126
+ raise # Re-raise for retry logic
127
  except Exception as e:
128
  span.update_trace(output={"response": f"General error: {e}"})
129
  return f"AGENT ERROR: {e}"