# lrj021029/ai-knowledge-base architecture

> Architecture diagram of the GitHub repository lrj021029/ai-knowledge-base, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lrj021029/ai-knowledge-base
- Repository: https://github.com/lrj021029/ai-knowledge-base

## Overview

This is a FastAPI-backed RAG knowledge base. Users upload documents for parsing, enrichment, chunking, embedding, and indexing, then submit questions for retrieval, ranking, and answer generation; query history is also managed. The README additionally describes MCP web search and book recommendation/detail capabilities. Graph edges follow explicit README statements or source-index references and shown calls; unsampled wiring is omitted rather than inferred.

Main entry points: [`app/process/query/agent/main_graph.py`](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/query/agent/main_graph.py) (Query Workflow), [`app/process/import_/agent/main_graph.py`](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/main_graph.py) (Import Workflow), [`app/infra/llm/providers.py`](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/llm/providers.py) (LLM Provider), [`app/api/http/query_server.py`](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/query_server.py) (Query API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_import_api["Import API<br/>[import_server.py]"]
  node_query_api["Query API<br/>[query_server.py]"]
end

subgraph group_import["Document Import"]
  node_import_flow["Import Workflow<br/>[main_graph.py]"]
  node_import_state["Import State<br/>[state.py]"]
  node_pdf_parse["PDF Parsing"]
  node_markdown_images["Markdown Images<br/>[node_md_img.py]"]
  node_document_split["Document Splitting<br/>[split_service.py]"]
  node_item_recognition["Item Recognition"]
  node_embedding_import["Chunk Embeddings"]
  node_indexing["Vector Indexing<br/>[index_service.py]"]
end

subgraph group_query["Question Answering"]
  node_query_flow["Query Workflow<br/>[main_graph.py]"]
  node_query_state["Query State<br/>[state.py]"]
  node_embedding_search["Hybrid Search"]
  node_hyde_search["HyDE Search"]
  node_web_search["MCP Web Search"]
  node_item_confirm["Item Confirmation"]
  node_rrf["RRF Fusion<br/>[rrf_service.py]"]
  node_rerank["Reranking<br/>[rerank_service.py]"]
  node_answer["Answer Generation<br/>[answer_service.py]"]
  node_book_features["Book Discovery<br/>[query_server.py]"]
end

subgraph group_infra["Models and Storage"]
  node_llm_provider["LLM Provider<br/>[providers.py]"]
  node_milvus[("Milvus Gateway<br/>[milvus_gateway.py]")]
  node_minio[("MinIO Gateway<br/>[minio_gateway.py]")]
  node_history_api[("History Repository")]
  node_mongo[("Mongo History Client")]
  node_infra_config["Infrastructure Config<br/>[providers.py]"]
end

subgraph group_shared["Shared Runtime"]
  node_task_tracking["Task Tracking<br/>[task_utils.py]"]
  node_sse["SSE Utilities<br/>[sse_utils.py]"]
  node_config["Shared Configuration<br/>[common.py]"]
  node_prompt_loader["Prompt Loader<br/>[load_prompt.py]"]
end

node_user(("User"))
node_user_external["Web Search Service"]

node_user -->|"uploads files"| node_import_api
node_import_api -->|"starts import"| node_import_flow
node_import_flow -->|"parses PDFs"| node_pdf_parse
node_import_flow -->|"processes images"| node_markdown_images
node_import_flow -->|"splits documents"| node_document_split
node_import_flow -->|"recognizes names"| node_item_recognition
node_import_flow -->|"embeds chunks"| node_embedding_import
node_import_flow -->|"indexes chunks"| node_indexing
node_embedding_import -->|"creates embeddings"| node_llm_provider
node_embedding_search -->|"embeds query"| node_llm_provider
node_embedding_search -->|"hybrid searches"| node_milvus
node_hyde_search -->|"generates HyDE"| node_llm_provider
node_hyde_search -->|"hybrid searches"| node_milvus
node_hyde_search -->|"loads prompt"| node_prompt_loader
node_item_recognition -->|"uses LLM"| node_llm_provider
node_item_recognition -->|"checks names"| node_milvus
node_user -->|"asks question"| node_query_api
node_query_api -->|"runs query"| node_query_flow
node_query_flow -->|"dispatches search"| node_embedding_search
node_query_flow -->|"dispatches HyDE"| node_hyde_search
node_query_flow -->|"dispatches web search"| node_web_search
node_query_flow -->|"dispatches confirmation"| node_item_confirm
node_query_flow -->|"dispatches fusion"| node_rrf
node_query_flow -->|"dispatches reranking"| node_rerank
node_query_flow -->|"dispatches answer"| node_answer
node_web_search -.->|"searches web"| node_user_external
node_answer -->|"generates response"| node_llm_provider
node_answer -->|"stores messages"| node_history_api
node_query_api -->|"reads history"| node_history_api
node_history_api -->|"delegates persistence"| node_mongo
node_query_api -->|"tracks tasks"| node_task_tracking
node_task_tracking -->|"pushes progress"| node_sse
node_import_api -->|"tracks imports"| node_task_tracking
node_query_api -->|"streams events"| node_sse
node_llm_provider -->|"reads settings"| node_infra_config
node_milvus -->|"reads settings"| node_infra_config
node_embedding_import -->|"updates state"| node_import_state
node_embedding_search -->|"reads state"| node_query_state
node_hyde_search -->|"reads state"| node_query_state
node_infra_config -->|"assembles settings"| node_config
node_user -->|"requests discovery"| node_book_features
node_book_features -.->|"book query flow"| node_query_flow

click node_import_api "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/import_server.py"
click node_query_api "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/query_server.py"
click node_import_flow "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/main_graph.py"
click node_import_state "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/state.py"
click node_pdf_parse "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/pdf_parse_service.py"
click node_markdown_images "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/nodes/node_md_img.py"
click node_document_split "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/split_service.py"
click node_item_recognition "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/item_name_service.py"
click node_embedding_import "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/embedding_service.py"
click node_indexing "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/index_service.py"
click node_query_flow "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/query/agent/main_graph.py"
click node_query_state "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/query/agent/state.py"
click node_embedding_search "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/embedding_search_service.py"
click node_hyde_search "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/hyde_search_service.py"
click node_web_search "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/web_search_service.py"
click node_item_confirm "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/item_name_confirm_service.py"
click node_rrf "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/rrf_service.py"
click node_rerank "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/rerank_service.py"
click node_answer "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/answer_service.py"
click node_book_features "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/query_server.py"
click node_llm_provider "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/llm/providers.py"
click node_milvus "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/vectorstore/milvus_gateway.py"
click node_minio "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/object_storage/minio_gateway.py"
click node_history_api "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/persistence/history_repository.py"
click node_mongo "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/clients/mongo_history_utils.py"
click node_infra_config "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/config/providers.py"
click node_task_tracking "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/utils/task_utils.py"
click node_sse "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/utils/sse_utils.py"
click node_config "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/config/common.py"
click node_prompt_loader "https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/runtime/load_prompt.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_import_api,node_query_api,node_user toneBlue
class node_import_flow,node_import_state,node_pdf_parse,node_markdown_images,node_document_split,node_item_recognition,node_embedding_import,node_indexing toneAmber
class node_query_flow,node_query_state,node_embedding_search,node_hyde_search,node_web_search,node_item_confirm,node_rrf,node_rerank,node_answer,node_book_features toneMint
class node_llm_provider,node_milvus,node_minio,node_history_api,node_mongo,node_infra_config toneRose
class node_task_tracking,node_sse,node_config,node_prompt_loader,node_user_external toneIndigo
```

## Components

### HTTP API

- [Import API](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/import_server.py): `app/api/http/import_server.py`
- [Query API](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/query_server.py): `app/api/http/query_server.py`

### Document Import

- [Import Workflow](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/main_graph.py): `app/process/import_/agent/main_graph.py`
- [Import State](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/state.py): `app/process/import_/agent/state.py`
- [PDF Parsing](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/pdf_parse_service.py): `app/rag/import_/pdf_parse_service.py`
- [Markdown Images](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/import_/agent/nodes/node_md_img.py): `app/process/import_/agent/nodes/node_md_img.py`
- [Document Splitting](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/split_service.py): `app/rag/import_/split_service.py`
- [Item Recognition](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/item_name_service.py): `app/rag/import_/item_name_service.py`
- [Chunk Embeddings](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/embedding_service.py): `app/rag/import_/embedding_service.py`
- [Vector Indexing](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/import_/index_service.py): `app/rag/import_/index_service.py`

### Question Answering

- [Query Workflow](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/query/agent/main_graph.py): `app/process/query/agent/main_graph.py`
- [Query State](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/process/query/agent/state.py): `app/process/query/agent/state.py`
- [Hybrid Search](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/embedding_search_service.py): `app/rag/query/embedding_search_service.py`
- [HyDE Search](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/hyde_search_service.py): `app/rag/query/hyde_search_service.py`
- [MCP Web Search](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/web_search_service.py): `app/rag/query/web_search_service.py`
- [Item Confirmation](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/item_name_confirm_service.py): `app/rag/query/item_name_confirm_service.py`
- [RRF Fusion](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/rrf_service.py): `app/rag/query/rrf_service.py`
- [Reranking](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/rerank_service.py): `app/rag/query/rerank_service.py`
- [Answer Generation](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/rag/query/answer_service.py): `app/rag/query/answer_service.py`
- [Book Discovery](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/api/http/query_server.py): `app/api/http/query_server.py`

### Models and Storage

- [LLM Provider](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/llm/providers.py): `app/infra/llm/providers.py`
- [Milvus Gateway](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/vectorstore/milvus_gateway.py): `app/infra/vectorstore/milvus_gateway.py`
- [MinIO Gateway](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/object_storage/minio_gateway.py): `app/infra/object_storage/minio_gateway.py`
- [History Repository](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/persistence/history_repository.py): `app/infra/persistence/history_repository.py`
- [Mongo History Client](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/clients/mongo_history_utils.py): `app/shared/clients/mongo_history_utils.py`
- [Infrastructure Config](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/infra/config/providers.py): `app/infra/config/providers.py`

### Shared Runtime

- [Task Tracking](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/utils/task_utils.py): `app/shared/utils/task_utils.py`
- [SSE Utilities](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/utils/sse_utils.py): `app/shared/utils/sse_utils.py`
- [Shared Configuration](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/config/common.py): `app/shared/config/common.py`
- [Prompt Loader](https://github.com/lrj021029/ai-knowledge-base/blob/main/app/shared/runtime/load_prompt.py): `app/shared/runtime/load_prompt.py`

### Other components

- **User**
- **Web Search Service**

## Connections

- User → Import API: uploads files
- Import API → Import Workflow: starts import (evidence: `app/api/http/import_server.py`)
- Import Workflow → PDF Parsing: parses PDFs (evidence: `app/process/import_/agent/main_graph.py`)
- Import Workflow → Markdown Images: processes images (evidence: `app/process/import_/agent/main_graph.py`)
- Import Workflow → Document Splitting: splits documents (evidence: `app/process/import_/agent/main_graph.py`)
- Import Workflow → Item Recognition: recognizes names (evidence: `app/process/import_/agent/main_graph.py`)
- Import Workflow → Chunk Embeddings: embeds chunks (evidence: `app/process/import_/agent/main_graph.py`)
- Import Workflow → Vector Indexing: indexes chunks (evidence: `app/process/import_/agent/main_graph.py`)
- Chunk Embeddings → LLM Provider: creates embeddings (evidence: `app/rag/import_/embedding_service.py`)
- Hybrid Search → LLM Provider: embeds query (evidence: `app/rag/query/embedding_search_service.py`)
- Hybrid Search → Milvus Gateway: hybrid searches (evidence: `app/rag/query/embedding_search_service.py`)
- HyDE Search → LLM Provider: generates HyDE (evidence: `app/rag/query/hyde_search_service.py`)
- HyDE Search → Milvus Gateway: hybrid searches (evidence: `app/rag/query/hyde_search_service.py`)
- HyDE Search → Prompt Loader: loads prompt (evidence: `app/rag/query/hyde_search_service.py`)
- Item Recognition → LLM Provider: uses LLM (evidence: `app/rag/import_/item_name_service.py`)
- Item Recognition → Milvus Gateway: checks names (evidence: `app/rag/import_/item_name_service.py`)
- User → Query API: asks question
- Query API → Query Workflow: runs query (evidence: `app/api/http/query_server.py`)
- Query Workflow → Hybrid Search: dispatches search (evidence: `app/process/query/agent/main_graph.py`)
- Query Workflow → HyDE Search: dispatches HyDE (evidence: `app/process/query/agent/main_graph.py`)
- Query Workflow → MCP Web Search: dispatches web search (evidence: `app/process/query/agent/main_graph.py`)
- Query Workflow → Item Confirmation: dispatches confirmation (evidence: `app/process/query/agent/main_graph.py`)
- Query Workflow → RRF Fusion: dispatches fusion (evidence: `app/process/query/agent/main_graph.py`)
- Query Workflow → Reranking: dispatches reranking (evidence: `app/process/query/agent/main_graph.py`)
- Query Workflow → Answer Generation: dispatches answer (evidence: `app/process/query/agent/main_graph.py`)
- MCP Web Search → Web Search Service: searches web
- Answer Generation → LLM Provider: generates response (evidence: `app/rag/query/answer_service.py`)
- Answer Generation → History Repository: stores messages (evidence: `app/rag/query/answer_service.py`)
- Query API → History Repository: reads history (evidence: `app/api/http/query_server.py`)
- History Repository → Mongo History Client: delegates persistence (evidence: `app/infra/persistence/history_repository.py`)
- Query API → Task Tracking: tracks tasks (evidence: `app/api/http/query_server.py`)
- Task Tracking → SSE Utilities: pushes progress (evidence: `app/shared/utils/task_utils.py`)
- Import API → Task Tracking: tracks imports (evidence: `app/api/http/import_server.py`)
- Query API → SSE Utilities: streams events (evidence: `app/api/http/query_server.py`)
- LLM Provider → Infrastructure Config: reads settings (evidence: `app/infra/llm/providers.py`)
- Milvus Gateway → Infrastructure Config: reads settings (evidence: `app/infra/vectorstore/milvus_gateway.py`)
- Chunk Embeddings → Import State: updates state (evidence: `app/rag/import_/embedding_service.py`)
- Hybrid Search → Query State: reads state (evidence: `app/rag/query/embedding_search_service.py`)
- HyDE Search → Query State: reads state (evidence: `app/rag/query/hyde_search_service.py`)
- Infrastructure Config → Shared Configuration: assembles settings (evidence: `app/infra/config/providers.py`)
- User → Book Discovery: requests discovery
- Book Discovery → Query Workflow: book query flow (evidence: `app/api/http/query_server.py`)
