Files
langchain_llms/memory/conversation_buffer_memory.ipynb
2023-08-26 11:32:01 -07:00

337 lines
14 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "e60a0f23-bcc0-4347-b5de-cc3d548184bb",
"metadata": {},
"source": [
"# ConversationBufferMemory"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "dbb93429-5923-412b-8ed4-765a3435c4c2",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.chains import ConversationChain\n",
"from langchain.memory import ConversationBufferMemory"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d8fa93d8-6eb7-45d3-9c1d-be1f56599b01",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0)\n",
"memory = ConversationBufferMemory()"
]
},
{
"cell_type": "markdown",
"id": "03b156d0-a996-4364-8564-b336990ccb5d",
"metadata": {},
"source": [
"# Connect Conversation to Memory"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "581890dc-82e3-4548-b9ed-d0cc151a57fa",
"metadata": {},
"outputs": [],
"source": [
"conversation = ConversationChain(\n",
" llm=llm,\n",
" memory=memory,\n",
" verbose=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "224d8e1e-1cb4-4040-b091-28dd529028e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Human: Hello, nice to meet you!\n",
"AI:\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\"Hello! Nice to meet you too! I'm an AI trained to have conversations and provide information. How can I assist you today?\""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"Hello, nice to meet you!\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2399aa92-1d9d-49de-86ed-ea4f5a0b9d30",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"Human: Hello, nice to meet you!\n",
"AI: Hello! Nice to meet you too! I'm an AI trained to have conversations and provide information. How can I assist you today?\n",
"Human: Tell me about Robert Oppenheimer\n",
"AI:\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'Robert Oppenheimer was an American theoretical physicist and professor of physics at the University of California, Berkeley. He is best known for his role as the scientific director of the Manhattan Project, which was a research and development project that produced the first atomic bombs during World War II. Oppenheimer is often referred to as the \"father of the atomic bomb\" due to his significant contributions to the project. He played a crucial role in the design and development of the bombs, particularly the \"Little Boy\" bomb that was dropped on Hiroshima. Oppenheimer\\'s involvement in the Manhattan Project brought him both fame and controversy. After the war, he became an advocate for international control of atomic energy and worked towards preventing the further spread of nuclear weapons. Oppenheimer\\'s contributions to science and his complex legacy continue to be subjects of study and debate.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"Tell me about Robert Oppenheimer\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "80a417d5-4132-4ec7-a3c0-10baf853d238",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Human: Hello, nice to meet you!\n",
"AI: Hello! Nice to meet you too! I'm an AI trained to have conversations and provide information. How can I assist you today?\n",
"Human: Tell me about Robert Oppenheimer\n",
"AI: Robert Oppenheimer was an American theoretical physicist and professor of physics at the University of California, Berkeley. He is best known for his role as the scientific director of the Manhattan Project, which was a research and development project that produced the first atomic bombs during World War II. Oppenheimer is often referred to as the \"father of the atomic bomb\" due to his significant contributions to the project. He played a crucial role in the design and development of the bombs, particularly the \"Little Boy\" bomb that was dropped on Hiroshima. Oppenheimer's involvement in the Manhattan Project brought him both fame and controversy. After the war, he became an advocate for international control of atomic energy and worked towards preventing the further spread of nuclear weapons. Oppenheimer's contributions to science and his complex legacy continue to be subjects of study and debate.\n"
]
}
],
"source": [
"print(memory.buffer)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f4e03b47-7f3e-45ea-81bb-c181d5549cf3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'history': 'Human: Hello, nice to meet you!\\nAI: Hello! Nice to meet you too! I\\'m an AI trained to have conversations and provide information. How can I assist you today?\\nHuman: Tell me about Robert Oppenheimer\\nAI: Robert Oppenheimer was an American theoretical physicist and professor of physics at the University of California, Berkeley. He is best known for his role as the scientific director of the Manhattan Project, which was a research and development project that produced the first atomic bombs during World War II. Oppenheimer is often referred to as the \"father of the atomic bomb\" due to his significant contributions to the project. He played a crucial role in the design and development of the bombs, particularly the \"Little Boy\" bomb that was dropped on Hiroshima. Oppenheimer\\'s involvement in the Manhattan Project brought him both fame and controversy. After the war, he became an advocate for international control of atomic energy and worked towards preventing the further spread of nuclear weapons. Oppenheimer\\'s contributions to science and his complex legacy continue to be subjects of study and debate.'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory.load_memory_variables({})"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "241cc1cc-89a4-43b4-9789-2db8f198022e",
"metadata": {},
"outputs": [],
"source": [
"memory.save_context({\"input\": \"Very Interesting.\"},\n",
" {\"output\": \"Yes, it was my pleasure as an AI to answer.\"})"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3322e433-9fe0-4ad9-98a0-3240bcfc0276",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'history': 'Human: Hello, nice to meet you!\\nAI: Hello! Nice to meet you too! I\\'m an AI trained to have conversations and provide information. How can I assist you today?\\nHuman: Tell me about Robert Oppenheimer\\nAI: Robert Oppenheimer was an American theoretical physicist and professor of physics at the University of California, Berkeley. He is best known for his role as the scientific director of the Manhattan Project, which was a research and development project that produced the first atomic bombs during World War II. Oppenheimer is often referred to as the \"father of the atomic bomb\" due to his significant contributions to the project. He played a crucial role in the design and development of the bombs, particularly the \"Little Boy\" bomb that was dropped on Hiroshima. Oppenheimer\\'s involvement in the Manhattan Project brought him both fame and controversy. After the war, he became an advocate for international control of atomic energy and worked towards preventing the further spread of nuclear weapons. Oppenheimer\\'s contributions to science and his complex legacy continue to be subjects of study and debate.\\nHuman: Very Interesting.\\nAI: Yes, it was my pleasure as an AI to answer.'}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory.load_memory_variables({})"
]
},
{
"cell_type": "markdown",
"id": "421ceb7e-db92-462e-870f-4f38402ae8a7",
"metadata": {},
"source": [
"# Saving and Loading Memory"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "18e0df0a-bcc4-4cd3-916a-2db5d0c71944",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ConversationBufferMemory(chat_memory=ChatMessageHistory(messages=[HumanMessage(content='Hello, nice to meet you!', additional_kwargs={}, example=False), AIMessage(content=\"Hello! Nice to meet you too! I'm an AI trained to have conversations and provide information. How can I assist you today?\", additional_kwargs={}, example=False), HumanMessage(content='Tell me about Robert Oppenheimer', additional_kwargs={}, example=False), AIMessage(content='Robert Oppenheimer was an American theoretical physicist and professor of physics at the University of California, Berkeley. He is best known for his role as the scientific director of the Manhattan Project, which was a research and development project that produced the first atomic bombs during World War II. Oppenheimer is often referred to as the \"father of the atomic bomb\" due to his significant contributions to the project. He played a crucial role in the design and development of the bombs, particularly the \"Little Boy\" bomb that was dropped on Hiroshima. Oppenheimer\\'s involvement in the Manhattan Project brought him both fame and controversy. After the war, he became an advocate for international control of atomic energy and worked towards preventing the further spread of nuclear weapons. Oppenheimer\\'s contributions to science and his complex legacy continue to be subjects of study and debate.', additional_kwargs={}, example=False), HumanMessage(content='Very Interesting.', additional_kwargs={}, example=False), AIMessage(content='Yes, it was my pleasure as an AI to answer.', additional_kwargs={}, example=False)]), output_key=None, input_key=None, return_messages=False, human_prefix='Human', ai_prefix='AI', memory_key='history')"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.memory"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d7ab7fcb-9105-4402-a5eb-0b792e6a707b",
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"pickled_str = pickle.dumps(conversation.memory)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1d064b9f-6c39-477d-8ecf-f01c051fca30",
"metadata": {},
"outputs": [],
"source": [
"with open(\"memory.pkl\", \"wb\") as f:\n",
" f.write(pickled_str)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "d8abdf3f-b0c4-4bd9-b8d7-4ab605760ec1",
"metadata": {},
"outputs": [],
"source": [
"new_memory = open(\"memory.pkl\", \"rb\").read()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "499ec6f7-9c5b-4ccf-8e7d-91e5efb2d8e1",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0)\n",
"reload_conversation = ConversationChain(\n",
" llm=llm,\n",
" memory=pickle.loads(new_memory),\n",
" verbose=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3238e0b1-a870-4cc6-a207-0911e4ebd645",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Human: Hello, nice to meet you!\\nAI: Hello! Nice to meet you too! I\\'m an AI trained to have conversations and provide information. How can I assist you today?\\nHuman: Tell me about Robert Oppenheimer\\nAI: Robert Oppenheimer was an American theoretical physicist and professor of physics at the University of California, Berkeley. He is best known for his role as the scientific director of the Manhattan Project, which was a research and development project that produced the first atomic bombs during World War II. Oppenheimer is often referred to as the \"father of the atomic bomb\" due to his significant contributions to the project. He played a crucial role in the design and development of the bombs, particularly the \"Little Boy\" bomb that was dropped on Hiroshima. Oppenheimer\\'s involvement in the Manhattan Project brought him both fame and controversy. After the war, he became an advocate for international control of atomic energy and worked towards preventing the further spread of nuclear weapons. Oppenheimer\\'s contributions to science and his complex legacy continue to be subjects of study and debate.\\nHuman: Very Interesting.\\nAI: Yes, it was my pleasure as an AI to answer.'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reload_conversation.memory.buffer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "934dbcb0-a06b-46cd-ac92-a39fd4939bda",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}