This document provides an overview of the BrainDrive Memory AI Agent architecture, explaining how various components interact to deliver a comprehensive Personal Knowledge Management (PKM) system.
BrainDrive Memory follows a modular architecture with several key components:
┌───────────────────┐ ┌───────────────────┐ ┌────────────────────┐
│ │ │ │ │ │
│ Presentation │────▶│ Agent Layer │────▶│ Data Services │
│ (Streamlit UI) │ │ (LangGraph) │ │ │
│ │ │ │ │ │
└───────────────────┘ └───────────────────┘ └────────────────────┘
│ │
▼ ▼
┌───────────────────┐ ┌────────────────────┐
│ │ │ │
│ Tools & │ │ Data Sources │
│ Adapters │ │ │
│ │ │ │
└───────────────────┘ └────────────────────┘
The presentation layer handles user interaction through a Streamlit-based chat interface (app/presentation/web/streamlit/chat_ui.py). Key responsibilities:
- Rendering the chat interface
- File upload handling
- User authentication via Google OAuth
- Streaming responses from the LLM
- Managing conversation state
The agent layer orchestrates the AI-driven interactions, powered by LangGraph and LangChain. The primary component is:
app/agents/react_graph_agent.py: Implements a ReAct-style agent using LangGraph for reasoning and tool execution
The data services layer manages data operations and abstractions:
app/data_source_manager.py: Provides a unified interface to different data sourcesapp/services/pdf_processing_service.py: Handles PDF upload and processingapp/services/auth_service.py: Manages user authentication
The tools layer implements specific functionalities that can be invoked by the agent:
app/tools/search/search_tool.py: Search through knowledge baseapp/tools/add/add_tool.py: Add new information to knowledge baseapp/tools/update/update_tool.py: Update existing informationapp/tools/delete/delete_tool.py: Delete informationapp/tools/documents_search/documents_search_tool.py: Search through uploaded documents
Adapters provide standardized interfaces to various external services:
app/adapters/graph_db_adapter.py: Interface to Neo4japp/adapters/vector_store_adapter.py: Interface to Supabase vector storeapp/adapters/llm_adapter.py: Interface to language models (OpenAI, Groq, etc.)app/adapters/embedder_adapter.py: Interface to embedding modelsapp/adapters/vertex_ai_search_adapter.py: Interface to Vertex AI Search
The interfaces layer defines abstract interfaces that adapters implement:
app/interfaces/graph_db_interface.pyapp/interfaces/vector_store_interface.pyapp/interfaces/vertex_ai_search_interface.py
When a user uploads a PDF document:
- The PDF is uploaded via Streamlit UI (
chat_ui.py) - The file is saved temporarily and passed to
parseAndIngestPDFsinapp/services/document_processing_service.py - The file is uploaded to Google Cloud Storage
- LLMSherpa processes the document, extracting structure, text, and tables
- Document structure and content are stored in Neo4j, with embeddings generated for each section, chunk, and table
- Relevant metadata is stored in the vector database for semantic search
When a user asks a question:
- The query is sent from the UI to the agent (
react_graph_agent.py) - The agent processes the query using LangGraph
- The agent selects appropriate tools based on the query intent
- Tools interact with data sources via adapters
- Results are returned to the agent, which formulates a response
- The response is streamed back to the UI
The knowledge graph in Neo4j uses the following node types:
Document: Represents an uploaded documentSection: Represents a section within a documentChunk: Represents a text chunk within a sectionTable: Represents a table extracted from a document
Relationships between nodes:
(:Section)-[:HAS_DOCUMENT]->(:Document)(:Chunk)-[:HAS_PARENT]->(:Section)(:Table)-[:HAS_PARENT]->(:Section)(:Table)-[:HAS_PARENT]->(:Document)(:Section)-[:UNDER_SECTION]->(:Section)
The vector store contains:
- Document chunks with embeddings
- Metadata including source, document ID, and context
The application uses a hierarchical configuration system:
app_env.py: Centralizes environment variable management using Pydantic.envfile: Stores local configuration- Environment variables: Can override
.envvalues
The modularized architecture supports several extension points:
- Define a new interface in
app/interfaces/ - Implement an adapter in
app/adapters/ - Update
app/data_source_manager.pyto include the new data source - Add appropriate settings in
app/app_env.py
- Update
app/adapters/llm_adapter.pyto support the new provider - Add appropriate settings in
app/app_env.py
- Implement the tool in
app/tools/ - Add the tool to the tools list in
app/agents/react_graph_agent.py
For production deployments, consider the following architecture:
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ │ │ │ │ │
│ Load Balancer │───▶│ Memory AI Agent │───▶│ Neo4j Graph │
│ │ │ Containers │ │ Database │
└──────────────────┘ └──────────────────┘ └──────────────────┘
│ ▲
│ │
▼ │
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ │ │ │ │ │
│ Google Cloud │◀───│ LLM/Embedding │───▶│ Supabase Vector │
│ Storage │ │ Services │ │ Store │
│ │ │ │ │ │
└──────────────────┘ └──────────────────┘ └──────────────────┘
This architecture provides scalability and resilience for production workloads.