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

README example issue #2239

Closed
bonk1t opened this issue Feb 21, 2025 · 1 comment
Closed

README example issue #2239

bonk1t opened this issue Feb 21, 2025 · 1 comment

Comments

@bonk1t
Copy link

bonk1t commented Feb 21, 2025

In the readme.md we add the system message to memories on every iteration:

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()

Is this a desired behavior?

@Dev-Khant
Copy link
Member

Hey @bonk1t Currently we don't utilize the system prompt passed in the messages in Mem0 client. So passing system message multiple times won't have any impact on memory creation. This is a simple example to showcase the functionality but if you are interested more in how to alter memory creation based on your requirements you should check out this doc: https://docs.mem0.ai/features/custom-instructions

Here the system prompt is only used by the OpenAI call.

@bonk1t bonk1t closed this as completed Feb 21, 2025
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

2 participants