Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final_asnwer as Json #104

Open
lightScout opened this issue Jan 7, 2025 · 3 comments
Open

final_asnwer as Json #104

lightScout opened this issue Jan 7, 2025 · 3 comments

Comments

@lightScout
Copy link

I'm trying to get the agent's final answer in a JSON structure rather than a plain string. While defining the structure within the query works in most cases, it tends to break down in a multi-agent system. Does anyone have suggestions or best practices for handling this?

Thanks!

@grrowl
Copy link
Contributor

grrowl commented Jan 8, 2025

Can you share some example code? I imagine overriding final_answer with a more complex input type would be how you'd approach this?

@lightScout
Copy link
Author

hey @grrowl, thanks for getting back to me on this.
This an example set-up I'm using:

-Agent
agent = CodeAgent( tools=[DuckDuckGoSearchTool()], model=HfApiModel())
response = agent.run(query)
return jsonify({'response': response})

-Input
$task. The final_answer should be a json under this format and conditions. { "title": "string", "answer": "string", "sources": ["list of source URLs or leave empty if the source is you"] }. Ensure all results are combined into a single 'answer' field, even if multiple items are requested. Ensure the output is valid JSON, properly escaped, and formatted without extra characters or invalid tokens. Use double quotes (") for strings.

You mentioned overriding the 'final_answer' tool, can you expand on that please?

@grrowl
Copy link
Contributor

grrowl commented Jan 9, 2025

FinalAnswerTool is defined here and MultiStepAgent always has it baked in. I guess the move would be to extend your own class ComplexMultiStepAgent(MultiStepAgent): and override __init__ to do basically the same thing but add your own FinalAnswerTool to it. You'll note FinalAnswerTool just forwards the input to output -- you might want to json.dumps(answer) in case more of the smolagents plumbing expects str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants