Skip to content

Commit

Permalink
feat:增加æ千帆支持文心一言
Browse files Browse the repository at this point in the history
  • Loading branch information
moyangzhan committed Mar 4, 2024
1 parent ae26772 commit 0ec2521
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
### 接入的模型:
* ChatGPT 3.5
* 通义千问
* 文心一言
* DALL-E 2

### 技术
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class GenerateImage {
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void asyncCheckAndPushToClient(SseEmitter sseEmitter, User user, AskReq a
if (Boolean.TRUE.equals(conversation.getUnderstandContextEnable()) && user.getUnderstandContextMsgPairNum() > 0) {
List<ConversationMessage> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
@Slf4j
public class DashScopeLLMService extends AbstractLLMService<DashScopeSetting> {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void init() {
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));


Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions docs/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@
<artifactId>langchain4j-dashscope</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-qianfan</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down

0 comments on commit 0ec2521

Please sign in to comment.