Commit fa9328112dcf130b82fbd92ed2039add2dbfbad6
1 parent
f4a0058e
【新增】AI:openai 接入的配置调整
Showing
5 changed files
with
37 additions
and
13 deletions
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; |
| 7 | 7 | import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatClient; |
| 8 | 8 | import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; |
| 9 | 9 | import org.springframework.ai.ollama.OllamaChatClient; |
| 10 | +import org.springframework.ai.openai.OpenAiChatClient; | |
| 10 | 11 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | 12 | import org.springframework.context.ApplicationContext; |
| 12 | 13 | import org.springframework.stereotype.Component; |
| ... | ... | @@ -48,6 +49,8 @@ public class AiChatClientFactory { |
| 48 | 49 | return applicationContext.getBean(XingHuoChatClient.class); |
| 49 | 50 | } else if (AiPlatformEnum.OLLAMA == platformEnum) { |
| 50 | 51 | return applicationContext.getBean(OllamaChatClient.class); |
| 52 | + } else if (AiPlatformEnum.OPENAI == platformEnum) { | |
| 53 | + return applicationContext.getBean(OpenAiChatClient.class); | |
| 51 | 54 | } |
| 52 | 55 | throw new IllegalArgumentException("不支持的 chat client!"); |
| 53 | 56 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.service.impl; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.ObjUtil; |
| 4 | +import cn.hutool.core.util.StrUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | 7 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.message.AiChatMessageSendRespVO; |
| ... | ... | @@ -123,7 +124,8 @@ public class AiChatServiceImpl implements AiChatService { |
| 123 | 124 | // 3.3 流式返回 |
| 124 | 125 | StringBuffer contentBuffer = new StringBuffer(); |
| 125 | 126 | return streamResponse.map(response -> { |
| 126 | - String newContent = response.getResult().getOutput().getContent(); | |
| 127 | + String newContent = response.getResult() != null ? response.getResult().getOutput().getContent() : null; | |
| 128 | + newContent = StrUtil.nullToDefault(newContent, ""); // 避免 null 的 情况 | |
| 127 | 129 | contentBuffer.append(newContent); |
| 128 | 130 | // 响应结果 |
| 129 | 131 | return new AiChatMessageSendRespVO().setSend(BeanUtils.toBean(userMessage, AiChatMessageSendRespVO.Message.class)) |
| ... | ... | @@ -152,7 +154,8 @@ public class AiChatServiceImpl implements AiChatService { |
| 152 | 154 | |
| 153 | 155 | // 2. 构建 ChatOptions 对象 TODO 芋艿:临时注释掉;等文心一言兼容了; |
| 154 | 156 | // ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); |
| 155 | - return new Prompt(chatMessages, null); | |
| 157 | +// return new Prompt(chatMessages, null); | |
| 158 | + return new Prompt(chatMessages); | |
| 156 | 159 | } |
| 157 | 160 | |
| 158 | 161 | private AiChatMessageDO createChatMessage(Long conversationId, AiChatModelDO model, | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties deleted
| 1 | -# open ai TODO @fansili?????????????? | |
| 2 | - | |
| 3 | -# openAI https://openai.com/ | |
| 4 | -spring.ai.openai.api-key=${OPEN_AI_KEY} | |
| 5 | -spring.ai.openai.chat.options.model=gpt-3.5-turbo | |
| 6 | -spring.ai.openai.chat.options.temperature=0.7 | |
| 7 | -#spring.ai.vectorstore.milvus.client.connect-timeout-ms=50000 | |
| 8 | -#spring.ai.vectorstore.milvus.client.keep-alive-timeout-ms=50000 | |
| 9 | -#spring.ai.vectorstore.milvus.client.keep-alive-time-ms=80000 | |
| 10 | -#spring.ai.vectorstore.pinecone.server-side-timeout=100s | |
| 11 | - |
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; |
| 4 | 4 | import org.springframework.ai.image.ImageResponse; |
| 5 | 5 | import org.junit.Before; |
| 6 | 6 | import org.junit.Test; |
| 7 | +import org.springframework.ai.openai.OpenAiChatClient; | |
| 7 | 8 | import org.springframework.ai.openai.OpenAiImageClient; |
| 8 | 9 | import org.springframework.ai.openai.OpenAiImageOptions; |
| 10 | +import org.springframework.ai.openai.api.OpenAiApi; | |
| 9 | 11 | import org.springframework.ai.openai.api.OpenAiImageApi; |
| 12 | +import reactor.core.publisher.Flux; | |
| 10 | 13 | |
| 11 | 14 | import javax.imageio.ImageIO; |
| 12 | 15 | import javax.swing.*; |
| ... | ... | @@ -15,6 +18,7 @@ import java.io.ByteArrayInputStream; |
| 15 | 18 | import java.io.IOException; |
| 16 | 19 | import java.util.Base64; |
| 17 | 20 | import java.util.Scanner; |
| 21 | +import java.util.function.Function; | |
| 18 | 22 | |
| 19 | 23 | /** |
| 20 | 24 | * author: fansili |
| ... | ... | @@ -66,4 +70,22 @@ public class OpenAiImageClientTests { |
| 66 | 70 | return null; |
| 67 | 71 | } |
| 68 | 72 | } |
| 73 | + | |
| 74 | + public static void main(String[] args) { | |
| 75 | +// OpenAiApi api = new OpenAiApi("https://api.gptsapi.net", "sk-yzKea6d8e8212c3bdd99f9f44ced1cae37c097e5aa3BTS7z"); | |
| 76 | +// OpenAiApi api = new OpenAiApi("https://openkey.cloud", "sk-QmgIIPc5xiYd8lPb076b1b7774Ea49Af9eD2Ef172c8f7e43"); | |
| 77 | + OpenAiApi api = new OpenAiApi("https://api.chatanywhere.tech", "sk-gkgfYxhX9FxyZJznwxRZSJwKeGQYNPDVWjhby2PRRf17GHeT"); | |
| 78 | + OpenAiChatClient client = new OpenAiChatClient(api); | |
| 79 | +// String result = client.call("未来,英文是什么?"); | |
| 80 | +// System.out.println(result); | |
| 81 | + Flux<String> result = client.stream("未来,英文是什么?"); | |
| 82 | + result.map(new Function<String, String>() { | |
| 83 | + @Override | |
| 84 | + public String apply(String s) { | |
| 85 | + System.out.println(s); | |
| 86 | + return s; | |
| 87 | + } | |
| 88 | + }).blockLast(); | |
| 89 | + } | |
| 90 | + | |
| 69 | 91 | } | ... | ... |
yudao-server/src/main/resources/application.yaml
| ... | ... | @@ -149,6 +149,13 @@ spring.ai: |
| 149 | 149 | base-url: http://127.0.0.1:11434 |
| 150 | 150 | chat: |
| 151 | 151 | model: llama3 |
| 152 | + openai: | |
| 153 | +# api-key: sk-QmgIIPc5xiYd8lPb076b1b7774Ea49Af9eD2Ef172c8f7e43 | |
| 154 | +# base-url: https://openkey.cloud | |
| 155 | +# api-key: sk-gkgfYxhX9FxyZJznwxRZSJwKeGQYNPDVWjhby2PRRf17GHeT | |
| 156 | +# base-url: https://api.chatanywhere.tech | |
| 157 | + api-key: sk-yzKea6d8e8212c3bdd99f9f44ced1cae37c097e5aa3BTS7z | |
| 158 | + base-url: https://api.gptsapi.net | |
| 152 | 159 | |
| 153 | 160 | yudao.ai: |
| 154 | 161 | yiyan: | ... | ... |