|
@@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
|
|
|
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoOptions;
|
|
|
import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatOptions;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
+import cn.iocoder.yudao.module.ai.controller.admin.write.vo.AiWriteGenerateReqVO;
|
|
|
import cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants;
|
|
|
import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService;
|
|
|
import jakarta.annotation.Resource;
|
|
@@ -37,11 +38,11 @@ public class AiWriteServiceImpl implements AiWriteService {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public Flux<CommonResult<String>> generateComposition() {
|
|
|
+ public Flux<CommonResult<String>> generateComposition(AiWriteGenerateReqVO generateReqVO) {
|
|
|
StreamingChatClient chatClient = apiKeyService.getStreamingChatClient(6L);
|
|
|
AiPlatformEnum platform = AiPlatformEnum.validatePlatform("QianWen");
|
|
|
ChatOptions chatOptions = buildChatOptions(platform, "qwen-72b-chat", 1.0, 1000);
|
|
|
- Prompt prompt = new Prompt("请直接写一篇关于 气候变化 的文章,格式为自动,语气为自动,语言为自动,长度为自动。请确保涵盖主要观点,不需要标题,不需要任何额外的解释或道歉。", chatOptions);
|
|
|
+ Prompt prompt = new Prompt(buildWritingPrompt(generateReqVO), chatOptions);
|
|
|
Flux<ChatResponse> streamResponse = chatClient.stream(prompt);
|
|
|
// 3.3 流式返回
|
|
|
StringBuffer contentBuffer = new StringBuffer();
|
|
@@ -54,11 +55,23 @@ public class AiWriteServiceImpl implements AiWriteService {
|
|
|
}).doOnComplete(() -> {
|
|
|
log.info("generateComposition complete, content: {}", contentBuffer);
|
|
|
}).onErrorResume(error -> {
|
|
|
+ log.error("[AI 写作] 发生异常", error);
|
|
|
return Flux.just(error(ErrorCodeConstants.AI_CHAT_STREAM_ERROR));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private String buildWritingPrompt(AiWriteGenerateReqVO generateReqVO) {
|
|
|
+ String template = "请直接写一篇关于 [{}] 的文章,格式为:{},语气为:{},语言为:{},长度为:{}。请确保涵盖主要内容,不需要任何额外的解释或道歉。";
|
|
|
+ String content = generateReqVO.getContent();
|
|
|
+ String format = generateReqVO.getFormat();
|
|
|
+ String tone = generateReqVO.getTone();
|
|
|
+ String language = generateReqVO.getLanguage();
|
|
|
+ String length = generateReqVO.getLength();
|
|
|
+ return StrUtil.format(template, content, format, tone, language, length);
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 芋艿:复用
|
|
|
private static ChatOptions buildChatOptions(AiPlatformEnum platform, String model, Double temperature, Integer maxTokens) {
|
|
|
Float temperatureF = temperature != null ? temperature.floatValue() : null;
|
|
|
//noinspection EnhancedSwitchMigration
|