【新增】AI:openai 接入的配置调整

This commit is contained in:
YunaiV 2024-05-18 12:28:43 +08:00
parent f4a0058e88
commit fa9328112d
5 changed files with 37 additions and 13 deletions

View File

@ -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.xinghuo.XingHuoChatClient;
import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient; import cn.iocoder.yudao.framework.ai.core.model.yiyan.YiYanChatClient;
import org.springframework.ai.ollama.OllamaChatClient; import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -48,6 +49,8 @@ public class AiChatClientFactory {
return applicationContext.getBean(XingHuoChatClient.class); return applicationContext.getBean(XingHuoChatClient.class);
} else if (AiPlatformEnum.OLLAMA == platformEnum) { } else if (AiPlatformEnum.OLLAMA == platformEnum) {
return applicationContext.getBean(OllamaChatClient.class); return applicationContext.getBean(OllamaChatClient.class);
} else if (AiPlatformEnum.OPENAI == platformEnum) {
return applicationContext.getBean(OpenAiChatClient.class);
} }
throw new IllegalArgumentException("不支持的 chat client!"); throw new IllegalArgumentException("不支持的 chat client!");
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.ai.service.impl; package cn.iocoder.yudao.module.ai.service.impl;
import cn.hutool.core.util.ObjUtil; 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.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.message.AiChatMessageSendRespVO; import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.message.AiChatMessageSendRespVO;
@ -123,7 +124,8 @@ public class AiChatServiceImpl implements AiChatService {
// 3.3 流式返回 // 3.3 流式返回
StringBuffer contentBuffer = new StringBuffer(); StringBuffer contentBuffer = new StringBuffer();
return streamResponse.map(response -> { 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); contentBuffer.append(newContent);
// 响应结果 // 响应结果
return new AiChatMessageSendRespVO().setSend(BeanUtils.toBean(userMessage, AiChatMessageSendRespVO.Message.class)) return new AiChatMessageSendRespVO().setSend(BeanUtils.toBean(userMessage, AiChatMessageSendRespVO.Message.class))
@ -152,7 +154,8 @@ public class AiChatServiceImpl implements AiChatService {
// 2. 构建 ChatOptions 对象 TODO 芋艿临时注释掉等文心一言兼容了 // 2. 构建 ChatOptions 对象 TODO 芋艿临时注释掉等文心一言兼容了
// ChatOptions chatOptions = ChatOptionsBuilder.builder().withTemperature(conversation.getTemperature().floatValue()).build(); // 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, private AiChatMessageDO createChatMessage(Long conversationId, AiChatModelDO model,

View File

@ -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

View File

@ -4,9 +4,12 @@ import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse; import org.springframework.ai.image.ImageResponse;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiImageClient; import org.springframework.ai.openai.OpenAiImageClient;
import org.springframework.ai.openai.OpenAiImageOptions; import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.openai.api.OpenAiImageApi; import org.springframework.ai.openai.api.OpenAiImageApi;
import reactor.core.publisher.Flux;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
@ -15,6 +18,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import java.util.Scanner; import java.util.Scanner;
import java.util.function.Function;
/** /**
* author: fansili * author: fansili
@ -66,4 +70,22 @@ public class OpenAiImageClientTests {
return null; 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<String> result = client.stream("未来,英文是什么?");
result.map(new Function<String, String>() {
@Override
public String apply(String s) {
System.out.println(s);
return s;
}
}).blockLast();
}
} }

View File

@ -149,6 +149,13 @@ spring.ai:
base-url: http://127.0.0.1:11434 base-url: http://127.0.0.1:11434
chat: chat:
model: llama3 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: yudao.ai:
yiyan: yiyan: