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

180 lines
4.3 KiB
Plaintext

{
"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
}