知识分享社区 ai 模块总览与架构

发表于 2026-08-11 815 字 5 min read

文章目录
知识分享社区项目的 ai 模块总览与架构

1. 模块是什么

AI 模块是知识分享社区的人工智能能力层,提供两大能力:

  1. AI 摘要生成 — 基于知文正文,用 DeepSeek Chat 生成不超过 50 字的中文描述
  2. RAG 问答 — 将知文正文切块向量化存入 ES,用户提问时检索相关内容块,交给 LLM 流式作答(SSE)

重要特征: AI 模块没有自己的 Controller——它的能力通过 content 模块的 KnowPostAiController(摘要建议)和 KnowPostRagController(RAG 问答/重索引)对外暴露。

2. 两大子系统

子系统入口服务LLM输出使用方
摘要生成DescriptionApplicationServiceDeepSeek Chat (deepseek-chat)同步单次结果content 的 description/suggest 接口
RAG 问答RagApplicationServiceDeepSeek Chat + Ollama EmbeddingSSE 流 (Flux<String>)content 的 qa/stream、rag/reindex

3. 分层架构

graph TD
    subgraph application["application/ 应用层"]
        DAS["DescriptionApplicationService<br/>摘要用例"]
        RAS["RagApplicationService<br/>索引/检索/问答用例 + 可见性校验"]
    end
    subgraph domain["domain/ 领域层"]
        ADI["AiDescriptionDomainService (接口)"]
        RDI["RagDomainService (接口)"]
        RR["RagRepository (接口)"]
        VO["AiDescription/ChunkSpec/RagChunk/RagQuery 值对象"]
    end
    subgraph infrastructure["infrastructure/ 基础设施层"]
        DSC["DeepSeekDescriptionClient<br/>实现 AiDescriptionDomainService"]
        RRI["RagRepositoryImpl<br/>实现 RagRepository + RagDomainService"]
        LC["LlmConfig (ChatClient Bean)"]
    end
    DAS --> ADI
    RAS --> RDI
    RAS --> RR
    ADI --> DSC
    RDI --> RRI
    RR --> RRI
    DSC --> LC

⚠️ 架构特征: 与 user/social/content 不同,本模块的领域服务是接口,由基础设施类实现:

  • AiDescriptionDomainService 接口 ← DeepSeekDescriptionClient(infra)
  • RagDomainService + RagRepository 两个接口 ← 同一个 RagRepositoryImpl(infra)

即”领域服务”实际是基础设施对 AI 客户端的薄封装,DDD 纯度较低(务实取向)。

4. 文件清单与职责(14 个文件)

application/ 应用层 (2 个)

文件职责
service/DescriptionApplicationService.java摘要用例(薄封装)
service/RagApplicationService.javaRAG 用例:reindex/ensureIndexed/query + 可见性校验(published+public)

domain/ 领域层 (8 个)

文件职责
service/AiDescriptionDomainService.java摘要领域服务接口
service/RagDomainService.javaRAG 领域服务接口(indexPost/query)
repository/RagRepository.java向量仓储接口(indexChunks/searchSimilar/deleteByPostId/fingerprintExists)
model/valueobject/AiDescription.javaAI 描述值对象(归一化 + 50 字截断)
model/valueobject/ChunkSpec.java切块规格(800 字/100 字重叠)
model/valueobject/RagChunk.javaRAG 文本块
model/valueobject/RagQuery.javaRAG 查询(postId/question/topK 校验)

infrastructure/ 基础设施层 (4 个)

文件职责
llm/LlmConfig.javaChatClient Bean(基于 deepSeekChatModel)
llm/DeepSeekDescriptionClient.javaDeepSeek 摘要调用(ChatClient + DeepSeekChatOptions)
vectorstore/RagRepositoryImpl.javaRAG 实现:切块/向量索引/检索/LLM 流式问答(同时实现两个领域接口)

5. 被调用入口

AI 模块无 REST 接口,由 content 模块暴露:

HTTP 接口Controller (content)ai 服务
POST /api/v1/knowposts/description/suggestKnowPostAiControllerDescriptionApplicationService.generateDescription
GET /api/v1/knowposts/{id}/qa/stream (SSE)KnowPostRagControllerRagApplicationService.query
POST /api/v1/knowposts/{id}/rag/reindexKnowPostRagControllerRagApplicationService.reindex
发布/内容确认自动触发content 业务层RagApplicationService.ensureIndexed

6. 架构图

flowchart LR
    subgraph content 模块
        AIC[KnowPostAiController]
        RAGC[KnowPostRagController]
        KPS[KnowPostApplicationServiceImpl<br/>publish/confirmContent]
    end
    subgraph ai 模块
        DAS[DescriptionApplicationService]
        RAS[RagApplicationService]
        DSC[DeepSeekDescriptionClient]
        RRI[RagRepositoryImpl]
    end
    subgraph 外部
        DS[DeepSeek Chat API]
        OL[Ollama Embedding<br/>qwen3-embedding:0.6b]
        ES[(Elasticsearch<br/>zhiguang-ai-index)]
    end
    AIC --> DAS --> DSC --> DS
    RAGC -->|SSE Flux| RAS
    KPS -->|ensureIndexed| RAS
    RAS --> RRI
    RRI -->|embedding + 向量检索| OL
    RRI -->|Document 写入/查询| ES
    RRI -->|LLM 流式回答| DS

7. 外部依赖

依赖用途配置
DeepSeek Chat摘要生成 + RAG 问答spring.ai.deepseek.api-key(.env 注入)
Ollama Embedding文本向量化spring.ai.ollama.base-url: http://localhost:11434,模型 qwen3-embedding:0.6b
Elasticsearch (Spring AI VectorStore)向量存储/余弦检索spring.ai.vectorstore.elasticsearch.index-name: zhiguang-ai-index
ES Java Client (shared)精确查询(指纹/删除)spring.elasticsearch.uris
content 模块正文读取KnowPostMapper.findDetailById

下一篇: 01-接口层与请求详解