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

Properly working output handlers for Langchain & LlamaIndex agents #54

Merged
merged 26 commits into from
Jul 1, 2024

Conversation

BespalovSergey
Copy link
Collaborator

No description provided.

Comment on lines 113 to 118
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
)


class LangchainOutputHandlerMixin:

def agent_plane_decorator(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def agent_plane_decorator(self):
def agent_plan_decorator(self):

Comment on lines 17 to 41
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):
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 put these into LangchainOutputHandlingAgentMixin

object.__setattr__(
self._agent,
"_take_next_step",
self.take_next_step_decorator()(self._agent._take_next_step),
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 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)
Copy link
Contributor

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":
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if tool.name == "output_handler":
if tool.name == self.output_handler.name:

Copy link
Contributor

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

return AgentAction(
tool=self._agent_finish_blocker_tool.name,
tool_input=step.return_values,
log="\nUse tool: {}".format(self._agent_finish_blocker_tool.name),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
log="\nUse tool: {}".format(self._agent_finish_blocker_tool.name),
log="\nDetected AgentFinish, blocking it to force output via output handler.\n",

@whimo whimo changed the title Llama index output handler Properly working output handlers for Langchain & LlamaIndex agents Jul 1, 2024
@whimo whimo merged commit 15f9cfb into main Jul 1, 2024
36 checks passed
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

Successfully merging this pull request may close these issues.

2 participants