Skip to content

Commit

Permalink
Merge pull request #895 from Undertone0809/hizeros/mem0
Browse files Browse the repository at this point in the history
Hizeros/mem0
  • Loading branch information
Undertone0809 authored Sep 6, 2024
2 parents ac1bb82 + 218155b commit 8b06e45
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Empty file added example/mem0/README.md
Empty file.
48 changes: 48 additions & 0 deletions example/mem0/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pne
import streamlit as st
from core import PersonalHealingAssistant


def main():
config = pne.beta.st.model_sidebar()
# todo llm default answer with cn
with st.sidebar:
mem0_user_id = st.text_input("mem0 user id", type="password")
mem0_api_key = st.text_input(
"mem0 API Key", key="provider_mem0_api_key", type="password"
)

st.title("PersonalHealingAssistant")
st.caption(
"""
Personal Healing Assistant combines pne and mem0ai to create a personalized healing assistant for you \n
🚀 Power by [promptulate](https://github.com/Undertone0809/promptulate)
""" # noqa
)
st.chat_message("assistant").write(
"I am your personal healing assistant, how can I help you? "
)

ai_assistant = PersonalHealingAssistant()

if prompt := st.chat_input("Please enter what you want to know "):
if not config.api_key:
st.info("Please add your model API key to continue.")
st.stop()

if not mem0_api_key:
st.error("Please provide your mem0 API Key to continue.")
st.stop()

ai_assistant.set_mem0_api_key(mem0_api_key)

answer = ai_assistant.ask_question(
question=prompt, user_id=mem0_user_id, config=config
)

st.chat_message("user").write(prompt)
st.chat_message("assistant").write_stream(answer)


if __name__ == "__main__":
main()
41 changes: 41 additions & 0 deletions example/mem0/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pne
from mem0 import MemoryClient


class PersonalHealingAssistant:
def __init__(self):
self.memory = None
self.messages = [
{"role": "system", "content": "You are a personal healing AI Assistant."}
]

def set_mem0_api_key(self, mem0_api_key: str):
self.memory = MemoryClient(api_key=mem0_api_key)

def ask_question(self, question: str, user_id: str, config) -> str:
# Fetch previous related memories
previous_memories = self.search_memories(question, user_id=user_id)
prompt = question
if previous_memories:
prompt = f"User input: {question}\n Previous memories: {previous_memories}"
self.messages.append({"role": "user", "content": prompt})

response = pne.chat(
model=config.model_name,
stream=True,
messages=self.messages,
model_config={"api_base": config.api_base, "api_key": config.api_key},
)
self.messages.append({"role": "assistant", "content": response})

# Store the question in memory
self.memory.add(question, user_id=user_id)
return response

def get_memories(self, user_id):
memories = self.memory.get_all(user_id=user_id)
return memories

def search_memories(self, query, user_id):
memories = self.memory.search(query, user_id=user_id)
return memories
3 changes: 3 additions & 0 deletions example/mem0/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
streamlit>=1.28
pne
mem0ai

0 comments on commit 8b06e45

Please sign in to comment.