Skip to content

Commit

Permalink
Improve error handling and response parsing in Agent.run method
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
sambhavnoobcoder authored Oct 18, 2024
1 parent fbf6509 commit 65dec85
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,45 +711,50 @@ def run(
while attempt < self.retry_attempts and not success:
try:
if self.long_term_memory is not None:
logger.info(
"Querying long term memory..."
)
logger.info("Querying long term memory...")
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:
response_args = (
(task_prompt, *args)
if img is None
else (task_prompt, img, *args)
)
response = self.call_llm(
*response_args, **kwargs
)
raise ValueError(f"Unexpected response format: {type(response)}")

# Log the step metadata
logged = self.log_step_metadata(
loop_count, task_prompt, response
)
logger.info(logged)
# Check and execute tools
if self.tools is not None:
print(f"self.tools is not None: {response}")
self.parse_and_execute_tools(response)

# Conver to a str if the response is not a str
response = self.llm_output_parser(
response
)
# Log the step metadata
logged = self.log_step_metadata(
loop_count, task_prompt, response
)
logger.info(logged)

# Print
if self.streaming_on is True:
self.stream_response(response)
else:
print(response)
# Conver to a str if the response is not a str
response = self.llm_output_parser(
response
)

# Add the response to the memory
self.short_memory.add(
role=self.agent_name, content=response
)
# Print
if self.streaming_on is True:
self.stream_response(response)
else:
print(response)

# Add the response to the memory
self.short_memory.add(
role=self.agent_name, content=response
)

# Add to all responses
all_responses.append(response)
# Add to all responses
all_responses.append(response)

# TODO: Implement reliablity check
if self.tools is not None:
Expand Down Expand Up @@ -806,8 +811,7 @@ def run(

except Exception as e:
logger.error(
f"Attempt {attempt+1}: Error generating"
f" response: {e}"
f"Attempt {attempt+1}: Error generating response: {e}"
)
attempt += 1

Expand Down

0 comments on commit 65dec85

Please sign in to comment.