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_neo4j import Neo4jGraph |
| #7 | except ImportError: |
| #8 | raise ImportError("langchain_neo4j is not installed. Please install it using pip install langchain-neo4j") |
| #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 = Neo4jGraph( |
| #33 | self.config.graph_store.config.url, |
| #34 | self.config.graph_store.config.username, |
| #35 | self.config.graph_store.config.password, |
| #36 | self.config.graph_store.config.database, |
| #37 | refresh_schema=False, |
| #38 | driver_config={"notifications_min_severity": "OFF"}, |
| #39 | ) |
| #40 | self.embedding_model = EmbedderFactory.create( |
| #41 | self.config.embedder.provider, self.config.embedder.config, self.config.vector_store.config |
| #42 | ) |
| #43 | self.node_label = ":`__Entity__`" if self.config.graph_store.config.base_label else "" |
| #44 | |
| #45 | if self.config.graph_store.config.base_label: |
| #46 | # Safely add user_id index |
| #47 | try: |
| #48 | self.graph.query(f"CREATE INDEX entity_single IF NOT EXISTS FOR (n {self.node_label}) ON (n.user_id)") |
| #49 | except Exception: |
| #50 | pass |
| #51 | try: # Safely try to add composite index (Enterprise only) |
| #52 | self.graph.query( |
| #53 | f"CREATE INDEX entity_composite IF NOT EXISTS FOR (n {self.node_label}) ON (n.name, n.user_id)" |
| #54 | ) |
| #55 | except Exception: |
| #56 | pass |
| #57 | |
| #58 | # Default to openai if no specific provider is configured |
| #59 | self.llm_provider = "openai" |
| #60 | if self.config.llm and self.config.llm.provider: |
| #61 | self.llm_provider = self.config.llm.provider |
| #62 | if self.config.graph_store and self.config.graph_store.llm and self.config.graph_store.llm.provider: |
| #63 | self.llm_provider = self.config.graph_store.llm.provider |
| #64 | |
| #65 | # Get LLM config with proper null checks |
| #66 | llm_config = None |
| #67 | if self.config.graph_store and self.config.graph_store.llm and hasattr(self.config.graph_store.llm, "config"): |
| #68 | llm_config = self.config.graph_store.llm.config |
| #69 | elif hasattr(self.config.llm, "config"): |
| #70 | llm_config = self.config.llm.config |
| #71 | self.llm = LlmFactory.create(self.llm_provider, llm_config) |
| #72 | self.user_id = None |
| #73 | # Use threshold from graph_store config, default to 0.7 for backward compatibility |
| #74 | self.threshold = self.config.graph_store.threshold if hasattr(self.config.graph_store, 'threshold') else 0.7 |
| #75 | |
| #76 | def add(self, data, filters): |
| #77 | """ |
| #78 | Adds data to the graph. |
| #79 | |
| #80 | Args: |
| #81 | data (str): The data to add to the graph. |
| #82 | filters (dict): A dictionary containing filters to be applied during the addition. |
| #83 | """ |
| #84 | entity_type_map = self._retrieve_nodes_from_data(data, filters) |
| #85 | to_be_added = self._establish_nodes_relations_from_data(data, filters, entity_type_map) |
| #86 | search_output = self._search_graph_db(node_list=list(entity_type_map.keys()), filters=filters) |
| #87 | to_be_deleted = self._get_delete_entities_from_search_output(search_output, data, filters) |
| #88 | |
| #89 | # TODO: Batch queries with APOC plugin |
| #90 | # TODO: Add more filter support |
| #91 | deleted_entities = self._delete_entities(to_be_deleted, filters) |
| #92 | added_entities = self._add_entities(to_be_added, filters, entity_type_map) |
| #93 | |
| #94 | return {"deleted_entities": deleted_entities, "added_entities": added_entities} |
| #95 | |
| #96 | def search(self, query, filters, limit=100): |
| #97 | """ |
| #98 | Search for memories and related graph data. |
| #99 | |
| #100 | Args: |
| #101 | query (str): Query to search for. |
| #102 | filters (dict): A dictionary containing filters to be applied during the search. |
| #103 | limit (int): The maximum number of nodes and relationships to retrieve. Defaults to 100. |
| #104 | |
| #105 | Returns: |
| #106 | dict: A dictionary containing: |
| #107 | - "contexts": List of search results from the base data store. |
| #108 | - "entities": List of related graph data based on the query. |
| #109 | """ |
| #110 | entity_type_map = self._retrieve_nodes_from_data(query, filters) |
| #111 | search_output = self._search_graph_db(node_list=list(entity_type_map.keys()), filters=filters) |
| #112 | |
| #113 | if not search_output: |
| #114 | return [] |
| #115 | |
| #116 | search_outputs_sequence = [ |
| #117 | [item["source"], item["relationship"], item["destination"]] for item in search_output |
| #118 | ] |
| #119 | bm25 = BM25Okapi(search_outputs_sequence) |
| #120 | |
| #121 | tokenized_query = query.split(" ") |
| #122 | reranked_results = bm25.get_top_n(tokenized_query, search_outputs_sequence, n=5) |
| #123 | |
| #124 | search_results = [] |
| #125 | for item in reranked_results: |
| #126 | search_results.append({"source": item[0], "relationship": item[1], "destination": item[2]}) |
| #127 | |
| #128 | logger.info(f"Returned {len(search_results)} search results") |
| #129 | |
| #130 | return search_results |
| #131 | |
| #132 | def delete_all(self, filters): |
| #133 | # Build node properties for filtering |
| #134 | node_props = ["user_id: $user_id"] |
| #135 | if filters.get("agent_id"): |
| #136 | node_props.append("agent_id: $agent_id") |
| #137 | if filters.get("run_id"): |
| #138 | node_props.append("run_id: $run_id") |
| #139 | node_props_str = ", ".join(node_props) |
| #140 | |
| #141 | cypher = f""" |
| #142 | MATCH (n {self.node_label} {{{node_props_str}}}) |
| #143 | DETACH DELETE n |
| #144 | """ |
| #145 | params = {"user_id": filters["user_id"]} |
| #146 | if filters.get("agent_id"): |
| #147 | params["agent_id"] = filters["agent_id"] |
| #148 | if filters.get("run_id"): |
| #149 | params["run_id"] = filters["run_id"] |
| #150 | self.graph.query(cypher, params=params) |
| #151 | |
| #152 | def get_all(self, filters, limit=100): |
| #153 | """ |
| #154 | Retrieves all nodes and relationships from the graph database based on optional filtering criteria. |
| #155 | Args: |
| #156 | filters (dict): A dictionary containing filters to be applied during the retrieval. |
| #157 | limit (int): The maximum number of nodes and relationships to retrieve. Defaults to 100. |
| #158 | Returns: |
| #159 | list: A list of dictionaries, each containing: |
| #160 | - 'contexts': The base data store response for each memory. |
| #161 | - 'entities': A list of strings representing the nodes and relationships |
| #162 | """ |
| #163 | params = {"user_id": filters["user_id"], "limit": limit} |
| #164 | |
| #165 | # Build node properties based on filters |
| #166 | node_props = ["user_id: $user_id"] |
| #167 | if filters.get("agent_id"): |
| #168 | node_props.append("agent_id: $agent_id") |
| #169 | params["agent_id"] = filters["agent_id"] |
| #170 | if filters.get("run_id"): |
| #171 | node_props.append("run_id: $run_id") |
| #172 | params["run_id"] = filters["run_id"] |
| #173 | node_props_str = ", ".join(node_props) |
| #174 | |
| #175 | query = f""" |
| #176 | MATCH (n {self.node_label} {{{node_props_str}}})-[r]->(m {self.node_label} {{{node_props_str}}}) |
| #177 | RETURN n.name AS source, type(r) AS relationship, m.name AS target |
| #178 | LIMIT $limit |
| #179 | """ |
| #180 | results = self.graph.query(query, params=params) |
| #181 | |
| #182 | final_results = [] |
| #183 | for result in results: |
| #184 | final_results.append( |
| #185 | { |
| #186 | "source": result["source"], |
| #187 | "relationship": result["relationship"], |
| #188 | "target": result["target"], |
| #189 | } |
| #190 | ) |
| #191 | |
| #192 | logger.info(f"Retrieved {len(final_results)} relationships") |
| #193 | |
| #194 | return final_results |
| #195 | |
| #196 | def _retrieve_nodes_from_data(self, data, filters): |
| #197 | """Extracts all the entities mentioned in the query.""" |
| #198 | _tools = [EXTRACT_ENTITIES_TOOL] |
| #199 | if self.llm_provider in ["azure_openai_structured", "openai_structured"]: |
| #200 | _tools = [EXTRACT_ENTITIES_STRUCT_TOOL] |
| #201 | search_results = self.llm.generate_response( |
| #202 | messages=[ |
| #203 | { |
| #204 | "role": "system", |
| #205 | "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.", |
| #206 | }, |
| #207 | {"role": "user", "content": data}, |
| #208 | ], |
| #209 | tools=_tools, |
| #210 | ) |
| #211 | |
| #212 | entity_type_map = {} |
| #213 | |
| #214 | try: |
| #215 | for tool_call in search_results["tool_calls"]: |
| #216 | if tool_call["name"] != "extract_entities": |
| #217 | continue |
| #218 | for item in tool_call["arguments"]["entities"]: |
| #219 | entity_type_map[item["entity"]] = item["entity_type"] |
| #220 | except Exception as e: |
| #221 | logger.exception( |
| #222 | f"Error in search tool: {e}, llm_provider={self.llm_provider}, search_results={search_results}" |
| #223 | ) |
| #224 | |
| #225 | entity_type_map = {k.lower().replace(" ", "_"): v.lower().replace(" ", "_") for k, v in entity_type_map.items()} |
| #226 | logger.debug(f"Entity type map: {entity_type_map}\n search_results={search_results}") |
| #227 | return entity_type_map |
| #228 | |
| #229 | def _establish_nodes_relations_from_data(self, data, filters, entity_type_map): |
| #230 | """Establish relations among the extracted nodes.""" |
| #231 | |
| #232 | # Compose user identification string for prompt |
| #233 | user_identity = f"user_id: {filters['user_id']}" |
| #234 | if filters.get("agent_id"): |
| #235 | user_identity += f", agent_id: {filters['agent_id']}" |
| #236 | if filters.get("run_id"): |
| #237 | user_identity += f", run_id: {filters['run_id']}" |
| #238 | |
| #239 | if self.config.graph_store.custom_prompt: |
| #240 | system_content = EXTRACT_RELATIONS_PROMPT.replace("USER_ID", user_identity) |
| #241 | # Add the custom prompt line if configured |
| #242 | system_content = system_content.replace("CUSTOM_PROMPT", f"4. {self.config.graph_store.custom_prompt}") |
| #243 | messages = [ |
| #244 | {"role": "system", "content": system_content}, |
| #245 | {"role": "user", "content": data}, |
| #246 | ] |
| #247 | else: |
| #248 | system_content = EXTRACT_RELATIONS_PROMPT.replace("USER_ID", user_identity) |
| #249 | messages = [ |
| #250 | {"role": "system", "content": system_content}, |
| #251 | {"role": "user", "content": f"List of entities: {list(entity_type_map.keys())}. \n\nText: {data}"}, |
| #252 | ] |
| #253 | |
| #254 | _tools = [RELATIONS_TOOL] |
| #255 | if self.llm_provider in ["azure_openai_structured", "openai_structured"]: |
| #256 | _tools = [RELATIONS_STRUCT_TOOL] |
| #257 | |
| #258 | extracted_entities = self.llm.generate_response( |
| #259 | messages=messages, |
| #260 | tools=_tools, |
| #261 | ) |
| #262 | |
| #263 | entities = [] |
| #264 | if extracted_entities.get("tool_calls"): |
| #265 | entities = extracted_entities["tool_calls"][0].get("arguments", {}).get("entities", []) |
| #266 | |
| #267 | entities = self._remove_spaces_from_entities(entities) |
| #268 | logger.debug(f"Extracted entities: {entities}") |
| #269 | return entities |
| #270 | |
| #271 | def _search_graph_db(self, node_list, filters, limit=100): |
| #272 | """Search similar nodes among and their respective incoming and outgoing relations.""" |
| #273 | result_relations = [] |
| #274 | |
| #275 | # Build node properties for filtering |
| #276 | node_props = ["user_id: $user_id"] |
| #277 | if filters.get("agent_id"): |
| #278 | node_props.append("agent_id: $agent_id") |
| #279 | if filters.get("run_id"): |
| #280 | node_props.append("run_id: $run_id") |
| #281 | node_props_str = ", ".join(node_props) |
| #282 | |
| #283 | for node in node_list: |
| #284 | n_embedding = self.embedding_model.embed(node) |
| #285 | |
| #286 | cypher_query = f""" |
| #287 | MATCH (n {self.node_label} {{{node_props_str}}}) |
| #288 | WHERE n.embedding IS NOT NULL |
| #289 | WITH n, round(2 * vector.similarity.cosine(n.embedding, $n_embedding) - 1, 4) AS similarity // denormalize for backward compatibility |
| #290 | WHERE similarity >= $threshold |
| #291 | CALL {{ |
| #292 | WITH n |
| #293 | MATCH (n)-[r]->(m {self.node_label} {{{node_props_str}}}) |
| #294 | RETURN n.name AS source, elementId(n) AS source_id, type(r) AS relationship, elementId(r) AS relation_id, m.name AS destination, elementId(m) AS destination_id |
| #295 | UNION |
| #296 | WITH n |
| #297 | MATCH (n)<-[r]-(m {self.node_label} {{{node_props_str}}}) |
| #298 | RETURN m.name AS source, elementId(m) AS source_id, type(r) AS relationship, elementId(r) AS relation_id, n.name AS destination, elementId(n) AS destination_id |
| #299 | }} |
| #300 | WITH distinct source, source_id, relationship, relation_id, destination, destination_id, similarity |
| #301 | RETURN source, source_id, relationship, relation_id, destination, destination_id, similarity |
| #302 | ORDER BY similarity DESC |
| #303 | LIMIT $limit |
| #304 | """ |
| #305 | |
| #306 | params = { |
| #307 | "n_embedding": n_embedding, |
| #308 | "threshold": self.threshold, |
| #309 | "user_id": filters["user_id"], |
| #310 | "limit": limit, |
| #311 | } |
| #312 | if filters.get("agent_id"): |
| #313 | params["agent_id"] = filters["agent_id"] |
| #314 | if filters.get("run_id"): |
| #315 | params["run_id"] = filters["run_id"] |
| #316 | |
| #317 | ans = self.graph.query(cypher_query, params=params) |
| #318 | result_relations.extend(ans) |
| #319 | |
| #320 | return result_relations |
| #321 | |
| #322 | def _get_delete_entities_from_search_output(self, search_output, data, filters): |
| #323 | """Get the entities to be deleted from the search output.""" |
| #324 | search_output_string = format_entities(search_output) |
| #325 | |
| #326 | # Compose user identification string for prompt |
| #327 | user_identity = f"user_id: {filters['user_id']}" |
| #328 | if filters.get("agent_id"): |
| #329 | user_identity += f", agent_id: {filters['agent_id']}" |
| #330 | if filters.get("run_id"): |
| #331 | user_identity += f", run_id: {filters['run_id']}" |
| #332 | |
| #333 | system_prompt, user_prompt = get_delete_messages(search_output_string, data, user_identity) |
| #334 | |
| #335 | _tools = [DELETE_MEMORY_TOOL_GRAPH] |
| #336 | if self.llm_provider in ["azure_openai_structured", "openai_structured"]: |
| #337 | _tools = [ |
| #338 | DELETE_MEMORY_STRUCT_TOOL_GRAPH, |
| #339 | ] |
| #340 | |
| #341 | memory_updates = self.llm.generate_response( |
| #342 | messages=[ |
| #343 | {"role": "system", "content": system_prompt}, |
| #344 | {"role": "user", "content": user_prompt}, |
| #345 | ], |
| #346 | tools=_tools, |
| #347 | ) |
| #348 | |
| #349 | to_be_deleted = [] |
| #350 | for item in memory_updates.get("tool_calls", []): |
| #351 | if item.get("name") == "delete_graph_memory": |
| #352 | to_be_deleted.append(item.get("arguments")) |
| #353 | # Clean entities formatting |
| #354 | to_be_deleted = self._remove_spaces_from_entities(to_be_deleted) |
| #355 | logger.debug(f"Deleted relationships: {to_be_deleted}") |
| #356 | return to_be_deleted |
| #357 | |
| #358 | def _delete_entities(self, to_be_deleted, filters): |
| #359 | """Delete the entities from the graph.""" |
| #360 | user_id = filters["user_id"] |
| #361 | agent_id = filters.get("agent_id", None) |
| #362 | run_id = filters.get("run_id", None) |
| #363 | results = [] |
| #364 | |
| #365 | for item in to_be_deleted: |
| #366 | source = item["source"] |
| #367 | destination = item["destination"] |
| #368 | relationship = item["relationship"] |
| #369 | |
| #370 | # Build the agent filter for the query |
| #371 | |
| #372 | params = { |
| #373 | "source_name": source, |
| #374 | "dest_name": destination, |
| #375 | "user_id": user_id, |
| #376 | } |
| #377 | |
| #378 | if agent_id: |
| #379 | params["agent_id"] = agent_id |
| #380 | if run_id: |
| #381 | params["run_id"] = run_id |
| #382 | |
| #383 | # Build node properties for filtering |
| #384 | source_props = ["name: $source_name", "user_id: $user_id"] |
| #385 | dest_props = ["name: $dest_name", "user_id: $user_id"] |
| #386 | if agent_id: |
| #387 | source_props.append("agent_id: $agent_id") |
| #388 | dest_props.append("agent_id: $agent_id") |
| #389 | if run_id: |
| #390 | source_props.append("run_id: $run_id") |
| #391 | dest_props.append("run_id: $run_id") |
| #392 | source_props_str = ", ".join(source_props) |
| #393 | dest_props_str = ", ".join(dest_props) |
| #394 | |
| #395 | # Delete the specific relationship between nodes |
| #396 | cypher = f""" |
| #397 | MATCH (n {self.node_label} {{{source_props_str}}}) |
| #398 | -[r:{relationship}]-> |
| #399 | (m {self.node_label} {{{dest_props_str}}}) |
| #400 | |
| #401 | DELETE r |
| #402 | RETURN |
| #403 | n.name AS source, |
| #404 | m.name AS target, |
| #405 | type(r) AS relationship |
| #406 | """ |
| #407 | |
| #408 | result = self.graph.query(cypher, params=params) |
| #409 | results.append(result) |
| #410 | |
| #411 | return results |
| #412 | |
| #413 | def _add_entities(self, to_be_added, filters, entity_type_map): |
| #414 | """Add the new entities to the graph. Merge the nodes if they already exist.""" |
| #415 | user_id = filters["user_id"] |
| #416 | agent_id = filters.get("agent_id", None) |
| #417 | run_id = filters.get("run_id", None) |
| #418 | results = [] |
| #419 | for item in to_be_added: |
| #420 | # entities |
| #421 | source = item["source"] |
| #422 | destination = item["destination"] |
| #423 | relationship = item["relationship"] |
| #424 | |
| #425 | # types |
| #426 | source_type = entity_type_map.get(source, "__User__") |
| #427 | source_label = self.node_label if self.node_label else f":`{source_type}`" |
| #428 | source_extra_set = f", source:`{source_type}`" if self.node_label else "" |
| #429 | destination_type = entity_type_map.get(destination, "__User__") |
| #430 | destination_label = self.node_label if self.node_label else f":`{destination_type}`" |
| #431 | destination_extra_set = f", destination:`{destination_type}`" if self.node_label else "" |
| #432 | |
| #433 | # embeddings |
| #434 | source_embedding = self.embedding_model.embed(source) |
| #435 | dest_embedding = self.embedding_model.embed(destination) |
| #436 | |
| #437 | # search for the nodes with the closest embeddings |
| #438 | source_node_search_result = self._search_source_node(source_embedding, filters, threshold=self.threshold) |
| #439 | destination_node_search_result = self._search_destination_node(dest_embedding, filters, threshold=self.threshold) |
| #440 | |
| #441 | # TODO: Create a cypher query and common params for all the cases |
| #442 | if not destination_node_search_result and source_node_search_result: |
| #443 | # Build destination MERGE properties |
| #444 | merge_props = ["name: $destination_name", "user_id: $user_id"] |
| #445 | if agent_id: |
| #446 | merge_props.append("agent_id: $agent_id") |
| #447 | if run_id: |
| #448 | merge_props.append("run_id: $run_id") |
| #449 | merge_props_str = ", ".join(merge_props) |
| #450 | |
| #451 | cypher = f""" |
| #452 | MATCH (source) |
| #453 | WHERE elementId(source) = $source_id |
| #454 | SET source.mentions = coalesce(source.mentions, 0) + 1 |
| #455 | WITH source |
| #456 | MERGE (destination {destination_label} {{{merge_props_str}}}) |
| #457 | ON CREATE SET |
| #458 | destination.created = timestamp(), |
| #459 | destination.mentions = 1 |
| #460 | {destination_extra_set} |
| #461 | ON MATCH SET |
| #462 | destination.mentions = coalesce(destination.mentions, 0) + 1 |
| #463 | WITH source, destination |
| #464 | CALL db.create.setNodeVectorProperty(destination, 'embedding', $destination_embedding) |
| #465 | WITH source, destination |
| #466 | MERGE (source)-[r:{relationship}]->(destination) |
| #467 | ON CREATE SET |
| #468 | r.created = timestamp(), |
| #469 | r.mentions = 1 |
| #470 | ON MATCH SET |
| #471 | r.mentions = coalesce(r.mentions, 0) + 1 |
| #472 | RETURN source.name AS source, type(r) AS relationship, destination.name AS target |
| #473 | """ |
| #474 | |
| #475 | params = { |
| #476 | "source_id": source_node_search_result[0]["elementId(source_candidate)"], |
| #477 | "destination_name": destination, |
| #478 | "destination_embedding": dest_embedding, |
| #479 | "user_id": user_id, |
| #480 | } |
| #481 | if agent_id: |
| #482 | params["agent_id"] = agent_id |
| #483 | if run_id: |
| #484 | params["run_id"] = run_id |
| #485 | |
| #486 | elif destination_node_search_result and not source_node_search_result: |
| #487 | # Build source MERGE properties |
| #488 | merge_props = ["name: $source_name", "user_id: $user_id"] |
| #489 | if agent_id: |
| #490 | merge_props.append("agent_id: $agent_id") |
| #491 | if run_id: |
| #492 | merge_props.append("run_id: $run_id") |
| #493 | merge_props_str = ", ".join(merge_props) |
| #494 | |
| #495 | cypher = f""" |
| #496 | MATCH (destination) |
| #497 | WHERE elementId(destination) = $destination_id |
| #498 | SET destination.mentions = coalesce(destination.mentions, 0) + 1 |
| #499 | WITH destination |
| #500 | MERGE (source {source_label} {{{merge_props_str}}}) |
| #501 | ON CREATE SET |
| #502 | source.created = timestamp(), |
| #503 | source.mentions = 1 |
| #504 | {source_extra_set} |
| #505 | ON MATCH SET |
| #506 | source.mentions = coalesce(source.mentions, 0) + 1 |
| #507 | WITH source, destination |
| #508 | CALL db.create.setNodeVectorProperty(source, 'embedding', $source_embedding) |
| #509 | WITH source, destination |
| #510 | MERGE (source)-[r:{relationship}]->(destination) |
| #511 | ON CREATE SET |
| #512 | r.created = timestamp(), |
| #513 | r.mentions = 1 |
| #514 | ON MATCH SET |
| #515 | r.mentions = coalesce(r.mentions, 0) + 1 |
| #516 | RETURN source.name AS source, type(r) AS relationship, destination.name AS target |
| #517 | """ |
| #518 | |
| #519 | params = { |
| #520 | "destination_id": destination_node_search_result[0]["elementId(destination_candidate)"], |
| #521 | "source_name": source, |
| #522 | "source_embedding": source_embedding, |
| #523 | "user_id": user_id, |
| #524 | } |
| #525 | if agent_id: |
| #526 | params["agent_id"] = agent_id |
| #527 | if run_id: |
| #528 | params["run_id"] = run_id |
| #529 | |
| #530 | elif source_node_search_result and destination_node_search_result: |
| #531 | cypher = f""" |
| #532 | MATCH (source) |
| #533 | WHERE elementId(source) = $source_id |
| #534 | SET source.mentions = coalesce(source.mentions, 0) + 1 |
| #535 | WITH source |
| #536 | MATCH (destination) |
| #537 | WHERE elementId(destination) = $destination_id |
| #538 | SET destination.mentions = coalesce(destination.mentions, 0) + 1 |
| #539 | MERGE (source)-[r:{relationship}]->(destination) |
| #540 | ON CREATE SET |
| #541 | r.created_at = timestamp(), |
| #542 | r.updated_at = timestamp(), |
| #543 | r.mentions = 1 |
| #544 | ON MATCH SET r.mentions = coalesce(r.mentions, 0) + 1 |
| #545 | RETURN source.name AS source, type(r) AS relationship, destination.name AS target |
| #546 | """ |
| #547 | |
| #548 | params = { |
| #549 | "source_id": source_node_search_result[0]["elementId(source_candidate)"], |
| #550 | "destination_id": destination_node_search_result[0]["elementId(destination_candidate)"], |
| #551 | "user_id": user_id, |
| #552 | } |
| #553 | if agent_id: |
| #554 | params["agent_id"] = agent_id |
| #555 | if run_id: |
| #556 | params["run_id"] = run_id |
| #557 | |
| #558 | else: |
| #559 | # Build dynamic MERGE props for both source and destination |
| #560 | source_props = ["name: $source_name", "user_id: $user_id"] |
| #561 | dest_props = ["name: $dest_name", "user_id: $user_id"] |
| #562 | if agent_id: |
| #563 | source_props.append("agent_id: $agent_id") |
| #564 | dest_props.append("agent_id: $agent_id") |
| #565 | if run_id: |
| #566 | source_props.append("run_id: $run_id") |
| #567 | dest_props.append("run_id: $run_id") |
| #568 | source_props_str = ", ".join(source_props) |
| #569 | dest_props_str = ", ".join(dest_props) |
| #570 | |
| #571 | cypher = f""" |
| #572 | MERGE (source {source_label} {{{source_props_str}}}) |
| #573 | ON CREATE SET source.created = timestamp(), |
| #574 | source.mentions = 1 |
| #575 | {source_extra_set} |
| #576 | ON MATCH SET source.mentions = coalesce(source.mentions, 0) + 1 |
| #577 | WITH source |
| #578 | CALL db.create.setNodeVectorProperty(source, 'embedding', $source_embedding) |
| #579 | WITH source |
| #580 | MERGE (destination {destination_label} {{{dest_props_str}}}) |
| #581 | ON CREATE SET destination.created = timestamp(), |
| #582 | destination.mentions = 1 |
| #583 | {destination_extra_set} |
| #584 | ON MATCH SET destination.mentions = coalesce(destination.mentions, 0) + 1 |
| #585 | WITH source, destination |
| #586 | CALL db.create.setNodeVectorProperty(destination, 'embedding', $dest_embedding) |
| #587 | WITH source, destination |
| #588 | MERGE (source)-[rel:{relationship}]->(destination) |
| #589 | ON CREATE SET rel.created = timestamp(), rel.mentions = 1 |
| #590 | ON MATCH SET rel.mentions = coalesce(rel.mentions, 0) + 1 |
| #591 | RETURN source.name AS source, type(rel) AS relationship, destination.name AS target |
| #592 | """ |
| #593 | |
| #594 | params = { |
| #595 | "source_name": source, |
| #596 | "dest_name": destination, |
| #597 | "source_embedding": source_embedding, |
| #598 | "dest_embedding": dest_embedding, |
| #599 | "user_id": user_id, |
| #600 | } |
| #601 | if agent_id: |
| #602 | params["agent_id"] = agent_id |
| #603 | if run_id: |
| #604 | params["run_id"] = run_id |
| #605 | result = self.graph.query(cypher, params=params) |
| #606 | results.append(result) |
| #607 | return results |
| #608 | |
| #609 | def _remove_spaces_from_entities(self, entity_list): |
| #610 | for item in entity_list: |
| #611 | item["source"] = item["source"].lower().replace(" ", "_") |
| #612 | # Use the sanitization function for relationships to handle special characters |
| #613 | item["relationship"] = sanitize_relationship_for_cypher(item["relationship"].lower().replace(" ", "_")) |
| #614 | item["destination"] = item["destination"].lower().replace(" ", "_") |
| #615 | return entity_list |
| #616 | |
| #617 | def _search_source_node(self, source_embedding, filters, threshold=0.9): |
| #618 | # Build WHERE conditions |
| #619 | where_conditions = ["source_candidate.embedding IS NOT NULL", "source_candidate.user_id = $user_id"] |
| #620 | if filters.get("agent_id"): |
| #621 | where_conditions.append("source_candidate.agent_id = $agent_id") |
| #622 | if filters.get("run_id"): |
| #623 | where_conditions.append("source_candidate.run_id = $run_id") |
| #624 | where_clause = " AND ".join(where_conditions) |
| #625 | |
| #626 | cypher = f""" |
| #627 | MATCH (source_candidate {self.node_label}) |
| #628 | WHERE {where_clause} |
| #629 | |
| #630 | WITH source_candidate, |
| #631 | round(2 * vector.similarity.cosine(source_candidate.embedding, $source_embedding) - 1, 4) AS source_similarity // denormalize for backward compatibility |
| #632 | WHERE source_similarity >= $threshold |
| #633 | |
| #634 | WITH source_candidate, source_similarity |
| #635 | ORDER BY source_similarity DESC |
| #636 | LIMIT 1 |
| #637 | |
| #638 | RETURN elementId(source_candidate) |
| #639 | """ |
| #640 | |
| #641 | params = { |
| #642 | "source_embedding": source_embedding, |
| #643 | "user_id": filters["user_id"], |
| #644 | "threshold": threshold, |
| #645 | } |
| #646 | if filters.get("agent_id"): |
| #647 | params["agent_id"] = filters["agent_id"] |
| #648 | if filters.get("run_id"): |
| #649 | params["run_id"] = filters["run_id"] |
| #650 | |
| #651 | result = self.graph.query(cypher, params=params) |
| #652 | return result |
| #653 | |
| #654 | def _search_destination_node(self, destination_embedding, filters, threshold=0.9): |
| #655 | # Build WHERE conditions |
| #656 | where_conditions = ["destination_candidate.embedding IS NOT NULL", "destination_candidate.user_id = $user_id"] |
| #657 | if filters.get("agent_id"): |
| #658 | where_conditions.append("destination_candidate.agent_id = $agent_id") |
| #659 | if filters.get("run_id"): |
| #660 | where_conditions.append("destination_candidate.run_id = $run_id") |
| #661 | where_clause = " AND ".join(where_conditions) |
| #662 | |
| #663 | cypher = f""" |
| #664 | MATCH (destination_candidate {self.node_label}) |
| #665 | WHERE {where_clause} |
| #666 | |
| #667 | WITH destination_candidate, |
| #668 | round(2 * vector.similarity.cosine(destination_candidate.embedding, $destination_embedding) - 1, 4) AS destination_similarity // denormalize for backward compatibility |
| #669 | |
| #670 | WHERE destination_similarity >= $threshold |
| #671 | |
| #672 | WITH destination_candidate, destination_similarity |
| #673 | ORDER BY destination_similarity DESC |
| #674 | LIMIT 1 |
| #675 | |
| #676 | RETURN elementId(destination_candidate) |
| #677 | """ |
| #678 | |
| #679 | params = { |
| #680 | "destination_embedding": destination_embedding, |
| #681 | "user_id": filters["user_id"], |
| #682 | "threshold": threshold, |
| #683 | } |
| #684 | if filters.get("agent_id"): |
| #685 | params["agent_id"] = filters["agent_id"] |
| #686 | if filters.get("run_id"): |
| #687 | params["run_id"] = filters["run_id"] |
| #688 | |
| #689 | result = self.graph.query(cypher, params=params) |
| #690 | return result |
| #691 | |
| #692 | # Reset is not defined in base.py |
| #693 | def reset(self): |
| #694 | """Reset the graph by clearing all nodes and relationships.""" |
| #695 | logger.warning("Clearing graph...") |
| #696 | cypher_query = """ |
| #697 | MATCH (n) DETACH DELETE n |
| #698 | """ |
| #699 | return self.graph.query(cypher_query) |
| #700 |