-
Notifications
You must be signed in to change notification settings - Fork 34
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
Properly working output handlers for Langchain & LlamaIndex agents #54
Conversation
motleycrew/agents/crewai/crewai.py
Outdated
create_agent_executor = ModelField( | ||
name="create_agent_executor", type_=Callable, class_validators={}, model_config=BaseConfig | ||
) | ||
self._agent.__fields__["create_agent_executor"] = create_agent_executor | ||
self._agent.create_agent_executor = self._create_agent_executor_decorator()( | ||
self._agent.create_agent_executor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_agent_executor = ModelField( | |
name="create_agent_executor", type_=Callable, class_validators={}, model_config=BaseConfig | |
) | |
self._agent.__fields__["create_agent_executor"] = create_agent_executor | |
self._agent.create_agent_executor = self._create_agent_executor_decorator()( | |
self._agent.create_agent_executor) | |
object.__setattr__( | |
self._agent, | |
"create_agent_executor", | |
self._create_agent_executor_decorator()(self._agent.create_agent_executor) | |
) |
motleycrew/agents/mixins.py
Outdated
|
||
class LangchainOutputHandlerMixin: | ||
|
||
def agent_plane_decorator(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def agent_plane_decorator(self): | |
def agent_plan_decorator(self): |
def _run_tool_direct_decorator(func: Callable): | ||
"""Decorator of the tool's _run method, for intercepting a DirectOutput exception""" | ||
|
||
def wrapper(*args, **kwargs): | ||
try: | ||
result = func(*args, **kwargs) | ||
except DirectOutput as direct_exc: | ||
return direct_exc | ||
return result | ||
|
||
class LangchainMotleyAgent(MotleyAgentParent): | ||
return wrapper | ||
|
||
|
||
def run_tool_direct_decorator(func: Callable): | ||
"""Decorator of the tool's run method, for intercepting a DirectOutput exception""" | ||
|
||
def wrapper(*args, **kwargs): | ||
result = func(*args, **kwargs) | ||
if isinstance(result, DirectOutput): | ||
raise result | ||
return result | ||
|
||
return wrapper | ||
|
||
class LangchainMotleyAgent(MotleyAgentParent, LangchainOutputHandlingAgentMixin): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put these into LangchainOutputHandlingAgentMixin
object.__setattr__( | ||
self._agent, | ||
"_take_next_step", | ||
self.take_next_step_decorator()(self._agent._take_next_step), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make it self.take_next_step_decorator(self._agent._take_next_step)
, no need for another function layer here.
"Agent is not a Runnable, cannot patch it to force output via output handler" | ||
) | ||
object.__setattr__( | ||
self._agent.agent, "plan", self.agent_plan_decorator()(self._agent.agent.plan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as below
|
||
prepared_output_handler = None | ||
for tool in self.agent.tools: | ||
if tool.name == "output_handler": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if tool.name == "output_handler": | |
if tool.name == self.output_handler.name: |
motleycrew/agents/parent.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for _run_and_catch_output
here anymore, let's remove it
motleycrew/agents/mixins.py
Outdated
return AgentAction( | ||
tool=self._agent_finish_blocker_tool.name, | ||
tool_input=step.return_values, | ||
log="\nUse tool: {}".format(self._agent_finish_blocker_tool.name), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log="\nUse tool: {}".format(self._agent_finish_blocker_tool.name), | |
log="\nDetected AgentFinish, blocking it to force output via output handler.\n", |
No description provided.