【代码优化】AI:通义千问的 tests 类

This commit is contained in:
YunaiV 2024-07-11 21:37:45 +08:00
parent db7315b8cd
commit c6c003707e
10 changed files with 82 additions and 59 deletions

View File

@ -1,7 +1,7 @@
{ {
"local": { "local": {
"baseUrl": "http://127.0.0.1:48080/admin-api", "baseUrl": "http://127.0.0.1:48080/admin-api",
"token": "Bearer 1c2ce60de96a4fb0bf5bea9604099a3d", "token": "test1",
"adminTenentId": "1", "adminTenentId": "1",
"appApi": "http://127.0.0.1:48080/app-api", "appApi": "http://127.0.0.1:48080/app-api",

View File

@ -39,6 +39,7 @@ public enum AiChatRoleEnum implements IntArrayValuable {
除此之外不要任何解释性语句 除此之外不要任何解释性语句
"""); """);
// TODO @xin这个 role 是不是删除掉好点哈= = 目前主要是没做角色枚举这里多了 role 反倒容易误解哈
/** /**
* 角色 * 角色
*/ */

View File

@ -60,9 +60,5 @@
<groupId>cn.iocoder.boot</groupId> <groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-test</artifactId> <artifactId>yudao-spring-boot-starter-test</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-excel</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.mindmap.AiMindMapDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
/** /**
* AI 音乐 Mapper * AI 思维导图 Mapper
* *
* @author xiaoxin * @author xiaoxin
*/ */

View File

@ -18,12 +18,15 @@ import com.alibaba.cloud.ai.tongyi.TongYiConnectionProperties;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatModel; import com.alibaba.cloud.ai.tongyi.chat.TongYiChatModel;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatProperties; import com.alibaba.cloud.ai.tongyi.chat.TongYiChatProperties;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel; import com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesProperties;
import com.alibaba.dashscope.aigc.generation.Generation; import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration; import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration;
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration; import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
import org.springframework.ai.autoconfigure.qianfan.QianFanAutoConfiguration; import org.springframework.ai.autoconfigure.qianfan.QianFanAutoConfiguration;
import org.springframework.ai.autoconfigure.qianfan.QianFanChatProperties; import org.springframework.ai.autoconfigure.qianfan.QianFanChatProperties;
import org.springframework.ai.autoconfigure.qianfan.QianFanConnectionProperties; import org.springframework.ai.autoconfigure.qianfan.QianFanConnectionProperties;
import org.springframework.ai.autoconfigure.qianfan.QianFanImageProperties;
import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiAutoConfiguration; import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiAutoConfiguration;
import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiChatProperties; import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiChatProperties;
import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiConnectionProperties; import org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiConnectionProperties;
@ -111,6 +114,10 @@ public class AiModelFactoryImpl implements AiModelFactory {
public ImageModel getDefaultImageModel(AiPlatformEnum platform) { public ImageModel getDefaultImageModel(AiPlatformEnum platform) {
//noinspection EnhancedSwitchMigration //noinspection EnhancedSwitchMigration
switch (platform) { switch (platform) {
case TONG_YI:
return SpringUtil.getBean(TongYiImagesModel.class);
case YI_YAN:
return SpringUtil.getBean(QianFanImageModel.class);
case OPENAI: case OPENAI:
return SpringUtil.getBean(OpenAiImageModel.class); return SpringUtil.getBean(OpenAiImageModel.class);
case STABLE_DIFFUSION: case STABLE_DIFFUSION:
@ -124,14 +131,14 @@ public class AiModelFactoryImpl implements AiModelFactory {
public ImageModel getOrCreateImageModel(AiPlatformEnum platform, String apiKey, String url) { public ImageModel getOrCreateImageModel(AiPlatformEnum platform, String apiKey, String url) {
//noinspection EnhancedSwitchMigration //noinspection EnhancedSwitchMigration
switch (platform) { switch (platform) {
case TONG_YI:
return buildTongYiImagesModel(apiKey);
case YI_YAN:
return buildQianFanImageModel(apiKey);
case OPENAI: case OPENAI:
return buildOpenAiImageModel(apiKey, url); return buildOpenAiImageModel(apiKey, url);
case STABLE_DIFFUSION: case STABLE_DIFFUSION:
return buildStabilityAiImageModel(apiKey, url); return buildStabilityAiImageModel(apiKey, url);
case TONG_YI:
return SpringUtil.getBean(TongYiImagesModel.class);
case YI_YAN:
return buildQianFanImageModel(apiKey);
default: default:
throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform)); throw new IllegalArgumentException(StrUtil.format("未知平台({})", platform));
} }
@ -175,6 +182,14 @@ public class AiModelFactoryImpl implements AiModelFactory {
return new TongYiAutoConfiguration().tongYiChatClient(generation, chatOptions, connectionProperties); return new TongYiAutoConfiguration().tongYiChatClient(generation, chatOptions, connectionProperties);
} }
private static TongYiImagesModel buildTongYiImagesModel(String key) {
ImageSynthesis imageSynthesis = SpringUtil.getBean(ImageSynthesis.class);
TongYiImagesProperties imagesOptions = SpringUtil.getBean(TongYiImagesProperties.class);
TongYiConnectionProperties connectionProperties = new TongYiConnectionProperties();
connectionProperties.setApiKey(key);
return new TongYiAutoConfiguration().tongYiImagesClient(imageSynthesis, imagesOptions, connectionProperties);
}
/** /**
* 可参考 {@link QianFanAutoConfiguration#qianFanChatModel(QianFanConnectionProperties, QianFanChatProperties, RestClient.Builder, RetryTemplate, ResponseErrorHandler)} * 可参考 {@link QianFanAutoConfiguration#qianFanChatModel(QianFanConnectionProperties, QianFanChatProperties, RestClient.Builder, RetryTemplate, ResponseErrorHandler)}
*/ */
@ -187,6 +202,18 @@ public class AiModelFactoryImpl implements AiModelFactory {
return new QianFanChatModel(qianFanApi); return new QianFanChatModel(qianFanApi);
} }
/**
* 可参考 {@link QianFanAutoConfiguration#qianFanImageModel(QianFanConnectionProperties, QianFanImageProperties, RestClient.Builder, RetryTemplate, ResponseErrorHandler)}
*/
private QianFanImageModel buildQianFanImageModel(String key) {
List<String> keys = StrUtil.split(key, '|');
Assert.equals(keys.size(), 2, "YiYanChatClient 的密钥需要 (appKey|secretKey) 格式");
String appKey = keys.get(0);
String secretKey = keys.get(1);
QianFanImageApi qianFanApi = new QianFanImageApi(appKey, secretKey);
return new QianFanImageModel(qianFanApi);
}
/** /**
* 可参考 {@link YudaoAiAutoConfiguration#deepSeekChatModel(YudaoAiProperties)} * 可参考 {@link YudaoAiAutoConfiguration#deepSeekChatModel(YudaoAiProperties)}
*/ */
@ -246,8 +273,4 @@ public class AiModelFactoryImpl implements AiModelFactory {
return new StabilityAiImageModel(stabilityAiApi); return new StabilityAiImageModel(stabilityAiApi);
} }
private QianFanImageModel buildQianFanImageModel(String key) {
List<String> keys = StrUtil.split(key, '|');
return new QianFanImageModel(new QianFanImageApi(keys.get(0), keys.get(1)));
}
} }

View File

@ -21,7 +21,7 @@ public class OpenAiImageModelTests {
"https://api.holdai.top", "https://api.holdai.top",
"sk-dZEPiVaNcT3FHhef51996bAa0bC74806BeAb620dA5Da10Bf", "sk-dZEPiVaNcT3FHhef51996bAa0bC74806BeAb620dA5Da10Bf",
RestClient.builder()); RestClient.builder());
private final OpenAiImageModel imageClient = new OpenAiImageModel(imageApi); private final OpenAiImageModel imageModel = new OpenAiImageModel(imageApi);
@Test @Test
@Disabled @Disabled
@ -34,7 +34,7 @@ public class OpenAiImageModelTests {
ImagePrompt prompt = new ImagePrompt("中国长城!", options); ImagePrompt prompt = new ImagePrompt("中国长城!", options);
// 方法调用 // 方法调用
ImageResponse response = imageClient.call(prompt); ImageResponse response = imageModel.call(prompt);
// 打印结果 // 打印结果
System.out.println(response); System.out.println(response);
} }

View File

@ -7,7 +7,6 @@ import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse; import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.qianfan.QianFanImageModel; import org.springframework.ai.qianfan.QianFanImageModel;
import org.springframework.ai.qianfan.QianFanImageOptions; import org.springframework.ai.qianfan.QianFanImageOptions;
import org.springframework.ai.qianfan.api.QianFanApi;
import org.springframework.ai.qianfan.api.QianFanImageApi; import org.springframework.ai.qianfan.api.QianFanImageApi;
/** /**
@ -19,7 +18,7 @@ public class QianFanImageTests {
public void callTest() { public void callTest() {
// todo @芋艿 千帆sdk有个错误暂时没找到问题 // todo @芋艿 千帆sdk有个错误暂时没找到问题
QianFanImageApi qianFanImageApi = new QianFanImageApi( QianFanImageApi qianFanImageApi = new QianFanImageApi(
"ghbbvbW2t7HK7WtYmEITAupm", "njJEr5AsQ5fkB3ucYYDjiQqsOZK20SGb"); "qS8k8dYr2nXunagK4SSU8Xjj", "pHGbx51ql2f0hOyabQvSZezahVC3hh3e");
QianFanImageModel qianFanImageModel = new QianFanImageModel(qianFanImageApi); QianFanImageModel qianFanImageModel = new QianFanImageModel(qianFanImageApi);
QianFanImageOptions imageOptions = QianFanImageOptions.builder() QianFanImageOptions imageOptions = QianFanImageOptions.builder()
@ -45,4 +44,6 @@ public class QianFanImageTests {
ImageResponse imageResponse = imageModel.call(imagePrompt); ImageResponse imageResponse = imageModel.call(imagePrompt);
} }
} }

View File

@ -24,7 +24,7 @@ public class StabilityAiImageModelTests {
private final StabilityAiApi imageApi = new StabilityAiApi( private final StabilityAiApi imageApi = new StabilityAiApi(
"sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx"); "sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx");
private final StabilityAiImageModel imageClient = new StabilityAiImageModel(imageApi); private final StabilityAiImageModel imageModel = new StabilityAiImageModel(imageApi);
@Test @Test
@Disabled @Disabled
@ -37,7 +37,7 @@ public class StabilityAiImageModelTests {
ImagePrompt prompt = new ImagePrompt("great wall", options); ImagePrompt prompt = new ImagePrompt("great wall", options);
// 方法调用 // 方法调用
ImageResponse response = imageClient.call(prompt); ImageResponse response = imageModel.call(prompt);
// 打印结果 // 打印结果
String b64Json = response.getResult().getOutput().getB64Json(); String b64Json = response.getResult().getOutput().getB64Json();
System.out.println(response); System.out.println(response);

View File

@ -0,0 +1,41 @@
package cn.iocoder.yudao.framework.ai.image;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.utils.Constants;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;
/**
* {@link com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel} 集成测试类
*
* @author fansili
*/
public class TongYiImagesModelTest {
private final ImageSynthesis imageApi = new ImageSynthesis();
private final TongYiImagesModel imageModel = new TongYiImagesModel(imageApi);
static {
Constants.apiKey = "sk-Zsd81gZYg7";
}
@Test
public void imageCallTest() {
// 准备参数
ImageOptions options = OpenAiImageOptions.builder()
.withModel(ImageSynthesis.Models.WANX_V1)
.withHeight(256).withWidth(256)
.build();
ImagePrompt prompt = new ImagePrompt("中国长城!", options);
// 方法调用
ImageResponse response = imageModel.call(prompt);
// 打印结果
System.out.println(response);
}
}

View File

@ -1,39 +0,0 @@
package cn.iocoder.yudao.framework.ai.image;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import java.util.Map;
// TODO @fan改成 TongYiImagesModel
/**
* 通义万象
*/
public class TongYiImagesModelTests {
@Test
public void imageCallTest() throws NoApiKeyException {
// 设置 api key
Constants.apiKey = "sk-Zsd81gZYg7";
ImageSynthesisParam param =
ImageSynthesisParam.builder()
.model(ImageSynthesis.Models.WANX_V1)
.n(4)
.size("1024*1024")
.prompt("雄鹰自由自在的在蓝天白云下飞翔")
.build();
// 创建 ImageSynthesis
ImageSynthesis is = new ImageSynthesis();
// 调用 call 生成 image
ImageSynthesisResult call = is.call(param);
System.err.println(JSON.toJSON(call));
for (Map<String, String> result : call.getOutput().getResults()) {
System.err.println("地址: " + result.get("url"));
}
}
}