【新增】AI:使用 DALL 绘图时,使用 b64_json 返回

This commit is contained in:
YunaiV 2024-06-01 11:24:39 +08:00
parent 458f5acdf0
commit 0563503102
12 changed files with 108 additions and 313 deletions

View File

@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* dall2/dall2 绘画
@ -14,9 +13,11 @@ import lombok.experimental.Accessors;
* @since 1.0
*/
@Data
@Accessors(chain = true)
public class AiImageDallReqVO {
@Schema(description = "模型平台", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private String platform; // 参见 AiPlatformEnum 枚举
@Schema(description = "提示词")
@NotNull(message = "提示词不能为空!")
@Size(max = 1200, message = "提示词最大1200")
@ -31,10 +32,10 @@ public class AiImageDallReqVO {
@Schema(description = "图片高度。对于dall-e-2模型尺寸可为256x256, 512x512, 或 1024x1024。对于dall-e-3模型尺寸可为1024x1024, 1792x1024, 或 1024x1792。")
@NotNull(message = "图片高度不能为空!")
private String height;
private Integer height;
@Schema(description = "图片宽度。对于dall-e-2模型尺寸可为256x256, 512x512, 或 1024x1024。对于dall-e-3模型尺寸可为1024x1024, 1792x1024, 或 1024x1792。")
@NotNull(message = "图片宽度不能为空!")
private String width;
private Integer width;
}

View File

@ -41,10 +41,10 @@ public class AiImageDO extends BaseDO {
private String model;
@Schema(description = "图片宽度")
private String width;
private Integer width;
@Schema(description = "图片高度")
private String height;
private Integer height;
// TODO @fan这种就注释绘画状态然后枚举类关联下就好啦
@Schema(description = "绘画状态:提交、排队、绘画中、绘画完成、绘画失败")
@ -56,6 +56,7 @@ public class AiImageDO extends BaseDO {
@Schema(description = "图片地址(自己服务器)")
private String picUrl;
// TODO @芋艿可能要删除掉
@Schema(description = "绘画图片地址(绘画好的服务器)")
private String originalPicUrl;

View File

@ -1,14 +1,15 @@
package cn.iocoder.yudao.module.ai.service.image;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageModelEnum;
import cn.iocoder.yudao.framework.ai.core.enums.OpenAiImageStyleEnum;
import cn.iocoder.yudao.framework.ai.core.exception.AiException;
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -30,11 +31,9 @@ import cn.iocoder.yudao.module.infra.api.file.FileApi;
import com.google.common.collect.ImmutableMap;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.image.ImageGeneration;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageClient;
import org.springframework.ai.image.*;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.stabilityai.api.StabilityAiImageOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
@ -42,8 +41,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.ErrorCodeConstants.AI_IMAGE_NOT_EXISTS;
/**
@ -62,7 +59,7 @@ public class AiImageServiceImpl implements AiImageService {
private FileApi fileApi;
@Resource
private OpenAiImageClient openAiImageClient;
private AiClientFactory aiClientFactory;
@Autowired
private MidjourneyProxyClient midjourneyProxyClient;
@ -81,50 +78,60 @@ public class AiImageServiceImpl implements AiImageService {
}
@Override
public Long dall(Long loginUserId, AiImageDallReqVO req) {
// 保存数据库
AiImageDO aiImageDO = BeanUtils.toBean(req, AiImageDO.class)
.setUserId(loginUserId)
.setWidth(req.getWidth())
.setHeight(req.getHeight())
public Long dall(Long userId, AiImageDallReqVO req) {
req.setPlatform("dall"); // TODO 芋艿临时写死
// 1. 保存数据库
AiImageDO image = BeanUtils.toBean(req, AiImageDO.class)
.setUserId(userId).setStatus(AiImageStatusEnum.IN_PROGRESS.getStatus())
.setWidth(req.getWidth()).setHeight(req.getHeight())
.setDrawRequest(ImmutableMap.of(AiCommonConstants.DRAW_REQ_KEY_STYLE, req.getStyle()))
.setPublicStatus(AiImagePublicStatusEnum.PRIVATE.getStatus())
.setStatus(AiImageStatusEnum.IN_PROGRESS.getStatus());
imageMapper.insert(aiImageDO);
// 异步执行
getSelf().doDall(aiImageDO, req);
// 转换 AiImageDallDrawingRespVO
return aiImageDO.getId();
.setPublicStatus(AiImagePublicStatusEnum.PRIVATE.getStatus());
imageMapper.insert(image);
// 2. 异步绘制后续前端通过返回的 id 进行伦旭
getSelf().doDall(image, req);
return image.getId();
}
@Async
public void doDall(AiImageDO aiImageDO, AiImageDallReqVO req) {
public void doDall(AiImageDO image, AiImageDallReqVO req) {
try {
// 获取 model
OpenAiImageModelEnum openAiImageModelEnum = OpenAiImageModelEnum.valueOfModel(req.getModel());
OpenAiImageStyleEnum openAiImageStyleEnum = OpenAiImageStyleEnum.valueOfStyle(req.getStyle());
// 1.1 构建请求
ImageOptions request = buildImageOptions(req);
// 1.2 执行请求
ImageClient imageClient = aiClientFactory.getDefaultImageClient(AiPlatformEnum.validatePlatform(req.getPlatform()));
ImageResponse response = imageClient.call(new ImagePrompt(req.getPrompt(), request));
// 转换openai 参数
// TODO @fan需要考虑不同平台参数不同
OpenAiImageOptions openAiImageOptions = new OpenAiImageOptions();
openAiImageOptions.setModel(openAiImageModelEnum.getModel());
openAiImageOptions.setStyle(openAiImageStyleEnum.getStyle());
openAiImageOptions.setSize(String.format(AiCommonConstants.DALL_SIZE_TEMPLATE, req.getWidth(), req.getHeight()));
ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(req.getPrompt(), openAiImageOptions));
// 发送
ImageGeneration imageGeneration = imageResponse.getResult();
// 图片保存到服务器
String filePath = fileApi.createFile(HttpUtil.downloadBytes(imageGeneration.getOutput().getUrl()));
// 更新数据库
imageMapper.updateById(new AiImageDO().setId(aiImageDO.getId()).setStatus(AiImageStatusEnum.COMPLETE.getStatus())
.setPicUrl(filePath).setOriginalPicUrl(imageGeneration.getOutput().getUrl()));
} catch (AiException aiException) {
// TODO @fan错误日志也打印下哈因为 aiException.getMessage() 比较精简
imageMapper.updateById(new AiImageDO().setId(aiImageDO.getId()).setStatus(AiImageStatusEnum.FAIL.getStatus())
.setErrorMessage(aiException.getMessage()));
// 2. 上传到文件服务
byte[] fileContent = Base64.decode(response.getResult().getOutput().getB64Json());
String filePath = fileApi.createFile(fileContent);
// 3. 更新数据库
imageMapper.updateById(new AiImageDO().setId(image.getId()).setStatus(AiImageStatusEnum.COMPLETE.getStatus())
.setPicUrl(filePath));
} catch (Exception ex) {
log.error("[doDall][image({}) 生成异常]", image, ex);
imageMapper.updateById(new AiImageDO().setId(image.getId())
.setStatus(AiImageStatusEnum.FAIL.getStatus()).setErrorMessage(ex.getMessage()));
}
}
private static ImageOptions buildImageOptions(AiImageDallReqVO draw) {
if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.OPEN_AI_DALL.getPlatform())) {
OpenAiImageOptions request = new OpenAiImageOptions();
request.setModel(OpenAiImageModelEnum.valueOfModel(draw.getModel()).getModel());
request.setStyle(OpenAiImageStyleEnum.valueOfStyle(draw.getStyle()).getStyle());
request.setSize(String.format(AiCommonConstants.DALL_SIZE_TEMPLATE, draw.getWidth(), draw.getHeight()));
request.setResponseFormat("b64_json");
return request;
} else {
// https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post
return StabilityAiImageOptions.builder().withModel(draw.getModel())
.withHeight(draw.getHeight()).withWidth(draw.getWidth())
.build();
}
// return null;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long midjourneyImagine(Long loginUserId, AiImageMidjourneyImagineReqVO req) {

View File

@ -1,61 +0,0 @@
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80034 (8.0.34)
Source Host : localhost:3306
Source Schema : ruoyi-vue-pro
Target Server Type : MySQL
Target Server Version : 80034 (8.0.34)
File Encoding : 65001
Date: 30/05/2024 17:20:37
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ai_image
-- ----------------------------
DROP TABLE IF EXISTS `ai_image`;
CREATE TABLE `ai_image` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint DEFAULT NULL,
`prompt` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '提示词\n',
`platform` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '平台',
`model` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '模型 dall2/dall3、MJ、NIJI',
`width` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '图片宽度',
`height` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '图片高度',
`status` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '绘画状态:提交、排队、绘画中、绘画完成、绘画失败\n',
`public_status` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '是否发布',
`pic_url` varchar(512) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '图片地址',
`original_pic_url` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '绘画图片地址\n',
`error_message` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '错误信息',
`draw_request` json DEFAULT NULL COMMENT '绘画request',
`draw_response` json DEFAULT NULL COMMENT '绘画response',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`creator` bigint DEFAULT NULL COMMENT '创建用户',
`updater` bigint DEFAULT NULL COMMENT '更新用户',
`deleted` bit(1) DEFAULT b'0' COMMENT '删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=107 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
-- ----------------------------
-- Records of ai_image
-- ----------------------------
BEGIN;
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (91, 1, '北极企鹅', NULL, 'dall-e-2', '1024', '1024', '20', NULL, 'http://test.yudao.iocoder.cn/75b4d733222b60aafdbdcd0475562ff88149eaaff93a25c4e4c66a95bd07f01f.png', 'https://oaidalleapiprodscus.blob.core.windows.net/private/org-FttVrm20iQRlsxxFE7BLEgkT/user-5p7zykU5aS1sYXCjczkTXn8I/img-z4KBxbWtUpLBYmgaYPvWXjBh.png?st=2024-05-29T04%3A50%3A54Z&se=2024-05-29T06%3A50%3A54Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-05-28T23%3A21%3A22Z&ske=2024-05-29T23%3A21%3A22Z&sks=b&skv=2023-11-03&sig=jSw/hJfuPWJqQgjSoINtVrt4w61FsaQ6ed4pRCM8UUA%3D', NULL, '{\"style\": \"vivid\"}', NULL, '2024-05-29 13:50:43', '2024-05-29 13:51:05', 1, 1, b'0');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (93, 1, '北极企鹅', NULL, 'dall-e-2', '1024', '1024', '20', 'private', 'http://test.yudao.iocoder.cn/ab326bbf3fae9a940770a5c36bbf39467ae539a5b94a030b6c6cc2f179d7dd31.png', 'https://oaidalleapiprodscus.blob.core.windows.net/private/org-FttVrm20iQRlsxxFE7BLEgkT/user-5p7zykU5aS1sYXCjczkTXn8I/img-CsJtCgLT9lTigTuBDJtPyO8X.png?st=2024-05-29T09%3A02%3A12Z&se=2024-05-29T11%3A02%3A12Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-05-28T23%3A21%3A54Z&ske=2024-05-29T23%3A21%3A54Z&sks=b&skv=2023-11-03&sig=tH6yFzHtcp3Dxwaua2crFYurCdE7B7%2BAXhyPNVVep1c%3D', NULL, '{\"style\": \"vivid\"}', NULL, '2024-05-29 18:02:03', '2024-05-29 18:02:17', 1, 1, b'0');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (94, 1, '北极大熊猫', NULL, 'dall-e-2', '1024', '1024', '20', 'private', 'http://test.yudao.iocoder.cn/ed9d43a6c841c4a967700e211c998778994eeea60cff2dfeb1c3a88b0542ebdd.png', 'https://oaidalleapiprodscus.blob.core.windows.net/private/org-FttVrm20iQRlsxxFE7BLEgkT/user-5p7zykU5aS1sYXCjczkTXn8I/img-GAwShv01C2408VY5lXvLTP4Q.png?st=2024-05-30T01%3A31%3A28Z&se=2024-05-30T03%3A31%3A28Z&sp=r&sv=2023-11-03&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-05-29T23%3A39%3A20Z&ske=2024-05-30T23%3A39%3A20Z&sks=b&skv=2023-11-03&sig=n/rqAnD0qJDkhbh/Qm12lz1Se70PQSdXetarmwCKoMY%3D', NULL, '{\"style\": \"vivid\"}', NULL, '2024-05-30 10:31:18', '2024-05-30 10:31:34', 1, 1, b'1');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (102, 1, '女少侠', 'midjourney', NULL, NULL, NULL, '30', 'private', NULL, NULL, '无可用的账号实例', NULL, '{}', '2024-05-30 16:11:13', '2024-05-30 16:11:13', 1, 1, b'1');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (103, 1, '123123', 'midjourney', NULL, NULL, NULL, '30', 'private', NULL, NULL, '无可用的账号实例', NULL, '{}', '2024-05-30 16:27:34', '2024-05-30 16:27:34', 1, 1, b'1');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (104, 1, '123123', 'midjourney', NULL, NULL, NULL, '30', 'private', NULL, NULL, '无可用的账号实例', NULL, '{}', '2024-05-30 16:28:23', '2024-05-30 16:28:24', 1, 1, b'1');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (105, 1, '123123', 'midjourney', NULL, NULL, NULL, '30', 'private', NULL, NULL, '无可用的账号实例', NULL, '{}', '2024-05-30 16:28:25', '2024-05-30 16:28:25', 1, 1, b'1');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `platform`, `model`, `width`, `height`, `status`, `public_status`, `pic_url`, `original_pic_url`, `error_message`, `draw_request`, `draw_response`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (106, 1, '123123', 'midjourney', NULL, NULL, NULL, '30', 'private', NULL, NULL, '无可用的账号实例', NULL, '{}', '2024-05-30 16:28:34', '2024-05-30 16:28:34', 1, 1, b'1');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -1,187 +0,0 @@
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80034 (8.0.34)
Source Host : localhost:3306
Source Schema : ruoyi-vue-pro
Target Server Type : MySQL
Target Server Version : 80034 (8.0.34)
File Encoding : 65001
Date: 08/05/2024 18:10:05
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ai_chat_conversation
-- ----------------------------
DROP TABLE IF EXISTS `ai_chat_conversation`;
CREATE TABLE `ai_chat_conversation` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint DEFAULT NULL COMMENT '用户id',
`role_id` bigint DEFAULT NULL COMMENT '聊天角色',
`title` varchar(256) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '标题',
`type` varchar(16) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '对话类型',
`chat_count` int DEFAULT NULL COMMENT '聊天次数',
`model_id` bigint DEFAULT NULL COMMENT '模型id',
`model` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '模型',
`pinned` blob COMMENT '是否置顶',
`temperature` double DEFAULT NULL COMMENT '温度参数',
`max_tokens` int DEFAULT NULL COMMENT '单条回复的最大 Token 数量',
`max_contexts` int DEFAULT NULL COMMENT '上下文的最大 Message 数量',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`creator` bigint DEFAULT NULL COMMENT '创建用户',
`updater` bigint DEFAULT NULL COMMENT '更新用户',
`deleted` bit(1) DEFAULT b'0' COMMENT '删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1781604279872581650 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
-- ----------------------------
-- Records of ai_chat_conversation
-- ----------------------------
BEGIN;
INSERT INTO `ai_chat_conversation` (`id`, `user_id`, `role_id`, `title`, `type`, `chat_count`, `model_id`, `model`, `pinned`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (1781604279872581647, 1, NULL, '新增对话', NULL, NULL, 9, 'ERNIE-3.5-8K', 0x30, NULL, NULL, NULL, '2024-05-07 16:20:06', '2024-05-07 16:20:06', 1, 1, b'1');
INSERT INTO `ai_chat_conversation` (`id`, `user_id`, `role_id`, `title`, `type`, `chat_count`, `model_id`, `model`, `pinned`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (1781604279872581648, 1, 9, '新增对话', NULL, NULL, 9, 'ERNIE-3.5-8K', 0x30, NULL, NULL, NULL, '2024-05-07 16:20:35', '2024-05-07 16:20:35', 1, 1, b'0');
INSERT INTO `ai_chat_conversation` (`id`, `user_id`, `role_id`, `title`, `type`, `chat_count`, `model_id`, `model`, `pinned`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (1781604279872581649, 1, NULL, '新增对话', NULL, NULL, 9, 'ERNIE-3.5-8K', 0x30, NULL, NULL, NULL, '2024-05-07 16:22:37', '2024-05-07 16:22:37', 1, 1, b'0');
COMMIT;
-- ----------------------------
-- Table structure for ai_chat_message
-- ----------------------------
DROP TABLE IF EXISTS `ai_chat_message`;
CREATE TABLE `ai_chat_message` (
`id` bigint NOT NULL AUTO_INCREMENT,
`conversation_id` bigint DEFAULT NULL COMMENT '对话id',
`user_id` bigint DEFAULT NULL COMMENT '用户id',
`role_id` bigint DEFAULT NULL COMMENT '角色id',
`type` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '类型system、user、assistant\n',
`model` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '模型',
`model_id` bigint DEFAULT NULL COMMENT '模型id',
`content` varchar(2048) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '内容',
`tokens` int DEFAULT NULL COMMENT '消耗 Token 数量',
`temperature` double DEFAULT NULL COMMENT '用于调整生成回复的随机性和多样性程度',
`max_tokens` int DEFAULT NULL COMMENT '单条回复的最大 Token 数量',
`max_contexts` int DEFAULT NULL COMMENT '上下文的最大 Message 数量',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`creator` bigint DEFAULT NULL COMMENT '创建用户',
`updater` bigint DEFAULT NULL COMMENT '更新用户',
`deleted` bit(1) DEFAULT b'0' COMMENT '删除',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
-- ----------------------------
-- Records of ai_chat_message
-- ----------------------------
BEGIN;
INSERT INTO `ai_chat_message` (`id`, `conversation_id`, `user_id`, `role_id`, `type`, `model`, `model_id`, `content`, `tokens`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (61, 1781604279872581649, 1, NULL, 'user', 'ERNIE-3.5-8K', 9, '苹果是什么颜色?', NULL, NULL, NULL, NULL, '2024-05-07 17:18:29', '2024-05-07 17:18:29', 1, 1, b'0');
INSERT INTO `ai_chat_message` (`id`, `conversation_id`, `user_id`, `role_id`, `type`, `model`, `model_id`, `content`, `tokens`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (62, 1781604279872581649, 1, NULL, 'system', 'ERNIE-3.5-8K', 9, '苹果是一种水果,其颜色可以因品种和成熟度而异。常见的苹果颜色包括:\n\n1. 红色:许多苹果品种,如红富士、红元帅等,在成熟时会呈现出鲜艳的红色。\n2. 绿色:一些苹果品种,如青苹果、青香蕉等,在成熟时保持绿色或带有绿色条纹。\n3. 黄色:金苹果、黄元帅等品种在成熟时呈黄色。\n\n此外还有一些苹果品种在成熟时会呈现出不同的颜色组合如红绿相间、红黄相间等。因此苹果的颜色并不是单一的而是根据品种和成熟度而有所不同。', 8, NULL, NULL, NULL, '2024-05-07 17:18:38', '2024-05-07 17:18:38', NULL, NULL, b'0');
INSERT INTO `ai_chat_message` (`id`, `conversation_id`, `user_id`, `role_id`, `type`, `model`, `model_id`, `content`, `tokens`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (63, 1781604279872581649, 1, NULL, 'user', 'ERNIE-3.5-8K', 9, '中国好看吗?', NULL, NULL, NULL, NULL, '2024-05-07 17:18:53', '2024-05-07 17:18:53', 1, 1, b'0');
INSERT INTO `ai_chat_message` (`id`, `conversation_id`, `user_id`, `role_id`, `type`, `model`, `model_id`, `content`, `tokens`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (64, 1781604279872581649, 1, NULL, 'system', 'ERNIE-3.5-8K', 9, '中国是一个拥有悠久历史、灿烂文化、广袤土地和多元民族的国家,自然景观和人文景观都非常丰富。从雄伟的长城、壮丽的黄山到神秘的西藏,从繁华的上海、历史悠久的北京到充满异域风情的云南,中国各地都有独特的魅力。\n\n此外中国还拥有丰富多彩的非物质文化遗产如京剧、川剧、皮影戏等传统艺术形式以及中秋节、春节等传统节日。这些文化遗产反映了中国人民的智慧和创造力也是中国文化的重要组成部分。\n\n因此可以说中国非常美丽值得人们去探索和发现它的魅力。无论是自然景观还是人文景观中国都有着独特的魅力和吸引力。', 1, NULL, NULL, NULL, '2024-05-07 17:19:03', '2024-05-07 17:19:03', 1, 1, b'0');
COMMIT;
-- ----------------------------
-- Table structure for ai_chat_model
-- ----------------------------
DROP TABLE IF EXISTS `ai_chat_model`;
CREATE TABLE `ai_chat_model` (
`id` bigint NOT NULL AUTO_INCREMENT,
`key_id` bigint DEFAULT NULL COMMENT 'API 秘钥编号',
`name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '模型名字\n',
`model` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '模型类型(自己定义qianwen、yiyan、xinghuo、openai)\n',
`platform` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '平台',
`sort` int DEFAULT NULL COMMENT '排序',
`status` tinyint DEFAULT NULL COMMENT '禁用 0、正常 1、禁用\n',
`temperature` double DEFAULT NULL COMMENT '温度参数',
`max_tokens` int DEFAULT NULL COMMENT '单条回复的最大 Token 数量',
`max_contexts` int DEFAULT NULL COMMENT '上下文的最大 Message 数量',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`creator` bigint DEFAULT NULL COMMENT '创建用户',
`updater` bigint DEFAULT NULL COMMENT '更新用户',
`deleted` bit(1) DEFAULT b'0' COMMENT '删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
-- ----------------------------
-- Records of ai_chat_model
-- ----------------------------
BEGIN;
INSERT INTO `ai_chat_model` (`id`, `key_id`, `name`, `model`, `platform`, `sort`, `status`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (9, 1, '小红书Ai写作大模型3.5 8k', 'ERNIE-3.5-8K', 'yiyan', 100, 0, NULL, NULL, NULL, '2024-05-07 15:08:22', '2024-05-07 15:20:32', 1, 1, b'0');
INSERT INTO `ai_chat_model` (`id`, `key_id`, `name`, `model`, `platform`, `sort`, `status`, `temperature`, `max_tokens`, `max_contexts`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (10, 1, '小红书Ai写作大模型4.0', 'ERNIE 4.0', 'yiyan', 100, 0, NULL, NULL, NULL, '2024-05-07 15:23:33', '2024-05-07 15:23:33', 1, 1, b'0');
COMMIT;
-- ----------------------------
-- Table structure for ai_chat_role
-- ----------------------------
DROP TABLE IF EXISTS `ai_chat_role`;
CREATE TABLE `ai_chat_role` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint DEFAULT NULL,
`model_id` bigint DEFAULT NULL COMMENT '模型编号',
`name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '角色名,角色的显示名称\n',
`avatar` varchar(256) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '头像',
`category` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '分类,角色所属的类别,如娱乐、创作等\n',
`sort` int DEFAULT NULL COMMENT '排序',
`description` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '角色描述',
`welcome_message` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '角色欢迎语',
`system_message` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '角色设定(消息)',
`public_status` blob COMMENT '是否公开 true - 公开false - 私有',
`status` tinyint DEFAULT NULL COMMENT '状态 0、开启 1、关闭',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`creator` bigint DEFAULT NULL COMMENT '创建用户',
`updater` bigint DEFAULT NULL COMMENT '更新用户',
`deleted` bit(1) DEFAULT b'0' COMMENT '删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
-- ----------------------------
-- Records of ai_chat_role
-- ----------------------------
BEGIN;
INSERT INTO `ai_chat_role` (`id`, `user_id`, `model_id`, `name`, `avatar`, `category`, `sort`, `description`, `welcome_message`, `system_message`, `public_status`, `status`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (8, 1, 9, '小红书写作v2', 'http://baidu.com', 'writing', 0, '采用gpt3.5模型,拥有小红书优质作者写作经验。', '欢迎使用小红书写作模型!', '你是一名优秀的小红书人文、风光作者,你热爱旅游,每去往一个城市你都会用美妙的文字抒写着这座城市的大街小巷,描述着这座城市的美好。', 0x31, 0, '2024-05-07 15:30:30', '2024-05-07 15:35:54', 1, 1, b'1');
INSERT INTO `ai_chat_role` (`id`, `user_id`, `model_id`, `name`, `avatar`, `category`, `sort`, `description`, `welcome_message`, `system_message`, `public_status`, `status`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (9, 1, 9, '小红书写作v1', 'http://baidu.com', 'writing', 0, '采用gpt3.5模型,拥有小红书优质作者写作经验。', '欢迎使用小红书写作模型!', '你是一名优秀的小红书人文、风光作者,你热爱旅游,每去往一个城市你都会用美妙的文字抒写着这座城市的大街小巷,描述着这座城市的美好。', 0x30, 0, '2024-05-07 15:36:40', '2024-05-07 15:36:40', 1, 1, b'0');
COMMIT;
-- ----------------------------
-- Table structure for ai_image
-- ----------------------------
DROP TABLE IF EXISTS `ai_image`;
CREATE TABLE `ai_image` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint DEFAULT NULL,
`prompt` varchar(2000) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '提示词\n',
`modal` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '模型\n',
`size` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '生成图像的尺寸大小。对于dall-e-2模型尺寸可为256x256, 512x512, 或 1024x1024。对于dall-e-3模型尺寸可为1024x1024, 1792x1024, 或 1024x1792。\n',
`drawing_status` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '绘画状态:提交、排队、绘画中、绘画完成、绘画失败\n',
`drawing_image_url` varchar(512) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '绘画图片地址\n',
`drawing_error_message` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '错误信息',
`mj_message_id` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '用户操作的消息编号(MJ返回)\n',
`mj_operation_id` varchar(128) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '用户操作的操作编号(MJ返回)\n',
`mj_operation_name` varchar(32) COLLATE utf8mb4_0900_bin DEFAULT NULL COMMENT '用户操作的操作名字(MJ返回)\n',
`mj_operations` json DEFAULT NULL COMMENT 'mj图片生产成功保存的 components json 数组\n',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`creator` bigint DEFAULT NULL COMMENT '创建用户',
`updater` bigint DEFAULT NULL COMMENT '更新用户',
`deleted` bit(1) DEFAULT b'0' COMMENT '删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
-- ----------------------------
-- Records of ai_image
-- ----------------------------
BEGIN;
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `modal`, `size`, `drawing_status`, `drawing_image_url`, `drawing_error_message`, `mj_message_id`, `mj_operation_id`, `mj_operation_name`, `mj_operations`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (22, 1, 'Cute cartoon style mobile game scene, a colorful camping car with an outdoor table and chairs next to it on the road in a spring forest, the simple structure of the camper van, soft lighting, C4D rendering, 3d model in the style of a cartoon, cute shape, a pastel color scheme, closeup view from the side angle, high resolution, bright colors, a happy atmosphere. --ar 1:2 --v 6.0', 'midjoureny', NULL, 'fail', NULL, 'You have reached the maximum allowed number of concurrent jobs. Don\'t worry, this job will start as soon as another one finishes!', NULL, NULL, NULL, NULL, '2024-05-08 17:26:01', '2024-05-08 17:26:04', 1, NULL, b'0');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `modal`, `size`, `drawing_status`, `drawing_image_url`, `drawing_error_message`, `mj_message_id`, `mj_operation_id`, `mj_operation_name`, `mj_operations`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (23, 1, 'Cute cartoon style mobile game scene, a colorful camping car with an outdoor table and chairs next to it on the road in a spring forest, the simple structure of the camper van, soft lighting, C4D rendering, 3d model in the style of a cartoon, cute shape, a pastel color scheme, closeup view from the side angle, high resolution, bright colors, a happy atmosphere. --ar 1:2 --v 6.0', 'midjoureny', NULL, 'fail', NULL, 'Your job queue is full. Please wait for a job to finish first, then resubmit this one.', '1788144718477979648', NULL, NULL, NULL, '2024-05-08 17:51:38', '2024-05-08 17:51:39', 1, NULL, b'0');
INSERT INTO `ai_image` (`id`, `user_id`, `prompt`, `modal`, `size`, `drawing_status`, `drawing_image_url`, `drawing_error_message`, `mj_message_id`, `mj_operation_id`, `mj_operation_name`, `mj_operations`, `create_time`, `update_time`, `creator`, `updater`, `deleted`) VALUES (24, 1, 'Cute cartoon style mobile game scene, a colorful camping car with an outdoor table and chairs next to it on the road in a spring forest, the simple structure of the camper van, soft lighting, C4D rendering, 3d model in the style of a cartoon, cute shape, a pastel color scheme, closeup view from the side angle, high resolution, bright colors, a happy atmosphere. --ar 1:2 --v 6.0', 'midjoureny', NULL, 'submit', NULL, NULL, '1788145293357699072', NULL, NULL, NULL, '2024-05-08 17:53:55', '2024-05-08 17:53:55', 1, 1, b'0');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -22,6 +22,11 @@
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>io.springboot.ai</groupId>
<artifactId>spring-ai-stability-ai</artifactId>
<version>1.0.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.springboot.ai</groupId>-->
<!-- <artifactId>spring-ai-vertex-ai-gemini</artifactId>-->

View File

@ -20,7 +20,8 @@ public enum AiPlatformEnum {
QIAN_WEN("QianWen", "千问"), // 阿里
GEMIR ("gemir ", "gemir "), // 谷歌
OPEN_AI_DALL("dall", "dall"), // TODO OpenAI 提供的绘图接入中
OPEN_AI_DALL("dall", "dall"), // TODO OpenAI 提供的绘图接入中TODO 要不要统一下
STABLE_DIFFUSION("StableDiffusion", "StableDiffusion"), // Stability AI
MIDJOURNEY("midjourney", "midjourney"), // TODO MJ 提供的绘图接入中
;

View File

@ -13,6 +13,7 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
@Deprecated
public enum OpenAiImageModelEnum {
DALL_E_2("dall-e-2", "dall-e-2"),

View File

@ -13,6 +13,7 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
@Deprecated
public enum OpenAiImageStyleEnum {
// 图像生成的风格可为vivid生动 natural自然vivid会使模型偏向生成超现实和戏剧性的图像而natural则会让模型产出更自然不那么超现实的图像该参数仅对dall-e-3模型有效

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.ai.core.factory;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.image.ImageClient;
/**
* AI 客户端工厂的接口类
@ -33,6 +34,16 @@ public interface AiClientFactory {
*/
StreamingChatClient getDefaultStreamingChatClient(AiPlatformEnum platform);
/**
* 基于默认配置获得 ImageClient 对象
*
* 默认配置指的是在 application.yaml 配置文件中的 spring.ai 相关的配置
*
* @param platform 平台
* @return ImageClient 对象
*/
ImageClient getDefaultImageClient(AiPlatformEnum platform);
/**
* 创建 Chat 参数
*

View File

@ -24,11 +24,13 @@ import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration;
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.image.ImageClient;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiImageClient;
import org.springframework.ai.openai.api.ApiUtils;
import org.springframework.ai.openai.api.OpenAiApi;
@ -84,6 +86,16 @@ public class AiClientFactoryImpl implements AiClientFactory {
}
}
@Override
public ImageClient getDefaultImageClient(AiPlatformEnum platform) {
switch (platform) {
case OPEN_AI_DALL:
return SpringUtil.getBean(OpenAiImageClient.class);
}
return null;
}
private static String buildClientCacheKey(Class<?> clazz, Object... params) {
if (ArrayUtil.isEmpty(params)) {
return clazz.getName();

View File

@ -144,7 +144,8 @@ spring:
--- #################### AI 相关配置 ####################
spring.ai:
spring:
ai:
ollama:
base-url: http://127.0.0.1:11434
chat:
@ -152,12 +153,15 @@ spring.ai:
openai:
api-key: sk-yzKea6d8e8212c3bdd99f9f44ced1cae37c097e5aa3BTS7z
base-url: https://api.gptsapi.net
stabilityai:
api-key: sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx
vertex:
ai:
gemini:
project-id: 1 # TODO 芋艿:缺配置
location: 2
yudao.ai:
yiyan:
enable: true
@ -202,7 +206,6 @@ yudao.ai:
suno:
enable: true
token: 16b4356581984d538652354b60d69ff0
--- #################### 芋道相关配置 ####################
yudao: