feat:增加æ千帆支持文心一言

This commit is contained in:
moyangzhan 2024-03-05 00:20:04 +08:00
parent ae26772ef5
commit 0ec252123d
9 changed files with 80 additions and 4 deletions

View File

@ -35,6 +35,7 @@
### 接入的模型: ### 接入的模型:
* ChatGPT 3.5 * ChatGPT 3.5
* 通义千问 * 通义千问
* 文心一言
* DALL-E 2 * DALL-E 2
### 技术 ### 技术

View File

@ -67,6 +67,7 @@ public class AdiConstant {
public static class SysConfigKey { public static class SysConfigKey {
public static final String OPENAI_SETTING = "openai_setting"; public static final String OPENAI_SETTING = "openai_setting";
public static final String DASHSCOPE_SETTING = "dashscope_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_TEXT_RATE_LIMIT = "request_text_rate_limit";
public static final String REQUEST_IMAGE_RATE_LIMIT = "request_image_rate_limit"; public static final String REQUEST_IMAGE_RATE_LIMIT = "request_image_rate_limit";
public static final String CONVERSATION_MAX_NUM = "conversation_max_num"; public static final String CONVERSATION_MAX_NUM = "conversation_max_num";

View File

@ -201,7 +201,7 @@ public class ConversationMessageService extends ServiceImpl<ConversationMessageM
if (Boolean.TRUE.equals(conversation.getUnderstandContextEnable()) && user.getUnderstandContextMsgPairNum() > 0) { if (Boolean.TRUE.equals(conversation.getUnderstandContextEnable()) && user.getUnderstandContextMsgPairNum() > 0) {
List<ConversationMessage> historyMsgList = this.lambdaQuery() List<ConversationMessage> historyMsgList = this.lambdaQuery()
.eq(ConversationMessage::getUserId, user.getId()) .eq(ConversationMessage::getUserId, user.getId())
.eq(ConversationMessage::getConversationId, askReq.getConversationUuid()) .eq(ConversationMessage::getConversationUuid, askReq.getConversationUuid())
.orderByDesc(ConversationMessage::getConversationId) .orderByDesc(ConversationMessage::getConversationId)
.last("limit " + user.getUnderstandContextMsgPairNum() * 2) .last("limit " + user.getUnderstandContextMsgPairNum() * 2)
.list(); .list();

View File

@ -21,8 +21,8 @@ import static com.moyz.adi.common.enums.ErrorEnum.B_LLM_SECRET_KEY_NOT_SET;
@Slf4j @Slf4j
public class DashScopeLLMService extends AbstractLLMService<DashScopeSetting> { public class DashScopeLLMService extends AbstractLLMService<DashScopeSetting> {
public DashScopeLLMService(String modelName, Proxy proxy) { public DashScopeLLMService(String modelName) {
super(modelName, AdiConstant.SysConfigKey.DASHSCOPE_SETTING, DashScopeSetting.class, proxy); super(modelName, AdiConstant.SysConfigKey.DASHSCOPE_SETTING, DashScopeSetting.class, null);
} }
@Override @Override

View File

@ -39,7 +39,8 @@ public class Initializer {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyHttpPort)); 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(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)); ImageModelContext.addImageModelService(OpenAiModelName.DALL_E_2, new OpenAiImageModelService(OpenAiModelName.DALL_E_2, proxy));

View File

@ -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<QianFanSetting> {
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();
}
}

View File

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

View File

@ -386,6 +386,8 @@ VALUES ('openai_setting', '{"secret_key":""}');
INSERT INTO adi_sys_config (name, value) INSERT INTO adi_sys_config (name, value)
VALUES ('dashscope_setting', '{"api_key":""}'); VALUES ('dashscope_setting', '{"api_key":""}');
INSERT INTO adi_sys_config (name, value) 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}'); VALUES ('request_text_rate_limit', '{"times":24,"minutes":3}');
INSERT INTO adi_sys_config (name, value) INSERT INTO adi_sys_config (name, value)
VALUES ('request_image_rate_limit', '{"times":6,"minutes":3}'); VALUES ('request_image_rate_limit', '{"times":6,"minutes":3}');

View File

@ -165,6 +165,11 @@
<artifactId>langchain4j-dashscope</artifactId> <artifactId>langchain4j-dashscope</artifactId>
<version>${langchain4j.version}</version> <version>${langchain4j.version}</version>
</dependency> </dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-qianfan</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>