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 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
41eeccd
falkor-integration
Sep 10, 2024
3bd49b5
Feature: milvus db integration (#1821)
ketangangal Sep 10, 2024
e3aca70
chore: version -> `0.1.12` (#1846)
Dev-Khant Sep 10, 2024
db835cd
Update README.md (#1847)
deshraj Sep 10, 2024
5eeeb4e
Fixing memory adding errors (#1848)
PranavPuranik Sep 10, 2024
ac7b7aa
Added custom prompt support (#1849)
prateekchhikara Sep 10, 2024
6c3d44b
fix-example
Sep 11, 2024
ca95bed
database-config-val
Sep 11, 2024
f40a2e7
Add CONTRIBUTING.md (#1836)
techcontributor Sep 11, 2024
47a8e67
Fixed environment variables priority in OpenAILLM (#1851)
FoliageOwO Sep 12, 2024
f9634b4
add test cases for embeddings (#1829)
reachAnushaKondam Sep 13, 2024
959f4bb
Add Support for Vertex AI Embeddings (#1840)
Divyanshu9822 Sep 13, 2024
d66654b
Add API-Reference docs for Organization/Project (#1858)
Dev-Khant Sep 14, 2024
dc5a26f
Update docs for exisiting APIs to support organization/project (#1859)
Dev-Khant Sep 14, 2024
8e2f7f2
Shows all responses in api-reference (#1865)
Dev-Khant Sep 14, 2024
5b9be67
Migrate session_id -> run_id (#1864)
Dev-Khant Sep 16, 2024
30edf49
chore: version -> 0.1.14 (#1869)
Dev-Khant Sep 16, 2024
3502344
Remove auto install library for chromadb (#1870)
Dev-Khant Sep 16, 2024
0a78cb9
added vector store test cases (#1868)
reachAnushaKondam Sep 16, 2024
55c54be
[Misc] Lint code and fix code smells (#1871)
deshraj Sep 17, 2024
495a66d
gen-graph-use
Sep 17, 2024
19db2cc
def-databasename
Sep 17, 2024
7f3b23d
update-docs
Sep 17, 2024
8c3c9e1
Docs update (#1875)
prateekchhikara Sep 17, 2024
fc88cae
update milvus docs (#1876)
Dev-Khant Sep 17, 2024
07de1c4
update-graph-use
Sep 18, 2024
12da12d
local-pas
Sep 18, 2024
852220a
Merge branch 'main' into falkor-int
galshubeli Sep 18, 2024
9a49aa8
comment-graph-query
Oct 10, 2024
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ 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:

Expand All @@ -166,11 +166,12 @@ from mem0 import Memory

config = {
"graph_store": {
"provider": "neo4j",
"provider": "falkordb",
"config": {
"url": "neo4j+s://xxx",
"username": "neo4j",
"password": "xxx"
"host": "---"
"username": "---",
"password": "---",
"port": "---"
}
},
"version": "v1.1"
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
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'],# if you are using local host, the username and password will not be needed
"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
22 changes: 13 additions & 9 deletions docs/open-source/graph_memory/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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/).
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*).

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:
Expand All @@ -44,11 +44,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": "---"
}
},
"version": "v1.1"
Expand All @@ -70,11 +72,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": "---"
}
},
"version": "v1.1"
Expand Down
38 changes: 34 additions & 4 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 @@ -22,14 +22,34 @@ def check_host_port_or_path(cls, values):
"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"
description="Provider of the data store (e.g., 'falkordb', 'neo4j')",
default="falkordb"
)
config: Neo4jConfig = Field(
config: Union[FalkorDBConfig, Neo4jConfig] = Field(
description="Configuration for the specific data store",
default=None
)
Expand All @@ -47,6 +67,16 @@ 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_"

if config.get("host") == "localhost" or config.get("host") == None:
config.pop("username", None)
config.pop("password", None)

return FalkorDBConfig(**config)
Copy link

Choose a reason for hiding this comment

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

Fix the comparison to None.

The changes to the validate_config method to handle the new FalkorDBConfig look good. However, please address the following static analysis hint:

75-75: Comparison to None should be cond is None

Replace config.get("host") == None with config.get("host") is None to improve code quality.

Apply this diff to fix the comparison:

-if config.get("host") == "localhost" or config.get("host") == None:
+if config.get("host") == "localhost" or config.get("host") is None:
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
elif provider == "falkordb":
config = v.model_dump()
# In case the user try to use diffrent database name
config["database"] = "_default_"
if config.get("host") == "localhost" or config.get("host") == None:
config.pop("username", None)
config.pop("password", None)
return FalkorDBConfig(**config)
elif provider == "falkordb":
config = v.model_dump()
# In case the user try to use diffrent database name
config["database"] = "_default_"
if config.get("host") == "localhost" or config.get("host") is None:
config.pop("username", None)
config.pop("password", None)
return FalkorDBConfig(**config)
Tools
Ruff

75-75: Comparison to None should be cond is None

Replace with cond is None

(E711)

else:
raise ValueError(f"Unsupported graph store provider: {provider}")

46 changes: 46 additions & 0 deletions mem0/graphs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,52 @@

Adhere strictly to these guidelines to ensure high-quality knowledge graph extraction."""

FALKORDB_QUERY = """
galshubeli marked this conversation as resolved.
Show resolved Hide resolved
MATCH (n)
WHERE n.embedding IS NOT NULL AND n.user_id = $user_id
WITH n,
reduce(dot = 0.0, i IN range(0, size(n.embedding)-1) | dot + n.embedding[i] * $n_embedding[i]) /
(sqrt(reduce(l2 = 0.0, i IN range(0, size(n.embedding)-1) | l2 + n.embedding[i] * n.embedding[i])) *
sqrt(reduce(l2 = 0.0, i IN range(0, size($n_embedding)-1) | l2 + $n_embedding[i] * $n_embedding[i]))) AS similarity
WHERE similarity >= $threshold
MATCH (n)-[r]->(m)
RETURN n.name AS source, Id(n) AS source_id, type(r) AS relation, Id(r) AS relation_id, m.name AS destination, Id(m) AS destination_id, similarity
UNION
MATCH (n)
WHERE n.embedding IS NOT NULL AND n.user_id = $user_id
WITH n,
reduce(dot = 0.0, i IN range(0, size(n.embedding)-1) | dot + n.embedding[i] * $n_embedding[i]) /
(sqrt(reduce(l2 = 0.0, i IN range(0, size(n.embedding)-1) | l2 + n.embedding[i] * n.embedding[i])) *
sqrt(reduce(l2 = 0.0, i IN range(0, size($n_embedding)-1) | l2 + $n_embedding[i] * $n_embedding[i]))) AS similarity
WHERE similarity >= $threshold
MATCH (m)-[r]->(n)
RETURN m.name AS source, Id(m) AS source_id, type(r) AS relation, Id(r) AS relation_id, n.name AS destination, Id(n) AS destination_id, similarity
ORDER BY similarity DESC
"""


NEO4J_QUERY = """
MATCH (n)
WHERE n.embedding IS NOT NULL AND n.user_id = $user_id
WITH n,
round(reduce(dot = 0.0, i IN range(0, size(n.embedding)-1) | dot + n.embedding[i] * $n_embedding[i]) /
(sqrt(reduce(l2 = 0.0, i IN range(0, size(n.embedding)-1) | l2 + n.embedding[i] * n.embedding[i])) *
sqrt(reduce(l2 = 0.0, i IN range(0, size($n_embedding)-1) | l2 + $n_embedding[i] * $n_embedding[i]))), 4) AS similarity
WHERE similarity >= $threshold
MATCH (n)-[r]->(m)
RETURN n.name AS source, elementId(n) AS source_id, type(r) AS relation, elementId(r) AS relation_id, m.name AS destination, elementId(m) AS destination_id, similarity
UNION
MATCH (n)
WHERE n.embedding IS NOT NULL AND n.user_id = $user_id
WITH n,
round(reduce(dot = 0.0, i IN range(0, size(n.embedding)-1) | dot + n.embedding[i] * $n_embedding[i]) /
(sqrt(reduce(l2 = 0.0, i IN range(0, size(n.embedding)-1) | l2 + n.embedding[i] * n.embedding[i])) *
sqrt(reduce(l2 = 0.0, i IN range(0, size($n_embedding)-1) | l2 + $n_embedding[i] * $n_embedding[i]))), 4) AS similarity
WHERE similarity >= $threshold
MATCH (m)-[r]->(n)
RETURN m.name AS source, elementId(m) AS source_id, type(r) AS relation, elementId(r) AS relation_id, n.name AS destination, elementId(n) AS destination_id, similarity
ORDER BY similarity DESC
"""


def get_update_memory_prompt(existing_memories, memory, template):
Expand Down
Loading