Improve error handling and response parsing in Agent.run method

- Add type checking for LLM response
- Handle different response formats (dict with 'choices' and plain string)
- Raise ValueError for unexpected response formats
- Improve error logging with more specific messages
- Enhance robustness of response processing in main agent loop
pull/612/head
Sambhav Dixit 6 months ago committed by GitHub
parent fbf65094a6
commit 65dec85326
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -711,20 +711,25 @@ class Agent:
while attempt < self.retry_attempts and not success: while attempt < self.retry_attempts and not success:
try: try:
if self.long_term_memory is not None: if self.long_term_memory is not None:
logger.info( logger.info("Querying long term memory...")
"Querying long term memory..."
)
self.memory_query(task_prompt) self.memory_query(task_prompt)
# Generate response using LLM
response = self.llm(task_prompt, *args, **kwargs)
# Check if response is a dictionary and has 'choices' key
if isinstance(response, dict) and 'choices' in response:
response = response['choices'][0]['message']['content']
elif isinstance(response, str):
# If response is already a string, use it as is
pass
else: else:
response_args = ( raise ValueError(f"Unexpected response format: {type(response)}")
(task_prompt, *args)
if img is None # Check and execute tools
else (task_prompt, img, *args) if self.tools is not None:
) print(f"self.tools is not None: {response}")
response = self.call_llm( self.parse_and_execute_tools(response)
*response_args, **kwargs
)
# Log the step metadata # Log the step metadata
logged = self.log_step_metadata( logged = self.log_step_metadata(
@ -806,8 +811,7 @@ class Agent:
except Exception as e: except Exception as e:
logger.error( logger.error(
f"Attempt {attempt+1}: Error generating" f"Attempt {attempt+1}: Error generating response: {e}"
f" response: {e}"
) )
attempt += 1 attempt += 1

Loading…
Cancel
Save