From fa9328112dcf130b82fbd92ed2039add2dbfbad6 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 18 May 2024 12:28:43 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91AI=EF=BC=9A?= =?UTF-8?q?openai=20=E6=8E=A5=E5=85=A5=E7=9A=84=E9=85=8D=E7=BD=AE=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/ai/config/AiChatClientFactory.java | 3 +++ .../ai/service/impl/AiChatServiceImpl.java | 7 ++++-- .../src/main/resources/application.properties | 11 ---------- .../openAiImage/OpenAiImageClientTests.java | 22 +++++++++++++++++++ .../src/main/resources/application.yaml | 7 ++++++ 5 files changed, 37 insertions(+), 13 deletions(-) delete mode 100644 yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java index e7a64c214..5efd264a9 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient; import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient; import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; import org.springframework.ai.ollama.OllamaChatClient; +import org.springframework.ai.openai.OpenAiChatClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @@ -48,6 +49,8 @@ public class AiChatClientFactory { return applicationContext.getBean(XingHuoChatClient.class); } else if (AiPlatformEnum.OLLAMA == platformEnum) { return applicationContext.getBean(OllamaChatClient.class); + } else if (AiPlatformEnum.OPENAI == platformEnum) { + return applicationContext.getBean(OpenAiChatClient.class); } throw new IllegalArgumentException("不支持的 chat client!"); } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java index b56b16648..9fd80d1c6 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.ai.service.impl; import cn.hutool.core.util.ObjUtil; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.message.AiChatMessageSendRespVO; @@ -123,7 +124,8 @@ public class AiChatServiceImpl implements AiChatService { // 3.3 流式返回 StringBuffer contentBuffer = new StringBuffer(); return streamResponse.map(response -> { - String newContent = response.getResult().getOutput().getContent(); + String newContent = response.getResult() != null ? response.getResult().getOutput().getContent() : null; + newContent = StrUtil.nullToDefault(newContent, ""); // 避免 null 的 情况 contentBuffer.append(newContent); // 响应结果 return new AiChatMessageSendRespVO().setSend(BeanUtils.toBean(userMessage, AiChatMessageSendRespVO.Message.class)) @@ -152,7 +154,8 @@ public class AiChatServiceImpl implements AiChatService { // 2. 构建 ChatOptions 对象 TODO 芋艿:临时注释掉;等文心一言兼容了; // ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); - return new Prompt(chatMessages, null); +// return new Prompt(chatMessages, null); + return new Prompt(chatMessages); } private AiChatMessageDO createChatMessage(Long conversationId, AiChatModelDO model, diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties b/yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties deleted file mode 100644 index 6ec2672e6..000000000 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties +++ /dev/null @@ -1,11 +0,0 @@ -# open ai TODO @fansili?????????????? - -# openAI https://openai.com/ -spring.ai.openai.api-key=${OPEN_AI_KEY} -spring.ai.openai.chat.options.model=gpt-3.5-turbo -spring.ai.openai.chat.options.temperature=0.7 -#spring.ai.vectorstore.milvus.client.connect-timeout-ms=50000 -#spring.ai.vectorstore.milvus.client.keep-alive-timeout-ms=50000 -#spring.ai.vectorstore.milvus.client.keep-alive-time-ms=80000 -#spring.ai.vectorstore.pinecone.server-side-timeout=100s - diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/openAiImage/OpenAiImageClientTests.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/openAiImage/OpenAiImageClientTests.java index 6944eed70..bf184a849 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/openAiImage/OpenAiImageClientTests.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/openAiImage/OpenAiImageClientTests.java @@ -4,9 +4,12 @@ import org.springframework.ai.image.ImagePrompt; import org.springframework.ai.image.ImageResponse; import org.junit.Before; import org.junit.Test; +import org.springframework.ai.openai.OpenAiChatClient; import org.springframework.ai.openai.OpenAiImageClient; import org.springframework.ai.openai.OpenAiImageOptions; +import org.springframework.ai.openai.api.OpenAiApi; import org.springframework.ai.openai.api.OpenAiImageApi; +import reactor.core.publisher.Flux; import javax.imageio.ImageIO; import javax.swing.*; @@ -15,6 +18,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.Base64; import java.util.Scanner; +import java.util.function.Function; /** * author: fansili @@ -66,4 +70,22 @@ public class OpenAiImageClientTests { return null; } } + + public static void main(String[] args) { +// OpenAiApi api = new OpenAiApi("https://api.gptsapi.net", "sk-yzKea6d8e8212c3bdd99f9f44ced1cae37c097e5aa3BTS7z"); +// OpenAiApi api = new OpenAiApi("https://openkey.cloud", "sk-QmgIIPc5xiYd8lPb076b1b7774Ea49Af9eD2Ef172c8f7e43"); + OpenAiApi api = new OpenAiApi("https://api.chatanywhere.tech", "sk-gkgfYxhX9FxyZJznwxRZSJwKeGQYNPDVWjhby2PRRf17GHeT"); + OpenAiChatClient client = new OpenAiChatClient(api); +// String result = client.call("未来,英文是什么?"); +// System.out.println(result); + Flux result = client.stream("未来,英文是什么?"); + result.map(new Function() { + @Override + public String apply(String s) { + System.out.println(s); + return s; + } + }).blockLast(); + } + } diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index 549fd9edd..e8e07c675 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -149,6 +149,13 @@ spring.ai: base-url: http://127.0.0.1:11434 chat: model: llama3 + openai: +# api-key: sk-QmgIIPc5xiYd8lPb076b1b7774Ea49Af9eD2Ef172c8f7e43 +# base-url: https://openkey.cloud +# api-key: sk-gkgfYxhX9FxyZJznwxRZSJwKeGQYNPDVWjhby2PRRf17GHeT +# base-url: https://api.chatanywhere.tech + api-key: sk-yzKea6d8e8212c3bdd99f9f44ced1cae37c097e5aa3BTS7z + base-url: https://api.gptsapi.net yudao.ai: yiyan: