langchain/tests/unit_tests
Jason Liu 54f9e4287f
Pass kwargs from initialize_agent into agent classmethod (#799)
# 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`

``` python
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

---------

Co-authored-by: Jason Liu <jason@jxnl.coA>
2023-01-30 14:54:09 -08:00
..
agents Pass kwargs from initialize_agent into agent classmethod (#799) 2023-01-30 14:54:09 -08:00
callbacks Suggestions for better debugging (#765) 2023-01-28 08:05:20 -08:00
chains move hyde into chains (#728) 2023-01-24 22:23:32 -08:00
data Prompt from file proof of concept using plain text (#127) 2022-11-13 13:15:30 -08:00
docstore wip: add method for both docstore and embeddings (#119) 2022-11-20 16:23:58 -08:00
llms Fix sqlalchemy warnings when running tests (#733) 2023-01-25 07:14:07 -08:00
prompts Docs refactor (#480) 2023-01-02 08:24:09 -08:00
utilities Centralize logic for loading from LangChainHub, add ability to pin dependencies (#805) 2023-01-30 14:52:17 -08:00
__init__.py initial commit 2022-10-24 14:51:15 -07:00
test_bash.py update branch name in gha (#274) 2022-12-06 22:28:50 -08:00
test_formatting.py initial commit 2022-10-24 14:51:15 -07:00
test_python.py Harrison/tools exp (#372) 2022-12-18 21:51:23 -05:00
test_sql_database_schema.py Fix sqlalchemy warnings when running tests (#733) 2023-01-25 07:14:07 -08:00
test_sql_database.py Sample row in table info for SQLDatabase (#769) (#782) 2023-01-28 13:37:07 -08:00
test_text_splitter.py Harrison/fix splitting (#563) 2023-01-08 19:19:32 -08:00