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

falkor-integration #2

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,40 @@ history = m.history(memory_id=<memory_id_1>)

### Graph Memory
To initialize Graph Memory you'll need to set up your configuration with graph store providers.
Currently, we support Neo4j as a graph store provider. You can setup [Neo4j](https://neo4j.com/) locally or use the hosted [Neo4j AuraDB](https://neo4j.com/product/auradb/).
Currently, we support FalkorDB and Neo4j as a graph store providers. You can set up [FalkorDB](https://www.falkordb.com/) or [Neo4j](https://neo4j.com/) locally or use the hosted [FalkorDB Cloud](https://app.falkordb.cloud/) or [Neo4j AuraDB](https://neo4j.com/product/auradb/).
Moreover, you also need to set the version to `v1.1` (*prior versions are not supported*).
Here's how you can do it:

```python
from mem0 import Memory

# FalkorDB
config = {
"graph_store": {
"provider": "falkordb",
"config": {
"host": "---"
"username": "---",
"password": "---",
"port": "---"
}
},
"version": "v1.1"
}
# Neo4j
config = {
"graph_store": {
"provider": "neo4j",
"config": {
"url": "neo4j+s://xxx",
"username": "neo4j",
"password": "xxx"
"url": "---"
"username": "---",
"password": "---",
}
},
"version": "v1.1"
}


m = Memory.from_config(config_dict=config)

```
Expand Down
40 changes: 40 additions & 0 deletions cookbooks/mem0_graph_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This example shows how to use graph config to use falkordb graph databese
galshubeli marked this conversation as resolved.
Show resolved Hide resolved
import os
from mem0 import Memory
from dotenv import load_dotenv

# Loading OpenAI API Key
load_dotenv()
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
USER_ID = "test"

# Creating the config dict from the environment variables
config = {
"llm": { # This is the language model configuration, use your carditionals
"provider": "openai",
"config": {
"model": "gpt-4o-mini",
"temperature": 0
}
},
"graph_store": { # See https://app.falkordb.cloud/ for the carditionals
"provider": "falkordb",
"config": {
"host": os.environ['HOST'],
"username": os.environ['USERNAME'],
"password": os.environ['PASSWORD'],
"port": os.environ['PORT']
}
},
"version": "v1.1"
}

# Create the memory class using from config
memory = Memory.from_config(config_dict=config)

# Use the Mem0 to add and search memories
memory.add("I like painting", user_id=USER_ID)
memory.add("I hate playing badminton", user_id=USER_ID)
print(memory.get_all(user_id=USER_ID))
memory.add("My friend name is john and john has a dog named tommy", user_id=USER_ID)
print(memory.search("What I like to do", user_id=USER_ID))
10 changes: 6 additions & 4 deletions docs/open-source/graph_memory/features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ from mem0 import Memory

config = {
"graph_store": {
"provider": "neo4j",
"provider": "falkordb",
"config": {
"url": "neo4j+s://xxx",
"username": "neo4j",
"password": "xxx"
"Database": "falkordb",
"host": "---"
"username": "---",
"password": "---",
"port": "---"
},
"custom_prompt": "Please only extract entities containing sports related relationships and nothing else.",
},
Expand Down
25 changes: 14 additions & 11 deletions docs/open-source/graph_memory/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ allowfullscreen
## Initialize Graph Memory

To initialize Graph Memory you'll need to set up your configuration with graph store providers.
Currently, we support Neo4j as a graph store provider. You can setup [Neo4j](https://neo4j.com/) locally or use the hosted [Neo4j AuraDB](https://neo4j.com/product/auradb/).
Moreover, you also need to set the version to `v1.1` (*prior versions are not supported*).
Currently, we support FalkorDB and Neo4j as a graph store providers. You can set up [FalkorDB](https://www.falkordb.com/) or [Neo4j](https://neo4j.com/) locally or use the hosted [FalkorDB Cloud](https://app.falkordb.cloud/) or [Neo4j AuraDB](https://neo4j.com/product/auradb/).
Moreover, you also need to set the version to `v1.1` (*prior versions are not supported*).

<Note>If you are using Neo4j locally, then you need to install [APOC plugins](https://neo4j.com/labs/apoc/4.1/installation/).</Note>


User can also customize the LLM for Graph Memory from the [Supported LLM list](https://docs.mem0.ai/components/llms/overview) with three levels of configuration:

1. **Main Configuration**: If `llm` is set in the main config, it will be used for all graph operations.
Expand All @@ -57,11 +56,13 @@ from mem0 import Memory

config = {
"graph_store": {
"provider": "neo4j",
"provider": "falkordb",
"config": {
"url": "neo4j+s://xxx",
"username": "neo4j",
"password": "xxx"
"Database": "falkordb",
"host": "---"
"username": "---",
"password": "---",
"port": "---"
Comment on lines +59 to +65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve configuration example clarity.

The configuration example could be enhanced:

  1. The Database parameter should be lowercase for consistency
  2. Consider using clearer placeholder values (e.g., "localhost" for host, "6379" for default port)
  3. Add comments explaining the expected format for each parameter
         "provider": "falkordb",
         "config": {
-            "Database": "falkordb",
-            "host": "---"
-            "username": "---",
-            "password": "---",
-            "port": "---"
+            "database": "falkordb",  # Name of your FalkorDB database
+            "host": "localhost",     # FalkorDB server hostname
+            "username": "<username>", # Your FalkorDB username
+            "password": "<password>", # Your FalkorDB password
+            "port": "6379"           # Default FalkorDB port
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"provider": "falkordb",
"config": {
"url": "neo4j+s://xxx",
"username": "neo4j",
"password": "xxx"
"Database": "falkordb",
"host": "---"
"username": "---",
"password": "---",
"port": "---"
"provider": "falkordb",
"config": {
"database": "falkordb", # Name of your FalkorDB database
"host": "localhost", # FalkorDB server hostname
"username": "<username>", # Your FalkorDB username
"password": "<password>", # Your FalkorDB password
"port": "6379" # Default FalkorDB port
}

}
},
"version": "v1.1"
Expand All @@ -83,11 +84,13 @@ config = {
}
},
"graph_store": {
"provider": "neo4j",
"provider": "falkordb",
"config": {
"url": "neo4j+s://xxx",
"username": "neo4j",
"password": "xxx"
"Database": "falkordb",
"host": "---"
"username": "---",
"password": "---",
"port": "---"
},
"llm" : {
"provider": "openai",
Expand Down
10 changes: 6 additions & 4 deletions docs/open-source/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ from mem0 import Memory

config = {
"graph_store": {
"provider": "neo4j",
"provider": "falkordb",
"config": {
"url": "neo4j+s://---",
"username": "neo4j",
"password": "---"
"Database": "falkordb",
"host": "---"
"username": "---",
"password": "---",
"port": "---"
Comment on lines +66 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Follow consistent parameter naming conventions.

The configuration parameters should follow consistent naming conventions:

  1. The Database parameter should be lowercase to match other parameters.
  2. Consider grouping connection-related parameters (host, port) together.

Apply this diff to improve the configuration structure:

         "provider": "falkordb",
         "config": {
-            "Database": "falkordb",
-            "host": "---"
-            "username": "---",
-            "password": "---",
-            "port": "---"
+            "database": "falkordb",
+            "host": "---",
+            "port": "---",
+            "username": "---",
+            "password": "---"
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"provider": "falkordb",
"config": {
"url": "neo4j+s://---",
"username": "neo4j",
"password": "---"
"Database": "falkordb",
"host": "---"
"username": "---",
"password": "---",
"port": "---"
"provider": "falkordb",
"config": {
"database": "falkordb",
"host": "---",
"port": "---",
"username": "---",
"password": "---"

💡 Codebase verification

Based on the search results, I can now provide a definitive response about the FalkorDB configuration parameters.

The Database parameter in the configuration is incorrect

The FalkorDB configuration in the documentation has an error. According to mem0/graphs/configs.py, the correct configuration structure should not include a Database parameter, as it's handled internally with a fixed value of "default". Here are the correct parameters:

  • host: Host address for the graph database
  • username: Username for the graph database
  • password: Password for the graph database
  • port: Port for the graph database

This is evidenced by the FalkorDBConfig class definition and the fact that the configuration is automatically processed to use "default" as the database name, as shown in the validator code:

config["database"] = "_default_"
🔗 Analysis chain

Verify FalkorDB configuration parameters.

The configuration structure has changed significantly from Neo4j. Let's verify that these are the correct parameters for FalkorDB integration.


Let me try a different search strategy to find FalkorDB configuration information.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for FalkorDB configuration examples and parameter definitions in the codebase.

# Test 1: Search for FalkorDB configuration examples
echo "Searching for FalkorDB configuration examples..."
rg -l 'falkordb.*config'

# Test 2: Search for FalkorDB parameter definitions
echo "Searching for FalkorDB parameter definitions..."
ast-grep --pattern 'class $_(FalkorDB|Config|Settings)'

Length of output: 296


Script:

#!/bin/bash
# Search for FalkorDB related files and configuration patterns

# Look for any files containing FalkorDB (case-insensitive)
echo "=== Files containing FalkorDB ==="
rg -i "falkordb" -A 5 -B 5

# Look for configuration examples in documentation
echo -e "\n=== Configuration examples in docs ==="
fd . docs -e md -e mdx -e txt -X rg -i "falkordb.*config|database.*config"

# Look for configuration related code files
echo -e "\n=== Configuration related files ==="
fd -e py -e js -e ts -e json -X rg -l "(?i)falkordb.*config|database.*connection"

Length of output: 16346

}
},
"version": "v1.1"
Expand Down
32 changes: 29 additions & 3 deletions mem0/graphs/configs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Union

from pydantic import BaseModel, Field, field_validator, model_validator

Expand All @@ -20,11 +20,31 @@ def check_host_port_or_path(cls, values):
if not url or not username or not password:
raise ValueError("Please provide 'url', 'username' and 'password'.")
return values

class FalkorDBConfig(BaseModel):
host: Optional[str] = Field(None, description="Host address for the graph database")
username: Optional[str] = Field(None, description="Username for the graph database")
password: Optional[str] = Field(None, description="Password for the graph database")
port: Optional[int] = Field(None, description="Port for the graph database")
# Default database name is mandatory in langchain
database: str = "_default_"

@model_validator(mode="before")
def check_host_port_or_path(cls, values):
host, port = (
values.get("host"),
values.get("port"),
)
if not host or not port:
raise ValueError(
"Please provide 'host' and 'port'."
)
return values


class GraphStoreConfig(BaseModel):
provider: str = Field(description="Provider of the data store (e.g., 'neo4j')", default="neo4j")
config: Neo4jConfig = Field(description="Configuration for the specific data store", default=None)
provider: str = Field(description="Provider of the data store (e.g., 'falkordb', 'neo4j')", default="falkordb")
config: Union[FalkorDBConfig, Neo4jConfig] = Field(description="Configuration for the specific data store", default=None)
llm: Optional[LlmConfig] = Field(description="LLM configuration for querying the graph store", default=None)
custom_prompt: Optional[str] = Field(
description="Custom prompt to fetch entities from the given text", default=None
Expand All @@ -35,5 +55,11 @@ def validate_config(cls, v, values):
provider = values.data.get("provider")
if provider == "neo4j":
return Neo4jConfig(**v.model_dump())
elif provider == "falkordb":
config = v.model_dump()
# In case the user try to use diffrent database name
config["database"] = "_default_"

return FalkorDBConfig(**config)
else:
raise ValueError(f"Unsupported graph store provider: {provider}")
Loading