From 0ec252123d8a8f7dbb1a94beb99f583cfcbd4f49 Mon Sep 17 00:00:00 2001 From: moyangzhan Date: Tue, 5 Mar 2024 00:20:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=C3=A6=E5=8D=83=E5=B8=86?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E5=BF=83=E4=B8=80=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../moyz/adi/common/cosntant/AdiConstant.java | 1 + .../service/ConversationMessageService.java | 2 +- .../common/service/DashScopeLLMService.java | 4 +- .../moyz/adi/common/service/Initializer.java | 3 +- .../adi/common/service/QianFanLLMService.java | 52 +++++++++++++++++++ .../moyz/adi/common/vo/QianFanSetting.java | 14 +++++ docs/create.sql | 2 + pom.xml | 5 ++ 9 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 adi-common/src/main/java/com/moyz/adi/common/service/QianFanLLMService.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/vo/QianFanSetting.java diff --git a/README.md b/README.md index a50e42c..7985286 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ ### 接入的模型: * ChatGPT 3.5 * 通义千问 +* 文心一言 * DALL-E 2 ### 技术 diff --git a/adi-common/src/main/java/com/moyz/adi/common/cosntant/AdiConstant.java b/adi-common/src/main/java/com/moyz/adi/common/cosntant/AdiConstant.java index caaf127..fcc4ba9 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/cosntant/AdiConstant.java +++ b/adi-common/src/main/java/com/moyz/adi/common/cosntant/AdiConstant.java @@ -67,6 +67,7 @@ public class AdiConstant { public static class SysConfigKey { public static final String OPENAI_SETTING = "openai_setting"; public static final String DASHSCOPE_SETTING = "dashscope_setting"; + public static final String QIANFAN_SETTING = "qianfan_setting"; public static final String REQUEST_TEXT_RATE_LIMIT = "request_text_rate_limit"; public static final String REQUEST_IMAGE_RATE_LIMIT = "request_image_rate_limit"; public static final String CONVERSATION_MAX_NUM = "conversation_max_num"; diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/ConversationMessageService.java b/adi-common/src/main/java/com/moyz/adi/common/service/ConversationMessageService.java index 385b149..00c1705 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/ConversationMessageService.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/ConversationMessageService.java @@ -201,7 +201,7 @@ public class ConversationMessageService extends ServiceImpl 0) { List historyMsgList = this.lambdaQuery() .eq(ConversationMessage::getUserId, user.getId()) - .eq(ConversationMessage::getConversationId, askReq.getConversationUuid()) + .eq(ConversationMessage::getConversationUuid, askReq.getConversationUuid()) .orderByDesc(ConversationMessage::getConversationId) .last("limit " + user.getUnderstandContextMsgPairNum() * 2) .list(); diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/DashScopeLLMService.java b/adi-common/src/main/java/com/moyz/adi/common/service/DashScopeLLMService.java index 23c2c40..780de68 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/DashScopeLLMService.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/DashScopeLLMService.java @@ -21,8 +21,8 @@ import static com.moyz.adi.common.enums.ErrorEnum.B_LLM_SECRET_KEY_NOT_SET; @Slf4j public class DashScopeLLMService extends AbstractLLMService { - public DashScopeLLMService(String modelName, Proxy proxy) { - super(modelName, AdiConstant.SysConfigKey.DASHSCOPE_SETTING, DashScopeSetting.class, proxy); + public DashScopeLLMService(String modelName) { + super(modelName, AdiConstant.SysConfigKey.DASHSCOPE_SETTING, DashScopeSetting.class, null); } @Override diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/Initializer.java b/adi-common/src/main/java/com/moyz/adi/common/service/Initializer.java index 2dce9cc..e05ec93 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/Initializer.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/Initializer.java @@ -39,7 +39,8 @@ public class Initializer { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyHttpPort)); } LLMContext.addLLMService(OpenAiModelName.GPT_3_5_TURBO, new OpenAiLLMService(OpenAiModelName.GPT_3_5_TURBO, proxy)); - LLMContext.addLLMService(QwenModelName.QWEN_MAX, new DashScopeLLMService(QwenModelName.QWEN_MAX, proxy)); + LLMContext.addLLMService(QwenModelName.QWEN_MAX, new DashScopeLLMService(QwenModelName.QWEN_MAX)); + LLMContext.addLLMService("ERNIE-Bot", new QianFanLLMService("ERNIE-Bot")); ImageModelContext.addImageModelService(OpenAiModelName.DALL_E_2, new OpenAiImageModelService(OpenAiModelName.DALL_E_2, proxy)); diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/QianFanLLMService.java b/adi-common/src/main/java/com/moyz/adi/common/service/QianFanLLMService.java new file mode 100644 index 0000000..0b45414 --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/service/QianFanLLMService.java @@ -0,0 +1,52 @@ +package com.moyz.adi.common.service; + +import com.moyz.adi.common.cosntant.AdiConstant; +import com.moyz.adi.common.interfaces.AbstractLLMService; +import com.moyz.adi.common.vo.QianFanSetting; +import dev.langchain4j.model.chat.ChatLanguageModel; +import dev.langchain4j.model.chat.StreamingChatLanguageModel; +import dev.langchain4j.model.qianfan.QianfanChatModel; +import dev.langchain4j.model.qianfan.QianfanStreamingChatModel; +import lombok.experimental.Accessors; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +/** + * QianFan LLM service + */ +@Slf4j +@Accessors(chain = true) +public class QianFanLLMService extends AbstractLLMService { + + public QianFanLLMService(String modelName) { + super(modelName, AdiConstant.SysConfigKey.QIANFAN_SETTING, QianFanSetting.class, null); + } + + @Override + public boolean isEnabled() { + return StringUtils.isNoneBlank(setting.getApiKey(), setting.getSecretKey()); + } + + @Override + protected ChatLanguageModel buildChatLLM() { + return QianfanChatModel.builder() + .modelName(modelName) + .temperature(0.7) + .topP(1.0) + .maxRetries(1) + .apiKey(setting.getApiKey()) + .secretKey(setting.getSecretKey()) + .build(); + } + + @Override + protected StreamingChatLanguageModel buildStreamingChatLLM() { + return QianfanStreamingChatModel.builder() + .modelName(modelName) + .temperature(0.7) + .topP(1.0) + .apiKey(setting.getApiKey()) + .secretKey(setting.getSecretKey()) + .build(); + } +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/vo/QianFanSetting.java b/adi-common/src/main/java/com/moyz/adi/common/vo/QianFanSetting.java new file mode 100644 index 0000000..9c50166 --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/vo/QianFanSetting.java @@ -0,0 +1,14 @@ +package com.moyz.adi.common.vo; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class QianFanSetting { + + @JsonProperty("api_key") + private String apiKey; + + @JsonProperty("secret_key") + private String secretKey; +} diff --git a/docs/create.sql b/docs/create.sql index 8169342..c428066 100644 --- a/docs/create.sql +++ b/docs/create.sql @@ -386,6 +386,8 @@ VALUES ('openai_setting', '{"secret_key":""}'); INSERT INTO adi_sys_config (name, value) VALUES ('dashscope_setting', '{"api_key":""}'); INSERT INTO adi_sys_config (name, value) +VALUES ('qianfan_setting', '{"api_key":"","secret_key":""}'); +INSERT INTO adi_sys_config (name, value) VALUES ('request_text_rate_limit', '{"times":24,"minutes":3}'); INSERT INTO adi_sys_config (name, value) VALUES ('request_image_rate_limit', '{"times":6,"minutes":3}'); diff --git a/pom.xml b/pom.xml index 16526df..bfa2069 100644 --- a/pom.xml +++ b/pom.xml @@ -165,6 +165,11 @@ langchain4j-dashscope ${langchain4j.version} + + dev.langchain4j + langchain4j-qianfan + ${langchain4j.version} + org.springframework.boot spring-boot-starter-test