Spring AI简介
Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI,并推广使用 POJO 作为 AI 领域应用程序的构建块。 特征:跨AI 提供商的可移植 API 支持,适用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能。
聊天模型:OpenAI、Azure Open AI、Amazon Bedrock、Cohere’s Command(AI21 Labs’ Jurassic-2、Meta’s LLama 2、Amazon’s Titan)、Google Vertex AI Palm、Google Gemini、HuggingFace (access thousands of models, including those from Meta such as Llama2)、Ollama(run AI models on your local machine)、MistralAI …
创建Spring AI项目
打开IDEA创建一个新的spring boot项目,填写项目名称和位置,类型选择maven,组、工件、软件包名称可以自定义,JDK选择17+即可,java语言标准和JDK相同即可 配置Spring Boot版本和开发所需的依赖,主要如下图所示
Spring Boot版本可以选择3.2.5或者更高的版本(作者使用3.2.5和3.2.6(SNAPSHOT)可以正常开发) Spring Boot DevTools:spring项目热部署工具,修改完代码(不含application和pom配置文件)即刻热部署项目 Lombok:通过配置快速配置对象的get、set、toString Spring AI:Spring AI是一个用于AI工程的应用框架 创建完成后,项目结构大体如下(这里删除了无用的maven文件内容、修改application的文件格式为yaml)
配置项目pom和application文件
注意:修改pom文件,重新下载spring ai依赖需要科学上网,请确保网络连接没有问题
打开项目的pom文件,修改spring ai的版本(项目默认使用稳定版0.8.1)
主要注意默认的spring ai版本和配置依赖jar包仓库(maven仓库中还没有spring ai的依赖)
< properties>
< java.version> 21</ java.version>
< spring-ai.version> 0.8.1</ spring-ai.version>
</ properties>
< repositories>
< repository>
< id> spring-snapshots</ id>
< name> Spring Snapshots</ name>
< url> https://repo.spring.io/snapshot</ url>
< releases>
< enabled> false</ enabled>
</ releases>
</ repository>
< repository>
< id> spring-milestones</ id>
< name> Spring Milestones</ name>
< url> https://repo.spring.io/milestone</ url>
< snapshots>
< enabled> false</ enabled>
</ snapshots>
</ repository>
</ repositories>
< pluginRepositories>
< pluginRepository>
< id> spring-snapshots</ id>
< name> Spring Snapshots</ name>
< url> https://repo.spring.io/snapshot</ url>
< releases>
< enabled> false</ enabled>
</ releases>
</ pluginRepository>
</ pluginRepositories>
配置application文件(api-key的获取参考Spring AI开发前期开发指导 )
spring:
application:
name: ChatImage
ai:
openai:
api-key: hk-xxx
base-url: https://api.openai-hk.com #请根据自己的api-key自定义配置
server:
port: 8081
controller接口开发
import jakarta. annotation. Resource ;
import org. springframework. ai. image. ImageOptionsBuilder ;
import org. springframework. ai. image. ImagePrompt ;
import org. springframework. ai. image. ImageResponse ;
import org. springframework. ai. openai. OpenAiImageClient ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestParam ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
public class ImageController {
@Resource
private OpenAiImageClient openAiImageClient;
@GetMapping ( "/ai/draw" )
public String drawImage ( @RequestParam ( value = "msg" ) String msg) {
ImageResponse response = openAiImageClient. call ( new ImagePrompt ( msg,
ImageOptionsBuilder
. builder ( )
. withModel ( "dall-e-3" )
. withN ( 1 )
. withWidth ( 1024 )
. withHeight ( 1024 )
. build ( )
)
) ;
return response. getResult ( ) . getOutput ( ) . getUrl ( ) ;
}
}
运行测试
http://localhost:8081/ai/draw?msg= 请画一幅顶级程序员的日常开发场景
http://localhost:8081/ai/draw?msg= 请画一幅中国考研大学生的精神状态