
目录
一、前言
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

最低0.47元/天 解锁文章
2539

被折叠的 条评论
为什么被折叠?



