repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import logging |
| #2 | |
| #3 | from mem0.memory.utils import format_entities, sanitize_relationship_for_cypher |
| #4 | |
| #5 | try: |
| #6 | from langchain_memgraph.graphs.memgraph import Memgraph |
| #7 | except ImportError: |
| #8 | raise ImportError("langchain_memgraph is not installed. Please install it using pip install langchain-memgraph") |
| #9 | |
| #10 | try: |
| #11 | from rank_bm25 import BM25Okapi |
| #12 | except ImportError: |
| #13 | raise ImportError("rank_bm25 is not installed. Please install it using pip install rank-bm25") |
| #14 | |
| #15 | from mem0.graphs.tools import ( |
| #16 | DELETE_MEMORY_STRUCT_TOOL_GRAPH, |
| #17 | DELETE_MEMORY_TOOL_GRAPH, |
| #18 | EXTRACT_ENTITIES_STRUCT_TOOL, |
| #19 | EXTRACT_ENTITIES_TOOL, |
| #20 | RELATIONS_STRUCT_TOOL, |
| #21 | RELATIONS_TOOL, |
| #22 | ) |
| #23 | from mem0.graphs.utils import EXTRACT_RELATIONS_PROMPT, get_delete_messages |
| #24 | from mem0.utils.factory import EmbedderFactory, LlmFactory |
| #25 | |
| #26 | logger = logging.getLogger(__name__) |
| #27 | |
| #28 | |
| #29 | class MemoryGraph: |
| #30 | def __init__(self, config): |
| #31 | self.config = config |
| #32 | self.graph = Memgraph( |
| #33 | self.config.graph_store.config.url, |
| #34 | self.config.graph_store.config.username, |
| #35 | self.config.graph_store.config.password, |
| #36 | ) |
| #37 | self.embedding_model = EmbedderFactory.create( |
| #38 | self.config.embedder.provider, |
| #39 | self.config.embedder.config, |
| #40 | {"enable_embeddings": True}, |
| #41 | ) |
| #42 | |
| #43 | # Default to openai if no specific provider is configured |
| #44 | self.llm_provider = "openai" |
| #45 | if self.config.llm and self.config.llm.provider: |
| #46 | self.llm_provider = self.config.llm.provider |
| #47 | if self.config.graph_store and self.config.graph_store.llm and self.config.graph_store.llm.provider: |
| #48 | self.llm_provider = self.config.graph_store.llm.provider |
| #49 | |
| #50 | # Get LLM config with proper null checks |
| #51 | llm_config = None |
| #52 | if self.config.graph_store and self.config.graph_store.llm and hasattr(self.config.graph_store.llm, "config"): |
| #53 | llm_config = self.config.graph_store.llm.config |
| #54 | elif hasattr(self.config.llm, "config"): |
| #55 | llm_config = self.config.llm.config |
| #56 | self.llm = LlmFactory.create(self.llm_provider, llm_config) |
| #57 | self.user_id = None |
| #58 | # Use threshold from graph_store config, default to 0.7 for backward compatibility |
| #59 | self.threshold = self.config.graph_store.threshold if hasattr(self.config.graph_store, 'threshold') else 0.7 |
| #60 | |
| #61 | # Setup Memgraph: |
| #62 | # 1. Create vector index (created Entity label on all nodes) |
| #63 | # 2. Create label property index for performance optimizations |
| #64 | embedding_dims = self.config.embedder.config["embedding_dims"] |
| #65 | index_info = self._fetch_existing_indexes() |
| #66 | |
| #67 | # Create vector index if not exists |
| #68 | if not self._vector_index_exists(index_info, "memzero"): |
| #69 | self.graph.query( |
| #70 | f"CREATE VECTOR INDEX memzero ON :Entity(embedding) WITH CONFIG {{'dimension': {embedding_dims}, 'capacity': 1000, 'metric': 'cos'}};" |
| #71 | ) |
| #72 | |
| #73 | # Create label+property index if not exists |
| #74 | if not self._label_property_index_exists(index_info, "Entity", "user_id"): |
| #75 | self.graph.query("CREATE INDEX ON :Entity(user_id);") |
| #76 | |
| #77 | # Create label index if not exists |
| #78 | if not self._label_index_exists(index_info, "Entity"): |
| #79 | self.graph.query("CREATE INDEX ON :Entity;") |
| #80 | |
| #81 | def add(self, data, filters): |
| #82 | """ |
| #83 | Adds data to the graph. |
| #84 | |
| #85 | Args: |
| #86 | data (str): The data to add to the graph. |
| #87 | filters (dict): A dictionary containing filters to be applied during the addition. |
| #88 | """ |
| #89 | entity_type_map = self._retrieve_nodes_from_data(data, filters) |
| #90 | to_be_added = self._establish_nodes_relations_from_data(data, filters, entity_type_map) |
| #91 | search_output = self._search_graph_db(node_list=list(entity_type_map.keys()), filters=filters) |
| #92 | to_be_deleted = self._get_delete_entities_from_search_output(search_output, data, filters) |
| #93 | |
| #94 | # TODO: Batch queries with APOC plugin |
| #95 | # TODO: Add more filter support |
| #96 | deleted_entities = self._delete_entities(to_be_deleted, filters) |
| #97 | added_entities = self._add_entities(to_be_added, filters, entity_type_map) |
| #98 | |
| #99 | return {"deleted_entities": deleted_entities, "added_entities": added_entities} |
| #100 | |
| #101 | def search(self, query, filters, limit=100): |
| #102 | """ |
| #103 | Search for memories and related graph data. |
| #104 | |
| #105 | Args: |
| #106 | query (str): Query to search for. |
| #107 | filters (dict): A dictionary containing filters to be applied during the search. |
| #108 | limit (int): The maximum number of nodes and relationships to retrieve. Defaults to 100. |
| #109 | |
| #110 | Returns: |
| #111 | dict: A dictionary containing: |
| #112 | - "contexts": List of search results from the base data store. |
| #113 | - "entities": List of related graph data based on the query. |
| #114 | """ |
| #115 | entity_type_map = self._retrieve_nodes_from_data(query, filters) |
| #116 | search_output = self._search_graph_db(node_list=list(entity_type_map.keys()), filters=filters) |
| #117 | |
| #118 | if not search_output: |
| #119 | return [] |
| #120 | |
| #121 | search_outputs_sequence = [ |
| #122 | [item["source"], item["relationship"], item["destination"]] for item in search_output |
| #123 | ] |
| #124 | bm25 = BM25Okapi(search_outputs_sequence) |
| #125 | |
| #126 | tokenized_query = query.split(" ") |
| #127 | reranked_results = bm25.get_top_n(tokenized_query, search_outputs_sequence, n=5) |
| #128 | |
| #129 | search_results = [] |
| #130 | for item in reranked_results: |
| #131 | search_results.append({"source": item[0], "relationship": item[1], "destination": item[2]}) |
| #132 | |
| #133 | logger.info(f"Returned {len(search_results)} search results") |
| #134 | |
| #135 | return search_results |
| #136 | |
| #137 | def delete_all(self, filters): |
| #138 | """Delete all nodes and relationships for a user or specific agent.""" |
| #139 | if filters.get("agent_id"): |
| #140 | cypher = """ |
| #141 | MATCH (n:Entity {user_id: $user_id, agent_id: $agent_id}) |
| #142 | DETACH DELETE n |
| #143 | """ |
| #144 | params = {"user_id": filters["user_id"], "agent_id": filters["agent_id"]} |
| #145 | else: |
| #146 | cypher = """ |
| #147 | MATCH (n:Entity {user_id: $user_id}) |
| #148 | DETACH DELETE n |
| #149 | """ |
| #150 | params = {"user_id": filters["user_id"]} |
| #151 | self.graph.query(cypher, params=params) |
| #152 | |
| #153 | def get_all(self, filters, limit=100): |
| #154 | """ |
| #155 | Retrieves all nodes and relationships from the graph database based on optional filtering criteria. |
| #156 | |
| #157 | Args: |
| #158 | filters (dict): A dictionary containing filters to be applied during the retrieval. |
| #159 | Supports 'user_id' (required) and 'agent_id' (optional). |
| #160 | limit (int): The maximum number of nodes and relationships to retrieve. Defaults to 100. |
| #161 | Returns: |
| #162 | list: A list of dictionaries, each containing: |
| #163 | - 'source': The source node name. |
| #164 | - 'relationship': The relationship type. |
| #165 | - 'target': The target node name. |
| #166 | """ |
| #167 | # Build query based on whether agent_id is provided |
| #168 | if filters.get("agent_id"): |
| #169 | query = """ |
| #170 | MATCH (n:Entity {user_id: $user_id, agent_id: $agent_id})-[r]->(m:Entity {user_id: $user_id, agent_id: $agent_id}) |
| #171 | RETURN n.name AS source, type(r) AS relationship, m.name AS target |
| #172 | LIMIT $limit |
| #173 | """ |
| #174 | params = {"user_id": filters["user_id"], "agent_id": filters["agent_id"], "limit": limit} |
| #175 | else: |
| #176 | query = """ |
| #177 | MATCH (n:Entity {user_id: $user_id})-[r]->(m:Entity {user_id: $user_id}) |
| #178 | RETURN n.name AS source, type(r) AS relationship, m.name AS target |
| #179 | LIMIT $limit |
| #180 | """ |
| #181 | params = {"user_id": filters["user_id"], "limit": limit} |
| #182 | |
| #183 | results = self.graph.query(query, params=params) |
| #184 | |
| #185 | final_results = [] |
| #186 | for result in results: |
| #187 | final_results.append( |
| #188 | { |
| #189 | "source": result["source"], |
| #190 | "relationship": result["relationship"], |
| #191 | "target": result["target"], |
| #192 | } |
| #193 | ) |
| #194 | |
| #195 | logger.info(f"Retrieved {len(final_results)} relationships") |
| #196 | |
| #197 | return final_results |
| #198 | |
| #199 | def _retrieve_nodes_from_data(self, data, filters): |
| #200 | """Extracts all the entities mentioned in the query.""" |
| #201 | _tools = [EXTRACT_ENTITIES_TOOL] |
| #202 | if self.llm_provider in ["azure_openai_structured", "openai_structured"]: |
| #203 | _tools = [EXTRACT_ENTITIES_STRUCT_TOOL] |
| #204 | search_results = self.llm.generate_response( |
| #205 | messages=[ |
| #206 | { |
| #207 | "role": "system", |
| #208 | "content": f"You are a smart assistant who understands entities and their types in a given text. If user message contains self reference such as 'I', 'me', 'my' etc. then use {filters['user_id']} as the source entity. Extract all the entities from the text. ***DO NOT*** answer the question itself if the given text is a question.", |
| #209 | }, |
| #210 | {"role": "user", "content": data}, |
| #211 | ], |
| #212 | tools=_tools, |
| #213 | ) |
| #214 | |
| #215 | entity_type_map = {} |
| #216 | |
| #217 | try: |
| #218 | for tool_call in search_results["tool_calls"]: |
| #219 | if tool_call["name"] != "extract_entities": |
| #220 | continue |
| #221 | for item in tool_call["arguments"]["entities"]: |
| #222 | if "entity" in item and "entity_type" in item: |
| #223 | entity_type_map[item["entity"]] = item["entity_type"] |
| #224 | except Exception as e: |
| #225 | logger.exception( |
| #226 | f"Error in search tool: {e}, llm_provider={self.llm_provider}, search_results={search_results}" |
| #227 | ) |
| #228 | |
| #229 | entity_type_map = {k.lower().replace(" ", "_"): v.lower().replace(" ", "_") for k, v in entity_type_map.items()} |
| #230 | logger.debug(f"Entity type map: {entity_type_map}\n search_results={search_results}") |
| #231 | return entity_type_map |
| #232 | |
| #233 | def _establish_nodes_relations_from_data(self, data, filters, entity_type_map): |
| #234 | """Eshtablish relations among the extracted nodes.""" |
| #235 | if self.config.graph_store.custom_prompt: |
| #236 | messages = [ |
| #237 | { |
| #238 | "role": "system", |
| #239 | "content": EXTRACT_RELATIONS_PROMPT.replace("USER_ID", filters["user_id"]).replace( |
| #240 | "CUSTOM_PROMPT", f"4. {self.config.graph_store.custom_prompt}" |
| #241 | ), |
| #242 | }, |
| #243 | {"role": "user", "content": data}, |
| #244 | ] |
| #245 | else: |
| #246 | messages = [ |
| #247 | { |
| #248 | "role": "system", |
| #249 | "content": EXTRACT_RELATIONS_PROMPT.replace("USER_ID", filters["user_id"]), |
| #250 | }, |
| #251 | { |
| #252 | "role": "user", |
| #253 | "content": f"List of entities: {list(entity_type_map.keys())}. \n\nText: {data}", |
| #254 | }, |
| #255 | ] |
| #256 | |
| #257 | _tools = [RELATIONS_TOOL] |
| #258 | if self.llm_provider in ["azure_openai_structured", "openai_structured"]: |
| #259 | _tools = [RELATIONS_STRUCT_TOOL] |
| #260 | |
| #261 | extracted_entities = self.llm.generate_response( |
| #262 | messages=messages, |
| #263 | tools=_tools, |
| #264 | ) |
| #265 | |
| #266 | entities = [] |
| #267 | if extracted_entities and extracted_entities.get("tool_calls"): |
| #268 | entities = extracted_entities["tool_calls"][0].get("arguments", {}).get("entities", []) |
| #269 | |
| #270 | entities = self._remove_spaces_from_entities(entities) |
| #271 | logger.debug(f"Extracted entities: {entities}") |
| #272 | return entities |
| #273 | |
| #274 | def _search_graph_db(self, node_list, filters, limit=100): |
| #275 | """Search similar nodes among and their respective incoming and outgoing relations.""" |
| #276 | result_relations = [] |
| #277 | |
| #278 | for node in node_list: |
| #279 | n_embedding = self.embedding_model.embed(node) |
| #280 | |
| #281 | # Build query based on whether agent_id is provided |
| #282 | if filters.get("agent_id"): |
| #283 | cypher_query = """ |
| #284 | CALL vector_search.search("memzero", $limit, $n_embedding) |
| #285 | YIELD distance, node, similarity |
| #286 | WITH node AS n, similarity |
| #287 | WHERE n:Entity AND n.user_id = $user_id AND n.agent_id = $agent_id AND n.embedding IS NOT NULL AND similarity >= $threshold |
| #288 | MATCH (n)-[r]->(m:Entity) |
| #289 | RETURN n.name AS source, id(n) AS source_id, type(r) AS relationship, id(r) AS relation_id, m.name AS destination, id(m) AS destination_id, similarity |
| #290 | UNION |
| #291 | CALL vector_search.search("memzero", $limit, $n_embedding) |
| #292 | YIELD distance, node, similarity |
| #293 | WITH node AS n, similarity |
| #294 | WHERE n:Entity AND n.user_id = $user_id AND n.agent_id = $agent_id AND n.embedding IS NOT NULL AND similarity >= $threshold |
| #295 | MATCH (m:Entity)-[r]->(n) |
| #296 | RETURN m.name AS source, id(m) AS source_id, type(r) AS relationship, id(r) AS relation_id, n.name AS destination, id(n) AS destination_id, similarity |
| #297 | ORDER BY similarity DESC |
| #298 | LIMIT $limit; |
| #299 | """ |
| #300 | params = { |
| #301 | "n_embedding": n_embedding, |
| #302 | "threshold": self.threshold, |
| #303 | "user_id": filters["user_id"], |
| #304 | "agent_id": filters["agent_id"], |
| #305 | "limit": limit, |
| #306 | } |
| #307 | else: |
| #308 | cypher_query = """ |
| #309 | CALL vector_search.search("memzero", $limit, $n_embedding) |
| #310 | YIELD distance, node, similarity |
| #311 | WITH node AS n, similarity |
| #312 | WHERE n:Entity AND n.user_id = $user_id AND n.embedding IS NOT NULL AND similarity >= $threshold |
| #313 | MATCH (n)-[r]->(m:Entity) |
| #314 | RETURN n.name AS source, id(n) AS source_id, type(r) AS relationship, id(r) AS relation_id, m.name AS destination, id(m) AS destination_id, similarity |
| #315 | UNION |
| #316 | CALL vector_search.search("memzero", $limit, $n_embedding) |
| #317 | YIELD distance, node, similarity |
| #318 | WITH node AS n, similarity |
| #319 | WHERE n:Entity AND n.user_id = $user_id AND n.embedding IS NOT NULL AND similarity >= $threshold |
| #320 | MATCH (m:Entity)-[r]->(n) |
| #321 | RETURN m.name AS source, id(m) AS source_id, type(r) AS relationship, id(r) AS relation_id, n.name AS destination, id(n) AS destination_id, similarity |
| #322 | ORDER BY similarity DESC |
| #323 | LIMIT $limit; |
| #324 | """ |
| #325 | params = { |
| #326 | "n_embedding": n_embedding, |
| #327 | "threshold": self.threshold, |
| #328 | "user_id": filters["user_id"], |
| #329 | "limit": limit, |
| #330 | } |
| #331 | |
| #332 | ans = self.graph.query(cypher_query, params=params) |
| #333 | result_relations.extend(ans) |
| #334 | |
| #335 | return result_relations |
| #336 | |
| #337 | def _get_delete_entities_from_search_output(self, search_output, data, filters): |
| #338 | """Get the entities to be deleted from the search output.""" |
| #339 | search_output_string = format_entities(search_output) |
| #340 | system_prompt, user_prompt = get_delete_messages(search_output_string, data, filters["user_id"]) |
| #341 | |
| #342 | _tools = [DELETE_MEMORY_TOOL_GRAPH] |
| #343 | if self.llm_provider in ["azure_openai_structured", "openai_structured"]: |
| #344 | _tools = [ |
| #345 | DELETE_MEMORY_STRUCT_TOOL_GRAPH, |
| #346 | ] |
| #347 | |
| #348 | memory_updates = self.llm.generate_response( |
| #349 | messages=[ |
| #350 | {"role": "system", "content": system_prompt}, |
| #351 | {"role": "user", "content": user_prompt}, |
| #352 | ], |
| #353 | tools=_tools, |
| #354 | ) |
| #355 | to_be_deleted = [] |
| #356 | for item in memory_updates["tool_calls"]: |
| #357 | if item["name"] == "delete_graph_memory": |
| #358 | to_be_deleted.append(item["arguments"]) |
| #359 | # in case if it is not in the correct format |
| #360 | to_be_deleted = self._remove_spaces_from_entities(to_be_deleted) |
| #361 | logger.debug(f"Deleted relationships: {to_be_deleted}") |
| #362 | return to_be_deleted |
| #363 | |
| #364 | def _delete_entities(self, to_be_deleted, filters): |
| #365 | """Delete the entities from the graph.""" |
| #366 | user_id = filters["user_id"] |
| #367 | agent_id = filters.get("agent_id", None) |
| #368 | results = [] |
| #369 | |
| #370 | for item in to_be_deleted: |
| #371 | source = item["source"] |
| #372 | destination = item["destination"] |
| #373 | relationship = item["relationship"] |
| #374 | |
| #375 | # Build the agent filter for the query |
| #376 | agent_filter = "" |
| #377 | params = { |
| #378 | "source_name": source, |
| #379 | "dest_name": destination, |
| #380 | "user_id": user_id, |
| #381 | } |
| #382 | |
| #383 | if agent_id: |
| #384 | agent_filter = "AND n.agent_id = $agent_id AND m.agent_id = $agent_id" |
| #385 | params["agent_id"] = agent_id |
| #386 | |
| #387 | # Delete the specific relationship between nodes |
| #388 | cypher = f""" |
| #389 | MATCH (n:Entity {{name: $source_name, user_id: $user_id}}) |
| #390 | -[r:{relationship}]-> |
| #391 | (m:Entity {{name: $dest_name, user_id: $user_id}}) |
| #392 | WHERE 1=1 {agent_filter} |
| #393 | DELETE r |
| #394 | RETURN |
| #395 | n.name AS source, |
| #396 | m.name AS target, |
| #397 | type(r) AS relationship |
| #398 | """ |
| #399 | |
| #400 | result = self.graph.query(cypher, params=params) |
| #401 | results.append(result) |
| #402 | |
| #403 | return results |
| #404 | |
| #405 | # added Entity label to all nodes for vector search to work |
| #406 | def _add_entities(self, to_be_added, filters, entity_type_map): |
| #407 | """Add the new entities to the graph. Merge the nodes if they already exist.""" |
| #408 | user_id = filters["user_id"] |
| #409 | agent_id = filters.get("agent_id", None) |
| #410 | results = [] |
| #411 | |
| #412 | for item in to_be_added: |
| #413 | # entities |
| #414 | source = item["source"] |
| #415 | destination = item["destination"] |
| #416 | relationship = item["relationship"] |
| #417 | |
| #418 | # types |
| #419 | source_type = entity_type_map.get(source, "__User__") |
| #420 | destination_type = entity_type_map.get(destination, "__User__") |
| #421 | |
| #422 | # embeddings |
| #423 | source_embedding = self.embedding_model.embed(source) |
| #424 | dest_embedding = self.embedding_model.embed(destination) |
| #425 | |
| #426 | # search for the nodes with the closest embeddings |
| #427 | source_node_search_result = self._search_source_node(source_embedding, filters, threshold=self.threshold) |
| #428 | destination_node_search_result = self._search_destination_node(dest_embedding, filters, threshold=self.threshold) |
| #429 | |
| #430 | # Prepare agent_id for node creation |
| #431 | agent_id_clause = "" |
| #432 | if agent_id: |
| #433 | agent_id_clause = ", agent_id: $agent_id" |
| #434 | |
| #435 | # TODO: Create a cypher query and common params for all the cases |
| #436 | if not destination_node_search_result and source_node_search_result: |
| #437 | cypher = f""" |
| #438 | MATCH (source:Entity) |
| #439 | WHERE id(source) = $source_id |
| #440 | MERGE (destination:{destination_type}:Entity {{name: $destination_name, user_id: $user_id{agent_id_clause}}}) |
| #441 | ON CREATE SET |
| #442 | destination.created = timestamp(), |
| #443 | destination.embedding = $destination_embedding, |
| #444 | destination:Entity |
| #445 | MERGE (source)-[r:{relationship}]->(destination) |
| #446 | ON CREATE SET |
| #447 | r.created = timestamp() |
| #448 | RETURN source.name AS source, type(r) AS relationship, destination.name AS target |
| #449 | """ |
| #450 | |
| #451 | params = { |
| #452 | "source_id": source_node_search_result[0]["id(source_candidate)"], |
| #453 | "destination_name": destination, |
| #454 | "destination_embedding": dest_embedding, |
| #455 | "user_id": user_id, |
| #456 | } |
| #457 | if agent_id: |
| #458 | params["agent_id"] = agent_id |
| #459 | |
| #460 | elif destination_node_search_result and not source_node_search_result: |
| #461 | cypher = f""" |
| #462 | MATCH (destination:Entity) |
| #463 | WHERE id(destination) = $destination_id |
| #464 | MERGE (source:{source_type}:Entity {{name: $source_name, user_id: $user_id{agent_id_clause}}}) |
| #465 | ON CREATE SET |
| #466 | source.created = timestamp(), |
| #467 | source.embedding = $source_embedding, |
| #468 | source:Entity |
| #469 | MERGE (source)-[r:{relationship}]->(destination) |
| #470 | ON CREATE SET |
| #471 | r.created = timestamp() |
| #472 | RETURN source.name AS source, type(r) AS relationship, destination.name AS target |
| #473 | """ |
| #474 | |
| #475 | params = { |
| #476 | "destination_id": destination_node_search_result[0]["id(destination_candidate)"], |
| #477 | "source_name": source, |
| #478 | "source_embedding": source_embedding, |
| #479 | "user_id": user_id, |
| #480 | } |
| #481 | if agent_id: |
| #482 | params["agent_id"] = agent_id |
| #483 | |
| #484 | elif source_node_search_result and destination_node_search_result: |
| #485 | cypher = f""" |
| #486 | MATCH (source:Entity) |
| #487 | WHERE id(source) = $source_id |
| #488 | MATCH (destination:Entity) |
| #489 | WHERE id(destination) = $destination_id |
| #490 | MERGE (source)-[r:{relationship}]->(destination) |
| #491 | ON CREATE SET |
| #492 | r.created_at = timestamp(), |
| #493 | r.updated_at = timestamp() |
| #494 | RETURN source.name AS source, type(r) AS relationship, destination.name AS target |
| #495 | """ |
| #496 | params = { |
| #497 | "source_id": source_node_search_result[0]["id(source_candidate)"], |
| #498 | "destination_id": destination_node_search_result[0]["id(destination_candidate)"], |
| #499 | "user_id": user_id, |
| #500 | } |
| #501 | if agent_id: |
| #502 | params["agent_id"] = agent_id |
| #503 | |
| #504 | else: |
| #505 | cypher = f""" |
| #506 | MERGE (n:{source_type}:Entity {{name: $source_name, user_id: $user_id{agent_id_clause}}}) |
| #507 | ON CREATE SET n.created = timestamp(), n.embedding = $source_embedding, n:Entity |
| #508 | ON MATCH SET n.embedding = $source_embedding |
| #509 | MERGE (m:{destination_type}:Entity {{name: $dest_name, user_id: $user_id{agent_id_clause}}}) |
| #510 | ON CREATE SET m.created = timestamp(), m.embedding = $dest_embedding, m:Entity |
| #511 | ON MATCH SET m.embedding = $dest_embedding |
| #512 | MERGE (n)-[rel:{relationship}]->(m) |
| #513 | ON CREATE SET rel.created = timestamp() |
| #514 | RETURN n.name AS source, type(rel) AS relationship, m.name AS target |
| #515 | """ |
| #516 | params = { |
| #517 | "source_name": source, |
| #518 | "dest_name": destination, |
| #519 | "source_embedding": source_embedding, |
| #520 | "dest_embedding": dest_embedding, |
| #521 | "user_id": user_id, |
| #522 | } |
| #523 | if agent_id: |
| #524 | params["agent_id"] = agent_id |
| #525 | |
| #526 | result = self.graph.query(cypher, params=params) |
| #527 | results.append(result) |
| #528 | return results |
| #529 | |
| #530 | def _remove_spaces_from_entities(self, entity_list): |
| #531 | for item in entity_list: |
| #532 | item["source"] = item["source"].lower().replace(" ", "_") |
| #533 | # Use the sanitization function for relationships to handle special characters |
| #534 | item["relationship"] = sanitize_relationship_for_cypher(item["relationship"].lower().replace(" ", "_")) |
| #535 | item["destination"] = item["destination"].lower().replace(" ", "_") |
| #536 | return entity_list |
| #537 | |
| #538 | def _search_source_node(self, source_embedding, filters, threshold=0.9): |
| #539 | """Search for source nodes with similar embeddings.""" |
| #540 | user_id = filters["user_id"] |
| #541 | agent_id = filters.get("agent_id", None) |
| #542 | |
| #543 | if agent_id: |
| #544 | cypher = """ |
| #545 | CALL vector_search.search("memzero", 1, $source_embedding) |
| #546 | YIELD distance, node, similarity |
| #547 | WITH node AS source_candidate, similarity |
| #548 | WHERE source_candidate.user_id = $user_id |
| #549 | AND source_candidate.agent_id = $agent_id |
| #550 | AND similarity >= $threshold |
| #551 | RETURN id(source_candidate); |
| #552 | """ |
| #553 | params = { |
| #554 | "source_embedding": source_embedding, |
| #555 | "user_id": user_id, |
| #556 | "agent_id": agent_id, |
| #557 | "threshold": threshold, |
| #558 | } |
| #559 | else: |
| #560 | cypher = """ |
| #561 | CALL vector_search.search("memzero", 1, $source_embedding) |
| #562 | YIELD distance, node, similarity |
| #563 | WITH node AS source_candidate, similarity |
| #564 | WHERE source_candidate.user_id = $user_id |
| #565 | AND similarity >= $threshold |
| #566 | RETURN id(source_candidate); |
| #567 | """ |
| #568 | params = { |
| #569 | "source_embedding": source_embedding, |
| #570 | "user_id": user_id, |
| #571 | "threshold": threshold, |
| #572 | } |
| #573 | |
| #574 | result = self.graph.query(cypher, params=params) |
| #575 | return result |
| #576 | |
| #577 | def _search_destination_node(self, destination_embedding, filters, threshold=0.9): |
| #578 | """Search for destination nodes with similar embeddings.""" |
| #579 | user_id = filters["user_id"] |
| #580 | agent_id = filters.get("agent_id", None) |
| #581 | |
| #582 | if agent_id: |
| #583 | cypher = """ |
| #584 | CALL vector_search.search("memzero", 1, $destination_embedding) |
| #585 | YIELD distance, node, similarity |
| #586 | WITH node AS destination_candidate, similarity |
| #587 | WHERE node.user_id = $user_id |
| #588 | AND node.agent_id = $agent_id |
| #589 | AND similarity >= $threshold |
| #590 | RETURN id(destination_candidate); |
| #591 | """ |
| #592 | params = { |
| #593 | "destination_embedding": destination_embedding, |
| #594 | "user_id": user_id, |
| #595 | "agent_id": agent_id, |
| #596 | "threshold": threshold, |
| #597 | } |
| #598 | else: |
| #599 | cypher = """ |
| #600 | CALL vector_search.search("memzero", 1, $destination_embedding) |
| #601 | YIELD distance, node, similarity |
| #602 | WITH node AS destination_candidate, similarity |
| #603 | WHERE node.user_id = $user_id |
| #604 | AND similarity >= $threshold |
| #605 | RETURN id(destination_candidate); |
| #606 | """ |
| #607 | params = { |
| #608 | "destination_embedding": destination_embedding, |
| #609 | "user_id": user_id, |
| #610 | "threshold": threshold, |
| #611 | } |
| #612 | |
| #613 | result = self.graph.query(cypher, params=params) |
| #614 | return result |
| #615 | |
| #616 | |
| #617 | def _vector_index_exists(self, index_info, index_name): |
| #618 | """ |
| #619 | Check if a vector index exists, compatible with both Memgraph versions. |
| #620 | |
| #621 | Args: |
| #622 | index_info (dict): Index information from _fetch_existing_indexes |
| #623 | index_name (str): Name of the index to check |
| #624 | |
| #625 | Returns: |
| #626 | bool: True if index exists, False otherwise |
| #627 | """ |
| #628 | vector_indexes = index_info.get("vector_index_exists", []) |
| #629 | |
| #630 | # Check for index by name regardless of version-specific format differences |
| #631 | return any( |
| #632 | idx.get("index_name") == index_name or |
| #633 | idx.get("index name") == index_name or |
| #634 | idx.get("name") == index_name |
| #635 | for idx in vector_indexes |
| #636 | ) |
| #637 | |
| #638 | def _label_property_index_exists(self, index_info, label, property_name): |
| #639 | """ |
| #640 | Check if a label+property index exists, compatible with both versions. |
| #641 | |
| #642 | Args: |
| #643 | index_info (dict): Index information from _fetch_existing_indexes |
| #644 | label (str): Label name |
| #645 | property_name (str): Property name |
| #646 | |
| #647 | Returns: |
| #648 | bool: True if index exists, False otherwise |
| #649 | """ |
| #650 | indexes = index_info.get("index_exists", []) |
| #651 | |
| #652 | return any( |
| #653 | (idx.get("index type") == "label+property" or idx.get("index_type") == "label+property") and |
| #654 | (idx.get("label") == label) and |
| #655 | (idx.get("property") == property_name or property_name in str(idx.get("properties", ""))) |
| #656 | for idx in indexes |
| #657 | ) |
| #658 | |
| #659 | def _label_index_exists(self, index_info, label): |
| #660 | """ |
| #661 | Check if a label index exists, compatible with both versions. |
| #662 | |
| #663 | Args: |
| #664 | index_info (dict): Index information from _fetch_existing_indexes |
| #665 | label (str): Label name |
| #666 | |
| #667 | Returns: |
| #668 | bool: True if index exists, False otherwise |
| #669 | """ |
| #670 | indexes = index_info.get("index_exists", []) |
| #671 | |
| #672 | return any( |
| #673 | (idx.get("index type") == "label" or idx.get("index_type") == "label") and |
| #674 | (idx.get("label") == label) |
| #675 | for idx in indexes |
| #676 | ) |
| #677 | |
| #678 | def _fetch_existing_indexes(self): |
| #679 | """ |
| #680 | Retrieves information about existing indexes and vector indexes in the Memgraph database. |
| #681 | |
| #682 | Returns: |
| #683 | dict: A dictionary containing lists of existing indexes and vector indexes. |
| #684 | """ |
| #685 | try: |
| #686 | index_exists = list(self.graph.query("SHOW INDEX INFO;")) |
| #687 | vector_index_exists = list(self.graph.query("SHOW VECTOR INDEX INFO;")) |
| #688 | return {"index_exists": index_exists, "vector_index_exists": vector_index_exists} |
| #689 | except Exception as e: |
| #690 | logger.warning(f"Error fetching indexes: {e}. Returning empty index info.") |
| #691 | return {"index_exists": [], "vector_index_exists": []} |
| #692 |