mem0 icon indicating copy to clipboard operation
mem0 copied to clipboard

How to apply mem0 in multi-turn dialog

Open chenk-gd opened this issue 10 months ago • 3 comments

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?

chenk-gd avatar Feb 25 '25 02:02 chenk-gd

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

deshraj avatar Feb 25 '25 02:02 deshraj

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.

deshraj avatar Feb 25 '25 02:02 deshraj

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

chenk-gd avatar Feb 25 '25 02:02 chenk-gd