From ec4942744cd8e2b3a9a4b5190a0db184301d0406 Mon Sep 17 00:00:00 2001 From: ShubhamSaboo Date: Thu, 18 Jul 2024 21:31:55 -0500 Subject: [PATCH] Added new demo --- llm_app_personalized_memory/README.md | 39 +++++++++++ llm_app_personalized_memory/llm_app_memory.py | 69 +++++++++++++++++++ llm_app_personalized_memory/requirements.txt | 3 + 3 files changed, 111 insertions(+) create mode 100644 llm_app_personalized_memory/README.md create mode 100644 llm_app_personalized_memory/llm_app_memory.py create mode 100644 llm_app_personalized_memory/requirements.txt diff --git a/llm_app_personalized_memory/README.md b/llm_app_personalized_memory/README.md new file mode 100644 index 0000000..2a2e995 --- /dev/null +++ b/llm_app_personalized_memory/README.md @@ -0,0 +1,39 @@ +## 🧠 LLM App with Memory +This Streamlit app is an AI-powered chatbot that uses OpenAI's GPT-4o model with a persistent memory feature. It allows users to have conversations with the AI while maintaining context across multiple interactions. + +### Features + +- Utilizes OpenAI's GPT-4o model for generating responses +- Implements persistent memory using Mem0 and Qdrant vector store +- Allows users to view their conversation history +- Provides a user-friendly interface with Streamlit + + +### How to get Started? + +1. Clone the GitHub repository +```bash +git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git +``` + +2. Install the required dependencies: + +```bash +pip install -r requirements.txt +``` + +3. Ensure Qdrant is running: +The app expects Qdrant to be running on localhost:6333. Adjust the configuration in the code if your setup is different. + +```bash +docker pull qdrant/qdrant + +docker run -p 6333:6333 -p 6334:6334 \ + -v $(pwd)/qdrant_storage:/qdrant/storage:z \ + qdrant/qdrant +``` + +4. Run the Streamlit App +```bash +streamlit run llm_app_memory.py +``` diff --git a/llm_app_personalized_memory/llm_app_memory.py b/llm_app_personalized_memory/llm_app_memory.py new file mode 100644 index 0000000..4712147 --- /dev/null +++ b/llm_app_personalized_memory/llm_app_memory.py @@ -0,0 +1,69 @@ +import streamlit as st +from mem0 import Memory +from openai import OpenAI + +st.title("LLM App with Memory 🧠") +st.caption("LLM App with personalized memory layer that remembers ever user's choice and interests") + +openai_api_key = st.text_input("Enter OpenAI API Key", type="password") + +if openai_api_key: + # Initialize OpenAI client + client = OpenAI(api_key=openai_api_key) + + # Initialize Mem0 with Qdrant + config = { + "vector_store": { + "provider": "qdrant", + "config": { + "host": "localhost", + "port": 6333, + } + }, + } + + memory = Memory.from_config(config) + + user_id = st.text_input("Enter your Username") + + prompt = st.text_input("Ask ChatGPT") + + if st.button('Chat with LLM'): + with st.spinner('Searching...'): + relevant_memories = memory.search(query=prompt, user_id=user_id) + # Prepare context with relevant memories + context = "Relevant past information:\n" + + for mem in relevant_memories: + context += f"- {mem['text']}\n" + + # Prepare the full prompt + full_prompt = f"{context}\nHuman: {prompt}\nAI:" + + # Get response from GPT-4 + response = client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "system", "content": "You are a helpful assistant with access to past conversations."}, + {"role": "user", "content": full_prompt} + ] + ) + + answer = response.choices[0].message.content + + st.write("Answer: ", answer) + + # Add AI response to memory + memory.add(answer, user_id=user_id) + + + # Sidebar option to show memory + st.sidebar.title("Memory Info") + if st.sidebar.button("View Memory Info"): + memories = memory.get_all(user_id=user_id) + if memories: + st.sidebar.write(f"You are viewing memory for user **{user_id}**") + for mem in memories: + st.sidebar.write(f"- {mem['text']}") + else: + st.sidebar.info("No learning history found for this user ID.") \ No newline at end of file diff --git a/llm_app_personalized_memory/requirements.txt b/llm_app_personalized_memory/requirements.txt new file mode 100644 index 0000000..088b5ab --- /dev/null +++ b/llm_app_personalized_memory/requirements.txt @@ -0,0 +1,3 @@ +streamlit +openai +mem0ai \ No newline at end of file