当前位置: 首页 > wzjs >正文

网站程序 制作河北省企业网站建设公司

网站程序 制作,河北省企业网站建设公司,用jsp做的购物网站,长春推广公司目录 一、前言二、什么是 Tool Calling?三、項目示例1. 创建工具 Bean2. 启动配置3. 注入并使用 ToolCallingChatClient4. 发起调用 四、背后机制解析1. 自动生成 Function Schema2. 调用流程 五、进阶集成:多轮示例 六、总结七、参考 一、前言 LLM&…

在这里插入图片描述

目录

  • 一、前言
  • 二、什么是 Tool Calling?
  • 三、項目示例
    • 1. 创建工具 Bean
    • 2. 启动配置
    • 3. 注入并使用 ToolCallingChatClient
    • 4. 发起调用
  • 四、背后机制解析
    • 1. 自动生成 Function Schema
    • 2. 调用流程
  • 五、进阶集成:
    • 多轮示例
  • 六、总结
  • 七、参考


一、前言

LLM(大语言模型)不只是聊天工具,而是可以调用后端服务、控制系统行为的智能体核心。

Spring AI 借助 Tool Specification 机制与函数调用能力,让 Java Bean 中的函数变成 LLM 可调的“工具”。

本篇将带你掌握:

  • 如何声明 Java 方法为可调用 Tool
  • 如何注册工具并被 LLM 自动发现
  • 如何结合上下文与多轮调用,构建智能任务执行系统

二、什么是 Tool Calling?

Tool Calling 是指:

LLM 在对话过程中,自动选择调用某些外部工具(函数、API、数据库查询等),辅助完成任务。

在 Spring AI 中,只需使用 Spring Boot 3.4+ 与 spring-ai-openai-spring-boot-starter,就能快速构建支持 Tool Calling 的服务。

三、項目示例

这里我们实战构建 IT 助手 Agent

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>spring-ai-tool-calling-demo</artifactId><version>1.0.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.3</version></parent><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai</artifactId><version>1.0.0-SNAPSHOT</version></dependency></dependencies>
</project>

1. 创建工具 Bean

package com.example.toolai;import org.springframework.ai.function.annotation.AiFunction;
import org.springframework.ai.function.annotation.AiParam;
import org.springframework.stereotype.Component;@Component
public class ToolFunctions {@AiFunctionpublic Integer add(@AiParam("a") Integer a, @AiParam("b") Integer b) {return a + b;}@AiFunctionpublic String weather(@AiParam("city") String city) {return city + ": 30°C 晴天";}
}

2. 启动配置

application.yml 中加入配置:

spring:ai:openai:api-key: sk-xxxchat:options:model: gpt-4o

3. 注入并使用 ToolCallingChatClient

@Autowired
ToolFunctions tools;@Autowired
OpenAiChatClient baseClient;FunctionCallingChatClient chatClient = new FunctionCallingChatClient(baseClient, List.of(tools));

4. 发起调用


package com.example.toolai;import org.springframework.ai.chat.client.*;
import org.springframework.ai.chat.memory.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.List;@SpringBootApplication
public class ToolCallingApp {public static void main(String[] args) {SpringApplication.run(ToolCallingApp.class, args);}@Beanpublic CommandLineRunner run(OpenAiChatClient baseClient, ToolFunctions tool, ServerOps ops) {return args -> {ChatMemory memory = new InMemoryChatMemory("session-001");FunctionCallingChatClient chatClient = new FunctionCallingChatClient(baseClient, List.of(tool, ops), memory);String userInput = "请问北京的天气是多少度?";ChatResponse response = chatClient.call(userInput);System.out.println("🧠 Response: " + response.getResult().getOutput().getContent());userInput = "那我明天适合跑步吗?";response = chatClient.call(userInput);System.out.println("🧠 Response: " + response.getResult().getOutput().getContent());userInput = "重启一下 CRM 服务";response = chatClient.call(userInput);System.out.println("🧠 Response: " + response.getResult().getOutput().getContent());};}
}

LLM 会自动根据描述调用 weather(city="北京")


四、背后机制解析

1. 自动生成 Function Schema

每个 @AiFunction 都会自动转换为 JSON 格式的 OpenAI 工具定义:

{"type": "function","function": {"name": "add","parameters": {"type": "object","properties": {"a": {"type": "integer"},"b": {"type": "integer"}},"required": ["a", "b"]}}
}

2. 调用流程

在这里插入图片描述


五、进阶集成:

可以结合 Memory 实现上下文追踪

Spring AI 提供 ChatMemory 接口,可保存历史上下文,如下例所示。

ChatMemory memory = new InMemoryChatMemory("session-001");
FunctionCallingChatClient chatClient = new FunctionCallingChatClient(baseClient, List.of(tools), memory);

多轮示例

用户:北京天气如何?

用户:那我明天适合跑步吗?(使用上轮结果)


六、总结

通过 Tool Calling,Spring AI 让你只用一个注解,就能将本地方法暴露为 LLM 的智能工具。

企业可以基于此快速构建数据接口封装、流程驱动服务、自动化运维系统等。

七、参考

《Java驱动AI革命:Spring AI八篇进阶指南——从架构基础到企业级智能系统实战》
在这里插入图片描述


文章转载自:

http://JOY3tE6O.rqLzz.cn
http://QJkOvfFV.rqLzz.cn
http://nXowEMBK.rqLzz.cn
http://BN5qmcln.rqLzz.cn
http://0lDmn33D.rqLzz.cn
http://gHctuS8L.rqLzz.cn
http://pdnIauOI.rqLzz.cn
http://atR89hvh.rqLzz.cn
http://WrJC8d5w.rqLzz.cn
http://3EkY4GhH.rqLzz.cn
http://6l0fmFk7.rqLzz.cn
http://41tMRhCQ.rqLzz.cn
http://c65J6cXx.rqLzz.cn
http://SqnC2oKY.rqLzz.cn
http://mwNM7n3c.rqLzz.cn
http://DfkRu0Zy.rqLzz.cn
http://mJvOow1p.rqLzz.cn
http://VDgQGtpw.rqLzz.cn
http://46Cx1AnR.rqLzz.cn
http://FoADQvq3.rqLzz.cn
http://y1C9doEV.rqLzz.cn
http://gKVYhSaI.rqLzz.cn
http://TQvab3vb.rqLzz.cn
http://d7tLFMqg.rqLzz.cn
http://Ta18UFbn.rqLzz.cn
http://d6YLnuGG.rqLzz.cn
http://clsrnqCY.rqLzz.cn
http://371XxBXj.rqLzz.cn
http://xaaDqSIw.rqLzz.cn
http://X8oLitkb.rqLzz.cn
http://www.dtcms.com/wzjs/727923.html

相关文章:

  • ps做汽车网站下载地址杭州网站建设推广公司
  • 手机上有趣的网站做社交网站 投入
  • 网站建设费用选网络专业linux怎么下载wordpress
  • vs做网站通过e浏览器海会网络做的网站怎么做优化
  • 人力招聘网站建设任务执行书php免费网站模板
  • 网站开发 托管合同上海本地生活的网站
  • 仿帝国网站源码网站建设策划实训总结
  • 部门网站建设多少钱网站设计依赖于什么设计
  • dw做网站怎么跳转郑州专业旅游网站建设
  • 网站建设和考核工作通知centos 6 wordpress
  • 长沙建设局网站腾讯网页版wordpress
  • 青岛城阳做网站阿里万网站建设
  • 杭州网站推广与优化做今网站
  • 网站后台改前台不变无极门户网站
  • 网站建设中源码下载重庆北碚网站制作
  • 网站建设公司客户分析网站做一年了没做301
  • 最专业微网站首选公司收费网站怎么做
  • 广州市网站制作黄山旅游必去十大景点
  • 可以做动画的网站yp77731域名查询
  • 外贸婚纱网站王烨重生
  • 东莞贸易公司寮步网站建设价格泰安网站营销推广
  • 网站开发中心做网站代理需要办什么营业执照
  • ps个人网站设计总结易企推
  • php网站怎么做谷粉搜索谷歌搜索
  • 网站页面头部设计说明网站app充值记账凭证怎么做
  • 怎样做金融理财网站营销型网站建设 案例
  • 江门网站建设策划面试网站开发
  • 网站建设架免费发布信息平台有哪些
  • 苏宁易购网站上的营销页面免费下载app软件网站
  • 连云港网站seo重庆广告公司网站建设