Skip to content

Commit

Permalink
mem0 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bboynton97 committed Nov 8, 2024
1 parent f8e8b0e commit 80ec8ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
52 changes: 26 additions & 26 deletions agentstack/templates/crewai/tools/mem0_tool.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import json

from crewai_tools import tool
from mem0 import Memory
from mem0 import MemoryClient
from dotenv import load_dotenv
import os

load_dotenv()

# Optionally configure mem0 to use any other store
# https://docs.mem0.ai/components/vectordbs/config#config
config = {
"graph_store": {
"provider": "neo4j",
"config": {
"url": os.getenv("NEO4J_URL"),
"username": os.getenv("NEO4J_USERNAME", 'neo4j'),
"password": os.getenv("NEO4J_PASSWORD"),
}
},
"version": "v1.1"
}

memory = Memory.from_config(config)
# These functions can be extended by changing the user_id parameter
# Memories are sorted by user_id

# These functions can be extended by taking an optional user_id parameter
# https://docs.mem0.ai/integrations/multion#add-memories-to-mem0
MEM0_API_KEY = os.getenv('MEM0_API_KEY')
client = MemoryClient(api_key=MEM0_API_KEY)

# These tools will only save information about the user
# "Potato is a vegetable" is not a memory
# "My favorite food is potatoes" IS a memory

@tool("Write to Memory")
def write_to_memory(data: str) -> str:
"""Writes data to the memory store"""
result = memory.add(data)
return f"Memory added with ID: {result['id']}"
def write_to_memory(user_message: str) -> str:
"""
Writes data to the memory store for a user. The tool will decide what
specific information is important to store as memory.
"""
messages = [
{"role": "user", "content": user_message},
]
result = client.add(messages, user_id='default') # configure user
return json.dumps(result)


@tool("Read from Memory")
def read_from_memory(query: str) -> str:
"""Reads memories based on a query."""
memories = memory.search(query=query)
if memories["memories"]:
return "\n".join([mem["data"] for mem in memories["memories"]])
"""
Reads memories related to user based on a query.
"""
memories = client.search(query=query, user_id='default')
if memories:
return "\n".join([mem['memory'] for mem in memories])
else:
return "No relevant memories found."
4 changes: 2 additions & 2 deletions agentstack/tools/mem0.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mem0",
"package": "poetry add mem0ai",
"env": "NEO4J_URL=...\nNEO4J_USERNAME=...\nNEO4J_PASSWORD=...",
"env": "MEM0_API_KEY=...",
"tools": ["write_to_memory", "read_from_memory"],
"cta": "Optionally: setup your graph db hosting at https://app.mem0.ai/login"
"cta": "Create your mem0 API key at https://mem0.ai/"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "agentstack"
version = "0.1.7-rc2"
version = "0.1.7-rc3"
description = "The fastest way to build robust AI agents"
authors = [
{ name="Braelyn Boynton", email="[email protected]" }
Expand Down

0 comments on commit 80ec8ae

Please sign in to comment.