Spring AI总结

一. Spring AI的设计

Spring AI 抽象了一层:

之前Java访问不同的大模型需要单独写适配的API接口,现在有了spring AI可以使用一套统一的API接口,换模型,不再需要修改java代码,只需要修改配置即可。

chatClient

.prompt()

.user("你好")

.call()

.content();

例如:

  1. spring:

ai:

deepseek:

api-key: xxx

chat:

options:

model: deepseek-chat

  1. spring:

ai:

openai:

api-key: xxx

chat:

options:

model: gpt-4o-mini

一句话总结:spring AI的优势,换大模型通常只需要换配置,不需要改业务代码。

二. 在企业项目中怎么选

三. Spring AI的Demo搭建流程

  1. 引入依赖

Spring Boot 3.x + Spring AI 1.x

<dependencies>

<!-- Spring Web -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- Spring AI OpenAI -->

<dependency>

<groupId>org.springframework.ai</groupId>

<artifactId>spring-ai-starter-model-openai</artifactId>

</dependency>

</dependencies>

  1. 配置秘钥API Key application.yml

spring:

ai:

openai:

api-key: sk-xxxxxx

chat:

options:

model: gpt-4o-mini

  1. 创建 ChatClient

package com.itcast.springaiparent;

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

import org.springframework.stereotype.Service;

@Service

public class AiService {

private final ChatClient chatClient;

public AiService(ChatClient.Builder builder){

this.chatClient =

builder.build();

}

public String chat(String message){

return chatClient

.prompt()

.user(message)

.call()

.content();

}

}

  1. 写 Controller 测试

package com.itcast.springaiparent;

import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/ai")

public class AiController {

private final AiService aiService;

public AiController(AiService aiService){

this.aiService = aiService;

}

@GetMapping("/chat")

public String chat(

@RequestParam String msg

){

return aiService.chat(msg);

}

}

四. Function Calling(工具调用)

  1. 项目结构
  1. 定义天气Tool

关键就是 @Tool 注解。

复制代码
@Component
public class WeatherTool {
    /**
     * @Tool:让模型能真正调用这个工具
     * @param city
     * @return
     */
    @Tool(description = "根据城市名称查询天气")
    public String getWeather( @ToolParam(description = "城市名称,例如:北京")String city) {
        return city + "今天晴,气温 25℃";
    }
}

description告诉 AI:我这里有一个工具,可以根据城市查询天气。

  1. 创建 ChatClient

package com.example.service;

import com.example.tool.WeatherTool;

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

import org.springframework.stereotype.Service;

@Service

public class AiService {

private final ChatClient chatClient;

public AiService(

ChatClient.Builder builder,

WeatherTool weatherTool

){

this.chatClient =

builder

.defaultTools(weatherTool)

.build();

}

public String chat(String message){

return chatClient

.prompt()

.user(message)

.call()

.content();

}

}

关键代码:

.defaultTools(weatherTool)

告诉 AI: 这个 Java 对象里面的方法,你可以调用。/

五. Controller测试

@RestController

@RequestMapping("/ai")

public class AiController {

private final AiService aiService;

public AiController(AiService aiService){

this.aiService=aiService;

}

@GetMapping("/chat")

public String chat(

String msg

){

return aiService.chat(msg);

}

}

六. 如果有多个Tool,大模型来根据 需求决定调用哪个

@Component

public class OrderTool {

@Tool(description="查询订单状态")

public String queryOrder(String orderId){

return "订单已发货";

}

}

然后:

.defaultTools(

weatherTool,

orderTool

)

Function Calling本质

以前:

用户

|

你的代码判断

|

if(天气)

调天气接口

if(订单)

查订单

现在:

用户

|

LLM

|

判断意图

|

选择Tool

|

执行Java方法

|

返回答案

五. Function Calling和MCP区别?

Function Calling MCP
工具在哪里 你的Java代码 独立服务
标准 模型厂商各自实现 统一协议
适合 单体应用 企业级Agent
Spring AI 支持 支持

六. RAG的实现原理

实现功能:

用户问:"公司的请假制度是什么?"

AI 不知道答案

→ 去知识库搜索

→ 找到《员工手册.pdf》

→ 把相关内容交给 GPT

→ AI 根据文档回答

1. RAG的整体工作流程

第一次准备

PDF / Word / txt

|

|

v

Document Reader

|

|

v

Text Splitter

|

|

v

Embedding模型

|

|

v

Vector DB

(向量数据库)

用户提问

用户:

"公司年假多少天?"

|

v

Embedding问题向量

|

v

Vector DB搜索相似内容

|

v

找到相关文档片段

|

v

Prompt增强

|

v

LLM回答

2. 技术选型

组件 技术
Spring AI 1.x
LLM OpenAI / Ollama
Embedding OpenAI / BGE
向量库 Chroma
文档 txt

3. 添加依赖

<dependencies>

<!-- Spring Web -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- Spring AI OpenAI -->

<dependency>

<groupId>org.springframework.ai</groupId>

<artifactId>spring-ai-starter-model-openai</artifactId>

</dependency>

<!-- 向量数据库 Chroma -->

<dependency>

<groupId>org.springframework.ai</groupId>

<artifactId>

spring-ai-starter-vector-store-chroma

</artifactId>

</dependency>

</dependencies>

  1. 配置

spring:

ai:

openai:

api-key: sk-xxxx

chat:

options:

model: gpt-4o-mini

vectorstore:

chroma:

client:

host: localhost

port: 8000

4. 加载文档进入向量数据库

@Component

public class DataLoader {

private final VectorStore vectorStore;

public DataLoader(

VectorStore vectorStore

){

this.vectorStore=vectorStore;

}

@PostConstruct

public void load(){

Resource resource =

new ClassPathResource(

"docs/company.txt"

);

TextReader reader =

new TextReader(resource);

List<Document> documents =

reader.get();

vectorStore.add(documents);

}

}

5. 创建 RAG 查询

@Service

public class RagService {

private final ChatClient chatClient;

public RagService(

ChatClient.Builder builder,

VectorStore vectorStore

){

this.chatClient =

builder

.defaultAdvisors(

new QuestionAnswerAdvisor(

vectorStore

)

)

.build();

}

public String ask(String question){

return chatClient

.prompt()

.user(question)

.call()

.content();

}

}

重点代码:

.defaultAdvisors(

new QuestionAnswerAdvisor(vectorStore)

)

问题

|

向量搜索

|

找文档

|

拼接Prompt

|

调用LLM

相关推荐
JAVA面经实录9171 小时前
Java高级后端 · 全套面试通关手册(线上故障排查)
java·jvm·面试
IT_Octopus1 小时前
【零基础入门 LLM 开发 · Day 11】:ChatOpenAI vs init_chat_model——一行换供应商
java·前端·javascript
敲代码的乔帮主1 小时前
MokioMind 各训练阶段数据样本对比
人工智能·深度学习·机器学习
做萤石二次开发的哈哈1 小时前
网络音箱如何接入?音频文件夹、TTS播报、音量管控与ISAPI透传对接详解
人工智能·物联网·蓝海aiot一站式工作台·aiot开发·网络音箱·智慧广播·二次开放
镭封1 小时前
做电视剧解说视频用什么软件?配音、文案处理一次解决
人工智能·小程序·音视频·语音识别·媒体
欣欣之王来了1 小时前
国外主流大模型深度对比:GPT、Claude、Gemini技术细节与实战选型指南
人工智能·ai·大模型
Wang's Blog1 小时前
Java 中间件之 RabbitMQ 快速入门: MQ 常见技术选型对比
java·中间件·java-rabbitmq
蜗牛互联网1 小时前
Java Agent 工具调用的 allowlist、参数校验与调用预算
java·开发语言·人工智能·后端·oracle
大文说跨境1 小时前
多账号环境隔离方案技术选型:指纹浏览器、VPS 与云手机的三种架构对比
java·开发语言·前端
MicrosoftReactor1 小时前
技术速递|使用 GitHub Security Lab Taskflow Agent 实现 AI 驱动的模糊测试
人工智能·ai·github·copilot·agent·模糊测试·ai-agent