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

Add support for custom AgentExecutor #56

Closed
1 task
rbbby opened this issue May 22, 2023 · 3 comments · Fixed by #57
Closed
1 task

Add support for custom AgentExecutor #56

rbbby opened this issue May 22, 2023 · 3 comments · Fixed by #57
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@rbbby
Copy link
Contributor

rbbby commented May 22, 2023

In more advanced agents use cases, you often want to have greater control of the loop that controls how agents act.

The langchain AgentExecutor class has a method _take_next_step which is encouraged to be overridden in order to "(...) take control of how the agent makes and acts on choices." (https://github.com/hwchase17/langchain/blob/master/langchain/agents/agent.py#L748)

Trying to do so in conjunction with the newly added agent streaming support raises:
"Error! Chain type 'CustomAgentExecutor' is not currently supported by 'AsyncStreamingResponseCallback'."

To reproduce the error you can run examples/app/zero_shot_agent.py and replace the zero_shot_agent_dependency with a modified version of the function. This function creates a dummy custom subclass of AgentExecutor and initializes it manually rather than using the initialize_agent helper function as it currently does not support custom agent executors.

def zero_shot_agent_dependency() -> Callable[[], AgentExecutor]:
    @lru_cache(maxsize=1)
    def dependency() -> AgentExecutor:
        llm = ChatOpenAI(
            temperature=0,
            streaming=True,
        )
        tools = load_tools(["llm-math"], llm=llm)
        # Additional imports
        from langchain.agents import AgentExecutor
        from langchain.agents.loading import AGENT_TO_CLASS
        # Create dummy custom agent executor
        class CustomAgentExecutor(AgentExecutor):
            pass
        # Initialize agent without initialize_agent helper function
        agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
        agent_cls = AGENT_TO_CLASS[agent]
        agent_obj = agent_cls.from_llm_and_tools(llm, tools)
        agent = CustomAgentExecutor.from_agent_and_tools(
            agent=agent_obj,
            tools=tools,
            )
        return agent

    return dependency

Acceptance Criteria

  • Running a subclass of AgentExecutor no longer raises an error
@rbbby rbbby added the feature New feature or request label May 22, 2023
@ajndkr
Copy link
Owner

ajndkr commented May 22, 2023

Hi! I need to update the error message here but you can already solve this by registering a custom callback handler. You can find the docs for it here: https://lanarky.readthedocs.io/en/latest/advanced/custom_callbacks.html

@rbbby
Copy link
Contributor Author

rbbby commented May 22, 2023

@ajndkr thanks this works if I import register_streaming_callback instead of register_callback as shown in the docs as that raises an error.

Keeping the ticket open in the meantime but feel free to close it when you want.

@ajndkr
Copy link
Owner

ajndkr commented May 22, 2023

you're right! i will update the docs.

@ajndkr ajndkr self-assigned this May 22, 2023
@ajndkr ajndkr added bug Something isn't working documentation Improvements or additions to documentation and removed feature New feature or request labels May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants