[docs] update agent cookbook lcel (#15349)

pull/14852/head
Harrison Chase 8 months ago committed by GitHub
parent 870b4033ed
commit 768334b6a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,18 +12,20 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"id": "af4381de",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import AgentExecutor, XMLAgent, tool\n",
"from langchain import hub\n",
"from langchain.agents import AgentExecutor, tool\n",
"from langchain.agents.output_parsers import XMLAgentOutputParser\n",
"from langchain.chat_models import ChatAnthropic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "24cc8134",
"metadata": {},
"outputs": [],
@ -33,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "67c0b0e4",
"metadata": {},
"outputs": [],
@ -46,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "7203b101",
"metadata": {},
"outputs": [],
@ -56,18 +58,18 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "b68e756d",
"metadata": {},
"outputs": [],
"source": [
"# Get prompt to use\n",
"prompt = XMLAgent.get_default_prompt()"
"# Get the prompt to use - you can modify this!\n",
"prompt = hub.pull(\"hwchase17/xml-agent-convo\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "61ab3e9a",
"metadata": {},
"outputs": [],
@ -107,27 +109,27 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"id": "e92f1d6f",
"metadata": {},
"outputs": [],
"source": [
"agent = (\n",
" {\n",
" \"question\": lambda x: x[\"question\"],\n",
" \"intermediate_steps\": lambda x: convert_intermediate_steps(\n",
" \"input\": lambda x: x[\"input\"],\n",
" \"agent_scratchpad\": lambda x: convert_intermediate_steps(\n",
" x[\"intermediate_steps\"]\n",
" ),\n",
" }\n",
" | prompt.partial(tools=convert_tools(tool_list))\n",
" | model.bind(stop=[\"</tool_input>\", \"</final_answer>\"])\n",
" | XMLAgent.get_default_output_parser()\n",
" | XMLAgentOutputParser()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 13,
"id": "6ce6ec7a",
"metadata": {},
"outputs": [],
@ -137,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 14,
"id": "fb5cb2e3",
"metadata": {},
"outputs": [
@ -148,10 +150,8 @@
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m <tool>search</tool>\n",
"<tool_input>weather in new york\u001b[0m\u001b[36;1m\u001b[1;3m32 degrees\u001b[0m\u001b[32;1m\u001b[1;3m\n",
"\n",
"<final_answer>The weather in New York is 32 degrees\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m <tool>search</tool><tool_input>weather in New York\u001b[0m\u001b[36;1m\u001b[1;3m32 degrees\u001b[0m\u001b[32;1m\u001b[1;3m <tool>search</tool>\n",
"<tool_input>weather in New York\u001b[0m\u001b[36;1m\u001b[1;3m32 degrees\u001b[0m\u001b[32;1m\u001b[1;3m <final_answer>The weather in New York is 32 degrees\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
@ -159,17 +159,17 @@
{
"data": {
"text/plain": [
"{'question': 'whats the weather in New york?',\n",
"{'input': 'whats the weather in New york?',\n",
" 'output': 'The weather in New York is 32 degrees'}"
]
},
"execution_count": 9,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_executor.invoke({\"question\": \"whats the weather in New york?\"})"
"agent_executor.invoke({\"input\": \"whats the weather in New york?\"})"
]
},
{

Loading…
Cancel
Save