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

Add support for custom prompt override in memory.add function #1998

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Zncl2222
Copy link
Contributor

Description

Add support for overriding the custom prompt by using the prompt argument in the memory.add function.

Originally, the prompt argument existed in memory.add but was not utilized. I have implemented functionality to allow it to override the global custom_prompt setting from the configuration.

Users can now easily override the default prompt when adding individual entries, without needing to change the global configuration. This makes memory management more flexible and adaptable to specific needs.

from mem0 import Memory

config = {
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-4o",
            "temperature": 0.2,
            "max_tokens": 1500,
        }
    },
    "custom_prompt": custom_prompt,
    "version": "v1.1"
}

# This use the custom_prompt from config
m = Memory.from_config(config_dict=config)

# Use second_custom_prompt to override the default prompt for this memory entry
second_custom_prompt = "Prompt...."
m.add("Yesterday, I ordered a laptop, the order id is 12345", user_id="alice", prompt=second_custom_prompt)

# Use third_custom_prompt to override the default prompt for this memory entry
third_custom_prompt = "Prompt...."
m.add("I like to eat pasta!", user_id="alice", prompt=third_custom_prompt)

Fixes # (No related issue, this is a new feature)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

  • Unit Test
  • Test Script

Unitest has been added, you can also test the functionality by the following code snippet:

import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "your-api-key"

custom_prompt = """
Add "User likes to play video games" to memory only.

Example:
Output: {"facts": ["User likes to play video games"]}

Return the facts and preferences in JSON format as shown above.
"""

config = {
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-4o",
            "temperature": 0.2,
            "max_tokens": 1500,
        }
    },
    "custom_prompt": custom_prompt,
    "version": "v1.1"
}

mem0 = Memory.from_config(config)

prompt = """
Add "User likes to eat cookie" to memory only.

Example:
Output: {"facts": ["User likes to eat cookie"]}

Return the facts and preferences in JSON format as shown above.
"""

# Add memory using the gloable custom_prompt configuration
# Output {'results': [{'id': '047f379f-e66e-4a31-b3c1-3cc65cad8233', 'memory': 'User likes to play video games', 'event': 'ADD'}], 'relations': []}
memory = mem0.add("My name is Zncl2222", user_id='zncl2222')
print(memory)

# Add memory using the specific prompt override
# Output{'results': [{'id': '639353d4-dc28-45cc-a3a6-5141f1daa7d7', 'memory': 'User likes to eat cookie', 'event': 'ADD'}], 'relations': []}
memory2 = mem0.add("My name is Zncl2222", user_id='zncl2222', prompt=prompt)
print(memory2)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Made sure Checks passed

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

Successfully merging this pull request may close these issues.

1 participant