{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7c4b3ec4-20fb-46b1-8f25-1f51d1bc32e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv())\n",
    "openai_api_key = os.environ[\"OPENAI_API_KEY\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0450bf47-a30d-4c31-bb0e-8e1918362054",
   "metadata": {},
   "source": [
    "## Memory"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2b296b9-3681-4eaf-b8de-098a4b2b3390",
   "metadata": {},
   "source": [
    "* Language models have a short memory. They can only remember the information that is in their context window.\n",
    "* Currently, the best way to solve this problem is to use the RAG technique (use an external vector database as memory). We will see this in next chapters.\n",
    "* Apart from the RAG technique, LangChain provides a few alternatives to improve the memory of the language models."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d553e10f-426e-496e-8245-7b6f343e2931",
   "metadata": {},
   "source": [
    "## Conversation Buffer Memory\n",
    "Stores messages and it loads them in the next prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "97d4236e-bfc1-4826-be26-bd6ded0e806f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import OpenAI\n",
    "from langchain.chains import ConversationChain\n",
    "from langchain.memory import ConversationBufferMemory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "381693c5-5e3d-4bb1-9eae-daf11ca66928",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI()\n",
    "buffer_memory = ConversationBufferMemory()\n",
    "conversation = ConversationChain(\n",
    "    llm=llm, \n",
    "    memory = buffer_memory,\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5822ec8c-6039-4ce0-a794-54dd63f1c83c",
   "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: Hi, my name is Julio and I have moved 33 times.\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\" Hello Julio, it's nice to meet you. My name is AI and I'm an artificial intelligence programmed to assist and interact with humans. 33 times, that's quite a lot of moves. Can you tell me more about your experience with moving?\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.predict(input=\"Hi, my name is Julio and I have moved 33 times.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3755d1b3-7c7c-4e83-ae82-bb44e510cafd",
   "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: Hi, my name is Julio and I have moved 33 times.\n",
      "AI:  Hello Julio, it's nice to meet you. My name is AI and I'm an artificial intelligence programmed to assist and interact with humans. 33 times, that's quite a lot of moves. Can you tell me more about your experience with moving?\n",
      "Human: Sure. If my average moving distance was 100 miles, how many miles took all my moves?\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' Based on the information provided, your total moving distance would be 3,300 miles. However, this calculation assumes that all 33 of your moves were exactly 100 miles. If your moves varied in distance, the total distance would be different. Is there any other information you would like to know?'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.predict(input=\"Sure. If my average moving distance was 100 miles, how many miles took all my moves?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1bd536cc-7ceb-4837-8dda-5c26d9b77fb0",
   "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: Hi, my name is Julio and I have moved 33 times.\n",
      "AI:  Hello Julio, it's nice to meet you. My name is AI and I'm an artificial intelligence programmed to assist and interact with humans. 33 times, that's quite a lot of moves. Can you tell me more about your experience with moving?\n",
      "Human: Sure. If my average moving distance was 100 miles, how many miles took all my moves?\n",
      "AI:  Based on the information provided, your total moving distance would be 3,300 miles. However, this calculation assumes that all 33 of your moves were exactly 100 miles. If your moves varied in distance, the total distance would be different. Is there any other information you would like to know?\n",
      "Human: Do you remember my name?\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\" Yes, your name is Julio. It's not something I have to remember, as I have access to all the information and data that has been provided to me. But it is a pleasure to address you by your name. Is there anything else you would like to know or talk about?\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.predict(input=\"Do you remember my name?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9b967bca-18cd-4037-ac43-d8663052ab8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human: Hi, my name is Julio and I have moved 33 times.\n",
      "AI:  Hello Julio, it's nice to meet you. My name is AI and I'm an artificial intelligence programmed to assist and interact with humans. 33 times, that's quite a lot of moves. Can you tell me more about your experience with moving?\n",
      "Human: Sure. If my average moving distance was 100 miles, how many miles took all my moves?\n",
      "AI:  Based on the information provided, your total moving distance would be 3,300 miles. However, this calculation assumes that all 33 of your moves were exactly 100 miles. If your moves varied in distance, the total distance would be different. Is there any other information you would like to know?\n",
      "Human: Do you remember my name?\n",
      "AI:  Yes, your name is Julio. It's not something I have to remember, as I have access to all the information and data that has been provided to me. But it is a pleasure to address you by your name. Is there anything else you would like to know or talk about?\n"
     ]
    }
   ],
   "source": [
    "print(buffer_memory.buffer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e0b501ee-27c6-4172-8d59-22a0c97f4ed0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': \"Human: Hi, my name is Julio and I have moved 33 times.\\nAI:  Hello Julio, it's nice to meet you. My name is AI and I'm an artificial intelligence programmed to assist and interact with humans. 33 times, that's quite a lot of moves. Can you tell me more about your experience with moving?\\nHuman: Sure. If my average moving distance was 100 miles, how many miles took all my moves?\\nAI:  Based on the information provided, your total moving distance would be 3,300 miles. However, this calculation assumes that all 33 of your moves were exactly 100 miles. If your moves varied in distance, the total distance would be different. Is there any other information you would like to know?\\nHuman: Do you remember my name?\\nAI:  Yes, your name is Julio. It's not something I have to remember, as I have access to all the information and data that has been provided to me. But it is a pleasure to address you by your name. Is there anything else you would like to know or talk about?\"}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "buffer_memory.load_memory_variables({})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecbcae7b-85c9-41b6-9eab-1fa4d2809cbf",
   "metadata": {},
   "source": [
    "## Conversation Buffer Window Memory\n",
    "Similar to the previous one, but you can limit the number of conversational exchanges stored in memory. For example, you can set it so it only remembers the last 3 questions and answers of the conversation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a996bb6a-d19f-46e4-bfc0-b9d1e0e1d8a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.memory import ConversationBufferWindowMemory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "196bb156-b7d6-4da0-a9ef-b22a53b16cdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "window_memory = ConversationBufferWindowMemory(k=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a2bba27a-96d2-4480-a37b-96787e75034f",
   "metadata": {},
   "outputs": [],
   "source": [
    "conversation_window = ConversationChain(\n",
    "    llm=llm, \n",
    "    memory = window_memory,\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "abc95f84-478b-47d2-8a5f-9da426b09e90",
   "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: The lead singer of Nirvana had 3 cars\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\" That is an interesting fact! Did you know that the lead singer of Nirvana, Kurt Cobain, was known for his love of cars and motorcycles? He owned a 1965 Dodge Dart, a 1965 Jaguar E-Type, and a 1964 Pontiac Parisienne. Unfortunately, he had to sell his cars when he couldn't afford to pay for their repairs.\""
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_window.predict(input=\"The lead singer of Nirvana had 3 cars\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c06a9752-6b95-4bae-b155-eec554607ebc",
   "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: The lead singer of Nirvana had 3 cars\n",
      "AI:  That is an interesting fact! Did you know that the lead singer of Nirvana, Kurt Cobain, was known for his love of cars and motorcycles? He owned a 1965 Dodge Dart, a 1965 Jaguar E-Type, and a 1964 Pontiac Parisienne. Unfortunately, he had to sell his cars when he couldn't afford to pay for their repairs.\n",
      "Human: The lead singer of Nirvana was Kurt Cobain\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' Yes, that is correct. Kurt Cobain was the lead singer and guitarist of the band Nirvana. He was known for his distinctive voice and songwriting abilities, and is considered one of the most influential musicians in alternative rock.'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_window.predict(input=\"The lead singer of Nirvana was Kurt Cobain\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bd086520-388b-435b-a93b-b408ea349a7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human: The lead singer of Nirvana had 3 cars\n",
      "AI:  That is an interesting fact! Did you know that the lead singer of Nirvana, Kurt Cobain, was known for his love of cars and motorcycles? He owned a 1965 Dodge Dart, a 1965 Jaguar E-Type, and a 1964 Pontiac Parisienne. Unfortunately, he had to sell his cars when he couldn't afford to pay for their repairs.\n",
      "Human: The lead singer of Nirvana was Kurt Cobain\n",
      "AI:  Yes, that is correct. Kurt Cobain was the lead singer and guitarist of the band Nirvana. He was known for his distinctive voice and songwriting abilities, and is considered one of the most influential musicians in alternative rock.\n"
     ]
    }
   ],
   "source": [
    "print(window_memory.buffer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8a053ebd-4729-4489-95eb-ee0001ac1ac4",
   "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: The lead singer of Nirvana had 3 cars\n",
      "AI:  That is an interesting fact! Did you know that the lead singer of Nirvana, Kurt Cobain, was known for his love of cars and motorcycles? He owned a 1965 Dodge Dart, a 1965 Jaguar E-Type, and a 1964 Pontiac Parisienne. Unfortunately, he had to sell his cars when he couldn't afford to pay for their repairs.\n",
      "Human: The lead singer of Nirvana was Kurt Cobain\n",
      "AI:  Yes, that is correct. Kurt Cobain was the lead singer and guitarist of the band Nirvana. He was known for his distinctive voice and songwriting abilities, and is considered one of the most influential musicians in alternative rock.\n",
      "Human: One Corvett, one Proche and one Mercedes\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' I\\'m not sure what you mean by \"One Corvett, one Proche and one Mercedes.\" Are you referring to Kurt Cobain\\'s cars? If so, I can provide more information about them for you.'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_window.predict(input=\"One Corvett, one Proche and one Mercedes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ec110ae9-01db-4449-a6fc-eff9027e54e1",
   "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: The lead singer of Nirvana had 3 cars\n",
      "AI:  That is an interesting fact! Did you know that the lead singer of Nirvana, Kurt Cobain, was known for his love of cars and motorcycles? He owned a 1965 Dodge Dart, a 1965 Jaguar E-Type, and a 1964 Pontiac Parisienne. Unfortunately, he had to sell his cars when he couldn't afford to pay for their repairs.\n",
      "Human: The lead singer of Nirvana was Kurt Cobain\n",
      "AI:  Yes, that is correct. Kurt Cobain was the lead singer and guitarist of the band Nirvana. He was known for his distinctive voice and songwriting abilities, and is considered one of the most influential musicians in alternative rock.\n",
      "Human: One Corvett, one Proche and one Mercedes\n",
      "AI:  I'm not sure what you mean by \"One Corvett, one Proche and one Mercedes.\" Are you referring to Kurt Cobain's cars? If so, I can provide more information about them for you.\n",
      "Human: The thing he liked the most was to show off\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'  Actually, Kurt Cobain was known for being quite introverted and shy. He often struggled with fame and the attention it brought. While he did enjoy collecting cars and motorcycles, it was not to show off, but rather because he had a genuine interest in them.'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_window.predict(input=\"The thing he liked the most was to show off\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "286132d0-4f46-42ab-adf0-ced1aa375101",
   "metadata": {},
   "source": [
    "**As you can see, the first conversational exchange is now out of the window memory**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0e7753f3-ee17-4af8-86b6-76ee32927777",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human: The lead singer of Nirvana was Kurt Cobain\n",
      "AI:  Yes, that is correct. Kurt Cobain was the lead singer and guitarist of the band Nirvana. He was known for his distinctive voice and songwriting abilities, and is considered one of the most influential musicians in alternative rock.\n",
      "Human: One Corvett, one Proche and one Mercedes\n",
      "AI:  I'm not sure what you mean by \"One Corvett, one Proche and one Mercedes.\" Are you referring to Kurt Cobain's cars? If so, I can provide more information about them for you.\n",
      "Human: The thing he liked the most was to show off\n",
      "AI:   Actually, Kurt Cobain was known for being quite introverted and shy. He often struggled with fame and the attention it brought. While he did enjoy collecting cars and motorcycles, it was not to show off, but rather because he had a genuine interest in them.\n"
     ]
    }
   ],
   "source": [
    "print(window_memory.buffer)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9ba145e-6e1a-40e1-9575-e164ab95fe34",
   "metadata": {},
   "source": [
    "## Conversation Token Buffer Memory\n",
    "Similar to the previous one, but this time you can limit the number of tokens stored in memory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2e5083b9-603f-478b-acf6-731ffc62ed2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install tiktoken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5ce22e1e-643c-4f51-99a3-93ad7f9bfd7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.memory import ConversationTokenBufferMemory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "efa35270-e9f6-4b83-8e02-5bbfa77dfeea",
   "metadata": {},
   "outputs": [],
   "source": [
    "token_memory = ConversationTokenBufferMemory(\n",
    "    llm=llm,\n",
    "    max_token_limit=50\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7354bef7-fd3e-4fa1-be03-6d3c208cc228",
   "metadata": {},
   "outputs": [],
   "source": [
    "conversation_token = ConversationChain(\n",
    "    llm=llm, \n",
    "    memory = token_memory,\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9aca9ef-e7bd-4881-852f-c49e53a52323",
   "metadata": {},
   "source": [
    "## Conversation Summary Memory\n",
    "Stores a summary of the previous conversational exchanges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6f7af856-89cb-49f4-831f-8749047598f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.memory import ConversationSummaryBufferMemory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8bcac341-3518-428a-889c-b7ef4177b307",
   "metadata": {},
   "outputs": [],
   "source": [
    "summary_memory = ConversationSummaryBufferMemory(\n",
    "    llm=llm,\n",
    "    max_token_limit=100\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9ca45767-26c1-4495-a495-d075969b06f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "conversation_summary = ConversationChain(\n",
    "    llm=llm, \n",
    "    memory = summary_memory,\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a8525488-6d3d-4eed-b3ba-ad40d18ae215",
   "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: \n",
      "Kurt Cobain dropped out of high school, then worked there as \n",
      "a janitor Even though he was by all accounts a slob, Kurt Cobain \n",
      "worked as a janitor at Weatherwax High School, not long after \n",
      "dropping out of that very school. The dancing janitor in the \n",
      "\"Smells Like Teen Spirit\" music video was an inside joke for \n",
      "those who knew of Cobain's old job.\n",
      "\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' That\\'s interesting! I didn\\'t know that. Kurt Cobain was a highly influential musician and the lead singer of the band Nirvana. He was born in Aberdeen, Washington and attended Weatherwax High School, where he dropped out his senior year. After he dropped out, he went on to work as a janitor at the same high school for a brief period of time before pursuing music full-time. The \"Smells Like Teen Spirit\" music video was filmed at a high school, and the dancing janitor in the video was indeed a nod to Cobain\\'s past job as a janitor. It\\'s a clever inside joke for fans who knew about his background.'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_summary.predict(input=\"\"\"\n",
    "Kurt Cobain dropped out of high school, then worked there as \n",
    "a janitor Even though he was by all accounts a slob, Kurt Cobain \n",
    "worked as a janitor at Weatherwax High School, not long after \n",
    "dropping out of that very school. The dancing janitor in the \n",
    "\"Smells Like Teen Spirit\" music video was an inside joke for \n",
    "those who knew of Cobain's old job.\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8091c886-d5bc-4503-9796-2ad77ef6349a",
   "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",
      "System: \n",
      "The human and AI discuss musician Kurt Cobain's high school years and his job as a janitor at Weatherwax High School. The AI learns that Cobain dropped out of high school and later worked at Weatherwax as a janitor, which was referenced in the \"Smells Like Teen Spirit\" music video.\n",
      "Human: \n",
      "There were at least five different drummers in the band \n",
      "before Dave Grohl. Cobain and Novoselic were always members \n",
      "of Nirvana—formerly known as Skid Row, Pen Cap Chew, Bliss, \n",
      "and Ted Ed Fred—but finding a permanent drummer proved to be \n",
      "even harder than coming up with a decent band name. In the \n",
      "beginning, there was trivia answer Aaron Burckhard, who pissed \n",
      "off Cobain by getting Kurt's car impounded after being arrested \n",
      "for fighting with a police officer. Then there was Melvins \n",
      "drummer Dale Crover, who pounded the skins for Cobain and \n",
      "Novoselic on their first demo tape before moving to San Francisco. \n",
      "Next came Dave Foster, who got arrested for assaulting the \n",
      "son of the mayor of Cosmopolis, Washington. Burckhard briefly \n",
      "returned before announcing he was too hungover to practice one day. \n",
      "Then a mutual friend introduced Cobain and Novoselic to Chad \n",
      "Channing, who hung around for two years before the group's \n",
      "co-founders decided he wasn't cutting it anymore. Mudhoney \n",
      "drummer Dan Peters played on the \"Sliver\" single.\n",
      "\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' It\\'s interesting to note that Cobain and Novoselic were always members of Nirvana, but they went through a string of different drummers before finding Dave Grohl. The first drummer was Aaron Burckhard, who ended up getting Kurt\\'s car impounded after getting arrested for fighting with a police officer. After that, they had Melvins drummer Dale Crover for a bit, but he eventually moved to San Francisco. Dave Foster was another drummer, but he got arrested for assaulting the son of the mayor of Cosmopolis, Washington. Burckhard did return briefly, but then Chad Channing became the drummer for two years before being replaced by Dave Grohl. It\\'s also worth mentioning that Mudhoney drummer Dan Peters played on the \"Sliver\" single. And speaking of high school, Cobain actually dropped out of high school and later worked as a janitor at Weatherwax High School, which was referenced in the \"Smells Like Teen Spirit\" music video.'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_summary.predict(input=\"\"\"\n",
    "There were at least five different drummers in the band \n",
    "before Dave Grohl. Cobain and Novoselic were always members \n",
    "of Nirvana—formerly known as Skid Row, Pen Cap Chew, Bliss, \n",
    "and Ted Ed Fred—but finding a permanent drummer proved to be \n",
    "even harder than coming up with a decent band name. In the \n",
    "beginning, there was trivia answer Aaron Burckhard, who pissed \n",
    "off Cobain by getting Kurt's car impounded after being arrested \n",
    "for fighting with a police officer. Then there was Melvins \n",
    "drummer Dale Crover, who pounded the skins for Cobain and \n",
    "Novoselic on their first demo tape before moving to San Francisco. \n",
    "Next came Dave Foster, who got arrested for assaulting the \n",
    "son of the mayor of Cosmopolis, Washington. Burckhard briefly \n",
    "returned before announcing he was too hungover to practice one day. \n",
    "Then a mutual friend introduced Cobain and Novoselic to Chad \n",
    "Channing, who hung around for two years before the group's \n",
    "co-founders decided he wasn't cutting it anymore. Mudhoney \n",
    "drummer Dan Peters played on the \"Sliver\" single.\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "819f2b96-1e9a-4581-a29a-9e8f5794f6bd",
   "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",
      "System: \n",
      "The human and AI discuss Kurt Cobain's high school years and his job as a janitor at Weatherwax High School. The AI learns that Cobain dropped out of high school and later worked at Weatherwax as a janitor. They also discuss the multiple drummers that Nirvana went through before finding Dave Grohl, including Aaron Burckhard, Dale Crover, Dave Foster, Chad Channing, and Dan Peters. It's also noted that Mudhoney drummer Dan Peters played on the \"Sliver\" single.\n",
      "Human: \n",
      "Back in Washington, Crover performed with Cobain and Novoselic \n",
      "on a seven date tour with Sonic Youth in August 1990, before \n",
      "Dave Grohl's band Scream broke up and Melvins frontman Buzz \n",
      "Osbourne introduced Grohl to Cobain and Novoselic, ending the \n",
      "vicious cycle of rotating drummers.\n",
      "\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\" Yes, that is correct. Before the Scream tour, Crover had actually played with Cobain and Novoselic on a few occasions, including a gig at the North Shore Surf Club in Olympia, Washington. It was during this tour that Grohl's band Scream broke up, and Melvins frontman Buzz Osbourne introduced him to Cobain and Novoselic, leading to him joining the band as their permanent drummer. This ended the cycle of rotating drummers and solidified the lineup of Nirvana.\""
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation_summary.predict(input=\"\"\"\n",
    "Back in Washington, Crover performed with Cobain and Novoselic \n",
    "on a seven date tour with Sonic Youth in August 1990, before \n",
    "Dave Grohl's band Scream broke up and Melvins frontman Buzz \n",
    "Osbourne introduced Grohl to Cobain and Novoselic, ending the \n",
    "vicious cycle of rotating drummers.\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "621a5520-9bdc-450c-b47f-0a29f34dc10b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(summary_memory.buffer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "78721fa3-3912-4f4e-b622-831ffc72f109",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'history': 'System: \\nThe human and AI discuss the early years of Kurt Cobain, including his time working as a janitor at Weatherwax High School and his multiple drummers before finding Dave Grohl. They also mention the tour with Sonic Youth and how Buzz Osbourne introduced Grohl to Cobain and Novoselic, solidifying the lineup of Nirvana.'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "summary_memory.load_memory_variables({})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64b4adf6-63dc-418a-9270-16ad66cc51dc",
   "metadata": {},
   "source": [
    "## More about Memory features in Langchain"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a519a2f-a509-452d-91a9-2a29ac162c8e",
   "metadata": {},
   "source": [
    "* There are other advanced Memory features in LangChain like memory for Agents and memory for Entities.\n",
    "* If you are interested in learning more about those you visit the LangChain documentation [here](https://python.langchain.com/docs/modules/memory/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32cdc07c-3dc5-419f-9654-646023384eb4",
   "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
}
