mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
【代码优化】AI:将 ChatClient 替换成 ChatModel,和 Spring AI 对齐
This commit is contained in:
@ -70,7 +70,7 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
|
||||
List<AiChatMessageDO> historyMessages = chatMessageMapper.selectListByConversationId(conversation.getId());
|
||||
// 1.2 校验模型
|
||||
AiChatModelDO model = chatModalService.validateChatModel(conversation.getModelId());
|
||||
ChatModel chatClient = apiKeyService.getChatClient(model.getKeyId());
|
||||
ChatModel chatModel = apiKeyService.getChatModel(model.getKeyId());
|
||||
|
||||
// 2. 插入 user 发送消息
|
||||
AiChatMessageDO userMessage = createChatMessage(conversation.getId(), null, model,
|
||||
@ -82,7 +82,7 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
|
||||
|
||||
// 3.2 创建 chat 需要的 Prompt
|
||||
Prompt prompt = buildPrompt(conversation, historyMessages, model, sendReqVO);
|
||||
ChatResponse chatResponse = chatClient.call(prompt);
|
||||
ChatResponse chatResponse = chatModel.call(prompt);
|
||||
|
||||
// 3.3 段式返回
|
||||
String newContent = chatResponse.getResult().getOutput().getContent();
|
||||
@ -101,7 +101,7 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
|
||||
List<AiChatMessageDO> historyMessages = chatMessageMapper.selectListByConversationId(conversation.getId());
|
||||
// 1.2 校验模型
|
||||
AiChatModelDO model = chatModalService.validateChatModel(conversation.getModelId());
|
||||
StreamingChatModel chatClient = apiKeyService.getChatClient(model.getKeyId());
|
||||
StreamingChatModel chatModel = apiKeyService.getChatModel(model.getKeyId());
|
||||
|
||||
// 2. 插入 user 发送消息
|
||||
AiChatMessageDO userMessage = createChatMessage(conversation.getId(), null, model,
|
||||
@ -113,7 +113,7 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
|
||||
|
||||
// 3.2 创建 chat 需要的 Prompt
|
||||
Prompt prompt = buildPrompt(conversation, historyMessages, model, sendReqVO);
|
||||
Flux<ChatResponse> streamResponse = chatClient.stream(prompt);
|
||||
Flux<ChatResponse> streamResponse = chatModel.stream(prompt);
|
||||
|
||||
// 3.3 流式返回
|
||||
// TODO 注意:Schedulers.immediate() 目的是,避免默认 Schedulers.parallel() 并发消费 chunk 导致 SSE 响应前端会乱序问题
|
||||
|
@ -98,8 +98,8 @@ public class AiImageServiceImpl implements AiImageService {
|
||||
// 1.1 构建请求
|
||||
ImageOptions request = buildImageOptions(req);
|
||||
// 1.2 执行请求
|
||||
ImageModel imageClient = apiKeyService.getImageClient(AiPlatformEnum.validatePlatform(req.getPlatform()));
|
||||
ImageResponse response = imageClient.call(new ImagePrompt(req.getPrompt(), request));
|
||||
ImageModel imageModel = apiKeyService.getImageModel(AiPlatformEnum.validatePlatform(req.getPlatform()));
|
||||
ImageResponse response = imageModel.call(new ImagePrompt(req.getPrompt(), request));
|
||||
|
||||
// 2. 上传到文件服务
|
||||
byte[] fileContent = Base64.decode(response.getResult().getOutput().getB64Json());
|
||||
|
@ -81,17 +81,17 @@ public interface AiApiKeyService {
|
||||
* @param id 编号
|
||||
* @return ChatModel 对象
|
||||
*/
|
||||
ChatModel getChatClient(Long id);
|
||||
ChatModel getChatModel(Long id);
|
||||
|
||||
/**
|
||||
* 获得 ImageClient 对象
|
||||
* 获得 ImageModel 对象
|
||||
*
|
||||
* TODO 可优化点:目前默认获取 platform 对应的第一个开启的配置用于绘画;后续可以支持配置选择
|
||||
*
|
||||
* @param platform 平台
|
||||
* @return ImageClient 对象
|
||||
* @return ImageModel 对象
|
||||
*/
|
||||
ImageModel getImageClient(AiPlatformEnum platform);
|
||||
ImageModel getImageModel(AiPlatformEnum platform);
|
||||
|
||||
/**
|
||||
* 获得 MidjourneyApi 对象
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.iocoder.yudao.module.ai.service.model;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiModelFactory;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
@ -35,7 +35,7 @@ public class AiApiKeyServiceImpl implements AiApiKeyService {
|
||||
private AiApiKeyMapper apiKeyMapper;
|
||||
|
||||
@Resource
|
||||
private AiClientFactory clientFactory;
|
||||
private AiModelFactory modelFactory;
|
||||
|
||||
@Override
|
||||
public Long createApiKey(AiApiKeySaveReqVO createReqVO) {
|
||||
@ -98,19 +98,19 @@ public class AiApiKeyServiceImpl implements AiApiKeyService {
|
||||
// ========== 与 spring-ai 集成 ==========
|
||||
|
||||
@Override
|
||||
public ChatModel getChatClient(Long id) {
|
||||
public ChatModel getChatModel(Long id) {
|
||||
AiApiKeyDO apiKey = validateApiKey(id);
|
||||
AiPlatformEnum platform = AiPlatformEnum.validatePlatform(apiKey.getPlatform());
|
||||
return clientFactory.getOrCreateChatClient(platform, apiKey.getApiKey(), apiKey.getUrl());
|
||||
return modelFactory.getOrCreateChatClient(platform, apiKey.getApiKey(), apiKey.getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageModel getImageClient(AiPlatformEnum platform) {
|
||||
public ImageModel getImageModel(AiPlatformEnum platform) {
|
||||
AiApiKeyDO apiKey = apiKeyMapper.selectFirstByPlatformAndStatus(platform.getName(), CommonStatusEnum.ENABLE.getStatus());
|
||||
if (apiKey == null) {
|
||||
throw exception(API_KEY_IMAGE_NODE_FOUND, platform.getName());
|
||||
}
|
||||
return clientFactory.getOrCreateImageClient(platform, apiKey.getApiKey(), apiKey.getUrl());
|
||||
return modelFactory.getOrCreateImageModel(platform, apiKey.getApiKey(), apiKey.getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -120,7 +120,7 @@ public class AiApiKeyServiceImpl implements AiApiKeyService {
|
||||
if (apiKey == null) {
|
||||
throw exception(API_KEY_MIDJOURNEY_NOT_FOUND);
|
||||
}
|
||||
return clientFactory.getOrCreateMidjourneyApi(apiKey.getApiKey(), apiKey.getUrl());
|
||||
return modelFactory.getOrCreateMidjourneyApi(apiKey.getApiKey(), apiKey.getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -130,7 +130,7 @@ public class AiApiKeyServiceImpl implements AiApiKeyService {
|
||||
if (apiKey == null) {
|
||||
throw exception(API_KEY_SUNO_NOT_FOUND);
|
||||
}
|
||||
return clientFactory.getOrCreateSunoApi(apiKey.getApiKey(), apiKey.getUrl());
|
||||
return modelFactory.getOrCreateSunoApi(apiKey.getApiKey(), apiKey.getUrl());
|
||||
}
|
||||
|
||||
}
|
@ -54,7 +54,7 @@ public class AiWriteServiceImpl implements AiWriteService {
|
||||
public Flux<CommonResult<String>> generateWriteContent(AiWriteGenerateReqVO generateReqVO, Long userId) {
|
||||
// 1.1 校验模型 TODO 芋艿 是不是取默认的模型也ok?;那可以,有限拿 chatRole 的角色;如果没有,则获取默认的;
|
||||
AiChatModelDO model = chatModalService.getRequiredDefaultChatModel();
|
||||
StreamingChatModel chatClient = apiKeyService.getChatClient(model.getKeyId());
|
||||
StreamingChatModel chatModel = apiKeyService.getChatModel(model.getKeyId());
|
||||
AiPlatformEnum platform = AiPlatformEnum.validatePlatform(model.getPlatform());
|
||||
|
||||
// 1.2 插入写作信息
|
||||
@ -65,7 +65,7 @@ public class AiWriteServiceImpl implements AiWriteService {
|
||||
// 2.1 构建提示词
|
||||
ChatOptions chatOptions = AiUtils.buildChatOptions(platform, model.getModel(), model.getTemperature(), model.getMaxTokens());
|
||||
Prompt prompt = new Prompt(buildWritingPrompt(generateReqVO), chatOptions);
|
||||
Flux<ChatResponse> streamResponse = chatClient.stream(prompt);
|
||||
Flux<ChatResponse> streamResponse = chatModel.stream(prompt);
|
||||
|
||||
// 2.2 流式返回
|
||||
StringBuffer contentBuffer = new StringBuffer();
|
||||
|
Reference in New Issue
Block a user