文章目录
- 前言
- 测试环境
- 项目构建
- 依赖引入
- 指定openai 相关配置
- 基于 application.yml 配置 Open AI 属性
- application.yml
- 编写测试类
- 测试请求
- 基于读取后配置请求
- 编写测试接口
- 测试效果展示
- 流式输出
前言
AI 技术越来越火爆,作为Java开发人员也不能拖了后腿。
前段时间使用LangChain4j
也写了一些技术博客,实现了比如一些基础对话功能
、流式输出功能
、多轮对话
等。但在尝试进行MCP
操作的时候,总感觉MCP Server
部分不能很好的独立出来,就像一个Function Call
一样,很别扭。尝试使用了Spring AI
,效果还行。
本篇博客依旧从最基本的Springboot 项目整合 Spring AI实现简单对话,流式输出开始,逐步说明怎么去玩 Spring AI。
测试环境
- jdk 17
- maven 3.6.3
- springboot 3.4.0
- spring-ai-bom 1.0.0-M6
项目构建
依赖引入
测试使用的是spring-ai-openai
,但经济受限,没有去购买国外的KEY
。但DeepSeek
天然支持Open AI
的格式。
本次测试依赖导入:
<properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-ai.version>1.0.0-M6</spring-ai.version>
</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 封装了各大模型的交互接口 --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency><!-- 用于各大模型进行自动装配 --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring-ai.version}</version><type>pom</type><!-- 父maven项目,由于已经引入 springboot-parent,此处只能这种方式引入 --><scope>import</scope></dependency></dependencies>
</dependencyManagement>
因为项目是基于Spring boot
构建的,项目创建的时候已经引入了一个spring-boot-dependencies
的父级依赖,无法再引入一个。而spring-ai-bom
也是一个父级框架,所以采取了dependencyManagement
的方式引入。