到这一层为止,我们已经拥有了:

  • 会说话的 AI(ai-core)
  • 会思考的 Prompt(ai-prompt)
  • 会查知识的 RAG(ai-rag)
  • 有记忆的 Memory(ai-memory)
  • 能执行的 Tools(ai-tools)
    项目地址:你也可以去 GitHub 上获取相配套的项目代码。
    但问题来了:

❌ 谁来“协调这一切”?


一、为什么必须有 AI Service?

如果没有这一层,你的系统会变成:

Controller
  ↓
直接调用 rag / memory / tools / core

结果:

❌ 业务逻辑散落

❌ 无法扩展

❌ if-else 地狱

❌ AI系统不可演进


二、AI Service 的本质是什么?

一句话:

👉 AI Service = AI 系统的“大脑(Orchestrator)”


它不负责:

  • ❌ 不生成答案
  • ❌ 不构建 prompt
  • ❌ 不做知识检索
  • ❌ 不执行工具

它只做一件事:

👉 协调所有能力模块


三、AI系统完整结构(最终形态)

Controller
   ↓
AI Service(唯一入口)
   ↓
 ├── Memory(记忆)
 ├── RAG(知识)
 ├── Tools(执行)
 ├── Prompt(构建)
 └── Core(生成)

四、AI Service 的核心流程

用户请求
   ↓
1️⃣ 加载 Memory(历史)
2️⃣ 检索 RAG(知识)
3️⃣ 执行 Tools(动作)
4️⃣ 构建 Prompt(ai-prompt)
5️⃣ 调用 LLM(ai-core)
6️⃣ 返回结果

五、核心代码设计

🧠 AiChatService(核心)

/**
 * 系统唯一编排入口:按策略聚合 memory / RAG / tools,经 prompt 构建后调用 LLM,不写具体领域实现。
 * <p>
 * 流程:load memory →(可选)RAG →(可选)tools → build prompt → LLM → save memory。<br>
 * 策略可替换以实现 Agent、多步工具链等扩展。
 */
@Service
public class AiChatService {

    private final ChatMemory memory;
    private final KnowledgeRetriever rag;
    private final ToolExecutor tools;
    private final PromptComposer promptComposer;
    private final LlmClient llm;
    private final RagEligibilityPolicy ragPolicy;
    private final ToolEligibilityPolicy toolPolicy;

    public AiChatService(ChatMemory memory,
                         KnowledgeRetriever rag,
                         ToolExecutor tools,
                         PromptComposer promptComposer,
                         LlmClient llm,
                         RagEligibilityPolicy ragPolicy,
                         ToolEligibilityPolicy toolPolicy) {
        this.memory = Objects.requireNonNull(memory);
        this.rag = Objects.requireNonNull(rag);
        this.tools = Objects.requireNonNull(tools);
        this.promptComposer = Objects.requireNonNull(promptComposer);
        this.llm = Objects.requireNonNull(llm);
        this.ragPolicy = Objects.requireNonNull(ragPolicy);
        this.toolPolicy = Objects.requireNonNull(toolPolicy);
    }

    /**
     * 完整对话编排:聚合上下文 → 生成 prompt → 调用模型 → 持久化本轮。
     *
     * @param sessionId 会话 ID
     * @param message   用户当前输入
     * @return 模型回复文本
     */
    public String chat(String sessionId, String message) {
        String history = memory.loadHistory(sessionId);

        List<String> context = ragPolicy.shouldRetrieveKnowledge(message)
                ? rag.retrieve(message)
                : Collections.emptyList();

        String toolResult = toolPolicy.shouldExecuteTools(message)
                ? tools.execute(message)
                : "";

        String prompt = promptComposer.build(history, context, toolResult, message);
        String answer = llm.chat(prompt);
        memory.saveMessage(sessionId, message, answer);
        return answer;
    }
}

六、AI Service 的设计哲学(核心)

🧠 1. 编排而不是实现

👉 AI Service 不“做事”,只“安排事情”

🧩 2. 解耦所有能力模块

每个模块只做一件事:

  • memory → 记忆
  • rag → 知识
  • tools → 执行
  • prompt → 结构
  • core → 生成

⚡ 3. 可扩展性优先

未来可以轻松升级:

  • Agent系统
  • 多模型路由
  • Tool Chain
  • RAG增强

七、为什么这一层是分水岭?

如果没有 AI Service:

👉 你写的是“AI功能集合”

有了 AI Service:

👉 你写的是“AI系统”


八、AI系统的演进路径

LLM(单点)
   ↓
Prompt工程
   ↓
RAG增强
   ↓
Memory系统
   ↓
Tools能力
   ↓
AI Service(编排层)
   ↓
Agent系统(下一阶段)

九、未来扩展(非常重要)

AI Service 下一步会进化成:

🚀 1. Agent Router

自动决定:

  • 用 RAG?
  • 用 Tools?
  • 直接 LLM?

🚀 2. 多策略编排

Strategy Pattern
Rule Engine
LLM Router

🚀 3. 多模型调度

  • GPT
  • Claude
  • DeepSeek

十、总结

这一篇你完成了整个 AI 系统最关键的一步:

👉 从“功能模块”升级为“系统架构”


🧠 一句话总结整个专栏当前阶段:

👉 AI Service = AI系统的“唯一大脑入口”


🚀 下一篇建议(非常关键)

下一篇你应该直接进入:

👉 《AI Eval系统:让AI能力提升“可量化,而不是凭感觉”

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐