mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-30 11:12:55 +08:00
feature: add dify-chat-ai type in config
This commit is contained in:
@ -14,6 +14,7 @@ const AITypeName = {
|
||||
[AIType.TONGYIQIANWENAI]: i18n('setting.tab.aiType.tongyiqianwen'),
|
||||
[AIType.OPENAI]: 'Open AI',
|
||||
[AIType.AZUREAI]: 'Azure AI',
|
||||
[AIType.DIFYCHAT]: 'Dify AI',
|
||||
[AIType.RESTAI]: i18n('setting.tab.custom'),
|
||||
};
|
||||
|
||||
@ -53,6 +54,10 @@ const AIFormConfig: Record<AIType, IAiConfigBooleans> = {
|
||||
apiHost: true,
|
||||
model: true,
|
||||
},
|
||||
[AIType.DIFYCHAT]: {
|
||||
apiKey: true,
|
||||
apiHost: true,
|
||||
},
|
||||
[AIType.RESTAI]: {
|
||||
apiKey: true,
|
||||
apiHost: true,
|
||||
|
@ -7,6 +7,8 @@ export enum AIType {
|
||||
OPENAI = 'OPENAI',
|
||||
AZUREAI = 'AZUREAI',
|
||||
RESTAI = 'RESTAI',
|
||||
DIFYCHAT = 'DIFYCHAT',
|
||||
|
||||
}
|
||||
|
||||
export interface IRemainingUse {
|
||||
|
@ -62,6 +62,11 @@ public enum AiSqlSourceEnum implements BaseEnum<String> {
|
||||
*/
|
||||
FASTCHATAI("FAST CHAT AI"),
|
||||
|
||||
/**
|
||||
* FAST CHAT AI
|
||||
*/
|
||||
DIFYCHAT("DIFY CHAT AI"),
|
||||
|
||||
;
|
||||
|
||||
final String description;
|
||||
|
@ -363,6 +363,41 @@ public class ChatController {
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* chat with azure openai
|
||||
*
|
||||
* @param queryRequest
|
||||
* @param sseEmitter
|
||||
* @param uid
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private SseEmitter chatWithDifyChat(ChatQueryRequest queryRequest, SseEmitter sseEmitter, String uid) throws IOException {
|
||||
String prompt = buildPrompt(queryRequest);
|
||||
if (prompt.length() / TOKEN_CONVERT_CHAR_LENGTH > MAX_PROMPT_LENGTH) {
|
||||
log.error("提示语超出最大长度:{},输入长度:{}, 请重新输入", MAX_PROMPT_LENGTH,
|
||||
prompt.length() / TOKEN_CONVERT_CHAR_LENGTH);
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
List<AzureChatMessage> messages = (List<AzureChatMessage>)LocalCache.CACHE.get(uid);
|
||||
if (CollectionUtils.isNotEmpty(messages)) {
|
||||
if (messages.size() >= contextLength) {
|
||||
messages = messages.subList(1, contextLength);
|
||||
}
|
||||
} else {
|
||||
messages = Lists.newArrayList();
|
||||
}
|
||||
AzureChatMessage currentMessage = new AzureChatMessage(AzureChatRole.USER).setContent(prompt);
|
||||
messages.add(currentMessage);
|
||||
|
||||
buildSseEmitter(sseEmitter, uid);
|
||||
|
||||
AzureOpenAIEventSourceListener sourceListener = new AzureOpenAIEventSourceListener(sseEmitter);
|
||||
AzureOpenAIClient.getInstance().streamCompletions(messages, sourceListener);
|
||||
LocalCache.CACHE.put(uid, messages, LocalCache.TIMEOUT);
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* chat with fast chat openai
|
||||
*
|
||||
|
@ -0,0 +1,22 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.client;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class DifyChatAIClient {
|
||||
|
||||
/**
|
||||
* ZHIPU OPENAI KEY
|
||||
*/
|
||||
public static final String DIFYCHAT_API_KEY = "difychat.apiKey";
|
||||
|
||||
/**
|
||||
* ZHIPU OPENAI HOST
|
||||
*/
|
||||
public static final String DIFYCHAT_HOST = "difychat.host";
|
||||
|
||||
|
||||
public static void refresh() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class DifyChatMessage {
|
||||
|
||||
@JsonProperty(value = "query")
|
||||
private String query;
|
||||
|
||||
@JsonProperty(value = "inputs")
|
||||
private Map<String,String> inputs;
|
||||
|
||||
@JsonProperty(value = "conversation_id")
|
||||
private String conversation_id;
|
||||
|
||||
private String user;
|
||||
|
||||
|
||||
|
||||
}
|
@ -15,6 +15,7 @@ import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.client.AzureOpenAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.baichuan.client.BaichuanAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.chat2db.client.Chat2dbAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.client.DifyChatAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.fastchat.client.FastChatAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.rest.client.RestAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.tongyi.client.TongyiChatAIClient;
|
||||
@ -103,10 +104,23 @@ public class ConfigController {
|
||||
case ZHIPUAI:
|
||||
saveZhipuChatAIConfig(request);
|
||||
break;
|
||||
case DIFYCHAT:
|
||||
saveDifyChatAIConfig(request);
|
||||
break;
|
||||
}
|
||||
return ActionResult.isSuccess();
|
||||
}
|
||||
|
||||
private void saveDifyChatAIConfig(AIConfigCreateRequest request) {
|
||||
SystemConfigParam param = SystemConfigParam.builder().code(DifyChatAIClient.DIFYCHAT_API_KEY).content(
|
||||
request.getApiKey()).build();
|
||||
configService.createOrUpdate(param);
|
||||
SystemConfigParam hostParam = SystemConfigParam.builder().code(DifyChatAIClient.DIFYCHAT_HOST).content(
|
||||
request.getApiHost()).build();
|
||||
configService.createOrUpdate(hostParam);
|
||||
DifyChatAIClient.refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* save chat2db ai config
|
||||
*
|
||||
@ -376,6 +390,12 @@ public class ConfigController {
|
||||
config.setApiHost(Objects.nonNull(zhipuApiHost.getData()) ? zhipuApiHost.getData().getContent() : "");
|
||||
config.setModel(Objects.nonNull(zhipuModel.getData()) ? zhipuModel.getData().getContent() : "");
|
||||
break;
|
||||
case DIFYCHAT:
|
||||
DataResult<Config> difyChatApiKey = configService.find(DifyChatAIClient.DIFYCHAT_API_KEY);
|
||||
DataResult<Config> difyChatApiHost = configService.find(DifyChatAIClient.DIFYCHAT_HOST);
|
||||
config.setApiKey(Objects.nonNull(difyChatApiKey.getData()) ? difyChatApiKey.getData().getContent() : "");
|
||||
config.setApiHost(Objects.nonNull(difyChatApiHost.getData()) ? difyChatApiHost.getData().getContent() : "");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user