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
feat(brains)
Christianf9e712eeab74
diff --git a/src/adapters/cache.py b/src/adapters/cache.pyindex 1c347d9..8d6311c 100644--- a/src/adapters/cache.py+++ b/src/adapters/cache.py@@ -8,6 +8,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@-----"""+from typing import Optionalfrom .interfaces.cache import CacheClient@@ -25,20 +26,26 @@ class CacheAdapter:"""self.cache = client- def get(self, key: str) -> str:+ def get(self, key: str, brain_id: str = "default") -> str:"""Get a value from the cache."""- return self.cache.get(key)+ return self.cache.get(key, brain_id)- def set(self, key: str, value: str, expires_in: int) -> bool:+ def set(+ self,+ key: str,+ value: str,+ brain_id: str = "default",+ expires_in: Optional[int] = None,+ ) -> bool:"""Set a value in the cache with an expiration time."""- return self.cache.set(key, value, expires_in)+ return self.cache.set(key, value, brain_id, expires_in)- def delete(self, key: str) -> bool:+ def delete(self, key: str, brain_id: str = "default") -> bool:"""Delete a value from the cache."""- return self.cache.delete(key)+ return self.cache.delete(key, brain_id)diff --git a/src/adapters/data.py b/src/adapters/data.pyindex 979d2b5..e781da8 100644--- a/src/adapters/data.py+++ b/src/adapters/data.py@@ -10,7 +10,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@from typing import List, Tuplefrom src.adapters.interfaces.data import DataClient, SearchResult-from src.constants.data import Observation, StructuredData, TextChunk+from src.constants.data import Brain, Observation, StructuredData, TextChunkclass DataAdapter:@@ -27,34 +27,52 @@ class DataAdapter:"""self.data = client- def save_text_chunk(self, text_chunk: TextChunk) -> TextChunk:+ def save_text_chunk(+ self, text_chunk: TextChunk, brain_id: str = "default"+ ) -> TextChunk:"""Save a text chunk to the data client."""- return self.data.save_text_chunk(text_chunk)+ return self.data.save_text_chunk(text_chunk, brain_id)- def save_observations(self, observations: List[Observation]) -> Observation:+ def save_observations(+ self, observations: List[Observation], brain_id: str = "default"+ ) -> Observation:"""Save a list of observations to the data client."""- return self.data.save_observations(observations)+ return self.data.save_observations(observations, brain_id)- def search(self, text: str) -> SearchResult:+ def search(self, text: str, brain_id: str = "default") -> SearchResult:"""Search data by text and return a list of text chunks and observations."""- return self.data.search(text)+ return self.data.search(text, brain_id)def get_text_chunks_by_ids(- self, ids: List[str], with_observations: bool+ self, ids: List[str], with_observations: bool, brain_id: str = "default") -> Tuple[List[TextChunk], List[Observation]]:"""Get data by their IDs."""- return self.data.get_text_chunks_by_ids(ids, with_observations)+ return self.data.get_text_chunks_by_ids(ids, with_observations, brain_id)- def save_structured_data(self, structured_data: StructuredData) -> StructuredData:+ def save_structured_data(+ self, structured_data: StructuredData, brain_id: str = "default"+ ) -> StructuredData:"""Save a structured data to the data client."""- return self.data.save_structured_data(structured_data)+ return self.data.save_structured_data(structured_data, brain_id)++ def create_brain(self, name_key: str) -> Brain:+ """+ Create a new brain in the data client.+ """+ return self.data.create_brain(name_key)++ def get_brain(self, name_key: str) -> Brain:+ """+ Get a brain from the data client.+ """+ return self.data.get_brain(name_key)diff --git a/src/adapters/embeddings.py b/src/adapters/embeddings.pyindex 2106079..5726ecf 100644--- a/src/adapters/embeddings.py+++ b/src/adapters/embeddings.py@@ -41,32 +41,43 @@ class VectorStoreAdapter:"""self.vector_store = client- def add_vectors(self, vectors: list[Vector], store: str) -> None:+ def add_vectors(+ self, vectors: list[Vector], store: str, brain_id: str = "default"+ ) -> None:"""Add vectors to the vector store."""- return self.vector_store.add_vectors(vectors, store)+ return self.vector_store.add_vectors(vectors, store, brain_id)- def search_vectors(self, query: str, store: str, k: int = 10) -> list[Vector]:+ def search_vectors(+ self, query: str, store: str, brain_id: str = "default", k: int = 10+ ) -> list[Vector]:"""Search vectors in the vector store and return the top k vectors."""- return self.vector_store.search_vectors(query, store, k)+ return self.vector_store.search_vectors(query, store, brain_id, k)- def get_by_ids(self, ids: list[str], store: str) -> list[Vector]:+ def get_by_ids(+ self, ids: list[str], store: str, brain_id: str = "default"+ ) -> list[Vector]:"""Get vectors by their IDs."""- return self.vector_store.get_by_ids(ids, store)+ return self.vector_store.get_by_ids(ids, store, brain_id)def search_similar_by_ids(- self, vector_ids: list[str], store: str, min_similarity: float, limit: int = 10+ self,+ vector_ids: list[str],+ store: str,+ min_similarity: float,+ limit: int = 10,+ brain_id: str = "default",) -> list[Vector]:"""Search similar vectors by their IDs."""return self.vector_store.search_similar_by_ids(- vector_ids, store, min_similarity, limit+ vector_ids, store, min_similarity, limit, brain_id)diff --git a/src/adapters/graph.py b/src/adapters/graph.pyindex cdc1ad6..636e09d 100644--- a/src/adapters/graph.py+++ b/src/adapters/graph.py@@ -16,7 +16,6 @@ from src.constants.kg import (Predicate,SearchEntitiesResult,SearchRelationshipsResult,- Triple,)@@ -50,12 +49,12 @@ class GraphAdapter:"""self.graph = client- def execute_operation(self, operation: str) -> str:+ def execute_operation(self, operation: str, brain_id: str = "default") -> str:"""Execute a generic graph operation."""try:- return self.graph.execute_operation(operation)+ return self.graph.execute_operation(operation, brain_id)except Exception as e: # pylint: disable=broad-exception-caughtprint(f"Error executing graph operation: {e} - {operation}")return f"Error executing graph operation: {e}"@@ -63,44 +62,47 @@ class GraphAdapter:def add_nodes(self,nodes: list[Node],+ brain_id: str = "default",identification_params: Optional[dict] = None,metadata: Optional[dict] = None,- database: Optional[str] = None,) -> list[Node] | str:"""Add nodes to the graph."""- return self.graph.add_nodes(nodes, identification_params, metadata, database)+ return self.graph.add_nodes(nodes, brain_id, identification_params, metadata)def add_relationship(self,subject: Node,predicate: Predicate,to_object: Node,+ brain_id: str = "default",) -> str:"""Add a relationship between two nodes to the graph."""- return self.graph.add_relationship(subject, predicate, to_object)+ return self.graph.add_relationship(subject, predicate, to_object, brain_id)def search_graph(self,nodes: list[Node],+ brain_id: str = "default",) -> list[Node]:"""Search the graph for nodes and 1 degree relationships."""- return self.graph.search_graph(nodes)+ return self.graph.search_graph(nodes, brain_id)- def node_text_search(self, text: str) -> list[Node]:+ def node_text_search(self, text: str, brain_id: str = "default") -> list[Node]:"""Search the graph for nodes by partial text match into the name of the nodes."""- return self.graph.node_text_search(text)+ return self.graph.node_text_search(text, brain_id)def get_nodes_by_uuid(self,uuids: list[str],+ brain_id: str = "default",with_relationships: Optional[bool] = False,relationships_depth: Optional[int] = 1,relationships_type: Optional[list[str]] = None,@@ -111,80 +113,83 @@ class GraphAdapter:"""return self.graph.get_nodes_by_uuid(uuids,+ brain_id,with_relationships,relationships_depth,relationships_type,preferred_labels,)- def get_graph_entities(self) -> list[str]:+ def get_graph_entities(self, brain_id: str = "default") -> list[str]:"""Get the entities of the graph."""- return self.graph.get_graph_entities()+ return self.graph.get_graph_entities(brain_id)- def get_graph_relationships(self) -> list[str]:+ def get_graph_relationships(self, brain_id: str = "default") -> list[str]:"""Get the relationships of the graph."""- return self.graph.get_graph_relationships()+ return self.graph.get_graph_relationships(brain_id)- def get_graph_property_keys(self) -> list[str]:+ def get_graph_property_keys(self, brain_id: str = "default") -> list[str]:"""Get the property keys of the graph."""- return self.graph.get_graph_property_keys()+ return self.graph.get_graph_property_keys(brain_id)- def get_by_uuid(self, uuid: str) -> Node:+ def get_by_uuid(self, uuid: str, brain_id: str = "default") -> Node:"""Get a node by its UUID."""- return self.graph.get_by_uuid(uuid)+ return self.graph.get_by_uuid(uuid, brain_id)- def get_by_uuids(self, uuids: list[str]) -> list[Node]:+ def get_by_uuids(self, uuids: list[str], brain_id: str = "default") -> list[Node]:"""Get nodes by their UUIDs."""- return self.graph.get_by_uuids(uuids)+ return self.graph.get_by_uuids(uuids, brain_id)def get_by_identification_params(self,identification_params: IdentificationParams,+ brain_id: str = "default",entity_types: Optional[list[str]] = None,) -> Node:"""Get a node by its identification params and entity types."""return self.graph.get_by_identification_params(- identification_params, entity_types+ identification_params, brain_id, entity_types)def get_neighbors(- self, node: Node, limit: int+ self, node: Node, limit: int, brain_id: str = "default") -> list[Tuple[Node, Predicate, Node]]:"""Get the neighbors of a node."""- return self.graph.get_neighbors(node, limit)+ return self.graph.get_neighbors(node, limit, brain_id)def get_node_with_rel_by_uuid(- self, rel_ids_with_node_ids: list[tuple[str, str]]+ self, rel_ids_with_node_ids: list[tuple[str, str]], brain_id: str = "default") -> list[dict]:"""Get the node with the relationships by their UUIDs."""- return self.graph.get_node_with_rel_by_uuid(rel_ids_with_node_ids)+ return self.graph.get_node_with_rel_by_uuid(rel_ids_with_node_ids, brain_id)def get_neighbor_node_tuples(- self, a_uuid: str, b_uuids: list[str]+ self, a_uuid: str, b_uuids: list[str], brain_id: str = "default") -> list[Tuple[Node, Predicate, Node]]:"""Get the neighbor node tuples by their UUIDs."""- return self.graph.get_neighbor_node_tuples(a_uuid, b_uuids)+ return self.graph.get_neighbor_node_tuples(a_uuid, b_uuids, brain_id)def get_connected_nodes(self,+ brain_id: str = "default",node: Optional[Node] = None,uuids: Optional[list[str]] = None,limit: Optional[int] = 10,@@ -194,11 +199,12 @@ class GraphAdapter:Get the connected nodes by their UUIDs."""return self.graph.get_connected_nodes(- node=node, uuids=uuids, limit=limit, with_labels=with_labels+ brain_id, node=node, uuids=uuids, limit=limit, with_labels=with_labels)def search_relationships(self,+ brain_id: str = "default",limit: int = 10,skip: int = 0,relationship_types: Optional[list[str]] = None,@@ -213,6 +219,7 @@ class GraphAdapter:relationship_uuids = []# TODO: semantic search + src/core/agents/tools/kg_agent/KGAgentAddTripletsTool.py:165return self.graph.search_relationships(+ brain_id,limit,skip,relationship_types,@@ -225,6 +232,7 @@ class GraphAdapter:def search_entities(self,+ brain_id: str = "default",limit: int = 10,skip: int = 0,node_labels: Optional[list[str]] = None,@@ -236,7 +244,7 @@ class GraphAdapter:node_uuids = []# TODO: semantic searchreturn self.graph.search_entities(- limit, skip, node_labels, node_uuids, query_text+ brain_id, limit, skip, node_labels, node_uuids, query_text)diff --git a/src/adapters/interfaces/cache.py b/src/adapters/interfaces/cache.pyindex 0329db6..3141b3b 100644--- a/src/adapters/interfaces/cache.py+++ b/src/adapters/interfaces/cache.py@@ -9,6 +9,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@"""from abc import ABC, abstractmethod+from typing import Optionalclass CacheClient(ABC):@@ -17,14 +18,16 @@ class CacheClient(ABC):"""@abstractmethod- def get(self, key: str) -> str:+ def get(self, key: str, brain_id: str) -> str:"""Get a value from the cache."""raise NotImplementedError("get method not implemented")@abstractmethod- def set(self, key: str, value: str, expires_in: int) -> bool:+ def set(+ self, key: str, value: str, brain_id: str, expires_in: Optional[int] = None+ ) -> bool:"""Set a value in the cache with an expiration time.@@ -39,7 +42,7 @@ class CacheClient(ABC):raise NotImplementedError("set method not implemented")@abstractmethod- def delete(self, key: str) -> bool:+ def delete(self, key: str, brain_id: str) -> bool:"""Delete a value from the cache."""diff --git a/src/adapters/interfaces/data.py b/src/adapters/interfaces/data.pyindex 83b6481..4102a42 100644--- a/src/adapters/interfaces/data.py+++ b/src/adapters/interfaces/data.py@@ -13,7 +13,7 @@ from typing import List, Tuplefrom pydantic import BaseModel-from src.constants.data import Observation, StructuredData, TextChunk+from src.constants.data import Brain, Observation, StructuredData, TextChunkclass SearchResult(BaseModel):@@ -31,21 +31,23 @@ class DataClient(ABC):"""@abstractmethod- def save_text_chunk(self, text_chunk: TextChunk) -> TextChunk:+ def save_text_chunk(self, text_chunk: TextChunk, brain_id: str) -> TextChunk:"""Save data to the data client."""raise NotImplementedError("save method not implemented")@abstractmethod- def save_observations(self, observations: List[Observation]) -> Observation:+ def save_observations(+ self, observations: List[Observation], brain_id: str+ ) -> Observation:"""Save a list of observations to the data client."""raise NotImplementedError("save_observations method not implemented")@abstractmethod- def search(self, text: str) -> SearchResult:+ def search(self, text: str, brain_id: str) -> SearchResult:"""Search data by text and return a list of text chunks and observations."""@@ -53,7 +55,7 @@ class DataClient(ABC):@abstractmethoddef get_text_chunks_by_ids(- self, ids: List[str], with_observations: bool+ self, ids: List[str], with_observations: bool, brain_id: str) -> Tuple[List[TextChunk], List[Observation]]:"""Get data by their IDs.@@ -61,8 +63,24 @@ class DataClient(ABC):raise NotImplementedError("get_by_ids method not implemented")@abstractmethod- def save_structured_data(self, structured_data: StructuredData) -> StructuredData:+ def save_structured_data(+ self, structured_data: StructuredData, brain_id: str+ ) -> StructuredData:"""Save a structured data to the data client."""raise NotImplementedError("save_structured_data method not implemented")++ @abstractmethod+ def create_brain(self, name_key: str) -> Brain:+ """+ Create a new brain in the data client.+ """+ raise NotImplementedError("create_brain method not implemented")++ @abstractmethod+ def get_brain(self, name_key: str) -> Brain:+ """+ Get a brain from the data client.+ """+ raise NotImplementedError("get_brain method not implemented")diff --git a/src/adapters/interfaces/embeddings.py b/src/adapters/interfaces/embeddings.pyindex 9d10895..9a37200 100644--- a/src/adapters/interfaces/embeddings.py+++ b/src/adapters/interfaces/embeddings.py@@ -32,21 +32,23 @@ class VectorStoreClient(ABC):"""@abstractmethod- def add_vectors(self, vectors: list[Vector], store: str) -> None:+ def add_vectors(self, vectors: list[Vector], store: str, brain_id: str) -> None:"""Add vectors to the vector store."""raise NotImplementedError("add_vectors method not implemented")@abstractmethod- def search_vectors(self, query: str, store: str, k: int = 10) -> list[Vector]:+ def search_vectors(+ self, query: str, brain_id: str, store: str, k: int = 10+ ) -> list[Vector]:"""Search vectors in the vector store and return the top k vectors."""raise NotImplementedError("search_vectors method not implemented")@abstractmethod- def get_by_ids(self, ids: list[str], store: str) -> list[Vector]:+ def get_by_ids(self, ids: list[str], store: str, brain_id: str) -> list[Vector]:"""Get vectors by their IDs."""@@ -54,7 +56,12 @@ class VectorStoreClient(ABC):@abstractmethoddef search_similar_by_ids(- self, vector_ids: list[str], store: str, min_similarity: float, limit: int = 10+ self,+ vector_ids: list[str],+ brain_id: str,+ store: str,+ min_similarity: float,+ limit: int = 10,) -> list[Vector]:"""Search similar vectors by their IDs.diff --git a/src/adapters/interfaces/graph.py b/src/adapters/interfaces/graph.pyindex 3abf31a..16527ee 100644--- a/src/adapters/interfaces/graph.py+++ b/src/adapters/interfaces/graph.py@@ -42,7 +42,7 @@ class GraphClient(ABC):raise NotImplementedError("graphdb_type method not implemented")@abstractmethod- def execute_operation(self, operation: str) -> str:+ def execute_operation(self, operation: str, brain_id: str) -> str:"""Execute a generic graph operation."""@@ -52,9 +52,9 @@ class GraphClient(ABC):def add_nodes(self,nodes: list[Node],+ brain_id: str,identification_params: Optional[dict] = None,metadata: Optional[dict] = None,- database: Optional[str] = None,) -> list[Node] | str:"""Add nodes to the graph.@@ -67,6 +67,7 @@ class GraphClient(ABC):subject: Node,predicate: Predicate,to_object: Node,+ brain_id: str,) -> str:"""Add a relationship between two nodes to the graph.@@ -77,6 +78,7 @@ class GraphClient(ABC):def search_graph(self,nodes: list[Node],+ brain_id: str,) -> list[Node]:"""Search the graph for nodes and 1 degree relationships.@@ -84,7 +86,7 @@ class GraphClient(ABC):raise NotImplementedError("search_graph method not implemented")@abstractmethod- def node_text_search(self, text: str) -> list[Node]:+ def node_text_search(self, text: str, brain_id: str) -> list[Node]:"""Search the graph for nodes by partial text match into the name of the nodes."""@@ -94,6 +96,7 @@ class GraphClient(ABC):def get_nodes_by_uuid(self,uuids: list[str],+ brain_id: str,with_relationships: Optional[bool] = False,relationships_depth: Optional[int] = 1,relationships_type: Optional[list[str]] = None,@@ -105,21 +108,21 @@ class GraphClient(ABC):raise NotImplementedError("get_nodes_by_uuid method not implemented")@abstractmethod- def get_graph_entities(self) -> list[str]:+ def get_graph_entities(self, brain_id: str) -> list[str]:"""Get the entities of the graph."""raise NotImplementedError("get_graph_entities method not implemented")@abstractmethod- def get_graph_relationships(self) -> list[str]:+ def get_graph_relationships(self, brain_id: str) -> list[str]:"""Get the relationships of the graph."""raise NotImplementedError("get_graph_relationships method not implemented")@abstractmethod- def get_graph_property_keys(self) -> list[str]:+ def get_graph_property_keys(self, brain_id: str) -> list[str]:"""Get the property keys of the graph."""@@ -129,6 +132,7 @@ class GraphClient(ABC):def get_by_uuid(self,uuid: str,+ brain_id: str,) -> Node:"""Get a node by its UUID.@@ -139,6 +143,7 @@ class GraphClient(ABC):def get_by_uuids(self,uuids: list[str],+ brain_id: str,) -> list[Node]:"""Get nodes by their UUIDs.@@ -149,6 +154,7 @@ class GraphClient(ABC):def get_by_identification_params(self,identification_params: IdentificationParams,+ brain_id: str,entity_types: Optional[list[str]] = None,) -> Node:"""@@ -161,6 +167,7 @@ class GraphClient(ABC):self,node: Node,limit: int,+ brain_id: str,) -> list[Tuple[Node, Predicate, Node]]:"""Get the neighbors of a node.@@ -171,6 +178,7 @@ class GraphClient(ABC):def get_node_with_rel_by_uuid(self,rel_ids_with_node_ids: list[tuple[str, str]],+ brain_id: str,) -> list[dict]:"""Get the node with the relationships by their UUIDs.@@ -182,6 +190,7 @@ class GraphClient(ABC):self,a_uuid: str,b_uuids: list[str],+ brain_id: str,) -> list[Tuple[Node, Predicate, Node]]:"""Get the neighbor node tuples by their UUIDs.@@ -191,6 +200,7 @@ class GraphClient(ABC):@abstractmethoddef get_connected_nodes(self,+ brain_id: str,node: Optional[Node] = None,uuids: Optional[list[str]] = None,limit: Optional[int] = 10,@@ -204,6 +214,7 @@ class GraphClient(ABC):@abstractmethoddef search_relationships(self,+ brain_id: str,limit: int = 10,skip: int = 0,relationship_types: Optional[list[str]] = None,@@ -223,6 +234,7 @@ class GraphClient(ABC):@abstractmethoddef search_entities(self,+ brain_id: str,limit: int = 10,skip: int = 0,node_labels: Optional[list[str]] = None,diff --git a/src/constants/data.py b/src/constants/data.pyindex 57e622b..e28a496 100644--- a/src/constants/data.py+++ b/src/constants/data.py@@ -81,3 +81,12 @@ class KGChanges(BaseModel):default_factory=datetime.now,description="The date and time the changes were made.",)+++class Brain(BaseModel):+ """+ Model for a single brain, stored into data db+ """++ id: str = Field(default_factory=lambda: str(uuid.uuid4()))+ name_key: str = Field(description="The key used to identify the brain.")diff --git a/src/constants/tasks/ingestion.py b/src/constants/tasks/ingestion.pyindex 7d88b48..506aa13 100644--- a/src/constants/tasks/ingestion.py+++ b/src/constants/tasks/ingestion.py@@ -75,3 +75,6 @@ class IngestionTaskArgs(BaseModel):"If not provided, the extraction will be done on all the entities in the data."),)+ brain_id: str = Field(+ default="default", description="The brain identifier to store the data in."+ )diff --git a/src/core/search/entities.py b/src/core/search/entities.pyindex b026aa2..a4fcff0 100644--- a/src/core/search/entities.py+++ b/src/core/search/entities.py@@ -18,9 +18,12 @@ def search_entities(skip: int = 0,node_labels: Optional[list[str]] = None,query_text: Optional[str] = None,+ brain_id: str = "default",) -> SearchEntitiesResult:"""Search the entities of the graph."""- result = graph_adapter.search_entities(limit, skip, node_labels, query_text)+ result = graph_adapter.search_entities(+ brain_id, limit, skip, node_labels, query_text+ )return resultdiff --git a/src/core/search/relationships.py b/src/core/search/relationships.pyindex 47ce194..eb9b2cd 100644--- a/src/core/search/relationships.py+++ b/src/core/search/relationships.py@@ -21,11 +21,13 @@ def search_relationships(to_node_labels: Optional[list[str]] = None,query_text: Optional[str] = None,query_search_target: Optional[str] = "all",+ brain_id: str = "default",) -> SearchRelationshipsResult:"""Search the relationships of the graph."""result = graph_adapter.search_relationships(+ brain_id,limit,skip,relationship_types,diff --git a/src/lib/milvus/client.py b/src/lib/milvus/client.pyindex 2d20c3a..f14b17e 100644--- a/src/lib/milvus/client.py+++ b/src/lib/milvus/client.py@@ -69,7 +69,7 @@ class MilvusClient(VectorStoreClient):raise ValueError("Invalid Milvus configuration")return self._client- def _ensure_store(self, store: str) -> None:+ def _ensure_store(self, store: str, brain_id: str) -> None:"""Ensure the store exists and is loaded."""@@ -84,7 +84,7 @@ class MilvusClient(VectorStoreClient):)try:- has_partition = self.client.has_partition(store, "_default")+ has_partition = self.client.has_partition(store, brain_id)if not has_partition:self.client.load_collection(store)except Exception:@@ -93,11 +93,11 @@ class MilvusClient(VectorStoreClient):except Exception:pass- def add_vectors(self, vectors: list[Vector], store: str) -> None:+ def add_vectors(self, vectors: list[Vector], store: str, brain_id: str) -> None:"""Add vectors to the vector store."""- self._ensure_store(store)+ self._ensure_store(store, brain_id)self.client.insert(store,[@@ -112,12 +112,12 @@ class MilvusClient(VectorStoreClient):)def search_vectors(- self, data_vector: list[float], store: str, k: int = 10+ self, data_vector: list[float], store: str, brain_id: str, k: int = 10) -> list[Vector]:"""Search vectors in the vector store and return the top k vectors."""- self._ensure_store(store)+ self._ensure_store(store, brain_id)try:self.client.load_collection(store)@@ -143,11 +143,11 @@ class MilvusClient(VectorStoreClient):)return results- def get_by_ids(self, ids: list[str], store: str) -> list[Vector]:+ def get_by_ids(self, ids: list[str], store: str, brain_id: str) -> list[Vector]:"""Get vectors by their IDs."""- self._ensure_store(store)+ self._ensure_store(store, brain_id)collection = self.client.query(collection_name=store,ids=[hash(id) % (2**63) for id in ids],@@ -160,11 +160,12 @@ class MilvusClient(VectorStoreClient):def search_similar_by_ids(self,vector_ids: list[str],+ brain_id: str,store: str,min_similarity: float,limit: int = 10,) -> dict[str, list[Vector]]:- self._ensure_store(store)+ self._ensure_store(store, brain_id)try:self.client.load_collection(store)except Exception:diff --git a/src/lib/mongo/client.py b/src/lib/mongo/client.pyindex 058bd0e..03cb9c5 100644--- a/src/lib/mongo/client.py+++ b/src/lib/mongo/client.py@@ -14,7 +14,7 @@ from pymongo.database import Databasefrom pymongo.collection import Collectionfrom src.adapters.interfaces.data import DataClient, SearchResultfrom src.config import config-from src.constants.data import Observation, StructuredData, TextChunk+from src.constants.data import Brain, Observation, StructuredData, TextChunkclass MongoClient(DataClient):@@ -37,25 +37,29 @@ class MongoClient(DataClient):authSource="admin",)- def get_database(self, database: str = "data") -> Database:+ def get_database(self, database: str = "default") -> Database:return self.client[database]- def get_collection(self, collection: str, database: str = "data") -> Collection:+ def get_collection(self, collection: str, database: str = "default") -> Collection:return self.get_database(database)[collection]- def save_text_chunk(self, text_chunk: TextChunk) -> TextChunk:- collection = self.get_collection("text_chunks")+ def save_text_chunk(self, text_chunk: TextChunk, brain_id: str) -> TextChunk:+ collection = self.get_collection("text_chunks", database=brain_id)collection.insert_one(text_chunk.model_dump(mode="json"))return text_chunk- def save_observations(self, observations: List[Observation]) -> Observation:- collection = self.get_collection("observations")+ def save_observations(+ self, observations: List[Observation], brain_id: str+ ) -> Observation:+ collection = self.get_collection("observations", database=brain_id)collection.insert_many([o.model_dump(mode="json") for o in observations])return observations- def search(self, text: str, collection: str = "*", limit: int = 10) -> SearchResult:- chunks_collection = self.get_collection(collection)- observations_collection = self.get_collection("observations")+ def search(+ self, text: str, brain_id: str, collection: str = "*", limit: int = 10+ ) -> SearchResult:+ chunks_collection = self.get_collection(collection, database=brain_id)+ observations_collection = self.get_collection("observations", database=brain_id)text_chunks_results = chunks_collection.find({"text": {"$regex": text, "$options": "i"}}@@ -84,13 +88,15 @@ class MongoClient(DataClient):return SearchResult(text_chunks=text_chunks, observations=observations)def get_text_chunks_by_ids(- self, ids: List[str], with_observations: bool = False+ self, ids: List[str], brain_id: str, with_observations: bool = False) -> List[TextChunk]:- chunks_collection = self.get_collection("text_chunks")+ chunks_collection = self.get_collection("text_chunks", database=brain_id)text_chunks = chunks_collection.find({"id": {"$in": ids}})if with_observations:- observations_collection = self.get_collection("observations")+ observations_collection = self.get_collection(+ "observations", database=brain_id+ )observations = observations_collection.find({"resource_id": {"$in": ids}})return (@@ -119,10 +125,24 @@ class MongoClient(DataClient):),)- def save_structured_data(self, structured_data: StructuredData) -> StructuredData:- collection = self.get_collection("structured_data")+ def save_structured_data(+ self, structured_data: StructuredData, brain_id: str+ ) -> StructuredData:+ collection = self.get_collection("structured_data", database=brain_id)collection.insert_one(structured_data.model_dump(mode="json"))return structured_data+ def create_brain(self, name_key: str) -> Brain:+ collection = self.get_collection("brains", "system")+ result = collection.insert_one({"name_key": name_key})+ return Brain(id=str(result.inserted_id), name_key=name_key)++ def get_brain(self, name_key: str) -> Brain:+ collection = self.get_collection("brains", "system")+ result = collection.find_one({"name_key": name_key})+ if not result:+ return None+ return Brain(id=str(result["_id"]), name_key=result["name_key"])+_mongo_client = MongoClient()diff --git a/src/lib/neo4j/client.py b/src/lib/neo4j/client.pyindex e556c81..efbb104 100644--- a/src/lib/neo4j/client.py+++ b/src/lib/neo4j/client.py@@ -29,18 +29,17 @@ class Neo4jClient(GraphClient):Neo4j client with support for multiple databases."""- def __init__(self, database: Optional[str] = None):+ def __init__(self):self.driver = GraphDatabase.driver(f"bolt://{config.neo4j.host}:{config.neo4j.port}",auth=(config.neo4j.username, config.neo4j.password),)- self.database = database or "neo4j"- def execute_operation(self, operation: str, database: Optional[str] = None) -> str:+ def execute_operation(self, operation: str, brain_id: str) -> str:"""Execute a Neo4j operation with database override."""- db = database or self.database+ db = brain_idreturn self.driver.execute_query(operation, database_=db)def ensure_database(self, database: str) -> None:@@ -49,13 +48,15 @@ class Neo4jClient(GraphClient):"""try:cypher_query = f"CREATE DATABASE {database}"- self.execute_operation(cypher_query, database="neo4j")+ self.driver.execute_query(cypher_query, database_="system")except Exception as e:error_msg = str(e).lower()if ("already exists" in error_msgor "not supported in community edition" in error_msgor "unsupported administration command" in error_msg+ or "database not found" in error_msg+ or "graph not found" in error_msg):passelse:@@ -122,9 +123,9 @@ class Neo4jClient(GraphClient):def add_nodes(self,nodes: list[Node],+ brain_id: str,identification_params: Optional[dict] = None,metadata: Optional[dict] = None,- database: Optional[str] = None,) -> list[Node] | str:"""Add nodes to the graph.@@ -191,9 +192,9 @@ class Neo4jClient(GraphClient):SET {properties_set}RETURN n"""- self.ensure_database(database)+ self.ensure_database(brain_id)try:- result = self.driver.execute_query(cypher_query, database_=database)+ result = self.driver.execute_query(cypher_query, database_=brain_id)results.append(result)log("Nodes added: ", results)except Exception as e:@@ -216,6 +217,7 @@ class Neo4jClient(GraphClient):subject: Node,predicate: Predicate,to_object: Node,+ brain_id: str,) -> str:"""Add a relationship between two nodes to the graph.@@ -228,10 +230,10 @@ class Neo4jClient(GraphClient):CREATE (a)-[:{":".join(self._clean_labels([predicate.name]))} {{ Description: '{safe_desc}' }}]->(b)RETURN a, b"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return result- def search_graph(self, nodes: list[Node]) -> list[Node]:+ def search_graph(self, nodes: list[Node], brain_id: str) -> list[Node]:"""Search the graph for nodes and 1 degree relationships."""@@ -246,10 +248,10 @@ class Neo4jClient(GraphClient):queries.append(query)cypher_query = " UNION ".join(queries)- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return result- def node_text_search(self, text: str) -> list[Node]:+ def node_text_search(self, text: str, brain_id: str) -> list[Node]:"""Search the graph for nodes by partial text match into the name of the nodes."""@@ -258,14 +260,14 @@ class Neo4jClient(GraphClient):WHERE toLower(n.Name) CONTAINS toLower('{text}')RETURN n"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return [Node(- uuid=node["uuid"],- name=node["name"],- labels=node["labels"],- description=node["description"],- properties=node["properties"],+ uuid=node.get("uuid", "") or "",+ name=node.get("name", "") or "",+ labels=node.get("labels", []) or [],+ description=node.get("description", "") or "",+ properties=node.get("properties", {}) or {},)for node in result.records]@@ -273,6 +275,7 @@ class Neo4jClient(GraphClient):def get_nodes_by_uuid(self,uuids: list[str],+ brain_id: str,with_relationships: Optional[bool] = False,relationships_depth: Optional[int] = 1,relationships_type: Optional[list[str]] = None,@@ -312,7 +315,7 @@ class Neo4jClient(GraphClient):if with_relationships:cypher_query += ", r, m.uuid as m_uuid, m.name as m_name, labels(m) as m_labels, m.description as m_description, properties(m) as m_properties"- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)if with_relationships:return [@@ -351,7 +354,7 @@ class Neo4jClient(GraphClient):for record in result.records]- def get_graph_entities(self) -> list[str]:+ def get_graph_entities(self, brain_id: str) -> list[str]:"""Get the entities of the graph."""@@ -359,10 +362,10 @@ class Neo4jClient(GraphClient):MATCH (n)RETURN DISTINCT labels(n) as labels"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return [label for record in result.records for label in record["labels"]]- def get_graph_relationships(self) -> list[str]:+ def get_graph_relationships(self, brain_id: str) -> list[str]:"""Get the relationships of the graph."""@@ -370,10 +373,10 @@ class Neo4jClient(GraphClient):CALL db.relationshipTypes() YIELD relationshipTypeRETURN relationshipType"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return [record["relationshipType"] for record in result.records]- def get_by_uuid(self, uuid: str) -> Node:+ def get_by_uuid(self, uuid: str, brain_id: str) -> Node:"""Get a node by its UUID."""@@ -381,7 +384,7 @@ class Neo4jClient(GraphClient):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"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)if not result.records or len(result.records) == 0:return Nonereturn Node(@@ -392,7 +395,7 @@ class Neo4jClient(GraphClient):properties=result.records[0].get("properties", {}) or {},)- def get_by_uuids(self, uuids: list[str]) -> list[Node]:+ def get_by_uuids(self, uuids: list[str], brain_id: str) -> list[Node]:"""Get nodes by their UUIDs."""@@ -400,14 +403,14 @@ class Neo4jClient(GraphClient):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"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return [Node(- uuid=record.get("uuid", ""),- name=record.get("name", "") or "",- labels=record.get("labels", []) or [],- description=record.get("description", "") or "",- properties=record.get("properties", {}) or {},+ uuid=record["uuid"],+ name=record["name"],+ labels=record["labels"],+ description=record["description"],+ properties=record["properties"],)for record in result.records]@@ -415,6 +418,7 @@ class Neo4jClient(GraphClient):def get_by_identification_params(self,identification_params: IdentificationParams,+ brain_id: str,entity_types: Optional[list[str]] = None,) -> Node:"""@@ -434,7 +438,7 @@ class Neo4jClient(GraphClient):MATCH (n{(":" + ":".join(self._clean_labels(entity_types))) if entity_types else ""}) {("WHERE " + " AND ".join(where_clauses)) if where_clauses else ""}RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)if not result.records or len(result.records) == 0:return Nonereturn Node(@@ -445,7 +449,7 @@ class Neo4jClient(GraphClient):properties=result.records[0].get("properties", {}) or {},)- def get_graph_property_keys(self) -> list[str]:+ def get_graph_property_keys(self, brain_id: str) -> list[str]:"""Get the property keys of the graph."""@@ -453,11 +457,11 @@ class Neo4jClient(GraphClient):CALL db.propertyKeys() YIELD propertyKeyRETURN propertyKey"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return [record["propertyKey"] for record in result.records]def get_neighbors(- self, node: Node, limit: int+ self, node: Node, limit: int, brain_id: str) -> list[Tuple[Node, Predicate, Node]]:"""Get the neighbors of a node.@@ -470,7 +474,7 @@ class Neo4jClient(GraphClient):CASE WHEN startNode(r2) = c THEN 'out' ELSE 'in' END AS direction,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"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)neighbors = []for record in result.records:@@ -512,14 +516,14 @@ class Neo4jClient(GraphClient):return [(n, p, c) for n, p, c in neighbors]def get_node_with_rel_by_uuid(- self, rel_ids_with_node_ids: list[tuple[str, str]]+ self, rel_ids_with_node_ids: list[tuple[str, str]], brain_id: str) -> list[dict]:"""Get the node with the relationships by their UUIDs."""def get_neighbor_node_tuples(- self, a_uuid: str, b_uuids: list[str]+ self, a_uuid: str, b_uuids: list[str], brain_id: str) -> list[Tuple[Node, Predicate, Node]]:"""Get the neighbor node tuples by their UUIDs.@@ -534,7 +538,7 @@ class Neo4jClient(GraphClient):m.uuid as m_uuid, m.name as m_name, labels(m) as m_labels,m.description as m_description, properties(m) as m_properties, r as rel"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)if not result.records:if os.getenv("DEBUG") == "true":@@ -599,6 +603,7 @@ class Neo4jClient(GraphClient):def get_connected_nodes(self,+ brain_id: str,node: Optional[Node] = None,uuids: Optional[list[str]] = None,limit: Optional[int] = 10,@@ -627,7 +632,7 @@ class Neo4jClient(GraphClient):n.uuid as n_uuid, n.name as n_name, labels(n) as n_labels, n.description as n_description, properties(n) as n_properties,CASE WHEN startNode(r) = n THEN 'out' ELSE 'in' END AS direction"""- result = self.driver.execute_query(cypher_query)+ result = self.driver.execute_query(cypher_query, database_=brain_id)return [(Node(@@ -655,6 +660,7 @@ class Neo4jClient(GraphClient):def search_relationships(self,+ brain_id: str,limit: int = 10,skip: int = 0,relationship_types: Optional[list[str]] = None,@@ -721,8 +727,8 @@ class Neo4jClient(GraphClient):{"WHERE " + " AND ".join(filters) if filters else ""}RETURN count(r) AS total"""- result = self.driver.execute_query(cypher_query)- count_result = self.driver.execute_query(cypher_count)+ result = self.driver.execute_query(cypher_query, database_=brain_id)+ count_result = self.driver.execute_query(cypher_count, database_=brain_id)total = 0if count_result and count_result.records:total = count_result.records[0].get("total") or 0@@ -813,6 +819,7 @@ class Neo4jClient(GraphClient):def search_entities(self,+ brain_id: str,limit: int = 10,skip: int = 0,node_labels: Optional[list[str]] = None,@@ -847,8 +854,8 @@ class Neo4jClient(GraphClient):{"WHERE " + " AND ".join(filters) if filters else ""}RETURN count(n) AS total"""- result = self.driver.execute_query(cypher_query)- count_result = self.driver.execute_query(cypher_count)+ result = self.driver.execute_query(cypher_query, database_=brain_id)+ count_result = self.driver.execute_query(cypher_count, database_=brain_id)total = 0if count_result and count_result.records:total = count_result.records[0].get("total") or 0diff --git a/src/lib/neo4j/docker-compose.yaml b/src/lib/neo4j/docker-compose.yamlindex ad5f538..23e54a2 100644--- a/src/lib/neo4j/docker-compose.yaml+++ b/src/lib/neo4j/docker-compose.yaml@@ -1,6 +1,6 @@services:neo4j:- image: neo4j:latest+ image: neo4j:5-enterprisevolumes:- /$HOME/neo4j/logs:/logs- ./config:/config@@ -8,6 +8,7 @@ services:- /$HOME/neo4j/plugins:/pluginsenvironment:- NEO4J_AUTH=neo4j/your_password+ - NEO4J_ACCEPT_LICENSE_AGREEMENT=evalports:- "7474:7474"- "7687:7687"diff --git a/src/lib/redis/client.py b/src/lib/redis/client.pyindex 8b8d939..22f761c 100644--- a/src/lib/redis/client.py+++ b/src/lib/redis/client.py@@ -8,6 +8,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@-----"""+from typing import Optionalfrom redis import Redisfrom redis.connection import ConnectionPoolfrom src.adapters.interfaces.cache import CacheClient@@ -31,23 +32,36 @@ class RedisClient(CacheClient):)self.client = Redis(connection_pool=pool)- def get(self, key: str) -> str:+ def _get_key(self, key: str, brain_id: str) -> str:+ """+ Get the prefixed key for a given brain_id.+ """+ return f"{brain_id}:{key}"++ def get(self, key: str, brain_id: str) -> str:"""Get a value from the cache."""- return self.client.get(key)+ prefixed_key = self._get_key(key, brain_id)+ return self.client.get(prefixed_key)- def set(self, key: str, value: str, expires_in: int) -> bool:+ def set(+ self, key: str, value: str, brain_id: str, expires_in: Optional[int] = None+ ) -> bool:"""Set a value in the cache with an expiration time."""- return self.client.set(key, value, ex=expires_in)+ prefixed_key = self._get_key(key, brain_id)+ return self.client.set(+ prefixed_key, value, **({"ex": expires_in} if expires_in else {})+ )- def delete(self, key: str) -> bool:+ def delete(self, key: str, brain_id: str) -> bool:"""Delete a value from the cache."""- return self.client.delete(key)+ prefixed_key = self._get_key(key, brain_id)+ return self.client.delete(prefixed_key)_redis_client = RedisClient()diff --git a/src/services/api/app.py b/src/services/api/app.pyindex 51b940f..3e54ad0 100644--- a/src/services/api/app.py+++ b/src/services/api/app.py@@ -15,6 +15,7 @@ from fastapi.middleware.cors import CORSMiddlewarefrom uvicorn import runfrom src.services.api.middlewares.auth import BrainPATMiddleware+from src.services.api.middlewares.brains import BrainMiddlewarefrom src.services.api.routes.ingest import ingest_routerfrom src.services.api.routes.retrieve import retrieve_routerfrom src.services.api.routes.meta import meta_router@@ -30,6 +31,7 @@ app.add_middleware(allow_methods=["*"],allow_headers=["*"],)+app.add_middleware(BrainMiddleware)app.include_router(ingest_router)app.include_router(retrieve_router)diff --git a/src/services/api/constants/requests.py b/src/services/api/constants/requests.pyindex 3656a95..0cb472e 100644--- a/src/services/api/constants/requests.py+++ b/src/services/api/constants/requests.py@@ -50,15 +50,14 @@ class IngestionStructuredRequestBody(BaseModel):"""data: List[IngestionStructuredDataElement]- store: Optional[str] = Field(- default="default",- description="The store where the data will be stored, if not provided, the data will be stored in the default store.",- )observate_for: Optional[List[str]] = Field(default=[],description="What to look for and describe in the data during observation. ""If not provided, the observations will be generic",)+ brain_id: str = Field(+ default="default", description="The brain identifier to store the data in."+ )class RetrieveRequestResponse(BaseModel):diff --git a/src/services/api/controllers/meta.py b/src/services/api/controllers/meta.pyindex 91583d8..a64bc5e 100644--- a/src/services/api/controllers/meta.py+++ b/src/services/api/controllers/meta.py@@ -16,5 +16,5 @@ async def get_entity_properties():"""Get the entity properties of the graph."""- result = await asyncio.to_thread(graph_adapter.get_graph_property_keys)+ result = await asyncio.to_thread(graph_adapter.get_graph_property_keys, brain_id)return resultdiff --git a/src/services/api/controllers/retrieve.py b/src/services/api/controllers/retrieve.pyindex 99a0051..d9cd9a5 100644--- a/src/services/api/controllers/retrieve.py+++ b/src/services/api/controllers/retrieve.py@@ -30,7 +30,7 @@ from src.utils.logging import log, logtasync def retrieve_data(- text: str, limit: int, preferred_entities: str+ text: str, limit: int, preferred_entities: str, brain_id: str = "default") -> RetrieveRequestResponse:"""Retrieve data from the knowledge graph and data store.@@ -43,7 +43,7 @@ async def retrieve_data(preferred_entities = []def _get_data():- text_embeddings = embeddings_adapter.embed_text(text)+ text_embeddings = embeddings_adapter.embed_text(text, brain_id)data_vectors = vector_store_adapter.search_vectors(text_embeddings.embeddings, "data", limit@@ -52,13 +52,13 @@ async def retrieve_data(text_embeddings.embeddings, "triplets", limit)- search_result = data_adapter.search(text)+ search_result = data_adapter.search(text, brain_id)ts_text_chunks = search_result.text_chunksts_observations = search_result.observationsv_text_chunks, v_observations = data_adapter.get_text_chunks_by_ids(- [dv.metadata.get("resource_id") for dv in data_vectors], True+ [dv.metadata.get("resource_id") for dv in data_vectors], True, brain_id)node_ids = [@@ -68,6 +68,7 @@ async def retrieve_data(]nodes = graph_adapter.get_nodes_by_uuid(uuids=node_ids,+ brain_id=brain_id,with_relationships=True,relationships_depth=1,relationships_type=[@@ -94,16 +95,19 @@ async def retrieve_neighbors(uuid: Optional[str] = None,identification_params: Optional[IdentificationParams] = None,limit: int = 10,+ brain_id: str = "default",) -> RetrieveNeighborsRequestResponse:def _get_neighbors():# ================= GETTING THE MAIN NODE =================node = Noneif uuid:- node = graph_adapter.get_by_uuid(uuid)+ node = graph_adapter.get_by_uuid(uuid, brain_id)elif identification_params:node = graph_adapter.get_by_identification_params(- identification_params, entity_types=identification_params.entity_types+ identification_params,+ brain_id=brain_id,+ entity_types=identification_params.entity_types,)if not node:raise HTTPException(status_code=404, detail="Entity not found")@@ -113,7 +117,7 @@ async def retrieve_neighbors(# That share a common node with the main node# The neighbors are of the same type (share at least one label) of the# main node- raw = graph_adapter.get_neighbors(node, limit)+ raw = graph_adapter.get_neighbors(node, limit, brain_id)normalized: List[RetrievedNeighborNode] = []@@ -155,7 +159,7 @@ async def retrieve_neighbors(# at least one label with the main nodeif len(normalized) < limit:neighbor_nodes = graph_adapter.get_nodes_by_uuid(- uuids=[node.uuid], with_relationships=True+ uuids=[node.uuid], brain_id=brain_id, with_relationships=True)# Mapping the ids and the nodes into a dictionary and ids array@@ -215,6 +219,7 @@ async def retrieve_neighbors(for k, v in same_main_labels_v_neighbors.items():neighbor_nodes = graph_adapter.get_by_uuids(uuids=[vector.metadata.get("uuid") for vector in v],+ brain_id=brain_id,)for n in neighbor_nodes:if len(normalized) >= limit:@@ -244,6 +249,7 @@ async def retrieve_neighbors(# the ones the are with same label as the main nodefor k, v in others_v_neighbors.items():neighbor_nodes = graph_adapter.get_connected_nodes(+ brain_id=brain_id,uuids=[vector.metadata.get("uuid") for vector in v],limit=limit - len(normalized),with_labels=node.labels,@@ -289,6 +295,7 @@ async def retrieve_neighbors_ai_mode(identification_params: IdentificationParams,looking_for: Optional[list[str]],limit: int,+ brain_id: str = "default",) -> RetrieveNeighborsRequestResponse:"""Retrieve neighbors of an entity from the knowledge graph.@@ -296,7 +303,9 @@ async def retrieve_neighbors_ai_mode(def _get_neighbors():node = graph_adapter.get_by_identification_params(- identification_params, entity_types=identification_params.entity_types+ identification_params,+ brain_id=brain_id,+ entity_types=identification_params.entity_types,)if not node:raise HTTPException(status_code=404, detail="Entity not found")@@ -306,7 +315,7 @@ async def retrieve_neighbors_ai_mode(ids = [neighbor.uuid for neighbor in result.neighbors]descriptions = [neighbor.description for neighbor in result.neighbors]- nodes = graph_adapter.get_nodes_by_uuid(uuids=ids)+ nodes = graph_adapter.get_nodes_by_uuid(uuids=ids, brain_id=brain_id)paired = list(zip(nodes, descriptions))return RetrieveNeighborsRequestResponse(neighbors=paired)@@ -324,6 +333,7 @@ async def get_relationships(to_node_labels: Optional[list[str]] = None,query_text: Optional[str] = None,query_search_target: Optional[str] = "all",+ brain_id: str = "default",):"""Get the relationships of the graph.@@ -337,6 +347,7 @@ async def get_relationships(to_node_labels,query_text,query_search_target,+ brain_id,)return JSONResponse(@@ -353,12 +364,13 @@ async def get_entities(skip: int = 0,node_labels: Optional[list[str]] = None,query_text: Optional[str] = None,+ brain_id: str = "default",):"""Get the entities of the graph."""entities = await asyncio.to_thread(- search_entities, limit, skip, node_labels, query_text+ search_entities, limit, skip, node_labels, query_text, brain_id)return JSONResponse(diff --git a/src/services/api/middlewares/brains.py b/src/services/api/middlewares/brains.pynew file mode 100644index 0000000..fc55fb5--- /dev/null+++ b/src/services/api/middlewares/brains.py@@ -0,0 +1,84 @@+"""+File: /brains.py+Created Date: Monday December 1st 2025+Author: Christian Nonis <[email protected]>+-----+Last Modified: Monday December 1st 2025 9:06:30 pm+Modified By: the developer formerly known as Christian Nonis at <[email protected]>+-----+"""++from fastapi import Request+from fastapi.responses import JSONResponse+from starlette.middleware.base import BaseHTTPMiddleware+from starlette import status++from src.services.data.main import data_adapter+from src.services.kg_agent.main import cache_adapter+++class BrainMiddleware(BaseHTTPMiddleware):+ async def dispatch(self, request: Request, call_next):+ brain_id = None++ brain_id = request.query_params.get("brain_id")+ if brain_id:+ brain_id = brain_id.rstrip()++ if brain_id is None and request.method in ("POST", "PUT", "PATCH"):+ body = await request.body()+ if body:+ try:+ import json++ body_data = json.loads(body)+ if isinstance(body_data, dict):+ brain_id = body_data.get("brain_id")+ if brain_id:+ brain_id = brain_id.rstrip()+ except (json.JSONDecodeError, ValueError):+ pass++ if body:++ async def receive():+ return {"type": "http.request", "body": body}++ request._receive = receive++ if brain_id == "system":+ return JSONResponse(+ status_code=status.HTTP_400_BAD_REQUEST,+ content={"detail": "System brain is not allowed to be used."},+ )+ if not brain_id.isalnum():+ return JSONResponse(+ status_code=status.HTTP_400_BAD_REQUEST,+ content={"detail": "Brain ID must be alphanumeric."},+ )++ if brain_id:+ cached_brain_id = cache_adapter.get(+ key=f"brain:{brain_id}", brain_id="system"+ )++ if not cached_brain_id:+ stored_brain = data_adapter.get_brain(name_key=brain_id)++ if not stored_brain:+ new_brain = data_adapter.create_brain(name_key=brain_id)+ cache_adapter.set(+ key=f"brain:{brain_id}",+ value=new_brain.id,+ brain_id="system",+ )+ else:+ cache_adapter.set(+ key=f"brain:{brain_id}",+ value=stored_brain.id,+ brain_id="system",+ )++ request.state.brain_id = brain_id++ return await call_next(request)diff --git a/src/workers/tasks/ingestion.py b/src/workers/tasks/ingestion.pyindex 2bfffdd..cf7d0f0 100644--- a/src/workers/tasks/ingestion.py+++ b/src/workers/tasks/ingestion.py@@ -66,7 +66,8 @@ def ingest_data(self, args: dict):else json.dumps(payload.data.json_data)),metadata=payload.meta_keys,- )+ ),+ brain_id=payload.brain_id,)text_chunk_vector = embeddings_adapter.embed_text(text_chunk.text)text_chunk_vector.metadata = {@@ -148,6 +149,7 @@ def ingest_structured_data(self, args: dict):},)],+ brain_id=payload.brain_id,identification_params=element.identification_params.model_dump(mode="json"),metadata=element.textual_data,)[0]@@ -171,7 +173,8 @@ def ingest_structured_data(self, args: dict):"labels": element.types,"name": element.identification_params.name,},- )+ ),+ brain_id=payload.brain_id,)vector = embeddings_adapter.embed_text("\n".join(@@ -187,7 +190,7 @@ def ingest_structured_data(self, args: dict):+ "\n"+ (", ".join(node.labels) if isinstance(node.labels, list) else node.labels)+ "\n"- + node.name+ + node.name,)vector.id = node.uuidvector.metadata = {@@ -233,7 +236,8 @@ def ingest_structured_data(self, args: dict):resource_id=node.uuid,)for obs in observations- ]+ ],+ brain_id=payload.brain_id,)# TODO: + vectorize the predicates and new nodes + save changelog&vectors