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

Pass kwargs from initialize_agent into agent classmethod #799

Merged
merged 9 commits into from
Jan 30, 2023

Conversation

jxnl
Copy link
Contributor

@jxnl jxnl commented Jan 29, 2023

Problem

I noticed that in order to change the prefix of the prompt in the zero-shot-react-description agent
we had to dig around to subset strings deep into the agent's attributes. It requires the user to inspect a long chain of attributes and classes.

initialize_agent -> AgentExecutor -> Agent -> LLMChain -> Prompt from Agent.create_prompt

agent = initialize_agent(
    tools=tools,
    llm=fake_llm,
    agent="zero-shot-react-description"
)
prompt_str = agent.agent.llm_chain.prompt.template
new_prompt_str = change_prefix(prompt_str)
agent.agent.llm_chain.prompt.template = new_prompt_str

Implemented Solution

initialize_agent accepts **kwargs but passes it to AgentExecutor but not ZeroShotAgent, by simply giving the kwargs to the agent class methods we can support changing the prefix and suffix for one agent while allowing future agents to take advantage of initialize_agent.

agent = initialize_agent(
    tools=tools,
    llm=fake_llm,
    agent="zero-shot-react-description",
    agent_kwargs={"prefix": prefix, "suffix": suffix}
)

To be fair, this was before finding docs around custom agents here: https://langchain.readthedocs.io/en/latest/modules/agents/examples/custom_agent.html?highlight=custom%20#custom-llmchain but i find that my use case just needed to change the prefix a little.

Changes

  • Pass kwargs to Agent class method
  • Added a test to check suffix and prefix

@@ -51,7 +51,7 @@ def initialize_agent(
)
agent_cls = AGENT_TO_CLASS[agent]
agent_obj = agent_cls.from_llm_and_tools(
llm, tools, callback_manager=callback_manager
llm, tools, callback_manager=callback_manager, **kwargs
Copy link
Contributor

Choose a reason for hiding this comment

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

these are the same kwargs also passed to the AgentExecutor though right? thats kind of an issue...

could we add another parameter that is agent_kwargs = Optional[dict] = None that we pass through here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@hwchase17 hwchase17 left a comment

Choose a reason for hiding this comment

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

awesome addition - thanks!

@hwchase17 hwchase17 merged commit 54f9e42 into langchain-ai:master Jan 30, 2023
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