最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用SpringAI整合Ollama實(shí)現(xiàn)工具鏈調(diào)用功能

 更新時(shí)間:2026年05月20日 08:43:34   作者:霸道流氓氣質(zhì)  
文章介紹了SpringAI的ToolCalling能力,允許大模型自主調(diào)用外部工具以完成多步驟任務(wù),具體而言,通過@Tool注解標(biāo)記方法,SpringAI自動解析生成JSONSchema,@Tools注解注冊多個(gè)工具,模型可自主選擇調(diào)用順序和組合,需要的朋友可以參考下

場景

Spring AI 提供了強(qiáng)大的 Tool Calling 能力,允許大模型在對話中自主決定調(diào)用哪些外部工具,

并自動組合它們的執(zhí)行順序。

這種機(jī)制讓智能體能夠完成復(fù)雜的多步驟任務(wù),例如“查詢北京天氣并用英文回答”

——模型會先調(diào)用天氣工具獲取數(shù)據(jù),再調(diào)用翻譯工具輸出英文。

核心概念

概念說明
Tool Calling大模型根據(jù)用戶問題,生成函數(shù)調(diào)用請求(含函數(shù)名和參數(shù)),Spring AI 攔截后執(zhí)行對應(yīng)的 Java 方法,將結(jié)果返回模型。
@Tool 注解標(biāo)記在方法上,Spring AI 自動解析方法簽名和注釋,生成 JSON Schema 供模型理解。
多工具注冊通過 defaultTools(...) 同時(shí)注冊多個(gè)工具,模型可自主選擇調(diào)用順序和組合。
鏈?zhǔn)秸{(diào)用模型在一次響應(yīng)中先后調(diào)用多個(gè)工具,前一個(gè)的輸出作為后一個(gè)的輸入,形成“鏈”。

實(shí)現(xiàn)

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.3</version> <!-- 降級為穩(wěn)定版,解決沖突 -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-ai-ollama-tool-chain</artifactId>
    <version>1.0</version>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.1.2</spring-ai.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring AI Ollama 核心 -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-ollama</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
    </dependencies>

application.yml

?
server:
  port: 886
spring:
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        model: qwen2.5:7b-instruct
        options:
          temperature: 0.7
          num-ctx: 4096                         # 上下文窗口大小
logging:
  level:
    org.springframework.ai.chat.client: DEBUG   # 查看工具調(diào)用詳情
?

天氣工具

package com.badao.ai.tools;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

@Component
public class WeatherTool {

    @Tool(name = "get_weather", description = "查詢指定城市的實(shí)時(shí)天氣")
    public String getWeather(@ToolParam(description = "城市名稱") String city) {
        System.out.println("調(diào)用了天氣工具");
        // 模擬天氣數(shù)據(jù)
        return String.format("%s當(dāng)前天氣:晴,溫度22℃,濕度45%%。", city);
    }
}

翻譯工具

package com.badao.ai.tools;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

@Component
public class TranslateTool {

    @Tool(name = "translate_to_english", description = "將中文文本翻譯成英文")
    public String translate(@ToolParam(description = "待翻譯的中文文本") String text) {
        System.out.println("調(diào)用了翻譯工具");
        // 模擬翻譯,實(shí)際可接入翻譯API
        return "Translated: " + text + " (This is the English version.)";
    }
}

工具注冊配置類

package com.badao.ai.config;

import com.badao.ai.tools.WeatherTool;
import com.badao.ai.tools.TranslateTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ToolConfig {

    @Bean
    public ChatClient chatClient(ChatModel chatModel,
                                 WeatherTool weatherTool,
                                 TranslateTool translateTool) {
        return ChatClient.builder(chatModel)
                .defaultTools(weatherTool, translateTool)   // 注冊天氣和翻譯工具
                .build();
    }
}

Agent 服務(wù)層

package com.badao.ai.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;

@Service
public class AgentService {

    private final ChatClient chatClient;

    public AgentService(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    public String ask(String question) {
        return chatClient.prompt()
                .user(question+ "(請先用天氣工具,再用翻譯工具)")
                .call()
                .content();
    }
}

控制器

package com.badao.ai.controller;

import com.badao.ai.service.AgentService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class AgentController {

    private final AgentService agentService;

    public AgentController(AgentService agentService) {
        this.agentService = agentService;
    }

    @PostMapping("/agent")
    public String ask(@RequestBody String question) {
        return agentService.ask(question);
    }
}

測試

測試單工具

測試工具鏈

以上就是使用SpringAI整合Ollama實(shí)現(xiàn)工具鏈調(diào)用功能的詳細(xì)內(nèi)容,更多關(guān)于SpringAI Ollama工具鏈調(diào)用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼

    Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼

    這篇文章主要介紹了Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 關(guān)于@PostConstruct、afterPropertiesSet和init-method的執(zhí)行順序

    關(guān)于@PostConstruct、afterPropertiesSet和init-method的執(zhí)行順序

    這篇文章主要介紹了關(guān)于@PostConstruct、afterPropertiesSet和init-method的執(zhí)行順序,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring?data?jpa緩存機(jī)制使用總結(jié)

    Spring?data?jpa緩存機(jī)制使用總結(jié)

    這篇文章主要介紹了Spring?data?jpa緩存機(jī)制使用總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • RestTemplate實(shí)現(xiàn)發(fā)送帶headers的GET請求

    RestTemplate實(shí)現(xiàn)發(fā)送帶headers的GET請求

    這篇文章主要介紹了RestTemplate實(shí)現(xiàn)發(fā)送帶headers的GET請求,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中ArrayList集合的常用方法大全

    Java中ArrayList集合的常用方法大全

    這篇文章主要給大家介紹了關(guān)于Java中ArrayList集合的常用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • java性能優(yōu)化之分代回收

    java性能優(yōu)化之分代回收

    這篇文章主要介紹了java性能優(yōu)化之分代回收,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Java工具jsch.jar實(shí)現(xiàn)上傳下載

    Java工具jsch.jar實(shí)現(xiàn)上傳下載

    這篇文章主要為大家詳細(xì)介紹了Java操作ftp的一款工具,利用jsch.jar針對sftp的上傳下載工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Spring Security將用戶數(shù)據(jù)存儲到數(shù)據(jù)庫的方法

    Spring Security將用戶數(shù)據(jù)存儲到數(shù)據(jù)庫的方法

    這篇文章主要介紹了Spring Security將用戶數(shù)據(jù)存儲到數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringMvc中的Bean加載機(jī)制詳解

    SpringMvc中的Bean加載機(jī)制詳解

    這篇文章主要介紹了SpringMvc中的Bean加載機(jī)制詳解,在Spring MVC中,Bean的作用主要是處理應(yīng)用程序的業(yè)務(wù)邏輯和數(shù)據(jù),例如,一個(gè)用戶管理應(yīng)用程序的Bean可能包括UserService、UserDao和UserController等,需要的朋友可以參考下
    2023-12-12
  • java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問題

    java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問題

    這篇文章主要為大家介紹了java圖論弗洛伊德算法和迪杰斯特拉算法解決最短路徑的問題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11

最新評論

永德县| 陕西省| 义乌市| 长乐市| 古交市| 金溪县| 城固县| 柞水县| 溧阳市| 长兴县| 进贤县| 贵港市| 蒲江县| 百色市| 枣阳市| 宁阳县| 武穴市| 凤山县| 罗定市| 济南市| 黑水县| 蓝山县| 孝感市| 嫩江县| 博野县| 甘泉县| 浦北县| 邳州市| 杂多县| 安塞县| 永春县| 溧水县| 错那县| 江永县| 榆社县| 西宁市| 安化县| 大同县| 宜阳县| 阿巴嘎旗| 秦安县|