langchain memory
This commit is contained in:
336
memory/conversation_buffer_memory.ipynb
Normal file
336
memory/conversation_buffer_memory.ipynb
Normal file
@@ -0,0 +1,336 @@
|
|||||||
|
{
|
||||||
|
"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
|
||||||
|
}
|
||||||
179
memory/conversation_buffer_window_memory.ipynb
Normal file
179
memory/conversation_buffer_window_memory.ipynb
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "ed724944-f8db-4e10-9eef-549ffe63079e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# ConversationBufferWindowMemory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "360289bc-f61a-472b-b619-fbe0d236364b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
|
"from langchain.chains import ConversationChain\n",
|
||||||
|
"from langchain.memory import ConversationBufferWindowMemory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "2b47dee4-d516-4eff-a29c-89879d2f8212",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"llm = ChatOpenAI(temperature=0)\n",
|
||||||
|
"memory = ConversationBufferWindowMemory(k=1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9fd619b6-8232-431c-be08-12b2906da968",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Connect conversation to memory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "d5b40fda-30b8-4b0e-a0a7-147412683036",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"conversation = ConversationChain(\n",
|
||||||
|
" llm=llm,\n",
|
||||||
|
" memory=memory,\n",
|
||||||
|
" verbose=True\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "4f01de60-34fd-4e70-8a9e-89c69fe36a88",
|
||||||
|
"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": "239f0816-33ea-44d7-8324-4d5499b2fc14",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[HumanMessage(content='Hello, nice to meet you!', additional_kwargs={}, example=False),\n",
|
||||||
|
" 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)]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"memory.buffer"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "0ff140ca-e9bd-4053-a61b-89e6caceb379",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"memory.save_context({\"input\": \"Very Interesting.\"},\n",
|
||||||
|
" {\"output\": \"Yes, it was my pleasure as and AI to answer.\"})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "f0bae707-b0de-4538-ac84-c609447c9cb6",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"{'history': 'Human: Very Interesting.\\nAI: Yes, it was my pleasure as and AI to answer.'}"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"memory.load_memory_variables({})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "884f3bdc-c555-4266-bf35-ab088e9511a7",
|
||||||
|
"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
|
||||||
|
}
|
||||||
104
memory/conversation_summary_memory.ipynb
Normal file
104
memory/conversation_summary_memory.ipynb
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "431f44e2-ad47-458c-8a21-c88d1b71251a",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# ConversationSummaryMemory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "bbb34950-bc59-4ef3-a87b-697d10058ac3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
|
"from langchain.chains import ConversationChain\n",
|
||||||
|
"from langchain.memory import ConversationSummaryBufferMemory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "1eee37a4-11f8-4657-bad9-cd046eac6fdb",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"llm = ChatOpenAI(temperature=0)\n",
|
||||||
|
"memory = ConversationSummaryBufferMemory(llm=llm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "5dcafc17-d2b1-4373-9255-9a270528bb98",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"skii_trip= \"\"\"\n",
|
||||||
|
"Guess what? I just came back from an incredible ski trip to Colorado and I cannot wait to tell you all about it!\n",
|
||||||
|
"\n",
|
||||||
|
"First things first, the mountains in Colorado are absolutely breathtaking. The picturesque landscapes of snow-capped peaks were straight out of a winter wonderland postcard. The moment I arrived, I couldn't help but feel this rush of excitement and anticipation for hitting the slopes.\n",
|
||||||
|
"\n",
|
||||||
|
"We stayed at this cozy ski lodge nestled right by the slopes, which made it super convenient for us to get on our skis and start exploring. The lodge had great amenities and a fireplace that was perfect for relaxing and warming up after a long day of skiing.\n",
|
||||||
|
"\n",
|
||||||
|
"Now, let me tell you about the skiing itself. I have to admit, I was a bit nervous at first because I'm not the most experienced skier, but the ski instructors there were so patient and helpful. They gave us pointers, taught us how to navigate the slopes safely, and even showed us a few tricks to improve our technique. I felt much more confident and excited about hitting the slopes after that.\n",
|
||||||
|
"\n",
|
||||||
|
"The ski resort had a variety of trails for all levels, from beginner-friendly slopes to more challenging ones. I started with the easier ones to get familiar with the snow.\n",
|
||||||
|
"\"\"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "fda66bfc-b5e8-466d-b146-694677cde698",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Conversation chain with build in summarizer"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "ee0bf642-8161-4af1-a367-52c3143e7685",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "5b4b284b-6812-447f-958b-45ac6fb7e1da",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"memory.save_context({},{})"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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
|
||||||
|
}
|
||||||
BIN
memory/memory.pkl
Normal file
BIN
memory/memory.pkl
Normal file
Binary file not shown.
Reference in New Issue
Block a user