Scott Cogan commited on
Commit
6373484
·
1 Parent(s): d445b1f

requirements update for llm compat

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -330,15 +330,37 @@ class BasicAgent:
330
 
331
  logger.info("Attempting to use primary LLM (Gemini)")
332
  try:
 
 
 
 
 
333
  response = self.primary_llm.invoke(
334
- messages_with_system,
335
  tools=[genai_tool]
336
  )
 
337
  if not response or not hasattr(response, 'content'):
338
  raise ValueError("Invalid response format from Gemini")
339
- logger.info("Successfully used primary LLM")
 
 
 
 
 
 
 
 
 
 
340
  except Exception as e:
341
- if "list index out of range" in str(e):
 
 
 
 
 
 
342
  # Try without tools if tool configuration fails
343
  response = self.primary_llm.invoke(messages_with_system)
344
  if not response or not hasattr(response, 'content'):
@@ -355,9 +377,14 @@ class BasicAgent:
355
  if hasattr(self, 'fallback_llm') and self.fallback_llm is not None:
356
  try:
357
  logger.info("Attempting to use fallback LLM (OpenAI)")
 
 
 
 
 
358
  # For OpenAI, we can use the system message directly
359
  response = self.fallback_llm.invoke(
360
- [self.sys_msg] + messages,
361
  tools=[{
362
  "type": "function",
363
  "function": {
@@ -376,9 +403,19 @@ class BasicAgent:
376
  }
377
  }]
378
  )
 
379
  if not response or not hasattr(response, 'content'):
380
  raise ValueError("Invalid response format from fallback LLM")
381
- logger.info("Successfully used fallback LLM")
 
 
 
 
 
 
 
 
 
382
  except Exception as fallback_error:
383
  logger.error(f"Fallback LLM error: {str(fallback_error)}")
384
  if "429" in str(fallback_error):
 
330
 
331
  logger.info("Attempting to use primary LLM (Gemini)")
332
  try:
333
+ # First try with explicit tool usage prompt
334
+ messages_with_tool_prompt = messages_with_system + [
335
+ HumanMessage(content="Please use the google_search tool to find information about Mercedes Sosa's studio albums between 2000 and 2009.")
336
+ ]
337
+
338
  response = self.primary_llm.invoke(
339
+ messages_with_tool_prompt,
340
  tools=[genai_tool]
341
  )
342
+
343
  if not response or not hasattr(response, 'content'):
344
  raise ValueError("Invalid response format from Gemini")
345
+
346
+ # Check if response contains tool call
347
+ if not hasattr(response, 'tool_calls') or not response.tool_calls:
348
+ # If no tool call, try without tools
349
+ response = self.primary_llm.invoke(messages_with_tool_prompt)
350
+ if not response or not hasattr(response, 'content'):
351
+ raise ValueError("Invalid response format from Gemini")
352
+ logger.info("Successfully used primary LLM without tools")
353
+ else:
354
+ logger.info("Successfully used primary LLM with tools")
355
+
356
  except Exception as e:
357
+ error_str = str(e)
358
+ if "429" in error_str:
359
+ # Handle rate limit
360
+ logger.warning("Rate limit hit for Gemini, waiting before retry...")
361
+ time.sleep(60) # Wait 60 seconds before retry
362
+ raise
363
+ elif "list index out of range" in error_str:
364
  # Try without tools if tool configuration fails
365
  response = self.primary_llm.invoke(messages_with_system)
366
  if not response or not hasattr(response, 'content'):
 
377
  if hasattr(self, 'fallback_llm') and self.fallback_llm is not None:
378
  try:
379
  logger.info("Attempting to use fallback LLM (OpenAI)")
380
+ # Add explicit tool usage prompt
381
+ messages_with_tool_prompt = [self.sys_msg] + messages + [
382
+ HumanMessage(content="Please use the google_search tool to find information about Mercedes Sosa's studio albums between 2000 and 2009.")
383
+ ]
384
+
385
  # For OpenAI, we can use the system message directly
386
  response = self.fallback_llm.invoke(
387
+ messages_with_tool_prompt,
388
  tools=[{
389
  "type": "function",
390
  "function": {
 
403
  }
404
  }]
405
  )
406
+
407
  if not response or not hasattr(response, 'content'):
408
  raise ValueError("Invalid response format from fallback LLM")
409
+
410
+ # Check if response contains tool call
411
+ if not hasattr(response, 'tool_calls') or not response.tool_calls:
412
+ # If no tool call, try without tools
413
+ response = self.fallback_llm.invoke(messages_with_tool_prompt)
414
+ if not response or not hasattr(response, 'content'):
415
+ raise ValueError("Invalid response format from fallback LLM")
416
+ logger.info("Successfully used fallback LLM without tools")
417
+ else:
418
+ logger.info("Successfully used fallback LLM with tools")
419
  except Exception as fallback_error:
420
  logger.error(f"Fallback LLM error: {str(fallback_error)}")
421
  if "429" in str(fallback_error):