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

Spring AI 入门学习指南

一: Spring AI 是什么?

1.1:简介

       Spring AI 是 Spring 团队推出的 AI 应用开发框架,它把大模型能力封装成 Spring Boot 风

,让我们像使用 RestTemplateJdbcTemplate 一样调用 LLM(大语言模型)

简单理解:
👉 Spring AI = Spring Boot + AI SDK + Prompt/RAG 封装

1.2:定位

大模型调用框架:支持 OpenAI、Ollama、Azure OpenAI、HuggingFace 等

Spring Boot 风格:配置驱动,提供 ChatClientPromptTemplate 等常用 API

应用层封装:支持 Prompt 模板、向量数据库(RAG)、结构化输出

1.3:对比 Python 的生态

Python 有 LangChain

Java 生态就有 Spring AILangChain4j

二:环境准备

2.1:依赖配置

springAi依赖

<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>1.0.0-M2</version>
</dependency>

如果你想用本地模型(Ollama):

<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId><version>1.0.0-M2</version>
</dependency>

2.2:配置文件

application.yml 里配置 API Key 或模型:

spring:ai:openai:api-key: sk-xxxx   # 你的 OpenAI Keybase-url: https://api.openai.com/v1

本地运行 Ollama:

spring:ai:ollama:base-url: http://localhost:11434model: llama3

三:快速上手:第一个 AI 接口

Spring AI 提供了 ChatClient,我们只需要注入即可使用

@RestController
@RequiredArgsConstructor
public class ChatController {private final ChatClient chatClient;@GetMapping("/chat")public String chat(@RequestParam String msg) {return chatClient.prompt().user(msg).call().content();}
}

启动项目后访问:

http://localhost:8080/chat?msg=你好

就能得到模型的回复

四:Spring AI 核心功能

4.1:Prompt 模板

支持占位符和变量替换:

@GetMapping("/summary")
public String summarize(@RequestParam String text) {String template = "请用一句话总结以下文本: {input}";return chatClient.prompt().user(template, Map.of("input", text)).call().content();
}

4.2:结构化输出

让大模型输出 JSON,Spring AI 会帮你转成 Java 对象:

@Data
public class WeatherInfo {private String location;private String forecast;private int temperature;
}@GetMapping("/weather")
public WeatherInfo getWeather(@RequestParam String place) {return chatClient.prompt().user("请提供 {place} 的天气情况", Map.of("place", place)).call().entity(WeatherInfo.class);
}

4.3 向量数据库 + RAG

Spring AI 内置了 VectorStore,可接入 Milvus、PgVector、Redis 等

@Autowired
private VectorStore vectorStore;@PostConstruct
public void init() {vectorStore.add(List.of(new Document("Spring 是一个流行的 Java 框架"),new Document("Spring AI 支持 OpenAI、Ollama 等模型")));
}@GetMapping("/ask")
public String ask(@RequestParam String question) {return chatClient.prompt().user(question).options(ChatOptions.builder().withDocuments(vectorStore.similaritySearch(question)).build()).call().content();
}

这样就实现了 基于知识库的问答

五:资料

官方文档:Spring AI
GitCode 示例:右击代码仓

http://www.dtcms.com/a/343093.html

相关文章:

  • 2025.8.21总结
  • CMake使用【c/c++】
  • 2025Java面试红皮书:1000道BAT真题详解
  • plc与plc无线通讯实现PLC1200和ET200SP无线通讯解决方案实践
  • uniapp 懒加载图片
  • 力扣面试150(62/150)
  • SAP FIORI Elements深度定制:注解扩展与审批流程增强完全指南
  • 软件工程 + AI 不是 “硬凑”,3 步走通落地关键环节
  • es6新语法
  • LLaVA-3D,Video-3D LLM,VG-LLM,SPAR论文解读
  • MySQL 时间筛选避坑指南:为什么格式化字符串比较会出错?
  • LMAD:用于可解释自动驾驶的集成端到端视觉-语言模型
  • 自动驾驶架构:人为接口与隐式特征的博弈
  • 杰里708n tws api 简介
  • K-Means 聚类算法详解与实战指南
  • QPS 每秒查询数
  • openEuler系统中如何将docker安装在指定目录
  • Qt5网络编程详细讲解
  • 僵尸进程和孤儿进程
  • Spring相关知识
  • 解决接口耗时长问题
  • 软考 系统架构设计师系列知识点之杂项集萃(130)
  • 上证50股指期货为何波动很小?
  • AP状态管理中提到的两种“业务逻辑”
  • 34、扩展仓储管理系统 (跨境汽车零部件模拟) - /物流与仓储组件/extended-warehouse-management
  • 家用电器,让现代家庭生活更美好
  • 华为云ModelArts+Dify AI:双剑合璧使能AI应用敏捷开发
  • 红日靶场5
  • 有鹿机器人:智慧清洁新时代的引领者
  • 今天,字节开源Seed-OSS-36B模型,512k上下文