docs: Get rid of ZeroShotAgent and use create_react_agent instead (#20157)

- **Issue:** #20122
 -  @baskaryan, @eyurtsev.
This commit is contained in:
Guangdong Liu
2024-04-10 01:00:29 +08:00
committed by GitHub
parent 0c848a25ad
commit 301dc3dfd2
2 changed files with 117 additions and 124 deletions

View File

@@ -136,45 +136,29 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain import hub\n",
"from langchain.agents import (\n",
" AgentExecutor,\n",
" ZeroShotAgent,\n",
" create_react_agent,\n",
")\n",
"from langchain.chains import LLMChain\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain_community.llms import NIBittensorLLM\n",
"from langchain_core.prompts import PromptTemplate\n",
"\n",
"memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
"\n",
"tools = [tool]\n",
"prefix = \"\"\"Answer prompt based on LLM if there is need to search something then use internet and observe internet result and give accurate reply of user questions also try to use authenticated sources\"\"\"\n",
"suffix = \"\"\"Begin!\n",
" {chat_history}\n",
" Question: {input}\n",
" {agent_scratchpad}\"\"\"\n",
"\n",
"prompt = ZeroShotAgent.create_prompt(\n",
" tools=tools,\n",
" prefix=prefix,\n",
" suffix=suffix,\n",
" input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"],\n",
")\n",
"prompt = hub.pull(\"hwchase17/react\")\n",
"\n",
"\n",
"llm = NIBittensorLLM(\n",
" system_prompt=\"Your task is to determine a response based on user prompt\"\n",
")\n",
"\n",
"llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
"\n",
"memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
"\n",
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
"agent_chain = AgentExecutor.from_agent_and_tools(\n",
" agent=agent, tools=tools, verbose=True, memory=memory\n",
")\n",
"agent = create_react_agent(llm, tools, prompt)\n",
"agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory)\n",
"\n",
"response = agent_chain.run(input=prompt)"
"response = agent_executor.invoke({\"input\": prompt})"
]
}
],