JAVA用框架SpringAI实现人工智能(三)根据文字生成图片壁纸

文字生图能力是大模型的基础能力之一,例如百度的【文心一言】就可以根据用户的要求,生成好看的图片。
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=给我生成东方明珠的壁纸

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cesske

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值