Skip to content

Commit

Permalink
Add : Conversation Summrize memory : Giving Memory to LLMs
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragjoshi12 committed May 16, 2024
1 parent daacf86 commit 70ab7bd
Showing 1 changed file with 1 addition and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cells":[{"cell_type":"markdown","metadata":{},"source":["#### **❗ Problem :** OpenAI and Other LLM haven’t Memory of past interactions within a session once the session ends or the conversation context is reset. <br>\n","\n","For Example,<br>\n","**User :** My Name is Chirag.<br>\n","**Assistant :** Hi Chirag! 🌟 It's great to meet you. How can I assist you with your studies today? Are there any subjects or topics\n","you're currently working on that you need a bit of help with?\n","\n","**User :** What is my name ? <br>\n","**Assistant :** Oh, I don't actually know your name yet! Would you like to tell me what it is? 😊\n","\n","#### **🧩 Solution :** Solution is **LangChain**. In this notebook we will build **Conversation Summrize memory** using LangChain for Giving Memory to LLMs"]},{"cell_type":"markdown","metadata":{},"source":["## Installing Dependencies 🖥️"]},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"outputs":[],"source":["!pip install langchain"]},{"cell_type":"markdown","metadata":{},"source":["## Importing Dependencies 🖥️"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from langchain.memory import ConversationSummaryMemory, ChatMessageHistory\n","from langchain import OpenAI\n","from langchain.chains import LLMChain\n","from langchain_core.prompts import PromptTemplate\n","import os"]},{"cell_type":"markdown","metadata":{},"source":["## Set YOUR_OPENAI_API_KEY 🔑"]},{"cell_type":"code","execution_count":2,"metadata":{"execution":{"iopub.execute_input":"2024-05-16T08:42:08.664729Z","iopub.status.busy":"2024-05-16T08:42:08.664195Z","iopub.status.idle":"2024-05-16T08:42:08.669827Z","shell.execute_reply":"2024-05-16T08:42:08.668701Z","shell.execute_reply.started":"2024-05-16T08:42:08.664687Z"},"trusted":true},"outputs":[],"source":["os.environ['OPENAI_API_KEY'] = \"YOUR_OPENAI_API_KEY\""]},{"cell_type":"markdown","metadata":{},"source":["## Create Prompt Template "]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["history = ChatMessageHistory()\n","\n","template = \"\"\"You are a chatbot having a conversation with a human.\n","\n","{chat_history}\n","Human: {human_input}\n","Chatbot:\"\"\"\n","\n","prompt = PromptTemplate(\n"," input_variables=[\"chat_history\", \"human_input\"], template=template\n",")"]},{"cell_type":"markdown","metadata":{},"source":["## Create Memory using `ConversationSummaryMemory`📝"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["memory = ConversationSummaryMemory(\n"," llm=OpenAI(temperature=0),\n"," chat_memory=history,\n"," return_messages=True,\n"," memory_key=\"chat_history\"\n",")"]},{"cell_type":"markdown","metadata":{},"source":["## Create LLM Chain 🔗"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["llm = OpenAI(temperature=0,model_name=\"gpt-4o\")\n","llm_chain = LLMChain(\n"," llm=llm,\n"," prompt=prompt,\n"," verbose=True,\n"," memory=memory,\n",")"]},{"cell_type":"markdown","metadata":{},"source":["## Ask Your Question 🙋"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["llm_chain.predict(human_input=\"Hey There, How are you ?\")"]},{"cell_type":"markdown","metadata":{},"source":["### Made with ❤️ by [Chirag Joshi](https://www.kaggle.com/chiragjoshi12/)"]}],"metadata":{"kernelspec":{"display_name":"Python 3","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.10.13"}},"nbformat":4,"nbformat_minor":4}

0 comments on commit 70ab7bd

Please sign in to comment.