hemantvirmani commited on
Commit
c9d63bb
·
1 Parent(s): b06fdec

adding hack for 504 error :)

Browse files
Files changed (2) hide show
  1. agents.py +37 -10
  2. config.py +5 -0
agents.py CHANGED
@@ -21,6 +21,7 @@ from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
21
 
22
  from custom_tools import get_custom_tools_list
23
  from system_prompt import SYSTEM_PROMPT
 
24
 
25
  # Suppress BeautifulSoup GuessedAtParserWarning
26
  try:
@@ -101,16 +102,42 @@ class MyLangGraphAgent:
101
  current_step = state.get("step_count", 0) + 1
102
  print(f"[STEP {current_step}] Calling assistant with {len(state['messages'])} messages")
103
 
104
- # Invoke LLM with tools enabled
105
- try:
106
- response = self.llm_client_with_tools.invoke(state["messages"])
107
- except Exception as e:
108
- print(f"[ERROR] LLM invocation failed: {e}")
109
- return {
110
- "messages": [],
111
- "answer": f"Error: LLM failed - {str(e)[:100]}",
112
- "step_count": current_step
113
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  # If no tool calls, set the final answer
116
  if not response.tool_calls:
 
21
 
22
  from custom_tools import get_custom_tools_list
23
  from system_prompt import SYSTEM_PROMPT
24
+ import config
25
 
26
  # Suppress BeautifulSoup GuessedAtParserWarning
27
  try:
 
102
  current_step = state.get("step_count", 0) + 1
103
  print(f"[STEP {current_step}] Calling assistant with {len(state['messages'])} messages")
104
 
105
+ # Invoke LLM with tools enabled, with retry logic for 504 errors
106
+ max_retries = config.MAX_RETRIES
107
+ delay = config.INITIAL_RETRY_DELAY
108
+
109
+ for attempt in range(max_retries + 1):
110
+ try:
111
+ response = self.llm_client_with_tools.invoke(state["messages"])
112
+ # Success - break out of retry loop
113
+ break
114
+ except Exception as e:
115
+ error_msg = str(e)
116
+
117
+ # Check if this is a 504 DEADLINE_EXCEEDED error
118
+ if "504" in error_msg and "DEADLINE_EXCEEDED" in error_msg:
119
+ if attempt < max_retries:
120
+ print(f"[RETRY] Attempt {attempt + 1}/{max_retries} failed with 504 DEADLINE_EXCEEDED")
121
+ print(f"[RETRY] Retrying in {delay:.1f} seconds...")
122
+ time.sleep(delay)
123
+ delay *= config.RETRY_BACKOFF_FACTOR
124
+ continue
125
+ else:
126
+ print(f"[RETRY] All {max_retries} retries exhausted for 504 error")
127
+ print(f"[ERROR] LLM invocation failed after retries: {e}")
128
+ return {
129
+ "messages": [],
130
+ "answer": f"Error: LLM failed after {max_retries} retries - {str(e)[:100]}",
131
+ "step_count": current_step
132
+ }
133
+ else:
134
+ # Not a 504 error - fail immediately without retry
135
+ print(f"[ERROR] LLM invocation failed: {e}")
136
+ return {
137
+ "messages": [],
138
+ "answer": f"Error: LLM failed - {str(e)[:100]}",
139
+ "step_count": current_step
140
+ }
141
 
142
  # If no tool calls, set the final answer
143
  if not response.tool_calls:
config.py CHANGED
@@ -33,3 +33,8 @@ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
33
  GEMINI_MODEL = "gemini-2.5-flash"
34
  GEMINI_TEMPERATURE = 0
35
  GEMINI_MAX_TOKENS = 1024
 
 
 
 
 
 
33
  GEMINI_MODEL = "gemini-2.5-flash"
34
  GEMINI_TEMPERATURE = 0
35
  GEMINI_MAX_TOKENS = 1024
36
+
37
+ # Retry Configuration for 504 DEADLINE_EXCEEDED errors
38
+ MAX_RETRIES = 3
39
+ INITIAL_RETRY_DELAY = 2.0 # seconds
40
+ RETRY_BACKOFF_FACTOR = 2.0