掘金 人工智能 05月23日 11:23
Spring AI文生图入门示例
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文详细介绍了如何使用Spring Boot框架集成Spring AI,并调用阿里云百炼大模型实现“文生图”功能。通过一个简单的Hello World示例,展示了从环境准备、依赖配置、代码编写到启动测试的完整流程。核心在于利用Spring AI提供的接口和类,简化了与阿里云百炼大模型AI服务的交互,让开发者能够快速集成文生图功能,体验AI图像生成技术。

💡 准备工作:在开始之前,需要确保JDK版本为17及以上,SpringBoot版本为3.x以上,并开通阿里云百炼大模型服务,获取API-KEY。API-KEY是后续代码中调用阿里云服务的关键。

📦 依赖配置:在pom.xml文件中,需要添加spring-boot-starter-web和spring-ai-alibaba-starter依赖。如果spring-ai相关依赖包未发布到中央仓库,则需在pom.xml中添加Spring Milestones仓库配置。

⚙️ 配置yml文件:在application-dev.yml文件中,配置server端口和spring应用名称,并在ai配置下添加dashscope的api-key,将API-KEY替换成从阿里云百炼平台申请的key。

🖼️ 创建ImageController:通过注入ImageModel实例,调用ImageModel API实现“文生图”的交互过程。该类接收文本提示,调用模型生成图片。ImageModel的入参为ImagePrompt,输出类型为ImageResponse。

🚀 启动与测试:启动Spring Boot工程后,在浏览器中输入指定地址,即可验证AI大模型文生图功能,查看生成的图像。

本文通过一个hello world简单示例演示,如何基于Spring Boot集成Spring AI开源框架,快速集成调用阿里云的百炼大模型AI服务,验证AI大模型“文生图”的功能。

在Spring AI框架中,文生图(Text-to-Image)功能允许开发者通过文本描述自动生成图像。这一特性依赖于预训练的生成模型,这些模型能够根据给定的文本提示创建视觉内容。为了实现这一目标,Spring AI提供了相应的接口和类来简化与这些模型的交互过程。

1、使用Spring AI 前提条件

2、创建SpringBoot工程

通过IDEA开发工具,创建一个普通的Java工程,注意JDK版本至少是17以上,我这里使用的是jdk21版本。

3、配置pom.xml文件

工程创建完成后,在pom.xml里添加依赖。

首先,需要在项目中添加 spring-boot-starter-parent声明,指定springboot框架的版本号:

org.springframework.boot

spring-boot-starter-parent

3.3.3

在dependencies标签中引入spring-boot-starter-web和spring-ai-alibaba-starter依赖:

org.springframework.boot spring-boot-starter-web

com.alibaba.cloud.ai

spring-ai-alibaba-starter

1.0.0-M3.3

由于 spring-ai 相关依赖包还没有发布到中央仓库,如出现 spring-ai-core 等相关依赖解析问题,请在您项目的 pom.xml 依赖中加入如下仓库配置。

spring-milestones

Spring Milestones

repo.spring.io/milestone

false

最后pom.xml文件完整内容如下:

\ 4.0.0

com.yuncheng\ spring-ai-helloworld\ 1.0-SNAPSHOT

21 21 UTF-8 org.springframework.boot spring-boot-starter-parent 3.3.3 org.springframework.boot spring-boot-starter-web com.alibaba.cloud.ai spring-ai-alibaba-starter 1.0.0-M3.3 spring-milestones Spring Milestones https://repo.spring.io/milestone false

4、配置yml文件

在工程的resources目录下的application-dev.yml文件里增加如下配置:

注意:api-key要替换成自己从阿里百炼平台申请的key

server:
port: 8080

spring:
application:
name: spring-ai-helloworld
ai:
dashscope:
api-key: sk-b90ad31bb3eb4a1585251928356d39dc5

5、创建ImageController

Spring AI框架的ImageModel API 抽象了应用程序通过模型调用实现“文生图”的交互过程,即应用程序接收文本,调用模型生成图片。ImageModel 的入参为包装类型ImagePrompt,输出类型为ImageResponse。

package com.yuncheng;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ai")
public class ImageController {
private final ImageModel imageModel;
private static final String DEFAULT_PROMPT = "为人工智能生成一张富有科技感的图片!";

public ImageController(ImageModel imageModel) {
this.imageModel = imageModel;
}

@GetMapping("/image")
public void image(HttpServletResponse response) {

ImageResponse imageResponse = imageModel.call(new ImagePrompt(DEFAULT_PROMPT));
String imageUrl = imageResponse.getResult().getOutput().getUrl();

try {
URL url = URI.create(imageUrl).toURL();
InputStream in = url.openStream();

response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);
response.getOutputStream().write(in.readAllBytes());
response.getOutputStream().flush();
} catch (IOException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}

}

本示例使用了spring ai alibaba开源框架,spring-ai-alibaba-starter AutoConfiguration 默认初始化了 ImageModel 实例,我们可以选择直接注入并使用默认实例。

6、创建Application启动类

package com.yuncheng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class AiApplication {

public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(AiApplication.class, args);
Environment env = application.getEnvironment();
String port = env.getProperty("server.port");
System.out.println("==============AiApplication启动成功,服务端口为:" + port);

}
}

7、启动Springboot工程并测试

工程启动成功后,把下面地址输入到浏览器里,验证AI大模型文生图:

http://localhost:8080/ai/image

浏览器显示:

8、总结

通过上述介绍可以看出,Spring AI框架中的文生图功能及其相关接口设计旨在为开发者提供便捷且强大的工具,用于探索和应用最新的图像生成技术。而ImageModel类则扮演着连接具体模型与应用程序逻辑的重要桥梁角色,帮助用户轻松集成并利用各种先进的图像生成能力。

1)Spring AI文生图接口使用

2)ImageModel 类的使用

ImageModel 类是Spring AI框架中用于表示图像生成模型的核心组件之一。它的主要职责包括但不限于:

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Spring Boot Spring AI 阿里云百炼 文生图 AI
相关文章