mem0ai / mem0

The Memory layer for your AI apps
https://mem0.ai
Apache License 2.0
21.6k stars 1.97k forks source link

add response to m.add() call #1732

Open femto opened 3 weeks ago

femto commented 3 weeks ago

Description

Added useful return information for m.add() call, discussed in https://github.com/mem0ai/mem0/issues/1499, where original implementation doesn't have useful information returned after calling m.add(), which is not good.

Fixes https://github.com/mem0ai/mem0/issues/1499

Type of change

Please delete options that are not relevant.

How Has This Been Tested?

use the following script, change password to suitable value

from mem0 import Memory
import os

from dotenv import load_dotenv
load_dotenv()

config = {
    "version": "v1.1",
    "llm": {
        "provider": "openai",
        "config": {
            #"model": "deepseek-coder",
            "model": "gpt-4o-mini",
            "temperature": 0.2,
            "max_tokens": 1500,
            "top_p" : 1.0
        }
    },
"embedder": {
        "provider": "ollama",
        "config": {
            "model": "nomic-embed-text:latest",
            "ollama_base_url": "http://localhost:11434",
        },
    },
"vector_store": {
        "provider": "qdrant",
        "config": {
            "collection_name": "mem0",
            "host": "localhost",
            "port": 6333,
            "on_disk": True,
            "embedding_model_dims": 768

        }
    },
"graph_store": {
        "provider": "neo4j",
        "config": {
            "url": "neo4j+s://4f9f66c6.databases.neo4j.io:7687",
            "username": "neo4j",
            "password": "xxx"
        }
    },
}
m = Memory.from_config(config)
result = m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
print(result)
result = m.add("doesn't like to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
print(result)
#print(all_memories)
# Store a memory from any unstructured text
#m.delete_all(user_id="alice")
result = m.add("I am working on improving my tennis skills. Suggest some online courses.", user_id="alice", metadata={"category": "hobbies"})
print(result)

outputs:

{'memories': defaultdict(<class 'list'>, {'delete': [{'id': '0f5579d1-03ea-4037-ac61-f0ccfffca6ca', 'data': None}]}), 'graph': defaultdict(<class 'list'>, {'update': [{'source': 'alice', 'destination': 'cricket', 'relationship': 'LIKES_TO_PLAY'}, {'source': 'alice', 'destination': 'weekends', 'relationship': 'PLAYS_ON'}]})}
{'memories': defaultdict(<class 'list'>, {'add': [{'id': '761826cb-1f46-4bda-9de6-7fbee9a1a86d', 'data': "Doesn't like to play cricket on weekends."}]}), 'graph': defaultdict(<class 'list'>, {'update': [{'source': 'alice', 'destination': 'cricket', 'relationship': 'dislikes playing cricket on weekends'}, {'source': 'alice', 'destination': 'weekends', 'relationship': 'dislikes playing cricket on weekends'}]})}
{'memories': defaultdict(<class 'list'>, {'update': [{'id': '761826cb-1f46-4bda-9de6-7fbee9a1a86d', 'data': "Doesn't like to play cricket on weekends. Working on improving tennis skills and interested in online courses for tennis."}], 'add': [{'id': '1bcc6290-0be9-4c1d-8a52-858554be0b4c', 'data': 'Working on improving tennis skills. Interested in online courses for tennis.'}]}), 'graph': defaultdict(<class 'list'>, {'update': [{'source': 'alice', 'destination': 'tennis', 'relationship': 'IMPROVING_SKILLS_IN'}]})}

Checklist:

Maintainer Checklist

CLAassistant commented 3 weeks ago

CLA assistant check
All committers have signed the CLA.