[docs] update agent cookbook lcel (#15349)

This commit is contained in:
Harrison Chase 2024-01-01 13:31:41 -08:00 committed by GitHub
parent 870b4033ed
commit 768334b6a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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