SpringAI结合SpringBoot图像生成功能实现
技术要求
技术要求与上篇一致:http://t.csdnimg.cn/NRBWa
工程整体结构
开发流程
- 创建工程,添加依赖与上文一致(省略)
- pom文件中详情
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--SpringBoot3.x父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- Generated by https://start.springboot.io -->
<!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
<groupId>cn.aopmin</groupId>
<artifactId>ai-images</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ai-images</name>
<description>ai-images</description>
<properties>
<!--jdk的编译版本-->
<java.version>17</java.version>
<!--锁定spring-ai的版本-->
<spring-ai.version>0.8.1</spring-ai.version>
</properties>
<dependencies>
<dependency>
<!--springbootweb开发起步依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring ai 起步依赖-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<!--热部署插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--s相当于是继承一个父项目:spring-ai-bom父项-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<!--配置本项目的仓库,因为maven中央仓库没有spring-ai的jar包-->
<repositories>
<!--里程碑版本的仓库-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 配置YAML文件
spring:
application:
name: ai-images
ai:
openai:
api-key: sk-xxxxxxxxxxxxxxxxxxxxx(在购买时问一下是否支持图片的生成)
base-url: https://xxx.xx.xx
chat:
options:
# model: gpt-3.5-turbo # gpt模型(模型是支持4.0的哦)
temperature: 0.9 # 控制随机性,值越大,生成的句子越多样化
server:
port: 1235
- 创建ImageController
package com.test.ai.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageClient;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/image")
@Slf4j
@RequiredArgsConstructor
public class ImageController {
private final OpenAiImageClient openAiImageClient;
/**
* 测试生成图片
*
* @param msg 提问的问题
* @return 生成图片
*/
@RequestMapping("/ai/images")//画一张世界和平图
public Object chat(@RequestParam(value = "msg", defaultValue = "画一张世界和平图") String msg) {
ImageResponse call = openAiImageClient.call(new ImagePrompt(msg));
log.info("Image response: {}", call.toString());
return call.getResult().getOutput();
}
/**
* 测试生成图片
*
* @param msg 提问的问题
* @return 生成图片
*/
@RequestMapping("/ai/images2")//画一张世界和平图
public Object chat2(@RequestParam(value = "msg", defaultValue = "画一张世界和平图") String msg) {
ImageResponse call = openAiImageClient.call(new ImagePrompt(msg, OpenAiImageOptions.builder()
.withQuality("hd") //图片的质量,hd表示高清
.withN(1) //图片生成的张数
.withHeight(1024)
.withWidth(1024).build()));
;
log.info("Image response: {}", call.toString());
return call.getResult().getOutput();
}
}
- 测试(图片生成,返回的是一个地址)
在不穿参数的情况下返回的图片
结语:SpringAI的小案例,希望可以一起学习,有问题可以私信或者留言!!!