Skip to content

Commit

Permalink
Enhance llm_output_parser for flexible response handling
Browse files Browse the repository at this point in the history
- Update llm_output_parser method in Agent class
- Add support for parsing dictionary responses with 'choices' key
- Handle plain string responses
- Implement fallback to string conversion for unexpected types
- Improve error logging for parsing failures
  • Loading branch information
sambhavnoobcoder authored Oct 18, 2024
1 parent 65dec85 commit 8328d0d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,20 +1762,21 @@ async def count_tokens_and_subtract_from_context_window(

return response

def llm_output_parser(self, response: Any) -> str:
"""
Parses the response from the LLM (Low-Level Monitor) and returns it as a string.
Args:
response (Any): The response from the LLM.
Returns:
str: The parsed response as a string.
"""
if response is not str:
response = str(response)

return response
def llm_output_parser(self, response):
"""Parse the output from the LLM"""
try:
if isinstance(response, dict):
if 'choices' in response:
return response['choices'][0]['message']['content']
else:
return json.dumps(response) # Convert dict to string
elif isinstance(response, str):
return response
else:
return str(response) # Convert any other type to string
except Exception as e:
logger.error(f"Error parsing LLM output: {e}")
return str(response) # Return string representation as fallback

def log_step_metadata(
self, loop: int, task: str, response: str
Expand Down

0 comments on commit 8328d0d

Please sign in to comment.