完整实战:知识感知聊天机器人
📅 2026/7/20 16:12:26
👁️ 阅读次数
📝 编程学习
下面是一个轻量级的 RAG 模式示例——用 ChatMessage.ofUserAugment() 把上下文注入到 Prompt 中:
import org.noear.solon.ai.chat.ChatModel;
import org.noear.solon.ai.chat.ChatResponse;
import org.noear.solon.ai.chat.message.ChatMessage;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
@Component
public class KnowledgeChatbot {
@Inject
ChatModel chatModel;
public String answer(String question, String referenceContext) throws Exception { // 将参考上下文与用户问题合并 ChatMessage augmented = ChatMessage.ofUserAugment(question, referenceContext); ChatResponse resp = chatModel.prompt(augmented) .options(o -> o .temperature(0.3) .systemPrompt("你是一个知识渊博的助手。请基于提供的参考资料回答。")) .call(); return resp.getMessage().getContent(); }}
这种模式——用上下文增强用户输入,再调用模型——正是 Solon AI 中 RAG(检索增强生成)的基础。
- 下一步
ChatModel 只是入口点。Solon AI 还提供:
工具调用 — 用 @ToolMapping 定义 LLM 可调用的 Java 方法
Talent 系统 — 可复用的能力模块
Agent — ReActAgent 和 TeamAgent 实现多步推理
RAG 流水线 — 完整的文档加载、切分、嵌入、检索流程
MCP 协议 — 连接 MCP 服务器使用外部工具
编程学习
技术分享
实战经验