Skip to content

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.

fix(auth + brains): handled multiple brains and single brain deployments

Christiane77a86a58600
diff --git a/.env.example b/.env.example
index bf1d75d..337112b 100644
--- a/.env.example
+++ b/.env.example
@@ -46,5 +46,5 @@ BRAINPAT_TOKEN="your_token"
# MultiBrain
# Choose to allow or block automatic creation of new brains on requests with non existing new brain_ids
BRAIN_CREATION_ALLOWED="true"
-# Choose whether to use for every brain the main pat or to use dedicated one for each brain
-USE_ONLY_SYSTEM_PAT="false"
\ No newline at end of file
+# Choose whether to fallback to default brain if not provided
+DEFAULT_BRAIN_FALLBACK="true"
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index 3779ac8..de78b33 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "brainapi2"
-version = "1.6.6-dev"
+version = "1.6.14-dev"
description = "Version 1.x.x of the BrainAPI memory layer."
authors = [
{name = "Christian",email = "[email protected]"}
diff --git a/src/config.py b/src/config.py
index ba063c9..8e50345 100644
--- a/src/config.py
+++ b/src/config.py
@@ -78,7 +78,8 @@ class MilvusConfig:
def __init__(self):
self.host = os.getenv("MILVUS_HOST")
- self.port = os.getenv("MILVUS_PORT")
+ port_str = os.getenv("MILVUS_PORT")
+ self.port = int(port_str) if port_str else None
self.uri = os.getenv("MILVUS_URI")
self.token = os.getenv("MILVUS_TOKEN")
if [self.host, self.port].count(None) > 0 and [self.uri, self.token].count(
diff --git a/src/lib/milvus/client.py b/src/lib/milvus/client.py
index f14b17e..019fe80 100644
--- a/src/lib/milvus/client.py
+++ b/src/lib/milvus/client.py
@@ -38,6 +38,7 @@ class MilvusClient(VectorStoreClient):
def __init__(self):
self._client = None
self._lock = None
+ self._pid = None
def _get_lock(self):
if self._lock is None:
@@ -46,13 +47,28 @@ class MilvusClient(VectorStoreClient):
self._lock = threading.Lock()
return self._lock
+ def _reset_client_if_forked(self):
+ import os as os_module
+
+ current_pid = os_module.getpid()
+ if self._pid is not None and self._pid != current_pid:
+ if self._client is not None:
+ try:
+ self._client.close()
+ except Exception:
+ pass
+ self._client = None
+ self._pid = current_pid
+
@property
def client(self):
"""
Lazy initialization of the Milvus client.
"""
+ self._reset_client_if_forked()
if self._client is None:
with self._get_lock():
+ self._reset_client_if_forked()
if self._client is None:
if config.milvus.uri and config.milvus.token:
self._client = Milvus(
@@ -60,9 +76,9 @@ class MilvusClient(VectorStoreClient):
token=config.milvus.token,
)
elif config.milvus.host and config.milvus.port:
+ uri = f"http://{config.milvus.host}:{config.milvus.port}"
self._client = Milvus(
- host=config.milvus.host,
- port=config.milvus.port,
+ uri=uri,
token=config.milvus.token if config.milvus.token else None,
)
else:
diff --git a/src/lib/neo4j/client.py b/src/lib/neo4j/client.py
index efbb104..a24cf71 100644
--- a/src/lib/neo4j/client.py
+++ b/src/lib/neo4j/client.py
@@ -230,6 +230,7 @@ class Neo4jClient(GraphClient):
CREATE (a)-[:{":".join(self._clean_labels([predicate.name]))} {{ Description: '{safe_desc}' }}]->(b)
RETURN a, b
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return result
@@ -248,6 +249,7 @@ class Neo4jClient(GraphClient):
queries.append(query)
cypher_query = " UNION ".join(queries)
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return result
@@ -260,6 +262,7 @@ class Neo4jClient(GraphClient):
WHERE toLower(n.Name) CONTAINS toLower('{text}')
RETURN n
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return [
Node(
@@ -315,6 +318,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"
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
if with_relationships:
@@ -362,6 +366,7 @@ class Neo4jClient(GraphClient):
MATCH (n)
RETURN DISTINCT labels(n) as labels
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return [label for record in result.records for label in record["labels"]]
@@ -373,6 +378,7 @@ class Neo4jClient(GraphClient):
CALL db.relationshipTypes() YIELD relationshipType
RETURN relationshipType
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return [record["relationshipType"] for record in result.records]
@@ -384,6 +390,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
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
if not result.records or len(result.records) == 0:
return None
@@ -403,6 +410,7 @@ 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
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return [
Node(
@@ -438,6 +446,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
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
if not result.records or len(result.records) == 0:
return None
@@ -457,6 +466,7 @@ class Neo4jClient(GraphClient):
CALL db.propertyKeys() YIELD propertyKey
RETURN propertyKey
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return [record["propertyKey"] for record in result.records]
@@ -474,6 +484,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
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
neighbors = []
@@ -538,6 +549,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
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
if not result.records:
@@ -632,6 +644,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
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
return [
(
@@ -727,6 +740,7 @@ class Neo4jClient(GraphClient):
{"WHERE " + " AND ".join(filters) if filters else ""}
RETURN count(r) AS total
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
count_result = self.driver.execute_query(cypher_count, database_=brain_id)
total = 0
@@ -854,6 +868,7 @@ class Neo4jClient(GraphClient):
{"WHERE " + " AND ".join(filters) if filters else ""}
RETURN count(n) AS total
"""
+ self.ensure_database(brain_id)
result = self.driver.execute_query(cypher_query, database_=brain_id)
count_result = self.driver.execute_query(cypher_count, database_=brain_id)
total = 0
diff --git a/src/services/api/constants/requests.py b/src/services/api/constants/requests.py
index 0cb472e..285703d 100644
--- a/src/services/api/constants/requests.py
+++ b/src/services/api/constants/requests.py
@@ -155,6 +155,9 @@ class RetrieveNeighborsAiModeRequestBody(BaseModel):
...,
description="The description of the neighbors to look for.",
)
+ brain_id: str = Field(
+ default="default", description="The brain identifier to store the data in."
+ )
class RetrieveNeighborsWithIdentificationParamsRequestBody(BaseModel):
@@ -167,3 +170,14 @@ class RetrieveNeighborsWithIdentificationParamsRequestBody(BaseModel):
description="The identification parameters of the entity to get neighbors for.",
)
limit: int = Field(10, description="The number of neighbors to return.")
+ brain_id: str = Field(
+ default="default", description="The brain identifier to store the data in."
+ )
+
+
+class CreateBrainRequest(BaseModel):
+ """
+ Request body for the create brain endpoint.
+ """
+
+ brain_id: str
diff --git a/src/services/api/controllers/system.py b/src/services/api/controllers/system.py
index b291cbd..ea2eb93 100644
--- a/src/services/api/controllers/system.py
+++ b/src/services/api/controllers/system.py
@@ -11,6 +11,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@
import asyncio
from pydantic import BaseModel
+from src.services.api.constants.requests import CreateBrainRequest
from src.services.data.main import data_adapter
@@ -22,14 +23,6 @@ async def get_brains_list():
return result
-class CreateBrainRequest(BaseModel):
- """
- Request body for the create brain endpoint.
- """
-
- brain_id: str
-
-
async def create_new_brain(request: CreateBrainRequest):
"""
Create a new brain
diff --git a/src/services/api/middlewares/auth.py b/src/services/api/middlewares/auth.py
index e942588..9e37262 100644
--- a/src/services/api/middlewares/auth.py
+++ b/src/services/api/middlewares/auth.py
@@ -23,51 +23,50 @@ class BrainPATMiddleware(BaseHTTPMiddleware):
if request.method == "OPTIONS":
return await call_next(request)
+ # Variables ----------------------------------------------
brainpat = request.headers.get("BrainPAT") or getattr(
request.state, "pat", None
)
- brain_id = getattr(request.state, "brain_id", None)
-
- cachepat_key = f"brainpat:{brain_id or 'default'}"
+ system_pat = os.getenv("BRAINPAT_TOKEN")
- use_only_system_pat = os.getenv("USE_ONLY_SYSTEM_PAT") == "true"
-
- if use_only_system_pat:
- cachepat_key = "brainpat:system"
+ if request.url.path.startswith("/system") or request.url.path == "/":
+ if brainpat == system_pat:
+ return await call_next(request)
+ return JSONResponse(
+ status_code=status.HTTP_401_UNAUTHORIZED,
+ content={"detail": "Invalid or missing BrainPAT header"},
+ )
+ brain_id = getattr(request.state, "brain_id", None)
+ if not brain_id:
+ return JSONResponse(
+ status_code=status.HTTP_400_BAD_REQUEST,
+ content={"detail": "Brain ID is required."},
+ )
+ cachepat_key = f"brainpat:{brain_id}"
cached_brainpat = cache_adapter.get(key=cachepat_key, brain_id="system")
- if not cached_brainpat and not use_only_system_pat:
+ # Logic --------------------------------------------------
+ if brainpat == system_pat:
+ return await call_next(request)
+
+ if not cached_brainpat:
stored_brain = data_adapter.get_brain(name_key=brain_id)
- system_pat = os.getenv("BRAINPAT_TOKEN")
- if not stored_brain:
- if brainpat != system_pat:
- return JSONResponse(
- status_code=status.HTTP_401_UNAUTHORIZED,
- content={"detail": "Invalid or missing BrainPAT header"},
- )
- cached_brainpat = system_pat
- else:
- cached_brainpat = stored_brain.pat
- cache_adapter.set(
- key=cachepat_key,
- value=stored_brain.pat,
- brain_id="system",
+ if not stored_brain or stored_brain.pat != brainpat:
+ return JSONResponse(
+ status_code=status.HTTP_401_UNAUTHORIZED,
+ content={"detail": "Invalid or missing BrainPAT header"},
)
- if not cached_brainpat and use_only_system_pat:
- system_pat = os.getenv("BRAINPAT_TOKEN")
- cached_brainpat = system_pat
+ cached_brainpat = stored_brain.pat
cache_adapter.set(
- key="brainpat:system",
- value=system_pat,
- brain_id="system",
+ key=cachepat_key, value=stored_brain.pat, brain_id="system"
)
-
- if cached_brainpat != brainpat:
+ elif cached_brainpat != brainpat:
return JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
content={"detail": "Invalid or missing BrainPAT header"},
)
+
response = await call_next(request)
return response
diff --git a/src/services/api/middlewares/brains.py b/src/services/api/middlewares/brains.py
index 3da9465..a81a4fa 100644
--- a/src/services/api/middlewares/brains.py
+++ b/src/services/api/middlewares/brains.py
@@ -20,41 +20,53 @@ from src.services.kg_agent.main import cache_adapter
class BrainMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
- if request.method == "POST" and request.url.path == "/system/brains":
- return await call_next(request)
-
- brain_id = None
+ async def _get_brain_id():
+ brain_id = None
- brain_id = request.headers.get("X-Brain-ID")
- if brain_id:
- brain_id = brain_id.rstrip()
+ brain_id = request.headers.get("X-Brain-ID")
- if brain_id is 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 is 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
+ request.state.brain_id = brain_id
+ return brain_id
+
+ # Variables ----------------------------------------------
+ brain_id = await _get_brain_id()
+ brain_creation_allowed = os.getenv("BRAIN_CREATION_ALLOWED") == "true"
+ default_brain_fallback = os.getenv("DEFAULT_BRAIN_FALLBACK") == "true"
+ cached_brain_id = cache_adapter.get(key=f"brain:{brain_id}", brain_id="system")
+
+ # Bypassing system routes --------------------------------
+ if request.url.path.startswith("/system") or request.url.path == "/":
+ return await call_next(request)
+ # Cleanup checks -----------------------------------------
if brain_id == "system":
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
@@ -66,42 +78,47 @@ class BrainMiddleware(BaseHTTPMiddleware):
content={"detail": "Brain ID must be alphanumeric."},
)
- if brain_id:
- cached_brain_id = cache_adapter.get(
- key=f"brain:{brain_id}", brain_id="system"
- )
+ # Brains logic -------------------------------------------
+ if brain_id and not cached_brain_id:
+ stored_brain = data_adapter.get_brain(name_key=brain_id)
- brain_creation_allowed = os.getenv("BRAIN_CREATION_ALLOWED") == "true"
-
- if not cached_brain_id and brain_creation_allowed:
- stored_brain = data_adapter.get_brain(name_key=brain_id)
- new_brain = None
- 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",
- )
-
- cached_brain_id = new_brain.id if new_brain else stored_brain.id
-
- use_only_system_pat = os.getenv("USE_ONLY_SYSTEM_PAT") == "true"
- if not use_only_system_pat:
- request.state.pat = new_brain.pat if new_brain else stored_brain.pat
-
- if not cached_brain_id:
- return JSONResponse(
- status_code=status.HTTP_406_NOT_ACCEPTABLE,
- content={"detail": "Brain not found or creation is not allowed."},
+ if not stored_brain and brain_creation_allowed:
+ 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",
+ )
+ request.state.brain_id = new_brain.id
+ elif stored_brain:
+ cache_adapter.set(
+ key=f"brain:{brain_id}",
+ value=stored_brain.id,
+ brain_id="system",
+ )
+ request.state.brain_id = stored_brain.id
+ elif not brain_id and default_brain_fallback:
+ default_brain = data_adapter.get_brain(name_key="default")
+ if not default_brain:
+ default_brain = data_adapter.create_brain(name_key="default")
+ cache_adapter.set(
+ key="brain:default",
+ value=default_brain.id,
+ brain_id="system",
)
+ request.state.brain_id = "default"
+ else:
+ cache_adapter.set(
+ key="brain:default",
+ value=default_brain.id,
+ brain_id="system",
+ )
+ request.state.brain_id = "default"
- request.state.brain_id = brain_id
+ if getattr(request.state, "brain_id", None) is None:
+ return JSONResponse(
+ status_code=status.HTTP_406_NOT_ACCEPTABLE,
+ content={"detail": "Brain not found or creation is not allowed."},
+ )
return await call_next(request)
diff --git a/src/services/api/routes/retrieve.py b/src/services/api/routes/retrieve.py
index d919265..1701b3a 100644
--- a/src/services/api/routes/retrieve.py
+++ b/src/services/api/routes/retrieve.py
@@ -38,11 +38,12 @@ async def retrieve(
...,
description="The entities to prioritize in the relationships, separated by commas.",
),
+ brain_id: str = "default",
):
"""
Retrieve data from the knowledge graph and data store.
"""
- return await retrieve_data_controller(text, limit, preferred_entities)
+ return await retrieve_data_controller(text, limit, preferred_entities, brain_id)
@retrieve_router.get(
@@ -51,11 +52,14 @@ async def retrieve(
async def get_neighbors(
uuid: str = Query(..., description="The UUID of the entity to get neighbors for."),
limit: int = Query(10, description="The number of neighbors to return."),
+ brain_id: str = "default",
):
"""
Get the neighbors of an entity.
"""
- return await retrieve_neighbors_controller(uuid=uuid, limit=limit)
+ return await retrieve_neighbors_controller(
+ uuid=uuid, limit=limit, brain_id=brain_id
+ )
@retrieve_router.post(
@@ -103,6 +107,7 @@ async def get_relationships(
to_node_labels: Optional[str] = None,
query_text: Optional[str] = None,
query_search_target: Optional[str] = "all",
+ brain_id: str = "default",
):
"""
Get the relationships of the graph.
@@ -130,10 +135,13 @@ async def get_entities(
skip: int = 0,
node_labels: Optional[str] = None,
query_text: Optional[str] = None,
+ brain_id: str = "default",
):
"""
Get the entities of the graph.
"""
if node_labels:
node_labels = node_labels.split(",")
- return await retrieve_get_entities_controller(limit, skip, node_labels, query_text)
+ return await retrieve_get_entities_controller(
+ limit, skip, node_labels, query_text, brain_id
+ )
diff --git a/src/services/api/routes/system.py b/src/services/api/routes/system.py
index 79c0a2b..00b073f 100644
--- a/src/services/api/routes/system.py
+++ b/src/services/api/routes/system.py
@@ -9,6 +9,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@
"""
from fastapi import APIRouter
+from src.services.api.constants.requests import CreateBrainRequest
from src.services.api.controllers.system import (
get_brains_list as get_brains_list_controller,
create_new_brain as create_new_brain_controller,
@@ -26,8 +27,8 @@ async def get_brains_list():
@system_router.post(path="/brains")
-async def create_brain(brain_id: str):
+async def create_brain(request: CreateBrainRequest):
"""
Create a new brain
"""
- return await create_new_brain_controller(brain_id)
+ return await create_new_brain_controller(request)