graphiti-core 0.12.0rc1__py3-none-any.whl → 0.24.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphiti_core/cross_encoder/bge_reranker_client.py +12 -2
- graphiti_core/cross_encoder/gemini_reranker_client.py +161 -0
- graphiti_core/cross_encoder/openai_reranker_client.py +7 -5
- graphiti_core/decorators.py +110 -0
- graphiti_core/driver/__init__.py +19 -0
- graphiti_core/driver/driver.py +124 -0
- graphiti_core/driver/falkordb_driver.py +362 -0
- graphiti_core/driver/graph_operations/graph_operations.py +191 -0
- graphiti_core/driver/kuzu_driver.py +182 -0
- graphiti_core/driver/neo4j_driver.py +117 -0
- graphiti_core/driver/neptune_driver.py +305 -0
- graphiti_core/driver/search_interface/search_interface.py +89 -0
- graphiti_core/edges.py +287 -172
- graphiti_core/embedder/azure_openai.py +71 -0
- graphiti_core/embedder/client.py +2 -1
- graphiti_core/embedder/gemini.py +116 -22
- graphiti_core/embedder/voyage.py +13 -2
- graphiti_core/errors.py +8 -0
- graphiti_core/graph_queries.py +162 -0
- graphiti_core/graphiti.py +705 -193
- graphiti_core/graphiti_types.py +4 -2
- graphiti_core/helpers.py +87 -10
- graphiti_core/llm_client/__init__.py +16 -0
- graphiti_core/llm_client/anthropic_client.py +159 -56
- graphiti_core/llm_client/azure_openai_client.py +115 -0
- graphiti_core/llm_client/client.py +98 -21
- graphiti_core/llm_client/config.py +1 -1
- graphiti_core/llm_client/gemini_client.py +290 -41
- graphiti_core/llm_client/groq_client.py +14 -3
- graphiti_core/llm_client/openai_base_client.py +261 -0
- graphiti_core/llm_client/openai_client.py +56 -132
- graphiti_core/llm_client/openai_generic_client.py +91 -56
- graphiti_core/models/edges/edge_db_queries.py +259 -35
- graphiti_core/models/nodes/node_db_queries.py +311 -32
- graphiti_core/nodes.py +420 -205
- graphiti_core/prompts/dedupe_edges.py +46 -32
- graphiti_core/prompts/dedupe_nodes.py +67 -42
- graphiti_core/prompts/eval.py +4 -4
- graphiti_core/prompts/extract_edges.py +27 -16
- graphiti_core/prompts/extract_nodes.py +74 -31
- graphiti_core/prompts/prompt_helpers.py +39 -0
- graphiti_core/prompts/snippets.py +29 -0
- graphiti_core/prompts/summarize_nodes.py +23 -25
- graphiti_core/search/search.py +158 -82
- graphiti_core/search/search_config.py +39 -4
- graphiti_core/search/search_filters.py +126 -35
- graphiti_core/search/search_helpers.py +5 -6
- graphiti_core/search/search_utils.py +1405 -485
- graphiti_core/telemetry/__init__.py +9 -0
- graphiti_core/telemetry/telemetry.py +117 -0
- graphiti_core/tracer.py +193 -0
- graphiti_core/utils/bulk_utils.py +364 -285
- graphiti_core/utils/datetime_utils.py +13 -0
- graphiti_core/utils/maintenance/community_operations.py +67 -49
- graphiti_core/utils/maintenance/dedup_helpers.py +262 -0
- graphiti_core/utils/maintenance/edge_operations.py +339 -197
- graphiti_core/utils/maintenance/graph_data_operations.py +50 -114
- graphiti_core/utils/maintenance/node_operations.py +319 -238
- graphiti_core/utils/maintenance/temporal_operations.py +11 -3
- graphiti_core/utils/ontology_utils/entity_types_utils.py +1 -1
- graphiti_core/utils/text_utils.py +53 -0
- graphiti_core-0.24.3.dist-info/METADATA +726 -0
- graphiti_core-0.24.3.dist-info/RECORD +86 -0
- {graphiti_core-0.12.0rc1.dist-info → graphiti_core-0.24.3.dist-info}/WHEEL +1 -1
- graphiti_core-0.12.0rc1.dist-info/METADATA +0 -350
- graphiti_core-0.12.0rc1.dist-info/RECORD +0 -66
- /graphiti_core/{utils/maintenance/utils.py → migrations/__init__.py} +0 -0
- {graphiti_core-0.12.0rc1.dist-info → graphiti_core-0.24.3.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright 2024, Zep Software, Inc.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from openai import AsyncAzureOpenAI, AsyncOpenAI
|
|
21
|
+
|
|
22
|
+
from .client import EmbedderClient
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class AzureOpenAIEmbedderClient(EmbedderClient):
|
|
28
|
+
"""Wrapper class for Azure OpenAI that implements the EmbedderClient interface.
|
|
29
|
+
|
|
30
|
+
Supports both AsyncAzureOpenAI and AsyncOpenAI (with Azure v1 API endpoint).
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
azure_client: AsyncAzureOpenAI | AsyncOpenAI,
|
|
36
|
+
model: str = 'text-embedding-3-small',
|
|
37
|
+
):
|
|
38
|
+
self.azure_client = azure_client
|
|
39
|
+
self.model = model
|
|
40
|
+
|
|
41
|
+
async def create(self, input_data: str | list[str] | Any) -> list[float]:
|
|
42
|
+
"""Create embeddings using Azure OpenAI client."""
|
|
43
|
+
try:
|
|
44
|
+
# Handle different input types
|
|
45
|
+
if isinstance(input_data, str):
|
|
46
|
+
text_input = [input_data]
|
|
47
|
+
elif isinstance(input_data, list) and all(isinstance(item, str) for item in input_data):
|
|
48
|
+
text_input = input_data
|
|
49
|
+
else:
|
|
50
|
+
# Convert to string list for other types
|
|
51
|
+
text_input = [str(input_data)]
|
|
52
|
+
|
|
53
|
+
response = await self.azure_client.embeddings.create(model=self.model, input=text_input)
|
|
54
|
+
|
|
55
|
+
# Return the first embedding as a list of floats
|
|
56
|
+
return response.data[0].embedding
|
|
57
|
+
except Exception as e:
|
|
58
|
+
logger.error(f'Error in Azure OpenAI embedding: {e}')
|
|
59
|
+
raise
|
|
60
|
+
|
|
61
|
+
async def create_batch(self, input_data_list: list[str]) -> list[list[float]]:
|
|
62
|
+
"""Create batch embeddings using Azure OpenAI client."""
|
|
63
|
+
try:
|
|
64
|
+
response = await self.azure_client.embeddings.create(
|
|
65
|
+
model=self.model, input=input_data_list
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
return [embedding.embedding for embedding in response.data]
|
|
69
|
+
except Exception as e:
|
|
70
|
+
logger.error(f'Error in Azure OpenAI batch embedding: {e}')
|
|
71
|
+
raise
|
graphiti_core/embedder/client.py
CHANGED
|
@@ -14,12 +14,13 @@ See the License for the specific language governing permissions and
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
|
+
import os
|
|
17
18
|
from abc import ABC, abstractmethod
|
|
18
19
|
from collections.abc import Iterable
|
|
19
20
|
|
|
20
21
|
from pydantic import BaseModel, Field
|
|
21
22
|
|
|
22
|
-
EMBEDDING_DIM = 1024
|
|
23
|
+
EMBEDDING_DIM = int(os.getenv('EMBEDDING_DIM', 1024))
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
class EmbedderConfig(BaseModel):
|
graphiti_core/embedder/gemini.py
CHANGED
|
@@ -14,15 +14,32 @@ See the License for the specific language governing permissions and
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
|
+
import logging
|
|
17
18
|
from collections.abc import Iterable
|
|
19
|
+
from typing import TYPE_CHECKING
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from google import genai
|
|
23
|
+
from google.genai import types
|
|
24
|
+
else:
|
|
25
|
+
try:
|
|
26
|
+
from google import genai
|
|
27
|
+
from google.genai import types
|
|
28
|
+
except ImportError:
|
|
29
|
+
raise ImportError(
|
|
30
|
+
'google-genai is required for GeminiEmbedder. '
|
|
31
|
+
'Install it with: pip install graphiti-core[google-genai]'
|
|
32
|
+
) from None
|
|
18
33
|
|
|
19
|
-
from google import genai # type: ignore
|
|
20
|
-
from google.genai import types # type: ignore
|
|
21
34
|
from pydantic import Field
|
|
22
35
|
|
|
23
36
|
from .client import EmbedderClient, EmbedderConfig
|
|
24
37
|
|
|
25
|
-
|
|
38
|
+
logger = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
DEFAULT_EMBEDDING_MODEL = 'text-embedding-001' # gemini-embedding-001 or text-embedding-005
|
|
41
|
+
|
|
42
|
+
DEFAULT_BATCH_SIZE = 100
|
|
26
43
|
|
|
27
44
|
|
|
28
45
|
class GeminiEmbedderConfig(EmbedderConfig):
|
|
@@ -35,15 +52,38 @@ class GeminiEmbedder(EmbedderClient):
|
|
|
35
52
|
Google Gemini Embedder Client
|
|
36
53
|
"""
|
|
37
54
|
|
|
38
|
-
def __init__(
|
|
55
|
+
def __init__(
|
|
56
|
+
self,
|
|
57
|
+
config: GeminiEmbedderConfig | None = None,
|
|
58
|
+
client: 'genai.Client | None' = None,
|
|
59
|
+
batch_size: int | None = None,
|
|
60
|
+
):
|
|
61
|
+
"""
|
|
62
|
+
Initialize the GeminiEmbedder with the provided configuration and client.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
config (GeminiEmbedderConfig | None): The configuration for the GeminiEmbedder, including API key, model, base URL, temperature, and max tokens.
|
|
66
|
+
client (genai.Client | None): An optional async client instance to use. If not provided, a new genai.Client is created.
|
|
67
|
+
batch_size (int | None): An optional batch size to use. If not provided, the default batch size will be used.
|
|
68
|
+
"""
|
|
39
69
|
if config is None:
|
|
40
70
|
config = GeminiEmbedderConfig()
|
|
71
|
+
|
|
41
72
|
self.config = config
|
|
42
73
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
74
|
+
if client is None:
|
|
75
|
+
self.client = genai.Client(api_key=config.api_key)
|
|
76
|
+
else:
|
|
77
|
+
self.client = client
|
|
78
|
+
|
|
79
|
+
if batch_size is None and self.config.embedding_model == 'gemini-embedding-001':
|
|
80
|
+
# Gemini API has a limit on the number of instances per request
|
|
81
|
+
# https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api
|
|
82
|
+
self.batch_size = 1
|
|
83
|
+
elif batch_size is None:
|
|
84
|
+
self.batch_size = DEFAULT_BATCH_SIZE
|
|
85
|
+
else:
|
|
86
|
+
self.batch_size = batch_size
|
|
47
87
|
|
|
48
88
|
async def create(
|
|
49
89
|
self, input_data: str | list[str] | Iterable[int] | Iterable[Iterable[int]]
|
|
@@ -71,19 +111,73 @@ class GeminiEmbedder(EmbedderClient):
|
|
|
71
111
|
return result.embeddings[0].values
|
|
72
112
|
|
|
73
113
|
async def create_batch(self, input_data_list: list[str]) -> list[list[float]]:
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
model=self.config.embedding_model or DEFAULT_EMBEDDING_MODEL,
|
|
77
|
-
contents=input_data_list, # type: ignore[arg-type] # mypy fails on broad union type
|
|
78
|
-
config=types.EmbedContentConfig(output_dimensionality=self.config.embedding_dim),
|
|
79
|
-
)
|
|
114
|
+
"""
|
|
115
|
+
Create embeddings for a batch of input data using Google's Gemini embedding model.
|
|
80
116
|
|
|
81
|
-
|
|
82
|
-
|
|
117
|
+
This method handles batching to respect the Gemini API's limits on the number
|
|
118
|
+
of instances that can be processed in a single request.
|
|
83
119
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
120
|
+
Args:
|
|
121
|
+
input_data_list: A list of strings to create embeddings for.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
A list of embedding vectors (each vector is a list of floats).
|
|
125
|
+
"""
|
|
126
|
+
if not input_data_list:
|
|
127
|
+
return []
|
|
128
|
+
|
|
129
|
+
batch_size = self.batch_size
|
|
130
|
+
all_embeddings = []
|
|
131
|
+
|
|
132
|
+
# Process inputs in batches
|
|
133
|
+
for i in range(0, len(input_data_list), batch_size):
|
|
134
|
+
batch = input_data_list[i : i + batch_size]
|
|
135
|
+
|
|
136
|
+
try:
|
|
137
|
+
# Generate embeddings for this batch
|
|
138
|
+
result = await self.client.aio.models.embed_content(
|
|
139
|
+
model=self.config.embedding_model or DEFAULT_EMBEDDING_MODEL,
|
|
140
|
+
contents=batch, # type: ignore[arg-type] # mypy fails on broad union type
|
|
141
|
+
config=types.EmbedContentConfig(
|
|
142
|
+
output_dimensionality=self.config.embedding_dim
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
if not result.embeddings or len(result.embeddings) == 0:
|
|
147
|
+
raise Exception('No embeddings returned')
|
|
148
|
+
|
|
149
|
+
# Process embeddings from this batch
|
|
150
|
+
for embedding in result.embeddings:
|
|
151
|
+
if not embedding.values:
|
|
152
|
+
raise ValueError('Empty embedding values returned')
|
|
153
|
+
all_embeddings.append(embedding.values)
|
|
154
|
+
|
|
155
|
+
except Exception as e:
|
|
156
|
+
# If batch processing fails, fall back to individual processing
|
|
157
|
+
logger.warning(
|
|
158
|
+
f'Batch embedding failed for batch {i // batch_size + 1}, falling back to individual processing: {e}'
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
for item in batch:
|
|
162
|
+
try:
|
|
163
|
+
# Process each item individually
|
|
164
|
+
result = await self.client.aio.models.embed_content(
|
|
165
|
+
model=self.config.embedding_model or DEFAULT_EMBEDDING_MODEL,
|
|
166
|
+
contents=[item], # type: ignore[arg-type] # mypy fails on broad union type
|
|
167
|
+
config=types.EmbedContentConfig(
|
|
168
|
+
output_dimensionality=self.config.embedding_dim
|
|
169
|
+
),
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
if not result.embeddings or len(result.embeddings) == 0:
|
|
173
|
+
raise ValueError('No embeddings returned from Gemini API')
|
|
174
|
+
if not result.embeddings[0].values:
|
|
175
|
+
raise ValueError('Empty embedding values returned')
|
|
176
|
+
|
|
177
|
+
all_embeddings.append(result.embeddings[0].values)
|
|
178
|
+
|
|
179
|
+
except Exception as individual_error:
|
|
180
|
+
logger.error(f'Failed to embed individual item: {individual_error}')
|
|
181
|
+
raise individual_error
|
|
182
|
+
|
|
183
|
+
return all_embeddings
|
graphiti_core/embedder/voyage.py
CHANGED
|
@@ -15,8 +15,19 @@ limitations under the License.
|
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
from collections.abc import Iterable
|
|
18
|
+
from typing import TYPE_CHECKING
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
import voyageai
|
|
22
|
+
else:
|
|
23
|
+
try:
|
|
24
|
+
import voyageai
|
|
25
|
+
except ImportError:
|
|
26
|
+
raise ImportError(
|
|
27
|
+
'voyageai is required for VoyageAIEmbedderClient. '
|
|
28
|
+
'Install it with: pip install graphiti-core[voyageai]'
|
|
29
|
+
) from None
|
|
18
30
|
|
|
19
|
-
import voyageai # type: ignore
|
|
20
31
|
from pydantic import Field
|
|
21
32
|
|
|
22
33
|
from .client import EmbedderClient, EmbedderConfig
|
|
@@ -38,7 +49,7 @@ class VoyageAIEmbedder(EmbedderClient):
|
|
|
38
49
|
if config is None:
|
|
39
50
|
config = VoyageAIEmbedderConfig()
|
|
40
51
|
self.config = config
|
|
41
|
-
self.client = voyageai.AsyncClient(api_key=config.api_key)
|
|
52
|
+
self.client = voyageai.AsyncClient(api_key=config.api_key) # type: ignore[reportUnknownMemberType]
|
|
42
53
|
|
|
43
54
|
async def create(
|
|
44
55
|
self, input_data: str | list[str] | Iterable[int] | Iterable[Iterable[int]]
|
graphiti_core/errors.py
CHANGED
|
@@ -73,3 +73,11 @@ class EntityTypeValidationError(GraphitiError):
|
|
|
73
73
|
def __init__(self, entity_type: str, entity_type_attribute: str):
|
|
74
74
|
self.message = f'{entity_type_attribute} cannot be used as an attribute for {entity_type} as it is a protected attribute name.'
|
|
75
75
|
super().__init__(self.message)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class GroupIdValidationError(GraphitiError):
|
|
79
|
+
"""Raised when a group_id contains invalid characters."""
|
|
80
|
+
|
|
81
|
+
def __init__(self, group_id: str):
|
|
82
|
+
self.message = f'group_id "{group_id}" must contain only alphanumeric characters, dashes, or underscores'
|
|
83
|
+
super().__init__(self.message)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Database query utilities for different graph database backends.
|
|
3
|
+
|
|
4
|
+
This module provides database-agnostic query generation for Neo4j and FalkorDB,
|
|
5
|
+
supporting index creation, fulltext search, and bulk operations.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing_extensions import LiteralString
|
|
9
|
+
|
|
10
|
+
from graphiti_core.driver.driver import GraphProvider
|
|
11
|
+
|
|
12
|
+
# Mapping from Neo4j fulltext index names to FalkorDB node labels
|
|
13
|
+
NEO4J_TO_FALKORDB_MAPPING = {
|
|
14
|
+
'node_name_and_summary': 'Entity',
|
|
15
|
+
'community_name': 'Community',
|
|
16
|
+
'episode_content': 'Episodic',
|
|
17
|
+
'edge_name_and_fact': 'RELATES_TO',
|
|
18
|
+
}
|
|
19
|
+
# Mapping from fulltext index names to Kuzu node labels
|
|
20
|
+
INDEX_TO_LABEL_KUZU_MAPPING = {
|
|
21
|
+
'node_name_and_summary': 'Entity',
|
|
22
|
+
'community_name': 'Community',
|
|
23
|
+
'episode_content': 'Episodic',
|
|
24
|
+
'edge_name_and_fact': 'RelatesToNode_',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_range_indices(provider: GraphProvider) -> list[LiteralString]:
|
|
29
|
+
if provider == GraphProvider.FALKORDB:
|
|
30
|
+
return [
|
|
31
|
+
# Entity node
|
|
32
|
+
'CREATE INDEX FOR (n:Entity) ON (n.uuid, n.group_id, n.name, n.created_at)',
|
|
33
|
+
# Episodic node
|
|
34
|
+
'CREATE INDEX FOR (n:Episodic) ON (n.uuid, n.group_id, n.created_at, n.valid_at)',
|
|
35
|
+
# Community node
|
|
36
|
+
'CREATE INDEX FOR (n:Community) ON (n.uuid)',
|
|
37
|
+
# RELATES_TO edge
|
|
38
|
+
'CREATE INDEX FOR ()-[e:RELATES_TO]-() ON (e.uuid, e.group_id, e.name, e.created_at, e.expired_at, e.valid_at, e.invalid_at)',
|
|
39
|
+
# MENTIONS edge
|
|
40
|
+
'CREATE INDEX FOR ()-[e:MENTIONS]-() ON (e.uuid, e.group_id)',
|
|
41
|
+
# HAS_MEMBER edge
|
|
42
|
+
'CREATE INDEX FOR ()-[e:HAS_MEMBER]-() ON (e.uuid)',
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
if provider == GraphProvider.KUZU:
|
|
46
|
+
return []
|
|
47
|
+
|
|
48
|
+
return [
|
|
49
|
+
'CREATE INDEX entity_uuid IF NOT EXISTS FOR (n:Entity) ON (n.uuid)',
|
|
50
|
+
'CREATE INDEX episode_uuid IF NOT EXISTS FOR (n:Episodic) ON (n.uuid)',
|
|
51
|
+
'CREATE INDEX community_uuid IF NOT EXISTS FOR (n:Community) ON (n.uuid)',
|
|
52
|
+
'CREATE INDEX relation_uuid IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.uuid)',
|
|
53
|
+
'CREATE INDEX mention_uuid IF NOT EXISTS FOR ()-[e:MENTIONS]-() ON (e.uuid)',
|
|
54
|
+
'CREATE INDEX has_member_uuid IF NOT EXISTS FOR ()-[e:HAS_MEMBER]-() ON (e.uuid)',
|
|
55
|
+
'CREATE INDEX entity_group_id IF NOT EXISTS FOR (n:Entity) ON (n.group_id)',
|
|
56
|
+
'CREATE INDEX episode_group_id IF NOT EXISTS FOR (n:Episodic) ON (n.group_id)',
|
|
57
|
+
'CREATE INDEX community_group_id IF NOT EXISTS FOR (n:Community) ON (n.group_id)',
|
|
58
|
+
'CREATE INDEX relation_group_id IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.group_id)',
|
|
59
|
+
'CREATE INDEX mention_group_id IF NOT EXISTS FOR ()-[e:MENTIONS]-() ON (e.group_id)',
|
|
60
|
+
'CREATE INDEX name_entity_index IF NOT EXISTS FOR (n:Entity) ON (n.name)',
|
|
61
|
+
'CREATE INDEX created_at_entity_index IF NOT EXISTS FOR (n:Entity) ON (n.created_at)',
|
|
62
|
+
'CREATE INDEX created_at_episodic_index IF NOT EXISTS FOR (n:Episodic) ON (n.created_at)',
|
|
63
|
+
'CREATE INDEX valid_at_episodic_index IF NOT EXISTS FOR (n:Episodic) ON (n.valid_at)',
|
|
64
|
+
'CREATE INDEX name_edge_index IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.name)',
|
|
65
|
+
'CREATE INDEX created_at_edge_index IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.created_at)',
|
|
66
|
+
'CREATE INDEX expired_at_edge_index IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.expired_at)',
|
|
67
|
+
'CREATE INDEX valid_at_edge_index IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.valid_at)',
|
|
68
|
+
'CREATE INDEX invalid_at_edge_index IF NOT EXISTS FOR ()-[e:RELATES_TO]-() ON (e.invalid_at)',
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def get_fulltext_indices(provider: GraphProvider) -> list[LiteralString]:
|
|
73
|
+
if provider == GraphProvider.FALKORDB:
|
|
74
|
+
from typing import cast
|
|
75
|
+
|
|
76
|
+
from graphiti_core.driver.falkordb_driver import STOPWORDS
|
|
77
|
+
|
|
78
|
+
# Convert to string representation for embedding in queries
|
|
79
|
+
stopwords_str = str(STOPWORDS)
|
|
80
|
+
|
|
81
|
+
# Use type: ignore to satisfy LiteralString requirement while maintaining single source of truth
|
|
82
|
+
return cast(
|
|
83
|
+
list[LiteralString],
|
|
84
|
+
[
|
|
85
|
+
f"""CALL db.idx.fulltext.createNodeIndex(
|
|
86
|
+
{{
|
|
87
|
+
label: 'Episodic',
|
|
88
|
+
stopwords: {stopwords_str}
|
|
89
|
+
}},
|
|
90
|
+
'content', 'source', 'source_description', 'group_id'
|
|
91
|
+
)""",
|
|
92
|
+
f"""CALL db.idx.fulltext.createNodeIndex(
|
|
93
|
+
{{
|
|
94
|
+
label: 'Entity',
|
|
95
|
+
stopwords: {stopwords_str}
|
|
96
|
+
}},
|
|
97
|
+
'name', 'summary', 'group_id'
|
|
98
|
+
)""",
|
|
99
|
+
f"""CALL db.idx.fulltext.createNodeIndex(
|
|
100
|
+
{{
|
|
101
|
+
label: 'Community',
|
|
102
|
+
stopwords: {stopwords_str}
|
|
103
|
+
}},
|
|
104
|
+
'name', 'group_id'
|
|
105
|
+
)""",
|
|
106
|
+
"""CREATE FULLTEXT INDEX FOR ()-[e:RELATES_TO]-() ON (e.name, e.fact, e.group_id)""",
|
|
107
|
+
],
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
if provider == GraphProvider.KUZU:
|
|
111
|
+
return [
|
|
112
|
+
"CALL CREATE_FTS_INDEX('Episodic', 'episode_content', ['content', 'source', 'source_description']);",
|
|
113
|
+
"CALL CREATE_FTS_INDEX('Entity', 'node_name_and_summary', ['name', 'summary']);",
|
|
114
|
+
"CALL CREATE_FTS_INDEX('Community', 'community_name', ['name']);",
|
|
115
|
+
"CALL CREATE_FTS_INDEX('RelatesToNode_', 'edge_name_and_fact', ['name', 'fact']);",
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
return [
|
|
119
|
+
"""CREATE FULLTEXT INDEX episode_content IF NOT EXISTS
|
|
120
|
+
FOR (e:Episodic) ON EACH [e.content, e.source, e.source_description, e.group_id]""",
|
|
121
|
+
"""CREATE FULLTEXT INDEX node_name_and_summary IF NOT EXISTS
|
|
122
|
+
FOR (n:Entity) ON EACH [n.name, n.summary, n.group_id]""",
|
|
123
|
+
"""CREATE FULLTEXT INDEX community_name IF NOT EXISTS
|
|
124
|
+
FOR (n:Community) ON EACH [n.name, n.group_id]""",
|
|
125
|
+
"""CREATE FULLTEXT INDEX edge_name_and_fact IF NOT EXISTS
|
|
126
|
+
FOR ()-[e:RELATES_TO]-() ON EACH [e.name, e.fact, e.group_id]""",
|
|
127
|
+
]
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def get_nodes_query(name: str, query: str, limit: int, provider: GraphProvider) -> str:
|
|
131
|
+
if provider == GraphProvider.FALKORDB:
|
|
132
|
+
label = NEO4J_TO_FALKORDB_MAPPING[name]
|
|
133
|
+
return f"CALL db.idx.fulltext.queryNodes('{label}', {query})"
|
|
134
|
+
|
|
135
|
+
if provider == GraphProvider.KUZU:
|
|
136
|
+
label = INDEX_TO_LABEL_KUZU_MAPPING[name]
|
|
137
|
+
return f"CALL QUERY_FTS_INDEX('{label}', '{name}', {query}, TOP := $limit)"
|
|
138
|
+
|
|
139
|
+
return f'CALL db.index.fulltext.queryNodes("{name}", {query}, {{limit: $limit}})'
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def get_vector_cosine_func_query(vec1, vec2, provider: GraphProvider) -> str:
|
|
143
|
+
if provider == GraphProvider.FALKORDB:
|
|
144
|
+
# FalkorDB uses a different syntax for regular cosine similarity and Neo4j uses normalized cosine similarity
|
|
145
|
+
return f'(2 - vec.cosineDistance({vec1}, vecf32({vec2})))/2'
|
|
146
|
+
|
|
147
|
+
if provider == GraphProvider.KUZU:
|
|
148
|
+
return f'array_cosine_similarity({vec1}, {vec2})'
|
|
149
|
+
|
|
150
|
+
return f'vector.similarity.cosine({vec1}, {vec2})'
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def get_relationships_query(name: str, limit: int, provider: GraphProvider) -> str:
|
|
154
|
+
if provider == GraphProvider.FALKORDB:
|
|
155
|
+
label = NEO4J_TO_FALKORDB_MAPPING[name]
|
|
156
|
+
return f"CALL db.idx.fulltext.queryRelationships('{label}', $query)"
|
|
157
|
+
|
|
158
|
+
if provider == GraphProvider.KUZU:
|
|
159
|
+
label = INDEX_TO_LABEL_KUZU_MAPPING[name]
|
|
160
|
+
return f"CALL QUERY_FTS_INDEX('{label}', '{name}', cast($query AS STRING), TOP := $limit)"
|
|
161
|
+
|
|
162
|
+
return f'CALL db.index.fulltext.queryRelationships("{name}", $query, {{limit: $limit}})'
|