```mermaid graph TD %% Main system components Client[Chat Interface Frontend] Server[Flask Server] MongoDB[(MongoDB)] Neo4j[(Neo4j)] LLM[OpenAI LLM API] %% Initialization flow Init[System Initialization] --> |1. Start server| Server Server --> |2. Connect to MongoDB| MongoDB Server --> |3. Load or create index| IdxInit Server --> |4. Connect to Neo4j| Neo4j %% Subgraphs for clarity subgraph "Initialization" IdxInit[Initialize Global Index] IdxInit --> |Load existing index| LoadIdx[Load from storage] IdxInit --> |Build new index| BuildIdx[Process documents] BuildIdx --> LlamaParser[LlamaParse Text & Images] LlamaParser --> VectorIdx[Create Vector Index] LlamaParser --> ExtractImages[Extract Images] LoadIdx --> LoadGraph[Load GraphRAG components] VectorIdx --> CreateGraph[Create GraphRAG components] CreateGraph --> KGExtract[Extract entities & relations] KGExtract --> CommDetect[Community detection] CommDetect --> CommSummary[Generate community summaries] LoadGraph -.-> |Check cache| CommCache[Load community cache] end %% Chat processing flow Client --> |5. Send message with sessionId| Server Server --> |6. Get session state| SessionManager subgraph "Session Management" SessionManager[Session Manager] SessionManager --> |Check cache| MemCache[In-memory cache] SessionManager --> |Check DB| SessionDB[Session in MongoDB] SessionManager --> |Create if needed| NewSession[Create new session] NewSession --> NewConv[Create conversation] end Server --> |7. Store user message| MongoDB Server --> |8. Process query| Agent subgraph "Agent Workflow" Agent[ReAct Agent] Agent --> |Select tool| Tools subgraph "Query Tools" Tools[Query Tools] VectorTool[Vector Query Tool] GraphTool[GraphRAG Tool] Tools --> VectorTool Tools --> GraphTool VectorTool --> VectorRetriever[Vector Retriever] VectorRetriever --> VectorRank[Top-K Similarity] GraphTool --> DualRetrieval[Dual Retrieval] DualRetrieval --> VectorRetriever DualRetrieval --> GraphRetriever[Graph Community Retriever] GraphRetriever --> ExtractEntities[Extract entities] ExtractEntities --> MapCommunities[Map to communities] MapCommunities --> GetSummaries[Get community summaries] end VectorRank --> |Context| GenResponse[Generate Response] GetSummaries --> |Context| GenResponse GenResponse --> |Use LLM| LLM end Agent --> |9. Format response with sources & images| Response Response --> |10. Store assistant message| MongoDB Response --> |11. Send response to client| Client %% Image handling flow subgraph "Image Processing" ImgProcess[Image Handling] ExtractImages --> SaveImages[Save to images directory] SaveImages --> LinkToNodes[Link images to nodes] Client --> |12. Request images| ImgEndpoint[Image endpoint] ImgEndpoint --> ServeImage[Serve image files] end %% Conversation management subgraph "Conversation Management" ConvMgmt[Conversation Management] Client --> |List conversations| GetConvs[Get conversations] GetConvs --> MongoDB Client --> |Get messages| GetMsgs[Get messages] GetMsgs --> MongoDB Client --> |Create new conversation| CreateConv[Create conversation] CreateConv --> MongoDB Client --> |Delete conversation| DeleteConv[Delete conversation] DeleteConv --> MongoDB end %% Styling classDef primary fill:#3498db,stroke:#2980b9,color:white; classDef secondary fill:#2ecc71,stroke:#27ae60,color:white; classDef storage fill:#9b59b6,stroke:#8e44ad,color:white; classDef apiService fill:#e74c3c,stroke:#c0392b,color:white; class Client,Server,Agent,Response primary; class SessionManager,Tools,ImgProcess,ConvMgmt secondary; class MongoDB,Neo4j storage; class LLM apiService; ```