文字生图能力是大模型的基础能力之一,例如百度的【文心一言】就可以根据用户的要求,生成好看的图片。
Spring AI 中的图像生成 API 被设计成一个简单且便携的接口,用于与各种专门用于图像生成的 AI 模型进行交互,允许开发者通过最少的代码在不同图像生成的模型之间切换。这种设计符合 Spring 的模块化和互换性理念,确保开发者能够快速地调整其应用程序,以适应各种与图像处理相关的 AI 功能。
代码示例:
package com.cesske.demo.controller;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* openAi 图片生成
*/
@RestController
public class AiImageController {
@Resource
private OpenAiImageModel openAiImageModel;
/**
* 图片生成
*
* @param description
* @return
*/
@GetMapping("/ai/image")
public Response image(@RequestParam(value = "description") String description) {
ImageResponse response = openAiImageModel.call(
new ImagePrompt(description,
OpenAiImageOptions.builder().withQuality("hd").withN(1).withHeight(1024).withWidth(1024).build()));
return Response.ok(response.getResults().get(0).getOutput().getUrl());
}
}
调用示例:
localhost:8080/ai/image?description=给我生成东方明珠的壁纸