动态工具发现,使用工具搜索工具

随着AI代理连接到更多服务——如Slack、GitHub、Jira和MCP服务器——工具库迅速增长。一个典型的多服务器设置可能会有超过50个工具在任何对话开始前消耗大量token。更糟糕的是,当模型面临30多个同名工具时,工具选择的准确性会下降。spring-doc.cadn.net.cn

The 工具搜索工具模式,由Anthropic倡导,解决了这一问题:它不是一开始就加载所有工具定义,而是模型在需要时动态发现工具。它只提供一个搜索工具,当需要时查询能力,然后根据需要扩展相关的工具定义。spring-doc.cadn.net.cn

Spring AI的实现实现了在OpenAI、Anthropic和Gemini模型中34%至64%的token减少,同时保留了数百个工具的完全访问权限。spring-doc.cadn.net.cn

简介

The 工具搜索工具项目扩展了Spring AI的 递归顾问,以实现跨 任何大语言模型提供商的动态工具发现功能。spring-doc.cadn.net.cn

关键优势:spring-doc.cadn.net.cn

博客文章博客文章

博客文章详细介绍了完整实现细节、性能基准测试以及高级使用场景。spring-doc.cadn.net.cn

快速开始

依赖项

在你的项目中添加一个依赖项:Tool Search Tool。spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springaicommunity</groupId>
    <artifactId>tool-search-tool</artifactId>
    <version>2.0.0</version>
</dependency>

<!-- Choose a search strategy -->
<dependency>
    <groupId>org.springaicommunity</groupId>
    <artifactId>tool-searcher-lucene</artifactId>
    <version>2.0.0</version>
</dependency>
dependencies {
    implementation 'org.springaicommunity:tool-search-tool:2.0.0'

    // Choose a search strategy
    implementation 'org.springaicommunity:tool-searcher-lucene:2.0.0'
}
Version v1.0.x 是 Spring AI 1.1.x / Spring Boot 3 相容的版本。Version v2.0.x 是 Spring AI 2.x / Spring Boot 4 相容的版本。

基本用法

@SpringBootApplication
public class Application {

    @Bean
    CommandLineRunner demo(ChatClient.Builder builder, ToolSearcher toolSearcher) {
        return args -> {
            var advisor = ToolSearchToolCallAdvisor.builder()
                .toolSearcher(toolSearcher)
                .build();

            ChatClient chatClient = builder
                .defaultTools(new MyTools())  // 100s of tools registered but NOT sent to LLM initially
                .defaultAdvisors(advisor)     // Activate Tool Search Tool
                .build();

            var answer = chatClient.prompt("""
                Help me plan what to wear today in Amsterdam.
                Please suggest clothing shops that are open right now.
                """).call().content();

            System.out.println(answer);
        };
    }

    static class MyTools {
        @Tool(description = "Get the weather for a given location at a given time")
        public String weather(String location,
            @ToolParam(description = "YYYY-MM-DDTHH:mm") String atTime) {
            // implementation
        }

        @Tool(description = "Get clothing shop names for a given location at a given time")
        public List<String> clothing(String location,
                @ToolParam(description = "YYYY-MM-DDTHH:mm") String openAtTime) {
            // implementation
        }

        @Tool(description = "Current date and time for a given location")
        public String currentTime(String location) {
            // implementation
        }

        // ... potentially hundreds more tools
    }
}

如何工作

The extends Spring AI’s to implement dynamic tool discovery:spring-doc.cadn.net.cn

Tool Search Tool Flow
  1. 索引: 在对话开始时,所有注册的工具都会在ToolSearcher中进行索引(但不会发送给大语言模型)spring-doc.cadn.net.cn

  2. Initial Request: 只将工搜索工具定义发送至LLMspring-doc.cadn.net.cn

  3. 发现调用: 当LLM需要能力时,它会通过搜索工具发送查询spring-doc.cadn.net.cn

  4. spring-doc.cadn.net.cn

    搜索与扩展

    : The ToolSearcher finds matching tools and their definitions are added to the next request

    spring-doc.cadn.net.cn

  5. 工具调用: LLM现在能够识别并调用搜索工具和发现的工具定义spring-doc.cadn.net.cn

  6. 工具执行: 发现的工具被执行,结果被返回spring-doc.cadn.net.cn

  7. 响应: LLM生成最终答案spring-doc.cadn.net.cn

搜索策略

The ToolSearcher interface supports multiple search implementations:spring-doc.cadn.net.cn

策略模式 实现 最适合

语义spring-doc.cadn.net.cn

VectorToolSearcherspring-doc.cadn.net.cn

自然语言查询,模糊匹配spring-doc.cadn.net.cn

关键字spring-doc.cadn.net.cn

LuceneToolSearcherspring-doc.cadn.net.cn

精确术语匹配,已知工具名称spring-doc.cadn.net.cn

正则表达式spring-doc.cadn.net.cn

RegexToolSearcherspring-doc.cadn.net.cn

工具名称模式(get_*_dataspring-doc.cadn.net.cn

See 工具搜索者 查看所有可用实现。spring-doc.cadn.net.cn

性能

初步基准测试结果表明,使用28种工具显著节省了Tokens数量:spring-doc.cadn.net.cn

模型 基于工具搜索 没有 储蓄

双子座spring-doc.cadn.net.cn

2,165 tokensspring-doc.cadn.net.cn

5,375 个标记spring-doc.cadn.net.cn

60%spring-doc.cadn.net.cn

OpenAIspring-doc.cadn.net.cn

4,706 tokensspring-doc.cadn.net.cn

7,175个tokenspring-doc.cadn.net.cn

34%spring-doc.cadn.net.cn

人类的spring-doc.cadn.net.cn

### 中文翻译 6,273 tokensspring-doc.cadn.net.cn

一万七千三百四十二个Tokensspring-doc.cadn.net.cn

64%spring-doc.cadn.net.cn

当使用时

工具搜索工具方法论 传统方法

20+ 工具在您的系统中spring-doc.cadn.net.cn

小型工具库(共20个以下工具)spring-doc.cadn.net.cn

工具定义消耗了超过5千个Tokensspring-doc.cadn.net.cn

在每个会话中,经常使用的工具spring-doc.cadn.net.cn

构建基于 MCP 的多服务器系统spring-doc.cadn.net.cn

非常简洁的工具定义spring-doc.cadn.net.cn

体验工具选择准确性问题spring-doc.cadn.net.cn

示例项目

参考资料