Spring AI项目Open AI绘画开发指导

Spring AI项目创建

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项目

  1. 打开IDEA创建一个新的spring boot项目,填写项目名称和位置,类型选择maven,组、工件、软件包名称可以自定义,JDK选择17+即可,java语言标准和JDK相同即可
  2. 配置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工程的应用框架
  3. 创建完成后,项目结构大体如下(这里删除了无用的maven文件内容、修改application的文件格式为yaml)

配置项目pom和application文件

  • 注意:修改pom文件,重新下载spring ai依赖需要科学上网,请确保网络连接没有问题
  1. 打开项目的pom文件,修改spring ai的版本(项目默认使用稳定版0.8.1)
    • 主要注意默认的spring ai版本和配置依赖jar包仓库(maven仓库中还没有spring ai的依赖)
xml 复制代码
<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>
  1. 配置application文件(api-key的获取参考Spring AI开发前期开发指导)
xml 复制代码
spring:
  application:
    name: ChatImage
  ai:
    openai:
      api-key: sk-xxx
      base-url: https://api.openai-hk.com #请根据自己的api-key自定义配置
server:
  port: 8081

controller接口开发

java 复制代码
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();
    }
}

运行测试

bash 复制代码
http://localhost:8081/ai/draw?msg=请画一幅顶级程序员的日常开发场景
bash 复制代码
http://localhost:8081/ai/draw?msg=请画一幅中国考研大学生的精神状态
相关推荐
漂流瓶jz2 小时前
【AI】大模型LoRA微调原理与实验:基于CMRC2018与Qwen3-0.6B训练实测
人工智能·算法·llm
对你无可奈何4 小时前
用 AI 开发一套 AIOps 平台:接入工具、编排 Agent,追踪任务和资源
人工智能
羲云网络4 小时前
GPT Image 2.6 海报改横竖版:文字安全区、排版分离与交付验收
图像处理·人工智能·python·aigc
weixin_418772724 小时前
公司年会年年办,AI 日历一年翻不了几页
人工智能·数字化转型·企业信息化
明川6 小时前
RAG 到底是什么
人工智能·llm
罗西的思考7 小时前
【轻量化复刻大模型 / KIMI K3】K3 PostTrain 后训练交互式演示
人工智能·算法·机器学习
ZhengEnCi7 小时前
黄仁勋回归英伟达:没有欢迎仪式,员工自发拥抱说明了什么
人工智能·nvidia
陆枫Larry8 小时前
从一头雾水到自己开发一个 MCP Server 来加深理解
人工智能
Csvn8 小时前
模型被「忽略指令」劫持怎么办?双层防线:输入过滤 + 输出校验(S02)
人工智能
Flynn Liang9 小时前
AI Agent Redis 生产踩坑实录:13 类故障的根因、复现与调优
数据库·人工智能·redis·缓存·ai agent·故障分析