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

feat: added on_agent_final_answer-support to Agent callback_manager #5736

Merged
merged 14 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions haystack/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ def __init__(
self.max_steps = max_steps
self.tm = tools_manager or ToolsManager()
self.memory = memory or NoMemory()
self.callback_manager = Events(("on_agent_start", "on_agent_step", "on_agent_finish", "on_new_token"))
self.callback_manager = Events(
("on_agent_start", "on_agent_step", "on_agent_finish", "on_agent_final_answer", "on_new_token")
)
self.prompt_node = prompt_node
prompt_template = prompt_template or prompt_node.default_prompt_template or "zero-shot-react"
resolved_prompt_template = prompt_node.get_prompt_template(prompt_template)
Expand Down Expand Up @@ -290,8 +292,12 @@ def on_agent_start(**kwargs: Any) -> None:
agent_name = kwargs.pop("name", "react")
print_text(f"\nAgent {agent_name} started with {kwargs}\n")

def on_agent_final_answer(final_answer: Dict[str, Any], **kwargs: Any) -> None:
pass

Comment on lines +295 to +297
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's provide a default implementation like for all the others

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZanSara what do you suggest? I did not want to do this because it might mess with the current use-cases of people.

self.tm.callback_manager.on_tool_finish += on_tool_finish
self.callback_manager.on_agent_start += on_agent_start
self.callback_manager.on_agent_final_answer += on_agent_final_answer

if streaming:
self.callback_manager.on_new_token += lambda token, **kwargs: print_text(token, color=agent_color)
Expand Down Expand Up @@ -359,7 +365,9 @@ def run(
agent_step = self._step(query, agent_step, params)
finally:
self.callback_manager.on_agent_finish(agent_step)
return agent_step.final_answer(query=query)
final_answer = agent_step.final_answer(query=query)
self.callback_manager.on_agent_final_answer(final_answer)
return final_answer

def _step(self, query: str, current_step: AgentStep, params: Optional[dict] = None):
# plan next step using the LLM
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
added support for using `on_final_answer` trough `Agent` `callback_manager`