BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.
Python ai-agentsai-memoryknowledge-graphknowledge-managementknowledge-representationreccomendation-system
Fixed coderabbit suggestions
Christianaa8c358eb5ba
diff --git a/src/adapters/graph.py b/src/adapters/graph.pyindex 24f40d1..7e9c1ac 100644--- a/src/adapters/graph.py+++ b/src/adapters/graph.py@@ -10,6 +10,7 @@ Modified By: Christian Nonis <[email protected]>from typing import Dict, List, Literal, Optional, Tuplefrom src.adapters.interfaces.graph import GraphClient+from src.constants.embeddings import Vectorfrom src.constants.kg import (IdentificationParams,Node,@@ -19,6 +20,7 @@ from src.constants.kg import ()from src.adapters.interfaces.embeddings import VectorStoreClientfrom src.utils.normalization.list_reduction import reduce_list+from src.utils.similarity.vectors import cosine_similarityclass GraphAdapter:@@ -392,8 +394,6 @@ class GraphAdapter:List[Tuple[Node, List[Tuple[Predicate, Node, List[Tuple[Predicate, Node]]]]]]: The list of 2nd degree hops."""- # TODO: add a way to light weight the output-def flatten_node(n):return ({"uuid": n.uuid, "labels": n.labels, "name": n.name} if flattened else n@@ -409,9 +409,18 @@ class GraphAdapter:nodes = self.get_by_uuids(from_uuids, brain_id)nodes_by_uuid = {n.uuid: n for n in nodes}- vs = vector_store_adapter.get_by_ids(- [n.properties["v_id"] for n in nodes], brain_id=brain_id, store="nodes"- )+ v_ids = [+ n.properties["v_id"] for n in nodes if getattr(n.properties, "v_id", None)+ ]+ if len(v_ids) == 0:+ print("[ ! ] No v_ids found for nodes:", from_uuids)+ return []++ vs = vector_store_adapter.get_by_ids(v_ids, brain_id=brain_id, store="nodes")+ if len(vs) == 0:+ print("[ ! ] No vectors found for nodes:", from_uuids)+ return []+averaged_vector = [sum(v.embeddings[i] for v in vs) / len(vs)for i in range(len(vs[0].embeddings))@@ -419,7 +428,7 @@ class GraphAdapter:all_fd_nodes = self.get_neighbors(list(nodes_by_uuid.keys()), brain_id=brain_id)- all_fd_v_ids = [+ all_fd_v_ids: List[str] = [fd[1].properties["v_id"]for fds in all_fd_nodes.values()for fd in fds@@ -428,10 +437,10 @@ class GraphAdapter:all_fd_vs = vector_store_adapter.get_by_ids(all_fd_v_ids, brain_id=brain_id, store="nodes")- fd_vs_by_uuid = {v.metadata["uuid"]: v for v in all_fd_vs}+ fd_vs_by_uuid: Dict[str, Vector] = {v.metadata["uuid"]: v for v in all_fd_vs}all_filtered_fd_uuids = []- filtered_fd_by_origin = {}+ filtered_fd_by_origin: Dict[str, List[Tuple[Predicate, Node]]] = {}for node_uuid, fd_list in all_fd_nodes.items():fd_nodes_by_uuid = {fd[1].uuid: fd[1] for fd in fd_list}@@ -461,9 +470,22 @@ class GraphAdapter:"with_": from_node.description,},)- filtered_uuids = {v["metadata"]["uuid"] for v in filtered}+ filtered_uuids = {+ v["metadata"]["uuid"]+ for v in filtered+ if cosine_similarity(averaged_vector, v["embeddings"])+ >= similarity_threshold+ }else:- filtered_uuids = {fd[1].uuid for fd in fd_list}+ filtered_uuids = {+ fd[1].uuid+ for fd in fd_list+ if fd[1].uuid in fd_vs_by_uuid+ and cosine_similarity(+ averaged_vector, fd_vs_by_uuid[fd[1].uuid].embeddings+ )+ >= similarity_threshold+ }filtered_fd_by_origin[node_uuid] = [fd for fd in fd_list if fd[1].uuid in filtered_uuids]@@ -471,7 +493,7 @@ class GraphAdapter:all_sd_nodes = self.get_neighbors(all_filtered_fd_uuids, brain_id=brain_id)- all_sd_v_ids = [+ all_sd_v_ids: List[str] = [getattr(sd[1], "properties", {}).get("v_id")for sds in all_sd_nodes.values()for sd in sds@@ -523,9 +545,19 @@ class GraphAdapter:"with_": from_node.description,},)- reduced_uuids = {v["metadata"]["uuid"] for v in reduced}+ reduced_uuids = {+ v["metadata"]["uuid"]+ for v in reduced+ if cosine_similarity(averaged_vector, v["embeddings"])+ >= similarity_threshold+ }else:- reduced_uuids = {sd["metadata"]["uuid"] for sd in sd_vs_with_desc}+ reduced_uuids = {+ sd["metadata"]["uuid"]+ for sd in sd_vs_with_desc+ if cosine_similarity(averaged_vector, sd["embeddings"])+ >= similarity_threshold+ }second_degree = [(flatten_pred(sd[0]), flatten_node(sd[1]))@@ -534,9 +566,6 @@ class GraphAdapter:and sd[1].uuid not in exclude_setand sd[1].uuid != from_uuid]-- if similarity_threshold > 0.0:- if fd_pred["metadata"]["v_id"]node_hops.append((flatten_pred(fd_pred), flatten_node(fd_node), second_degree)diff --git a/src/core/agents/architect_agent.py b/src/core/agents/architect_agent.pyindex 4bbd366..b2fb9ff 100644--- a/src/core/agents/architect_agent.py+++ b/src/core/agents/architect_agent.py@@ -61,6 +61,7 @@ from src.utils.tokens import token_detail_from_token_countsHISTORY_MAX_MESSAGES = 25HISTORY_MAX_MESSAGES_DELETE = 8+MAX_RECURSION_LIMIT = 100class ArchitectAgent:@@ -313,7 +314,13 @@ class ArchitectAgent:response = future.result(timeout=timeout)for m in response.get("messages", []):if hasattr(m, "usage_metadata"):- self.get_token_summary(m.usage_metadata)+ self._update_token_counts(m.usage_metadata)+ self.token_detail = token_detail_from_token_counts(+ self.input_tokens,+ self.output_tokens,+ self.cached_tokens,+ self.reasoning_tokens,+ )return responseexcept FutureTimeoutError:raise TimeoutError(@@ -439,7 +446,10 @@ class ArchitectAgent:)messages_list.append(user_message)- return self.agent.invoke({"messages": messages_list})+ return self.agent.invoke(+ {"messages": messages_list},+ config={"recursion_limit": MAX_RECURSION_LIMIT},+ )@retry(stop=stop_after_attempt(max_retries),diff --git a/src/core/agents/janitor_agent.py b/src/core/agents/janitor_agent.pyindex ddd61f7..dc862ed 100644--- a/src/core/agents/janitor_agent.py+++ b/src/core/agents/janitor_agent.py@@ -197,6 +197,7 @@ class JanitorAgent:flattened=True,vector_store_adapter=self.vector_store,brain_id=brain_id,+ similarity_threshold=0.35,)self._get_agent(diff --git a/src/core/agents/kg_agent.py b/src/core/agents/kg_agent.pyindex daf20b1..62e54f4 100644--- a/src/core/agents/kg_agent.py+++ b/src/core/agents/kg_agent.py@@ -168,7 +168,12 @@ class KGAgent:Update the knowledge graph with new information."""- self._get_agent(identification_params, metadata, brain_id=brain_id)+ self._get_agent(+ type_="normal",+ identification_params=identification_params,+ metadata=metadata,+ brain_id=brain_id,+ )preferred_entities_prompt = f"""You must prioritize the extraction of the following entities: {preferred_entities},@@ -207,7 +212,12 @@ class KGAgent:Update the knowledge graph with new structured information."""- self._get_agent(identification_params, metadata={}, brain_id=brain_id)+ self._get_agent(+ type_="normal",+ identification_params=identification_params,+ metadata={},+ brain_id=brain_id,+ )response = self.agent.invoke({@@ -227,7 +237,11 @@ class KGAgent:return responsedef retrieve_neighbors(- self, node: Node, looking_for: Optional[Union[str, Iterable[str]]], limit: int+ self,+ node: Node,+ looking_for: Optional[Union[str, Iterable[str]]],+ limit: int,+ brain_id: str = "default",) -> RetrieveNeighborsOutputSchema:"""Retrieve the neighbors of a node.@@ -247,6 +261,7 @@ class KGAgent:"""self._get_agent(+ type_="normal",identification_params={},metadata={},tools=[@@ -254,10 +269,12 @@ class KGAgent:self,self.kg,self.database_desc,+ brain_id=brain_id,)],output_schema=RetrieveNeighborsOutputSchema,extra_system_prompt=extra_system_prompt_str,+ brain_id=brain_id,)if isinstance(looking_for, str):diff --git a/src/core/agents/tools/janitor_agent/JanitorAgentSearchEntitiesTool.py b/src/core/agents/tools/janitor_agent/JanitorAgentSearchEntitiesTool.pyindex 097544a..d940d4f 100644--- a/src/core/agents/tools/janitor_agent/JanitorAgentSearchEntitiesTool.py+++ b/src/core/agents/tools/janitor_agent/JanitorAgentSearchEntitiesTool.py@@ -69,13 +69,11 @@ class JanitorAgentSearchEntitiesTool(BaseTool):)def _run(self, *args, **kwargs) -> str:- _query = ""+ _queries = []if len(args) > 0:- _query = args[0]+ _queries = args[0] if isinstance(args[0], list) else [args[0]]else:_queries = kwargs.get("queries", [])-- if len(_queries) == 0:return "No queries provided in the arguments or kwargs"found_entities: Dict[str, Union[str, List[Node]]] = {}@@ -116,7 +114,7 @@ class JanitorAgentSearchEntitiesTool(BaseTool):except Exception as e:print(f"Vector search failed, using KG results only: {e}")- if len(found_entities) == 0:+ if len(founds) == 0:found_entities[_clean_query(_query)] = ("Knowledge graph does not contain any entities that match the query")diff --git a/src/core/search/entity_info.py b/src/core/search/entity_info.pyindex c7ae5dc..6bfc95d 100644--- a/src/core/search/entity_info.py+++ b/src/core/search/entity_info.py@@ -200,8 +200,13 @@ class EventSynergyRetriever:days_ago = 0if node.happened_at:- days_ago = (datetime.now() - node.happened_at).days-+ try:+ happened_at = node.happened_at+ if isinstance(happened_at, str):+ happened_at = datetime.fromisoformat(happened_at)+ days_ago = max(0, (datetime.now() - happened_at).days)+ except Exception:+ days_ago = 0recency = 1 / (1 + np.log1p(days_ago)) if days_ago > 0 else 1.0base_score = similarity * 0.6 + recency * 0.2diff --git a/src/core/search/entity_sibilings.py b/src/core/search/entity_sibilings.pyindex ad5c3aa..9592220 100644--- a/src/core/search/entity_sibilings.py+++ b/src/core/search/entity_sibilings.py@@ -33,10 +33,10 @@ class EntitySinergyRetriever:target_node_vs = vector_store_adapter.search_vectors(target_embedding.embeddings, store="nodes", brain_id=self.brain_id)-+ if not target_node_vs:+ return None, []target_node_id = target_node_vs[0].metadata.get("uuid")target_node = graph_adapter.get_by_uuid(target_node_id, brain_id=self.brain_id)-if not target_node:return None, []diff --git a/src/lib/llm/client_small.py b/src/lib/llm/client_small.pyindex 1a0e01d..5ca18aa 100644--- a/src/lib/llm/client_small.py+++ b/src/lib/llm/client_small.py@@ -96,7 +96,7 @@ class LLMClientSmall(LLM):return self._langchain_modeldef generate_text(- self, prompt: str, max_new_tokens: int = 100000, timeout: int = None+ self, prompt: str, max_new_tokens: int = 65536, timeout: int = None) -> str:if not prompt or len(prompt.strip()) == 0:return "Input prompt is empty"@@ -151,7 +151,7 @@ class LLMClientSmall(LLM):def generate_json(self,prompt: str,- max_new_tokens: int = 100000,+ max_new_tokens: int = 65536,max_retries: int = 3,timeout: int = None,) -> dict:diff --git a/src/lib/neo4j/client.py b/src/lib/neo4j/client.pyindex eb2b6b7..7ad9e61 100644--- a/src/lib/neo4j/client.py+++ b/src/lib/neo4j/client.py@@ -199,9 +199,10 @@ class Neo4jClient(GraphClient):for node in nodes:identification_dict = {"name": node.name}+ merged_metadata = {**(metadata or {}), **(node.metadata or {})}all_properties = {**(node.properties or {}),- "metadata": metadata,+ "metadata": merged_metadata or None,}if identification_params:@@ -228,7 +229,9 @@ class Neo4jClient(GraphClient):]for attr in attributes:- if getattr(node, attr, None):+ if attr == "metadata":+ continue+ if getattr(node, attr, None) is not None:all_properties[attr] = getattr(node, attr)property_assignments = []@@ -463,7 +466,10 @@ class Neo4jClient(GraphClient):"""cypher_query = """MATCH (n) WHERE n.uuid = $uuid- RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties+ RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description,+ properties(n) as properties,+ n.polarity as polarity, n.happened_at as happened_at, n.last_updated as last_updated,+ n.observations as observations, n.metadata as metadata"""self.ensure_database(brain_id)result = self.driver.execute_query(@@ -485,7 +491,10 @@ class Neo4jClient(GraphClient):"""cypher_query = f"""MATCH (n) WHERE n.uuid IN ["{'","'.join(uuids)}"]- RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties+ RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description,+ properties(n) as properties,+ n.polarity as polarity, n.happened_at as happened_at, n.last_updated as last_updated,+ n.observations as observations, n.metadata as metadata"""self.ensure_database(brain_id)result = self.driver.execute_query(cypher_query, database_=brain_id)@@ -497,10 +506,31 @@ class Neo4jClient(GraphClient):description=record.get("description", ""),properties=record.get("properties", {}),polarity=record.get("polarity", "neutral"),- happened_at=record.get("happened_at", None),- last_updated=record.get("last_updated", datetime.now(timezone.utc)),- observations=record.get("observations", []),- metadata=record.get("metadata", {}),+ **(+ {"happened_at": record.get("happened_at", None)}+ if record.get("happened_at", None) is not None+ else {}+ ),+ **(+ {+ "last_updated": record.get(+ "last_updated", datetime.now(timezone.utc)+ )+ }+ if record.get("last_updated", datetime.now(timezone.utc))+ is not None+ else {}+ ),+ **(+ {"observations": record.get("observations", [])}+ if record.get("observations", []) is not None+ else {}+ ),+ **(+ {"metadata": record.get("metadata", {})}+ if record.get("metadata", {}) is not None+ else {}+ ),)for record in result.records]@@ -542,7 +572,10 @@ class Neo4jClient(GraphClient):cypher_query = f"""MATCH (n{labels_str}) {where_str}- RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties+ RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description,+ properties(n) as properties,+ n.polarity as polarity, n.happened_at as happened_at, n.last_updated as last_updated,+ n.observations as observations, n.metadata as metadata"""self.ensure_database(brain_id)@@ -601,10 +634,13 @@ class Neo4jClient(GraphClient):)cypher_query += """- RETURN n.uuid AS uuid, n.name AS name, labels(n) AS labels, n.description AS description, properties(n) AS properties, r AS rel,- CASE WHEN startNode(r) = n THEN 'out' ELSE 'in' END AS direction,- type(r) AS rel_type, r.description AS rel_description, properties(r) AS rel_properties, r.flow_key as rel_flowkey, r.uuid as rel_uuid,- c.uuid AS c_uuid, c.name AS c_name, labels(c) AS c_labels, c.description AS c_description, properties(c) AS c_properties+ RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description,+ properties(n) as properties,+ n.polarity as polarity, n.happened_at as happened_at, n.last_updated as last_updated,+ n.observations as observations, n.metadata as metadata, r AS rel,+ CASE WHEN startNode(r) = n THEN 'out' ELSE 'in' END AS direction,+ type(r) AS rel_type, r.description AS rel_description, properties(r) AS rel_properties, r.flow_key as rel_flowkey, r.uuid as rel_uuid,+ c.uuid AS c_uuid, c.name AS c_name, labels(c) AS c_labels, c.description AS c_description, properties(c) AS c_properties"""if limit:cypher_query += f" LIMIT {limit}"@@ -973,7 +1009,10 @@ class Neo4jClient(GraphClient):cypher_query = f"""MATCH (n){"WHERE " + " AND ".join(filters) if filters else ""}- RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties+ RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description,+ properties(n) as properties,+ n.polarity as polarity, n.happened_at as happened_at, n.last_updated as last_updated,+ n.observations as observations, n.metadata as metadataSKIP {skip}LIMIT {limit}"""diff --git a/src/services/api/controllers/kg.py b/src/services/api/controllers/kg.pyindex 0dbe1fa..2dfda6f 100644--- a/src/services/api/controllers/kg.py+++ b/src/services/api/controllers/kg.py@@ -4,7 +4,7 @@ Created Date: Wednesday December 31st 2025Author: Christian Nonis <[email protected]>-----Last Modified: Wednesday December 31st 2025 7:45:55 pm-Modified By: the developer formerly known as Christian Nonis at <[email protected]>+Modified By: Christian Nonis <[email protected]>-----"""@@ -21,6 +21,8 @@ async def get_hops():"""Get the hops in the graph for a given query.++ Currently only supports 2nd degree hops."""text_embeddings = embeddings_adapter.embed_text(query)data_vectors = vector_store_adapter.search_vectors(@@ -37,5 +39,6 @@ async def get_hops(flattened=flattened,vector_store_adapter=vector_store_adapter,brain_id=brain_id,+ similarity_threshold=0.0,)return hopsdiff --git a/src/services/api/controllers/retrieve.py b/src/services/api/controllers/retrieve.pyindex 5d4faa5..e48d896 100644--- a/src/services/api/controllers/retrieve.py+++ b/src/services/api/controllers/retrieve.py@@ -147,7 +147,6 @@ async def retrieve_neighbors(and not v.id.replace("-", "").isalpha() # likely not a UUID if all numeric (may have hyphens for uuid standard)- and v.id.isdigit())}fd_v_neighbors_ids = list(fd_v_neighbors_embeddings_map.keys())@@ -244,7 +243,7 @@ async def retrieve_neighbors_ai_mode(if not node:raise HTTPException(status_code=404, detail="Entity not found")- result = kg_agent.retrieve_neighbors(node, looking_for, limit)+ result = kg_agent.retrieve_neighbors(node, looking_for, limit, brain_id)ids = [neighbor.uuid for neighbor in result.neighbors]descriptions = [neighbor.description for neighbor in result.neighbors]diff --git a/src/services/api/routes/system.py b/src/services/api/routes/system.pyindex 00b073f..71b9d3a 100644--- a/src/services/api/routes/system.py+++ b/src/services/api/routes/system.py@@ -4,7 +4,7 @@ Created Date: Monday December 1st 2025Author: Christian Nonis <[email protected]>-----Last Modified: Monday December 1st 2025 10:12:48 pm-Modified By: the developer formerly known as Christian Nonis at <[email protected]>+Modified By: Christian Nonis <[email protected]>-----"""@@ -32,3 +32,27 @@ async def create_brain(request: CreateBrainRequest):Create a new brain"""return await create_new_brain_controller(request)+++@system_router.get(path="/brains/{brain_id}/reset") # TODO+async def reset(brain_id: str):+ """+ Resets the brain+ """+ raise NotImplementedError("Not implemented")+++@system_router.get(path="/brains/{brain_id}/delete") # TODO+async def delete(brain_id: str):+ """+ Deletes the brain and all its data+ """+ raise NotImplementedError("Not implemented")+++@system_router.post(path="/brains/{brain_id}/create-backup") # TODO+async def create_backup(brain_id: str):+ """+ Creates a backup of the brain and returns a task ID+ """+ raise NotImplementedError("Not implemented")diff --git a/src/workers/app.py b/src/workers/app.pyindex d05f270..f7abc2a 100644--- a/src/workers/app.py+++ b/src/workers/app.py@@ -48,7 +48,7 @@ ingestion_app.conf.update(task_time_limit=86400,task_soft_time_limit=86400,broker_transport_options={- "visibility_timeout": 7200,+ "visibility_timeout": 86400,"fanout_prefix": True,"fanout_patterns": True,},