{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "15d94dba-6acc-4d42-b4c9-5c417fae885d", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "from dotenv import load_dotenv, find_dotenv\n", "_ = load_dotenv(find_dotenv())\n", "\n", "from langchain.chains import ConversationChain\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.memory import ConversationBufferMemory\n", "\n", "llm = ChatOpenAI(temperature=0.0)\n", "memory = ConversationBufferMemory()\n", "conversion = ConversationChain(\n", " llm=llm,\n", " memory=memory,\n", " verbose=False\n", ")" ] }, { "cell_type": "code", "execution_count": 2, "id": "29f611c2-3693-493b-91da-ad11a9191aeb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"The average distance between the Sun and Earth is about 93 million miles (150 million kilometers). This distance is known as an astronomical unit (AU). However, it's important to note that the distance between the Sun and Earth varies slightly throughout the year due to the elliptical shape of Earth's orbit around the Sun.\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversion.predict(input='what is the distance between sun and earth?')" ] }, { "cell_type": "code", "execution_count": 3, "id": "5ebb0e23-9bde-49ad-b187-8a7b8765270a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'5 + 4 equals 9.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversion.predict(input='whats is 5+4')" ] }, { "cell_type": "code", "execution_count": 4, "id": "65e5b06b-3f08-43e2-8fd1-ac3d5106cf40", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Your name is Sushant.'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversion.predict(input='what is my name')" ] }, { "cell_type": "code", "execution_count": 5, "id": "8fd49cb9-b0f8-494b-87b4-84762ced5ba5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Human: Hi, My name is sushant\\nAI: Hello Sushant! It's nice to meet you. How can I assist you today?\\nHuman: whats is 5+4\\nAI: 5 + 4 equals 9.\\nHuman: what is my name\\nAI: Your name is Sushant.\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.buffer" ] }, { "cell_type": "code", "execution_count": 6, "id": "a326a54b-cdf3-4be1-ad6f-465433b60d6b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history': \"Human: Hi, My name is sushant\\nAI: Hello Sushant! It's nice to meet you. How can I assist you today?\\nHuman: whats is 5+4\\nAI: 5 + 4 equals 9.\\nHuman: what is my name\\nAI: Your name is Sushant.\"}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.load_memory_variables({})" ] }, { "cell_type": "code", "execution_count": 7, "id": "9b43105b-147f-4d45-bd12-11d04c5ce230", "metadata": {}, "outputs": [], "source": [ "memory.save_context({\"input\": \"Hi There!\"}, {\"output\": \"Hello\"})" ] }, { "cell_type": "code", "execution_count": 8, "id": "5be149da-ab1b-447d-87c2-17173aae4221", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history': \"Human: Hi, My name is sushant\\nAI: Hello Sushant! It's nice to meet you. How can I assist you today?\\nHuman: whats is 5+4\\nAI: 5 + 4 equals 9.\\nHuman: what is my name\\nAI: Your name is Sushant.\\nHuman: Hi There!\\nAI: Hello\"}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.load_memory_variables({})" ] }, { "cell_type": "code", "execution_count": 9, "id": "99869539-72ae-4ee9-a5dc-2a490d4a3301", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history': \"Human: I'm fine\\nAI: Ok\"}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.memory import ConversationBufferWindowMemory\n", "memory = ConversationBufferWindowMemory(k=1) # k=1 where k equals to no of conversion we want here 1.\n", "\n", "memory.save_context({\"input\": 'Hello'}, {\"output\": 'There'})\n", "memory.save_context({\"input\": \"I'm fine\"}, {\"output\": 'Ok'})\n", "\n", "memory.load_memory_variables({})" ] }, { "cell_type": "code", "execution_count": 10, "id": "eb7f4dca-2fd7-4980-91ab-ee3db6532ce2", "metadata": {}, "outputs": [], "source": [ "llm = ChatOpenAI(temperature=0.0)\n", "memory = ConversationBufferMemory()\n", "conversation = ConversationChain(\n", " llm=llm,\n", " memory=memory,\n", " verbose=True\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "id": "4da61d3b-a455-42a3-9654-5d54abb7ee76", "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 I'm Sushant\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'Hello Sushant! How can I assist you today?'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"Hello I'm Sushant\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "3cbafc44-0668-44bc-9cec-869b01f6f315", "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 I'm Sushant\n", "AI: Hello Sushant! How can I assist you today?\n", "Human: who is armstrong\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'Armstrong can refer to several different people. One famous person named Armstrong is Neil Armstrong, who was an American astronaut and the first person to walk on the moon. Another famous Armstrong is Louis Armstrong, who was a jazz musician and trumpeter. There are also many other people with the last name Armstrong, so it would depend on which specific Armstrong you are referring to.'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"who is armstrong\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "b30e8043-3df1-42c0-873b-958aaea17f04", "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 I'm Sushant\n", "AI: Hello Sushant! How can I assist you today?\n", "Human: who is armstrong\n", "AI: Armstrong can refer to several different people. One famous person named Armstrong is Neil Armstrong, who was an American astronaut and the first person to walk on the moon. Another famous Armstrong is Louis Armstrong, who was a jazz musician and trumpeter. There are also many other people with the last name Armstrong, so it would depend on which specific Armstrong you are referring to.\n", "Human: okay whats my name.?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\"I'm sorry, but I don't have access to personal information about individuals unless it has been shared with me in the course of our conversation. I am designed to respect user privacy and confidentiality.\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "conversation.predict(input=\"okay whats my name.?\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "d8c620c0-4ff3-4013-aada-c75982d0cc8f", "metadata": {}, "outputs": [], "source": [ "from langchain.memory import ConversationTokenBufferMemory\n", "from langchain.llms import OpenAI\n", "llm = ChatOpenAI(temperature=0.0)\n", "\n", "memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=10)\n", "memory.save_context({\"input\": \"Hi There can I ask you something\"}, {\"Output\": \"Yes You can please ask now\"})\n", "memory.save_context({\"input\": \"Hi There ng\"}, {\"Output\": \"ask now\"})" ] }, { "cell_type": "code", "execution_count": 17, "id": "1586a789-dfbb-4077-a5ae-d8031738ae40", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history': 'AI: ask now'}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.load_memory_variables({})" ] }, { "cell_type": "code", "execution_count": 19, "id": "78037aaa-080c-4dd4-aa96-bea8c8644efb", "metadata": {}, "outputs": [], "source": [ "from langchain.memory import ConversationSummaryBufferMemory\n", "schedule = \"There is a meeting at 8am with your product team. \\\n", "You will need your powerpoint presentation prepared. \\\n", "9am-12pm have time to work on your LangChain \\\n", "project which will go quickly because Langchain is such a powerful tool. \\\n", "At Noon, lunch at the italian resturant with a customer who is driving \\\n", "from over an hour away to meet you to understand the latest in AI. \\\n", "Be sure to bring your laptop to show the latest LLM demo.\"\n", "\n", "memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)\n", "memory.save_context({\"input\": \"Hello\"}, {\"output\": \"What's up\"})\n", "memory.save_context({\"input\": \"Not much, just hanging\"},\n", " {\"output\": \"Cool\"})\n", "memory.save_context({\"input\": \"What is on the schedule today?\"}, \n", " {\"output\": f\"{schedule}\"})\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "f326d9da-da8d-41a8-a449-c23c8fc08601", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history': 'System: The human and AI exchange greetings. The human mentions that they are not doing much. The AI informs the human about their schedule for the day, including a meeting with the product team, working on the LangChain project, and having lunch with a customer to discuss the latest in AI. The AI also reminds the human to bring their laptop to show a demo.'}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.load_memory_variables({})" ] }, { "cell_type": "code", "execution_count": 21, "id": "57c3a5fd-356a-42de-965b-7513a1f686c4", "metadata": {}, "outputs": [], "source": [ "conversation = ConversationChain(\n", " llm=llm, \n", " memory = memory,\n", " verbose=True\n", ")" ] }, { "cell_type": "code", "execution_count": 22, "id": "f5286d9b-cb56-4530-b532-c1b90f8f4e28", "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: The human and AI exchange greetings. The human mentions that they are not doing much. The AI informs the human about their schedule for the day, including a meeting with the product team, working on the LangChain project, and having lunch with a customer to discuss the latest in AI. The AI also reminds the human to bring their laptop to show a demo.\n", "Human: What would be a good demo to show?\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'A good demo to show would be a live demonstration of the LangChain project. You can showcase the features and functionality of the language translation software, highlighting its accuracy and efficiency. Additionally, you could also demonstrate any recent updates or improvements made to the project. This would give the customer a firsthand experience of the capabilities of the AI technology and its potential benefits for their business.'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "conversation.predict(input=\"What would be a good demo to show?\")" ] }, { "cell_type": "code", "execution_count": 23, "id": "620520ef-7cf1-421b-be81-b7f0c93ae6d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'history': 'System: The human and AI exchange greetings. The human mentions that they are not doing much. The AI informs the human about their schedule for the day, including a meeting with the product team, working on the LangChain project, and having lunch with a customer to discuss the latest in AI. The AI also reminds the human to bring their laptop to show a demo.\\nHuman: What would be a good demo to show?\\nAI: A good demo to show would be a live demonstration of the LangChain project. You can showcase the features and functionality of the language translation software, highlighting its accuracy and efficiency. Additionally, you could also demonstrate any recent updates or improvements made to the project. This would give the customer a firsthand experience of the capabilities of the AI technology and its potential benefits for their business.'}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "memory.load_memory_variables({})" ] }, { "cell_type": "code", "execution_count": null, "id": "8583b417-2698-408a-b84d-6ec92f91d494", "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 }