mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
f20c56db41
needs new versions of langchain-core and langchain --------- Co-authored-by: Nuno Campos <nuno@langchain.dev>
553 lines
25 KiB
Plaintext
553 lines
25 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "fa6802ac",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Shared memory across agents and tools\n",
|
||
"\n",
|
||
"This notebook goes over adding memory to **both** an Agent and its tools. Before going through this notebook, please walk through the following notebooks, as this will build on top of both of them:\n",
|
||
"\n",
|
||
"- [Adding memory to an LLM Chain](/docs/modules/memory/integrations/adding_memory)\n",
|
||
"- [Custom Agents](/docs/modules/agents/how_to/custom_agent)\n",
|
||
"\n",
|
||
"We are going to create a custom Agent. The agent has access to a conversation memory, search tool, and a summarization tool. The summarization tool also needs access to the conversation memory."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "8db95912",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain.agents import AgentExecutor, Tool, ZeroShotAgent\n",
|
||
"from langchain.chains import LLMChain\n",
|
||
"from langchain.llms import OpenAI\n",
|
||
"from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory\n",
|
||
"from langchain.prompts import PromptTemplate\n",
|
||
"from langchain.utilities import GoogleSearchAPIWrapper"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "06b7187b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"template = \"\"\"This is a conversation between a human and a bot:\n",
|
||
"\n",
|
||
"{chat_history}\n",
|
||
"\n",
|
||
"Write a summary of the conversation for {input}:\n",
|
||
"\"\"\"\n",
|
||
"\n",
|
||
"prompt = PromptTemplate(input_variables=[\"input\", \"chat_history\"], template=template)\n",
|
||
"memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
|
||
"readonlymemory = ReadOnlySharedMemory(memory=memory)\n",
|
||
"summary_chain = LLMChain(\n",
|
||
" llm=OpenAI(),\n",
|
||
" prompt=prompt,\n",
|
||
" verbose=True,\n",
|
||
" memory=readonlymemory, # use the read-only memory to prevent the tool from modifying the memory\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "97ad8467",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"search = GoogleSearchAPIWrapper()\n",
|
||
"tools = [\n",
|
||
" Tool(\n",
|
||
" name=\"Search\",\n",
|
||
" func=search.run,\n",
|
||
" description=\"useful for when you need to answer questions about current events\",\n",
|
||
" ),\n",
|
||
" Tool(\n",
|
||
" name=\"Summary\",\n",
|
||
" func=summary_chain.run,\n",
|
||
" description=\"useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary.\",\n",
|
||
" ),\n",
|
||
"]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "e3439cd6",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"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",
|
||
"\n",
|
||
"{chat_history}\n",
|
||
"Question: {input}\n",
|
||
"{agent_scratchpad}\"\"\"\n",
|
||
"\n",
|
||
"prompt = ZeroShotAgent.create_prompt(\n",
|
||
" tools,\n",
|
||
" prefix=prefix,\n",
|
||
" suffix=suffix,\n",
|
||
" input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"],\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "0021675b",
|
||
"metadata": {},
|
||
"source": [
|
||
"We can now construct the `LLMChain`, with the Memory object, and then create the agent."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "c56a0e73",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\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",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "ca4bc1fb",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: I should research ChatGPT to answer this question.\n",
|
||
"Action: Search\n",
|
||
"Action Input: \"ChatGPT\"\u001b[0m\n",
|
||
"Observation: \u001b[36;1m\u001b[1;3mNov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...\u001b[0m\n",
|
||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
||
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"\"ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\""
|
||
]
|
||
},
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(input=\"What is ChatGPT?\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "45627664",
|
||
"metadata": {},
|
||
"source": [
|
||
"To test the memory of this agent, we can ask a followup question that relies on information in the previous exchange to be answered correctly."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "eecc0462",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: I need to find out who developed ChatGPT\n",
|
||
"Action: Search\n",
|
||
"Action Input: Who developed ChatGPT\u001b[0m\n",
|
||
"Observation: \u001b[36;1m\u001b[1;3mChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...\u001b[0m\n",
|
||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
||
"Final Answer: ChatGPT was developed by OpenAI.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'ChatGPT was developed by OpenAI.'"
|
||
]
|
||
},
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(input=\"Who developed it?\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"id": "c34424cf",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: I need to simplify the conversation for a 5 year old.\n",
|
||
"Action: Summary\n",
|
||
"Action Input: My daughter 5 years old\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
||
"Prompt after formatting:\n",
|
||
"\u001b[32;1m\u001b[1;3mThis is a conversation between a human and a bot:\n",
|
||
"\n",
|
||
"Human: What is ChatGPT?\n",
|
||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
||
"Human: Who developed it?\n",
|
||
"AI: ChatGPT was developed by OpenAI.\n",
|
||
"\n",
|
||
"Write a summary of the conversation for My daughter 5 years old:\n",
|
||
"\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||
"\n",
|
||
"Observation: \u001b[33;1m\u001b[1;3m\n",
|
||
"The conversation was about ChatGPT, an artificial intelligence chatbot. It was created by OpenAI and can send and receive images while chatting.\u001b[0m\n",
|
||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
||
"Final Answer: ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.'"
|
||
]
|
||
},
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(\n",
|
||
" input=\"Thanks. Summarize the conversation, for my daughter 5 years old.\"\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "4ebd8326",
|
||
"metadata": {},
|
||
"source": [
|
||
"Confirm that the memory was correctly updated."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "b91f8c85",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Human: What is ChatGPT?\n",
|
||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
||
"Human: Who developed it?\n",
|
||
"AI: ChatGPT was developed by OpenAI.\n",
|
||
"Human: Thanks. Summarize the conversation, for my daughter 5 years old.\n",
|
||
"AI: ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(agent_chain.memory.buffer)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "cc3d0aa4",
|
||
"metadata": {},
|
||
"source": [
|
||
"For comparison, below is a bad example that uses the same memory for both the Agent and the tool."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"id": "3359d043",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"## This is a bad practice for using the memory.\n",
|
||
"## Use the ReadOnlySharedMemory class, as shown above.\n",
|
||
"\n",
|
||
"template = \"\"\"This is a conversation between a human and a bot:\n",
|
||
"\n",
|
||
"{chat_history}\n",
|
||
"\n",
|
||
"Write a summary of the conversation for {input}:\n",
|
||
"\"\"\"\n",
|
||
"\n",
|
||
"prompt = PromptTemplate(input_variables=[\"input\", \"chat_history\"], template=template)\n",
|
||
"memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
|
||
"summary_chain = LLMChain(\n",
|
||
" llm=OpenAI(),\n",
|
||
" prompt=prompt,\n",
|
||
" verbose=True,\n",
|
||
" memory=memory, # <--- this is the only change\n",
|
||
")\n",
|
||
"\n",
|
||
"search = GoogleSearchAPIWrapper()\n",
|
||
"tools = [\n",
|
||
" Tool(\n",
|
||
" name=\"Search\",\n",
|
||
" func=search.run,\n",
|
||
" description=\"useful for when you need to answer questions about current events\",\n",
|
||
" ),\n",
|
||
" Tool(\n",
|
||
" name=\"Summary\",\n",
|
||
" func=summary_chain.run,\n",
|
||
" description=\"useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary.\",\n",
|
||
" ),\n",
|
||
"]\n",
|
||
"\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",
|
||
"\n",
|
||
"{chat_history}\n",
|
||
"Question: {input}\n",
|
||
"{agent_scratchpad}\"\"\"\n",
|
||
"\n",
|
||
"prompt = ZeroShotAgent.create_prompt(\n",
|
||
" tools,\n",
|
||
" prefix=prefix,\n",
|
||
" suffix=suffix,\n",
|
||
" input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"],\n",
|
||
")\n",
|
||
"\n",
|
||
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\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",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"id": "970d23df",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: I should research ChatGPT to answer this question.\n",
|
||
"Action: Search\n",
|
||
"Action Input: \"ChatGPT\"\u001b[0m\n",
|
||
"Observation: \u001b[36;1m\u001b[1;3mNov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...\u001b[0m\n",
|
||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
||
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"\"ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\""
|
||
]
|
||
},
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(input=\"What is ChatGPT?\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"id": "d9ea82f0",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: I need to find out who developed ChatGPT\n",
|
||
"Action: Search\n",
|
||
"Action Input: Who developed ChatGPT\u001b[0m\n",
|
||
"Observation: \u001b[36;1m\u001b[1;3mChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...\u001b[0m\n",
|
||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
||
"Final Answer: ChatGPT was developed by OpenAI.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'ChatGPT was developed by OpenAI.'"
|
||
]
|
||
},
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(input=\"Who developed it?\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"id": "5b1f9223",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||
"\u001b[32;1m\u001b[1;3mThought: I need to simplify the conversation for a 5 year old.\n",
|
||
"Action: Summary\n",
|
||
"Action Input: My daughter 5 years old\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
||
"Prompt after formatting:\n",
|
||
"\u001b[32;1m\u001b[1;3mThis is a conversation between a human and a bot:\n",
|
||
"\n",
|
||
"Human: What is ChatGPT?\n",
|
||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
||
"Human: Who developed it?\n",
|
||
"AI: ChatGPT was developed by OpenAI.\n",
|
||
"\n",
|
||
"Write a summary of the conversation for My daughter 5 years old:\n",
|
||
"\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||
"\n",
|
||
"Observation: \u001b[33;1m\u001b[1;3m\n",
|
||
"The conversation was about ChatGPT, an artificial intelligence chatbot developed by OpenAI. It is designed to have conversations with humans and can also send and receive images.\u001b[0m\n",
|
||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
||
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.\u001b[0m\n",
|
||
"\n",
|
||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.'"
|
||
]
|
||
},
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"agent_chain.run(\n",
|
||
" input=\"Thanks. Summarize the conversation, for my daughter 5 years old.\"\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d07415da",
|
||
"metadata": {},
|
||
"source": [
|
||
"The final answer is not wrong, but we see the 3rd Human input is actually from the agent in the memory because the memory was modified by the summary tool."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"id": "32f97b21",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Human: What is ChatGPT?\n",
|
||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
||
"Human: Who developed it?\n",
|
||
"AI: ChatGPT was developed by OpenAI.\n",
|
||
"Human: My daughter 5 years old\n",
|
||
"AI: \n",
|
||
"The conversation was about ChatGPT, an artificial intelligence chatbot developed by OpenAI. It is designed to have conversations with humans and can also send and receive images.\n",
|
||
"Human: Thanks. Summarize the conversation, for my daughter 5 years old.\n",
|
||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(agent_chain.memory.buffer)"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.11.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|