【代码优化】AI:完善 OpenAIChatModelTests、OpenAiImageModelTests 单测,方便大家快速体验

This commit is contained in:
YunaiV 2024-07-06 14:11:21 +08:00
parent e0f08a0f02
commit 1feff2b12b
6 changed files with 108 additions and 73 deletions

View File

@ -101,7 +101,7 @@ public class AiApiKeyServiceImpl implements AiApiKeyService {
public ChatModel getChatModel(Long id) {
AiApiKeyDO apiKey = validateApiKey(id);
AiPlatformEnum platform = AiPlatformEnum.validatePlatform(apiKey.getPlatform());
return modelFactory.getOrCreateChatClient(platform, apiKey.getApiKey(), apiKey.getUrl());
return modelFactory.getOrCreateChatModel(platform, apiKey.getApiKey(), apiKey.getUrl());
}
@Override

View File

@ -23,7 +23,7 @@ public interface AiModelFactory {
* @param url API URL
* @return ChatModel 对象
*/
ChatModel getOrCreateChatClient(AiPlatformEnum platform, String apiKey, String url);
ChatModel getOrCreateChatModel(AiPlatformEnum platform, String apiKey, String url);
/**
* 基于默认配置获得 ChatModel 对象

View File

@ -50,7 +50,7 @@ import java.util.List;
public class AiModelFactoryImpl implements AiModelFactory {
@Override
public ChatModel getOrCreateChatClient(AiPlatformEnum platform, String apiKey, String url) {
public ChatModel getOrCreateChatModel(AiPlatformEnum platform, String apiKey, String url) {
String cacheKey = buildClientCacheKey(ChatModel.class, platform, apiKey, url);
return Singleton.get(cacheKey, (Func0<ChatModel>) () -> {
//noinspection EnhancedSwitchMigration

View File

@ -0,0 +1,64 @@
package cn.iocoder.yudao.framework.ai.chat;
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.List;
/**
* {@link XingHuoChatClient} 集成测试
*
* @author 芋道源码
*/
public class OpenAIChatModelTests {
private final OpenAiApi openAiApi = new OpenAiApi(
"https://api.holdai.top",
"sk-dZEPiVaNcT3FHhef51996bAa0bC74806BeAb620dA5Da10Bf");
private final OpenAiChatModel chatModel = new OpenAiChatModel(openAiApi,
OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_O).build());
@Test
@Disabled
public void testCall() {
// 准备参数
List<Message> messages = new ArrayList<>();
messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
messages.add(new UserMessage("1 + 1 = "));
// 调用
ChatResponse response = chatModel.call(new Prompt(messages));
// 打印结果
System.out.println(response);
System.out.println(response.getResult().getOutput());
}
@Test
@Disabled
public void testStream() {
// 准备参数
List<Message> messages = new ArrayList<>();
messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
messages.add(new UserMessage("1 + 1 = "));
// 调用
Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages));
// 打印结果
flux.doOnNext(response -> {
// System.out.println(response);
System.out.println(response.getResult().getOutput());
}).then().block();
}
}

View File

@ -1,70 +0,0 @@
package cn.iocoder.yudao.framework.ai.image;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.api.OpenAiImageApi;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Scanner;
// TODO 芋艿整理单测
/**
* author: fansili
* time: 2024/3/17 10:40
*/
public class OpenAiImageClientTests {
private OpenAiImageModel openAiImageClient;
@BeforeEach
public void setup() {
// 初始化 openAiImageClient
this.openAiImageClient = new OpenAiImageModel(
new OpenAiImageApi("")
// new OpenAiImageOptions().setResponseFormat(OpenAiImageOptions.ResponseFormatEnum.URL.getValue()) TODO 芋艿临时处理
);
}
@Test
public void callTest() {
ImageResponse call = openAiImageClient.call(new ImagePrompt("中国长城!"));
System.err.println("url: " + call.getResult().getOutput().getUrl());
System.err.println("base64: " + call.getResult().getOutput().getB64Json());
String base64String = call.getResult().getOutput().getB64Json();
ImageIcon imageIcon = new ImageIcon(decodeBase64ToImage(base64String));
JLabel label = new JLabel(imageIcon);
JFrame frame = new JFrame("Base64 Image Display");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
// 阻止退出
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
// 将Base64解码为BufferedImage
private static BufferedImage decodeBase64ToImage(String base64String) {
try {
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
return ImageIO.read(bis);
} catch (IOException e) {
System.out.println("Error decoding the base64 image: " + e.getMessage());
return null;
}
}
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.yudao.framework.ai.image;
import org.junit.jupiter.api.Disabled;
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.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.web.client.RestClient;
/**
* {@link OpenAiImageModel} 集成测试类
*
* @author fansili
*/
public class OpenAiImageModelTests {
private final OpenAiImageApi imageApi = new OpenAiImageApi(
"https://api.holdai.top",
"sk-dZEPiVaNcT3FHhef51996bAa0bC74806BeAb620dA5Da10Bf",
RestClient.builder());
private final OpenAiImageModel imageClient = new OpenAiImageModel(imageApi);
@Test
@Disabled
public void testCall() {
// 准备参数
ImageOptions options = OpenAiImageOptions.builder()
.withModel(OpenAiImageApi.ImageModel.DALL_E_2.getValue()) // 这个模型比较便宜
.withHeight(256).withWidth(256)
.build();
ImagePrompt prompt = new ImagePrompt("中国长城!", options);
// 方法调用
ImageResponse response = imageClient.call(prompt);
System.out.println(response);
}
}