5-工具调用 vs RAG-你喜欢主动还是被动?

1-背景故事

上一篇通过RAG,在AI助手回答问题前,将问题相关资料找出来提供给她,可以让她回答得更准确。

但是每次问她问题,还要提前帮她查好资料,感觉我比她还累😓。

另外她回答问题的好坏,完全取决于我的问题问得好不好、查出来的资料准不准😩。

能不能我告诉她在怎么查资料,她按需来查呢?

有了------我把查资料这事,封装成工具,让她按需来查不就行了!!

2-动手实践

本篇代码在上一篇代码的基础上,进行两点小改造即可。

2.1 封装一个知识库工具类:

java 复制代码
@Slf4j
@Component
public class KnowledgeRepositoryTools {
    private final VectorStore vectorStore;

    public KnowledgeRepositoryTools(VectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    @Tool(description = "我的个人资料库查询,可查询我的个人教育、工作经历、家庭成员等信息")
    public List<Document> queryKnowledge(String keyword) {
        log.info("查询个人档案,关键字:{}", keyword);
        // 注意这里由于没有进行向量检索优化,故把相似度阈值调低以便更容易匹配到结果
        var searchRequest = SearchRequest.builder().query(keyword).similarityThreshold(0.1).topK(5).build();
        return vectorStore.similaritySearch(searchRequest);
    }
}

2.2 聊天客户端注入工具

java 复制代码
@RestController
public class MyChatController {
    private final ChatClient chatClient;

    public MyChatController(ChatClient.Builder chatClientBuilder, ChatMemoryRepository chatMemoryRepository, KnowledgeRepositoryTools knowledgeRepositoryTools) {
        // ...
        this.chatClient = chatClientBuilder
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
                .defaultTools(new DateTimeTools())
                // 重点是这一句:添加知识库工具
                .defaultTools(knowledgeRepositoryTools)
                .build();
    }
    //...
}

2.3 改进时间工具

另外上一篇,我们发现大模型似乎不太喜欢我们的时间工具,改进一下工具描述,加上非常有用以提醒大模型调用:

java 复制代码
@Slf4j
@Component
public class DateTimeTools {
    // ...
    @Tool(description = "获取当前日期非常有用")
    String getCurrentLocalDate() {
        String current = LocalDate.now().toString();
        log.info("获取当前日期: {}", current);
        return current;
    }
    // ...
}

2.4 验证效果

shell 复制代码
# 提问:到今天为止我毕业几年了?
curl http://localhost:8080/ai/chat?message=%E5%88%B0%E4%BB%8A%E5%A4%A9%E4%B8%BA%E6%AD%A2%E6%88%91%E6%AF%95%E4%B8%9A%E5%87%A0%E5%B9%B4%E4%BA%86%EF%BC%9F
# AI助手回复(再次答对):
根据你的毕业时间为2022年6月30日,今天是2025年6月11日,你已经毕业大约3年了。

查看控制台日志,发现她非常聪明地调用了两次工具:

shell 复制代码
2025-06-11T16:32:44.740+08:00  INFO 72518 --- [ai-assistant] [omcat-handler-1] o.c.s.s.a.a.t.KnowledgeRepositoryTools   : 查询个人档案,关键字:毕业时间
2025-06-11T16:32:47.902+08:00  INFO 72518 --- [ai-assistant] [omcat-handler-1] o.c.s.s.a.assistant.tools.DateTimeTools  : 获取当前日期: 2025-06-11

🔔为演示效果,这里测试了多个提示词,选取了效果最好的一个,实际场景还有大量优化要做。

3-课外拓展

通过本篇的演示,我们探索了使用工具代替RAG的可能性,至于哪个做得更好,留待各位看官去探索。

相关推荐
counterxing7 小时前
Agent 跑起来之后,难的是复用、观测和评测
node.js·agent·ai编程
uccs7 小时前
大模型底层机制与Agent开发
agent·ai编程·claude
counterxing7 小时前
我把 Codex 里的 Skills 做成了一个 MCP,还支持分享
前端·agent·ai编程
夜雪闻竹7 小时前
vectra 向量索引文件损坏怎么办
ai编程·向量·vectra
ZzT8 小时前
Harness 到底指什么
openai·ai编程·claude
宅小年8 小时前
AI 创业最危险的地方:太容易做出来
openai·ai编程·claude
麦客奥德彪8 小时前
Android Skills
架构·ai编程
言萧凡_CookieBoty9 小时前
一文讲清 RAG:让 AI 读懂业务知识库的核心方法
ai编程
kyriewen10 小时前
产品经理把PRD写成“天书”,我用AI半小时重写了一遍,他当场愣住
前端·ai编程·cursor
Patrick_Wilson10 小时前
知识沉淀的四层模型:从个人笔记到企业资产,让文档真正长出复利
面试·程序员·ai编程