Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to apply mem0 in multi-turn dialog #2256

Open
chenk-gd opened this issue Feb 25, 2025 · 3 comments
Open

How to apply mem0 in multi-turn dialog #2256

chenk-gd opened this issue Feb 25, 2025 · 3 comments
Assignees

Comments

@chenk-gd
Copy link

Suppose there is a multi-turn dialog:
USER: What is the name of the hero of the novel The Count of Monte Cristo?
ASSISTANT:The hero of the novel "The Count of Monte Cristo" by Alexandre Dumas is Edmond Dantès.
USER:What is the name of his fiancee?

The correct answer is: His fiancée is named Mercédès.

if use mem0

from openai import OpenAI
from mem0 import Memory

openai_client = OpenAI()
memory = Memory()

def chat_with_memories(message: str, user_id: str = "default_user") -> str:
    # Retrieve relevant memories
    relevant_memories = memory.search(query=message, user_id=user_id, limit=3)
    memories_str = "\n".join(f"- {entry['memory']}" for entry in relevant_memories)
    
    # Generate Assistant response
    system_prompt = f"You are a helpful AI. Answer the question based on query and memories.\nUser Memories:\n{memories_str}"
    messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": message}]
    response = openai_client.chat.completions.create(model="gpt-4o-mini", messages=messages)
    assistant_response = response.choices[0].message.content

    # Create new memories from the conversation
    messages.append({"role": "assistant", "content": assistant_response})
    memory.add(messages, user_id=user_id)

    return assistant_response

def main():
    print("Chat with AI (type 'exit' to quit)")
    while True:
        user_input = input("You: ").strip()
        if user_input.lower() == 'exit':
            print("Goodbye!")
            break
        print(f"AI: {chat_with_memories(user_input)}")

if __name__ == "__main__":
    main()

Its response is: I'm sorry, but I don't have any information about someone's fiancée. If you provide more context or details, I may be able to help with something else.

I traced the execution of the code and found out that it was the LLM using FACT_RETRIEVAL_PROMPT to extract the message that returned null, so no message was added to memory.

So my question is, what support can mem0 provide to implement such a simple multi-turn dialog, or does it require the user to manually put the last few rounds of dialog into the prompt?

@deshraj
Copy link
Collaborator

deshraj commented Feb 25, 2025

Thanks for reporting the issue @chenk-gd. We are looking into the issue.

@deshraj deshraj assigned deshraj and unassigned prateekchhikara Feb 25, 2025
@deshraj
Copy link
Collaborator

deshraj commented Feb 25, 2025

Hey @chenk-gd, I tried the example you posted and seems like there is nothing to remember from the conversation and hence no memories were created.

Regarding the multi-turn conversation question, you will have to pass the last-k rounds of the conversation for now to extract the best possible memories. However, we are working on a newer version of the algorithm using which you will only have to pass the new messages and Mem0 will maintain the history for you.

@chenk-gd
Copy link
Author

chenk-gd commented Feb 25, 2025

@deshraj Thanks for the reply, I'll be following your progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants