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的可能性,至于哪个做得更好,留待各位看官去探索。

相关推荐
倔强的石头_19 小时前
https://blog.csdn.net/2302_78391795/article/details/154952565
openai·ai编程
radient20 小时前
Agent的"思考" - 智能体
后端·架构·ai编程
iFlow_AI21 小时前
增强AI编程助手效能:使用开源Litho(deepwiki-rs)深度上下文赋能iFlow
人工智能·ai·ai编程·命令模式·iflow·iflow cli·心流ai助手
uccs1 天前
langchain创建智能体
aigc·openai·ai编程
炫饭第一名1 天前
Cursor 一年深度开发实践:前端开发的效率革命🚀
前端·程序员·ai编程
玲小珑1 天前
LangChain.js 完全开发手册(十九)前端 AI 开发进阶技巧
前端·langchain·ai编程
Sailing1 天前
🔥 大模型时代最讽刺的职业出现了:“大模型善后工程师”
前端·openai·ai编程
云起SAAS1 天前
名字姓名起名打分评分抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·名字姓名起名打分评分
金木讲编程2 天前
Claude、Agent与Copilot协作生成Angular应用
前端·ai编程
AntBlack2 天前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程