-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #895 from Undertone0809/hizeros/mem0
Hizeros/mem0
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
streamlit>=1.28 | ||
pne | ||
mem0ai |