Scott Cogan commited on
Commit
ea9d55b
·
1 Parent(s): b3920f7

requirements update for llm compat

Browse files
Files changed (1) hide show
  1. app.py +43 -37
app.py CHANGED
@@ -328,59 +328,65 @@ class BasicAgent:
328
  }]
329
  )
330
 
 
331
  response = self.primary_llm.invoke(
332
  messages_with_system,
333
  tools=[genai_tool]
334
  )
 
 
335
  except Exception as e:
336
  error_str = str(e)
337
- if "429" in error_str:
338
- if "GenerateRequestsPerDayPerProjectPerModel-FreeTier" in error_str:
339
- logger.warning("Daily quota limit reached for primary LLM, trying fallback")
340
- if hasattr(self, 'fallback_llm') and self.fallback_llm is not None:
341
- try:
342
- # For OpenAI, we can use the system message directly
343
- response = self.fallback_llm.invoke(
344
- [self.sys_msg] + messages,
345
- tools=[{
346
- "type": "function",
347
- "function": {
348
- "name": "google_search",
349
- "description": "Search for information on the web",
350
- "parameters": {
351
- "type": "object",
352
- "properties": {
353
- "query": {
354
- "type": "string",
355
- "description": "The search query"
356
- }
357
- },
358
- "required": ["query"]
359
  }
360
- }
361
- }]
362
- )
363
- logger.info("Successfully used fallback LLM")
364
- except Exception as fallback_error:
365
- logger.error(f"Fallback LLM also failed: {str(fallback_error)}")
366
- return {
367
- "messages": [AIMessage(content="All LLM services are currently unavailable. Please try again later.")],
368
- "next": END
369
  }
 
 
 
 
 
 
 
 
 
 
370
  else:
371
- logger.warning("No fallback LLM available")
372
  return {
373
- "messages": [AIMessage(content="I've reached my daily limit for processing requests. Please try again tomorrow or contact support for assistance.")],
374
  "next": END
375
  }
376
- else:
377
- # For other rate limits, wait and retry
 
378
  wait_time = 60 * (retry_count + 1) # Exponential backoff
379
  logger.warning(f"Rate limit hit, waiting {wait_time} seconds before retry...")
380
  time.sleep(wait_time)
381
  raise # Re-raise to trigger retry
382
- else:
383
- raise
384
 
385
  logger.info("\n=== Model Output ===")
386
  log_message(response, " ")
 
328
  }]
329
  )
330
 
331
+ logger.info("Attempting to use primary LLM (Gemini)")
332
  response = self.primary_llm.invoke(
333
  messages_with_system,
334
  tools=[genai_tool]
335
  )
336
+ logger.info("Successfully used primary LLM")
337
+
338
  except Exception as e:
339
  error_str = str(e)
340
+ logger.error(f"Primary LLM error: {error_str}")
341
+
342
+ # Check if we should try fallback
343
+ if (hasattr(self, 'fallback_llm') and self.fallback_llm is not None and
344
+ ("429" in error_str or "object" in error_str or "string" in error_str)):
345
+ try:
346
+ logger.info("Attempting to use fallback LLM (OpenAI)")
347
+ # For OpenAI, we can use the system message directly
348
+ response = self.fallback_llm.invoke(
349
+ [self.sys_msg] + messages,
350
+ tools=[{
351
+ "type": "function",
352
+ "function": {
353
+ "name": "google_search",
354
+ "description": "Search for information on the web",
355
+ "parameters": {
356
+ "type": "object",
357
+ "properties": {
358
+ "query": {
359
+ "type": "string",
360
+ "description": "The search query"
 
361
  }
362
+ },
363
+ "required": ["query"]
364
+ }
 
 
 
 
 
 
365
  }
366
+ }]
367
+ )
368
+ logger.info("Successfully used fallback LLM")
369
+ except Exception as fallback_error:
370
+ logger.error(f"Fallback LLM error: {str(fallback_error)}")
371
+ if "429" in str(fallback_error):
372
+ return {
373
+ "messages": [AIMessage(content="All LLM services are currently rate limited. Please try again later.")],
374
+ "next": END
375
+ }
376
  else:
 
377
  return {
378
+ "messages": [AIMessage(content="All LLM services are currently unavailable. Please try again later.")],
379
  "next": END
380
  }
381
+ else:
382
+ # If no fallback available or error not related to rate limits
383
+ if "429" in error_str:
384
  wait_time = 60 * (retry_count + 1) # Exponential backoff
385
  logger.warning(f"Rate limit hit, waiting {wait_time} seconds before retry...")
386
  time.sleep(wait_time)
387
  raise # Re-raise to trigger retry
388
+ else:
389
+ raise
390
 
391
  logger.info("\n=== Model Output ===")
392
  log_message(response, " ")