fix agent memory docs (#382)

This commit is contained in:
Harrison Chase 2022-12-19 09:15:32 -05:00 committed by GitHub
parent 766b84a9d9
commit a01d3e6955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,12 +22,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 6,
"id": "8db95912", "id": "8db95912",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from langchain.agents import ZeroShotAgent, Tool\n", "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n",
"from langchain.chains.conversation.memory import ConversationBufferMemory\n", "from langchain.chains.conversation.memory import ConversationBufferMemory\n",
"from langchain import OpenAI, SerpAPIWrapper, LLMChain" "from langchain import OpenAI, SerpAPIWrapper, LLMChain"
] ]
@ -59,7 +59,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 8,
"id": "e3439cd6", "id": "e3439cd6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -68,13 +68,14 @@
"suffix = \"\"\"Begin!\"\n", "suffix = \"\"\"Begin!\"\n",
"\n", "\n",
"{chat_history}\n", "{chat_history}\n",
"Question: {input}\"\"\"\n", "Question: {input}\n",
"{agent_scratchpad}\"\"\"\n",
"\n", "\n",
"prompt = ZeroShotAgent.create_prompt(\n", "prompt = ZeroShotAgent.create_prompt(\n",
" tools, \n", " tools, \n",
" prefix=prefix, \n", " prefix=prefix, \n",
" suffix=suffix, \n", " suffix=suffix, \n",
" input_variables=[\"input\", \"chat_history\"]\n", " input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"]\n",
")\n", ")\n",
"memory = ConversationBufferMemory(memory_key=\"chat_history\")" "memory = ConversationBufferMemory(memory_key=\"chat_history\")"
] ]
@ -89,18 +90,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 16,
"id": "c56a0e73", "id": "c56a0e73",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt, memory=memory)\n", "llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n",
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)" "agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
"agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 17,
"id": "ca4bc1fb", "id": "ca4bc1fb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -110,30 +112,29 @@
"text": [ "text": [
"\n", "\n",
"\n", "\n",
"\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"How many people live in canada?\n", "\u001b[32;1m\u001b[1;3mThought: I need to find out the population of Canada\n",
"Thought:\u001b[32;1m\u001b[1;3m I should look up how many people live in canada\n",
"Action: Search\n", "Action: Search\n",
"Action Input: \"How many people live in canada?\"\u001b[0m\n", "Action Input: Population of Canada\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,555,354 as of Monday, December 19, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: The current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data.\u001b[0m\n", "Final Answer: The current population of Canada is 38,555,354.\u001b[0m\n",
"\u001b[1m> Finished chain.\u001b[0m\n" "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
] ]
}, },
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'The current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data.'" "'The current population of Canada is 38,555,354.'"
] ]
}, },
"execution_count": 5, "execution_count": 17,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"agent.run(\"How many people live in canada?\")" "agent_chain.run(input=\"How many people live in canada?\")"
] ]
}, },
{ {
@ -146,7 +147,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 20,
"id": "eecc0462", "id": "eecc0462",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -156,32 +157,29 @@
"text": [ "text": [
"\n", "\n",
"\n", "\n",
"\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"what is their national anthem called?\n", "\u001b[32;1m\u001b[1;3mThought: I need to find out what the national anthem of Canada is called.\n",
"Thought:\u001b[32;1m\u001b[1;3m\n",
"AI: I should look up the name of Canada's national anthem\n",
"Action: Search\n", "Action: Search\n",
"Action Input: \"What is the name of Canada's national anthem?\"\u001b[0m\n", "Action Input: National anthem of Canada\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mAfter 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ...\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mAfter 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ...\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"AI: I now know the final answer\n", "Final Answer: O Canada is the national anthem of Canada.\u001b[0m\n",
"Final Answer: After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa Lavallée.\u001b[0m\n", "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
"\u001b[1m> Finished chain.\u001b[0m\n"
] ]
}, },
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"\"After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa Lavallée.\"" "'O Canada is the national anthem of Canada.'"
] ]
}, },
"execution_count": 6, "execution_count": 20,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"agent.run(\"what is their national anthem called?\")" "agent_chain.run(input=\"what is their national anthem called?\")"
] ]
}, },
{ {
@ -196,7 +194,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 23,
"id": "3359d043", "id": "3359d043",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -204,21 +202,23 @@
"prefix = \"\"\"Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:\"\"\"\n", "prefix = \"\"\"Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:\"\"\"\n",
"suffix = \"\"\"Begin!\"\n", "suffix = \"\"\"Begin!\"\n",
"\n", "\n",
"Question: {input}\"\"\"\n", "Question: {input}\n",
"{agent_scratchpad}\"\"\"\n",
"\n", "\n",
"prompt = ZeroShotAgent.create_prompt(\n", "prompt = ZeroShotAgent.create_prompt(\n",
" tools, \n", " tools, \n",
" prefix=prefix, \n", " prefix=prefix, \n",
" suffix=suffix, \n", " suffix=suffix, \n",
" input_variables=[\"input\"]\n", " input_variables=[\"input\", \"agent_scratchpad\"]\n",
")\n", ")\n",
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n", "llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n",
"agent_without_memory = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)" "agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
"agent_without_memory = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 24,
"id": "970d23df", "id": "970d23df",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -228,24 +228,23 @@
"text": [ "text": [
"\n", "\n",
"\n", "\n",
"\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"How many people live in canada?\n", "\u001b[32;1m\u001b[1;3mThought: I should look up the answer\n",
"Thought:\u001b[32;1m\u001b[1;3m I should look up how many people live in canada\n",
"Action: Search\n", "Action: Search\n",
"Action Input: \"How many people live in canada?\"\u001b[0m\n", "Action Input: population of Canada\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,533,678 as of Friday, November 25, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mThe current population of Canada is 38,555,354 as of Monday, December 19, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada 2020 ...\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: The current population of Canada is 38,533,678\u001b[0m\n", "Final Answer: The current population of Canada is 38,555,354.\u001b[0m\n",
"\u001b[1m> Finished chain.\u001b[0m\n" "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
] ]
}, },
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'The current population of Canada is 38,533,678'" "'The current population of Canada is 38,555,354.'"
] ]
}, },
"execution_count": 10, "execution_count": 24,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -256,7 +255,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 25,
"id": "d9ea82f0", "id": "d9ea82f0",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -266,24 +265,23 @@
"text": [ "text": [
"\n", "\n",
"\n", "\n",
"\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"what is their national anthem called?\n", "\u001b[32;1m\u001b[1;3mThought: I should look up the answer\n",
"Thought:\u001b[32;1m\u001b[1;3m I should probably look this up\n",
"Action: Search\n", "Action: Search\n",
"Action Input: \"What is the national anthem of [country]\"\u001b[0m\n", "Action Input: national anthem of [country]\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mMost nation states have an anthem, defined as \"a song, as of praise, devotion, or patriotism\"; most anthems are either marches or hymns in style.\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\"Himno Nacional\" (\"National Anthem\") · \"Pátria\" (\"Fatherland\") · \"Salve, Oh Patria\" (\"We Salute You, Our Homeland\") · \"Bilady, Bilady, Bilady\" (\"My Country, My ...\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: The national anthem is called \"the national anthem.\"\u001b[0m\n", "Final Answer: The national anthem of [country] is called \"Himno Nacional\", \"Pátria\", \"Salve, Oh Patria\", and \"Bilady, Bilady, Bilady\".\u001b[0m\n",
"\u001b[1m> Finished chain.\u001b[0m\n" "\u001b[1m> Finished AgentExecutor chain.\u001b[0m\n"
] ]
}, },
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'The national anthem is called \"the national anthem.\"'" "'The national anthem of [country] is called \"Himno Nacional\", \"Pátria\", \"Salve, Oh Patria\", and \"Bilady, Bilady, Bilady\".'"
] ]
}, },
"execution_count": 11, "execution_count": 25,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -317,7 +315,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.6" "version": "3.10.8"
} }
}, },
"nbformat": 4, "nbformat": 4,