Added new demo
This commit is contained in:
parent
49df320fce
commit
ec4942744c
3 changed files with 111 additions and 0 deletions
39
llm_app_personalized_memory/README.md
Normal file
39
llm_app_personalized_memory/README.md
Normal file
|
|
@ -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
|
||||||
|
```
|
||||||
69
llm_app_personalized_memory/llm_app_memory.py
Normal file
69
llm_app_personalized_memory/llm_app_memory.py
Normal file
|
|
@ -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.")
|
||||||
3
llm_app_personalized_memory/requirements.txt
Normal file
3
llm_app_personalized_memory/requirements.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
streamlit
|
||||||
|
openai
|
||||||
|
mem0ai
|
||||||
Loading…
Reference in a new issue