mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-30 03:03:13 +08:00
@ -27,9 +27,6 @@ import ai.chat2db.server.web.api.controller.ai.claude.model.ClaudeChatCompletion
|
||||
import ai.chat2db.server.web.api.controller.ai.claude.model.ClaudeChatMessage;
|
||||
import ai.chat2db.server.web.api.controller.ai.config.LocalCache;
|
||||
import ai.chat2db.server.web.api.controller.ai.converter.ChatConverter;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.client.DifyChatAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.listener.DifyChatAIEventSourceListener;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.model.DifyChatConstant;
|
||||
import ai.chat2db.server.web.api.controller.ai.enums.PromptType;
|
||||
import ai.chat2db.server.web.api.controller.ai.fastchat.client.FastChatAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.fastchat.embeddings.FastChatEmbeddingResponse;
|
||||
@ -135,7 +132,7 @@ public class ChatController {
|
||||
/**
|
||||
* 自定义模型流式输出接口DEMO
|
||||
* <p>
|
||||
* Note:使用自己本地的流式输出的自定义AI,接口输入和输出需与该样例保持一致
|
||||
* Note:使用自己本地的流式输出的自定义AI,接口输入和输出需与该样例保持一致
|
||||
* </p>
|
||||
*
|
||||
* @param queryRequest
|
||||
@ -174,7 +171,7 @@ public class ChatController {
|
||||
/**
|
||||
* 自定义模型非流式输出接口DEMO
|
||||
* <p>
|
||||
* Note:使用自己本地的飞流式输出自定义AI,接口输入和输出需与该样例保持一致
|
||||
* Note:使用自己本地的飞流式输出自定义AI,接口输入和输出需与该样例保持一致
|
||||
* </p>
|
||||
*
|
||||
* @param queryRequest
|
||||
@ -199,7 +196,7 @@ public class ChatController {
|
||||
@GetMapping("/chat")
|
||||
@CrossOrigin
|
||||
public SseEmitter completions(ChatQueryRequest queryRequest, @RequestHeader Map<String, String> headers)
|
||||
throws IOException {
|
||||
throws IOException {
|
||||
//默认30秒超时,设置为0L则永不超时
|
||||
SseEmitter sseEmitter = new SseEmitter(CHAT_TIMEOUT);
|
||||
String uid = headers.get("uid");
|
||||
@ -233,14 +230,14 @@ public class ChatController {
|
||||
}
|
||||
uid = aiSqlSourceEnum.getCode() + uid;
|
||||
switch (Objects.requireNonNull(aiSqlSourceEnum)) {
|
||||
case OPENAI:
|
||||
case OPENAI :
|
||||
return chatWithOpenAi(queryRequest, sseEmitter, uid);
|
||||
case CHAT2DBAI:
|
||||
return chatWithChat2dbAi(queryRequest, sseEmitter, uid);
|
||||
case RESTAI:
|
||||
case RESTAI :
|
||||
case FASTCHATAI:
|
||||
return chatWithFastChatAi(queryRequest, sseEmitter, uid);
|
||||
case AZUREAI:
|
||||
case AZUREAI :
|
||||
return chatWithAzureAi(queryRequest, sseEmitter, uid);
|
||||
case CLAUDEAI:
|
||||
return chatWithClaudeAi(queryRequest, sseEmitter, uid);
|
||||
@ -252,35 +249,10 @@ public class ChatController {
|
||||
return chatWithTongyiChatAi(queryRequest, sseEmitter, uid);
|
||||
case ZHIPUAI:
|
||||
return chatWithZhipuChatAi(queryRequest, sseEmitter, uid);
|
||||
case DIFYCHAT:
|
||||
return chatWithDifyChatAi(queryRequest, sseEmitter, uid);
|
||||
}
|
||||
return chatWithOpenAi(queryRequest, sseEmitter, uid);
|
||||
}
|
||||
|
||||
private SseEmitter chatWithDifyChatAi(ChatQueryRequest queryRequest, SseEmitter sseEmitter, String uid) throws IOException {
|
||||
String prompt = buildPrompt(queryRequest);
|
||||
if (prompt.length() / TOKEN_CONVERT_CHAR_LENGTH > MAX_PROMPT_LENGTH) {
|
||||
log.error("exceed max token length:{},input length:{}", MAX_PROMPT_LENGTH,
|
||||
prompt.length() / TOKEN_CONVERT_CHAR_LENGTH);
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
|
||||
prompt = prompt.replaceAll("#", "");
|
||||
log.info(prompt);
|
||||
|
||||
Message currentMessage = Message.builder().content(prompt).role(Message.Role.USER).build();
|
||||
List<Message> messages = new ArrayList<>();
|
||||
messages.add(currentMessage);
|
||||
buildSseEmitter(sseEmitter, uid);
|
||||
|
||||
DifyChatAIEventSourceListener eventSourceListener = new DifyChatAIEventSourceListener(sseEmitter, uid);
|
||||
String conversationId = (String) LocalCache.CACHE.get(DifyChatConstant.CONVERSATION_CACHE_PREFIX + uid);
|
||||
DifyChatAIClient.getInstance().streamCompletions(messages, eventSourceListener, uid, conversationId);
|
||||
LocalCache.CACHE.put(uid, JSONUtil.toJsonStr(messages), LocalCache.TIMEOUT);
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用自定义AI接口进行聊天
|
||||
*
|
||||
@ -304,11 +276,11 @@ public class ChatController {
|
||||
* @throws IOException
|
||||
*/
|
||||
private SseEmitter chatWithOpenAi(ChatQueryRequest queryRequest, SseEmitter sseEmitter, String uid)
|
||||
throws IOException {
|
||||
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);
|
||||
prompt.length() / TOKEN_CONVERT_CHAR_LENGTH);
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
|
||||
@ -335,11 +307,11 @@ public class ChatController {
|
||||
* @throws IOException
|
||||
*/
|
||||
private SseEmitter chatWithChat2dbAi(ChatQueryRequest queryRequest, SseEmitter sseEmitter, String uid)
|
||||
throws IOException {
|
||||
throws IOException {
|
||||
String prompt = buildPrompt(queryRequest);
|
||||
if (prompt.length() / TOKEN_CONVERT_CHAR_LENGTH > MAX_PROMPT_LENGTH) {
|
||||
log.error("exceed max token length:{},input length:{}", MAX_PROMPT_LENGTH,
|
||||
prompt.length() / TOKEN_CONVERT_CHAR_LENGTH);
|
||||
prompt.length() / TOKEN_CONVERT_CHAR_LENGTH);
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
|
||||
@ -372,7 +344,7 @@ public class ChatController {
|
||||
prompt.length() / TOKEN_CONVERT_CHAR_LENGTH);
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
List<AzureChatMessage> messages = (List<AzureChatMessage>) LocalCache.CACHE.get(uid);
|
||||
List<AzureChatMessage> messages = (List<AzureChatMessage>)LocalCache.CACHE.get(uid);
|
||||
if (CollectionUtils.isNotEmpty(messages)) {
|
||||
if (messages.size() >= contextLength) {
|
||||
messages = messages.subList(1, contextLength);
|
||||
@ -391,7 +363,6 @@ public class ChatController {
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* chat with fast chat openai
|
||||
*
|
||||
@ -484,7 +455,7 @@ public class ChatController {
|
||||
* @return
|
||||
*/
|
||||
private List<FastChatMessage> getFastChatMessage(String uid, String prompt) {
|
||||
List<FastChatMessage> messages = (List<FastChatMessage>) LocalCache.CACHE.get(uid);
|
||||
List<FastChatMessage> messages = (List<FastChatMessage>)LocalCache.CACHE.get(uid);
|
||||
if (CollectionUtils.isNotEmpty(messages)) {
|
||||
if (messages.size() >= contextLength) {
|
||||
messages = messages.subList(1, contextLength);
|
||||
@ -560,17 +531,17 @@ public class ChatController {
|
||||
log.info(LocalDateTime.now() + ", uid#" + uid + ", on completion");
|
||||
});
|
||||
sseEmitter.onTimeout(
|
||||
() -> log.info(LocalDateTime.now() + ", uid#" + uid + ", on timeout#" + sseEmitter.getTimeout()));
|
||||
() -> log.info(LocalDateTime.now() + ", uid#" + uid + ", on timeout#" + sseEmitter.getTimeout()));
|
||||
sseEmitter.onError(
|
||||
throwable -> {
|
||||
try {
|
||||
log.info(LocalDateTime.now() + ", uid#" + "765431" + ", on error#" + throwable.toString());
|
||||
sseEmitter.send(SseEmitter.event().id("765431").name("发生异常!").data(throwable.getMessage())
|
||||
.reconnectTime(3000));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throwable -> {
|
||||
try {
|
||||
log.info(LocalDateTime.now() + ", uid#" + "765431" + ", on error#" + throwable.toString());
|
||||
sseEmitter.send(SseEmitter.event().id("765431").name("发生异常!").data(throwable.getMessage())
|
||||
.reconnectTime(3000));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
);
|
||||
return sseEmitter;
|
||||
}
|
||||
@ -583,13 +554,13 @@ public class ChatController {
|
||||
* @return
|
||||
*/
|
||||
private String buildTableColumn(TableQueryParam tableQueryParam,
|
||||
List<String> tableNames) {
|
||||
List<String> tableNames) {
|
||||
if (CollectionUtils.isEmpty(tableNames)) {
|
||||
return "";
|
||||
}
|
||||
List<String> schemaContent = Lists.newArrayList();
|
||||
try {
|
||||
schemaContent = tableNames.stream().map(tableName -> {
|
||||
schemaContent = tableNames.stream().map(tableName -> {
|
||||
tableQueryParam.setTableName(tableName);
|
||||
return queryTableDdl(tableName, tableQueryParam);
|
||||
}).collect(Collectors.toList());
|
||||
@ -639,19 +610,19 @@ public class ChatController {
|
||||
}
|
||||
String prompt = queryRequest.getMessage();
|
||||
String promptType = StringUtils.isBlank(queryRequest.getPromptType()) ? PromptType.NL_2_SQL.getCode()
|
||||
: queryRequest.getPromptType();
|
||||
: queryRequest.getPromptType();
|
||||
PromptType pType = EasyEnumUtils.getEnum(PromptType.class, promptType);
|
||||
String ext = StringUtils.isNotBlank(queryRequest.getExt()) ? queryRequest.getExt() : "";
|
||||
String schemaProperty = StringUtils.isNotEmpty(properties) ? String.format(
|
||||
"### 请根据以下table properties和SQL input%s. %s\n#\n### %s SQL tables, with their properties:\n#\n# "
|
||||
+ "%s\n#\n#\n### SQL input: %s", pType.getDescription(), ext, dataSourceType,
|
||||
properties, prompt) : String.format("### 请根据以下SQL input%s. %s\n#\n### SQL input: %s",
|
||||
pType.getDescription(), ext, prompt);
|
||||
"### 请根据以下table properties和SQL input%s. %s\n#\n### %s SQL tables, with their properties:\n#\n# "
|
||||
+ "%s\n#\n#\n### SQL input: %s", pType.getDescription(), ext, dataSourceType,
|
||||
properties, prompt) : String.format("### 请根据以下SQL input%s. %s\n#\n### SQL input: %s",
|
||||
pType.getDescription(), ext, prompt);
|
||||
switch (pType) {
|
||||
case SQL_2_SQL:
|
||||
schemaProperty = StringUtils.isNotBlank(queryRequest.getDestSqlType()) ? String.format(
|
||||
"%s\n#\n### 目标SQL类型: %s", schemaProperty, queryRequest.getDestSqlType()) : String.format(
|
||||
"%s\n#\n### 目标SQL类型: %s", schemaProperty, dataSourceType);
|
||||
"%s\n#\n### 目标SQL类型: %s", schemaProperty, queryRequest.getDestSqlType()) : String.format(
|
||||
"%s\n#\n### 目标SQL类型: %s", schemaProperty, dataSourceType);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -740,7 +711,7 @@ public class ChatController {
|
||||
DataResult<TableSchemaResponse> result = gatewayClientService.schemaVectorSearch(tableSchemaRequest);
|
||||
List<String> schemas = Lists.newArrayList();
|
||||
if (Objects.nonNull(result.getData()) && CollectionUtils.isNotEmpty(result.getData().getTableSchemas())) {
|
||||
for (TableSchema data : result.getData().getTableSchemas()) {
|
||||
for(TableSchema data: result.getData().getTableSchemas()){
|
||||
schemas.add(data.getTableSchema());
|
||||
}
|
||||
}
|
||||
@ -780,7 +751,7 @@ public class ChatController {
|
||||
DataResult<EsTableSchemaResponse> result = gatewayClientService.schemaEsSearch(tableSchemaRequest);
|
||||
List<String> schemas = Lists.newArrayList();
|
||||
if (Objects.nonNull(result.getData()) && CollectionUtils.isNotEmpty(result.getData().getTableSchemas())) {
|
||||
for (EsTableSchema data : result.getData().getTableSchemas()) {
|
||||
for(EsTableSchema data: result.getData().getTableSchemas()){
|
||||
schemas.add(data.getTableSchemaContent());
|
||||
}
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.chat2db.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class DifyChatCompletionsOptions {
|
||||
/**
|
||||
* (选填)以键值对方式提供用户输入字段,与提示词编排中的变量对应。Key 为变量名称,Value 是参数值。
|
||||
* 如果字段类型为 Select,传入的 Value 需为预设选项之一。
|
||||
*/
|
||||
private Map<String, String> inputs;
|
||||
|
||||
/**
|
||||
* 用户输入/提问内容
|
||||
*/
|
||||
private String query;
|
||||
|
||||
/**
|
||||
* blocking 阻塞型,等待执行完毕后返回结果。(请求若流程较长可能会被中断)
|
||||
* streaming 流式返回。基于 SSE(Server-Sent Events)实现流式返回。
|
||||
*/
|
||||
@JsonProperty("response_mode")
|
||||
private String responseMode = "blocking";
|
||||
|
||||
/**
|
||||
* (必填)‼️ 会话标识符,首次对话为 conversation_id: "" ‼️,如果要继续对话请传入上下文返回的 conversation_id
|
||||
*/
|
||||
@JsonProperty("conversation_id")
|
||||
private String conversationId;
|
||||
|
||||
/**
|
||||
* 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
|
||||
*/
|
||||
private String user;
|
||||
|
||||
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.client;
|
||||
|
||||
import ai.chat2db.server.domain.api.model.Config;
|
||||
import ai.chat2db.server.domain.api.service.ConfigService;
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.client.AzureOpenAiStreamClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.chat2db.client.Chat2DBAIStreamClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.chat2db.client.Chat2dbAIClient;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.listener.DifyChatAIEventSourceListener;
|
||||
import ai.chat2db.server.web.api.util.ApplicationContextUtil;
|
||||
import com.unfbx.chatgpt.constant.OpenAIConst;
|
||||
import com.unfbx.chatgpt.entity.chat.Message;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@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";
|
||||
|
||||
|
||||
private static DifyChatAiStreamClient DIFY_CHAT_STREAM_CLIENT;
|
||||
|
||||
public static DifyChatAiStreamClient getInstance() {
|
||||
if (DIFY_CHAT_STREAM_CLIENT != null) {
|
||||
return DIFY_CHAT_STREAM_CLIENT;
|
||||
} else {
|
||||
return singleton();
|
||||
}
|
||||
}
|
||||
|
||||
private static DifyChatAiStreamClient singleton() {
|
||||
if (DIFY_CHAT_STREAM_CLIENT == null) {
|
||||
synchronized (DifyChatAIClient.class) {
|
||||
if (DIFY_CHAT_STREAM_CLIENT == null) {
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
return DIFY_CHAT_STREAM_CLIENT;
|
||||
}
|
||||
|
||||
|
||||
public static void refresh() {
|
||||
|
||||
String apikey;
|
||||
String apiHost = ApplicationContextUtil.getProperty(DIFYCHAT_HOST);
|
||||
if (StringUtils.isBlank(apiHost)) {
|
||||
apiHost = OpenAIConst.OPENAI_HOST;
|
||||
}
|
||||
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
|
||||
Config apiHostConfig = configService.find(DIFYCHAT_HOST).getData();
|
||||
if (apiHostConfig != null) {
|
||||
apiHost = apiHostConfig.getContent();
|
||||
}
|
||||
Config config = configService.find(DIFYCHAT_API_KEY).getData();
|
||||
if (config != null) {
|
||||
apikey = config.getContent();
|
||||
} else {
|
||||
apikey = ApplicationContextUtil.getProperty(DIFYCHAT_API_KEY);
|
||||
}
|
||||
|
||||
log.info("refresh dify chat apiHost:{} apikey:{}", apiHost, maskApiKey(apikey));
|
||||
DIFY_CHAT_STREAM_CLIENT = DifyChatAiStreamClient.builder().apiHost(apiHost).apiKey(apikey).build();
|
||||
|
||||
}
|
||||
|
||||
private static String maskApiKey(String input) {
|
||||
if (input == null) {
|
||||
return input;
|
||||
}
|
||||
|
||||
StringBuilder maskedString = new StringBuilder(input);
|
||||
for (int i = input.length() / 4; i < input.length() / 2; i++) {
|
||||
maskedString.setCharAt(i, '*');
|
||||
}
|
||||
return maskedString.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,163 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.client;
|
||||
|
||||
import ai.chat2db.server.tools.common.exception.ParamBusinessException;
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.interceptor.AzureHeaderAuthorizationInterceptor;
|
||||
import ai.chat2db.server.web.api.controller.ai.chat2db.model.DifyChatCompletionsOptions;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.listener.DifyChatAIEventSourceListener;
|
||||
import cn.hutool.http.ContentType;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.unfbx.chatgpt.entity.chat.Message;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.sse.EventSource;
|
||||
import okhttp3.sse.EventSources;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.poi.hpsf.GUID;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
|
||||
public class DifyChatAiStreamClient {
|
||||
|
||||
/**
|
||||
* apikey
|
||||
*/
|
||||
@Getter
|
||||
@NotNull
|
||||
private String apiKey;
|
||||
|
||||
/**
|
||||
* apiHost
|
||||
*/
|
||||
@Getter
|
||||
@NotNull
|
||||
private String apiHost;
|
||||
|
||||
@Getter
|
||||
private OkHttpClient okHttpClient;
|
||||
|
||||
private DifyChatAiStreamClient(DifyChatAiStreamClient.Builder builder) {
|
||||
this.apiKey = builder.apiKey;
|
||||
this.apiHost = builder.apiHost;
|
||||
if (Objects.isNull(builder.okHttpClient)) {
|
||||
builder.okHttpClient = this.okHttpClient();
|
||||
}
|
||||
okHttpClient = builder.okHttpClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static DifyChatAiStreamClient.Builder builder() {
|
||||
return new DifyChatAiStreamClient.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* okhttpclient
|
||||
*/
|
||||
private OkHttpClient okHttpClient() {
|
||||
OkHttpClient okHttpClient = new OkHttpClient
|
||||
.Builder()
|
||||
.addInterceptor(new AzureHeaderAuthorizationInterceptor(this.apiKey))
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.writeTimeout(50, TimeUnit.SECONDS)
|
||||
.readTimeout(50, TimeUnit.SECONDS)
|
||||
.build();
|
||||
return okHttpClient;
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
private String apiKey;
|
||||
|
||||
private String apiHost;
|
||||
|
||||
/**
|
||||
* 自定义OkhttpClient
|
||||
*/
|
||||
private OkHttpClient okHttpClient;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
public DifyChatAiStreamClient.Builder apiKey(String apiKeyValue) {
|
||||
this.apiKey = apiKeyValue;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param apiHost
|
||||
* @return
|
||||
*/
|
||||
public DifyChatAiStreamClient.Builder apiHost(String apiHost) {
|
||||
this.apiHost = apiHost;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public DifyChatAiStreamClient.Builder okHttpClient(OkHttpClient val) {
|
||||
this.okHttpClient = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DifyChatAiStreamClient build() {
|
||||
return new DifyChatAiStreamClient(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void streamCompletions(List<Message> messages, DifyChatAIEventSourceListener eventSourceListener,
|
||||
String uid, String conversationId) {
|
||||
if (CollectionUtils.isEmpty(messages)) {
|
||||
log.error("param error:Dify Chat Prompt cannot be empty");
|
||||
throw new ParamBusinessException("prompt");
|
||||
}
|
||||
if (Objects.isNull(eventSourceListener)) {
|
||||
log.error("param error:DifyChatAIEventSourceListener cannot be empty");
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
String lastMessage = messages.get(messages.size() - 1).getContent();
|
||||
log.info("Dify Chat AI, uid:{} conversationId:{} prompt:{}", uid, conversationId, lastMessage);
|
||||
|
||||
try {
|
||||
DifyChatCompletionsOptions chatCompletionsOptions = new DifyChatCompletionsOptions();
|
||||
chatCompletionsOptions.setQuery(lastMessage);
|
||||
chatCompletionsOptions.setResponseMode("streaming");
|
||||
chatCompletionsOptions.setConversationId(conversationId);
|
||||
chatCompletionsOptions.setUser(uid);
|
||||
|
||||
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
String requestBody = mapper.writeValueAsString(chatCompletionsOptions);
|
||||
if (!apiHost.endsWith("/")) {
|
||||
apiHost = apiHost + "/";
|
||||
}
|
||||
String url = this.apiHost + "v1/chat-messages";
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.header("Authorization","Bearer "+apiKey)
|
||||
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
|
||||
.build();
|
||||
//创建事件
|
||||
EventSource eventSource = factory.newEventSource(request, eventSourceListener);
|
||||
log.info("finish invoking Dify Chat ai");
|
||||
} catch (Exception e) {
|
||||
log.error("Dify Chat error", e);
|
||||
eventSourceListener.onFailure(null, e, null);
|
||||
throw new ParamBusinessException();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.listener;
|
||||
|
||||
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.model.AzureChatChoice;
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.model.AzureChatCompletions;
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.model.AzureChatMessage;
|
||||
import ai.chat2db.server.web.api.controller.ai.azure.model.AzureCompletionsUsage;
|
||||
import ai.chat2db.server.web.api.controller.ai.config.LocalCache;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.model.DifyChatConstant;
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.model.DifyChatStreamEvent;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.unfbx.chatgpt.entity.chat.Message;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okhttp3.sse.EventSource;
|
||||
import okhttp3.sse.EventSourceListener;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
public class DifyChatAIEventSourceListener extends EventSourceListener {
|
||||
|
||||
private SseEmitter sseEmitter;
|
||||
|
||||
private String uid;
|
||||
|
||||
private ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
|
||||
|
||||
public DifyChatAIEventSourceListener(SseEmitter sseEmitter, String uid) {
|
||||
this.sseEmitter = sseEmitter;
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(@NotNull EventSource eventSource) {
|
||||
try {
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id("[DONE]")
|
||||
.data("[DONE]"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
sseEmitter.complete();
|
||||
log.info("DifyChatAI close sse connection...");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void onEvent(@NotNull EventSource eventSource, @Nullable String id, @Nullable String type, @NotNull String data) {
|
||||
log.info("DifyChatAI:{}", data);
|
||||
DifyChatStreamEvent event = mapper.readValue(data, DifyChatStreamEvent.class);
|
||||
if (DifyChatConstant.EVENT_END_TAG.equals(event.getEvent())) {
|
||||
log.info("DifyChatAI返回数据结束了");
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id("[DONE]")
|
||||
.data("[DONE]")
|
||||
.reconnectTime(3000));
|
||||
return;
|
||||
}
|
||||
|
||||
String text = event.getAnswer();
|
||||
LocalCache.CACHE.put(DifyChatConstant.CONVERSATION_CACHE_PREFIX + uid, event.getConversationId());
|
||||
log.info("Model ID={} is created at {}.", event.getId(), event.getCreatedAt());
|
||||
Message message = new Message();
|
||||
message.setContent(text);
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id(null)
|
||||
.data(message)
|
||||
.reconnectTime(3000));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NotNull EventSource eventSource, @Nullable Throwable t, @Nullable Response response) {
|
||||
try {
|
||||
if (Objects.isNull(response)) {
|
||||
String message = t.getMessage();
|
||||
Message sseMessage = new Message();
|
||||
sseMessage.setContent(message);
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id("[ERROR]")
|
||||
.data(sseMessage));
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id("[DONE]")
|
||||
.data("[DONE]"));
|
||||
sseEmitter.complete();
|
||||
return;
|
||||
}
|
||||
ResponseBody body = response.body();
|
||||
String bodyString = Objects.nonNull(t) ? t.getMessage() : "";
|
||||
if (Objects.nonNull(body)) {
|
||||
bodyString = body.string();
|
||||
if (StringUtils.isBlank(bodyString) && Objects.nonNull(t)) {
|
||||
bodyString = t.getMessage();
|
||||
}
|
||||
log.error("DifyChatAI sse response:{}", bodyString);
|
||||
} else {
|
||||
log.error("DifyChatAI sse response:{},error:{}", response, t);
|
||||
}
|
||||
eventSource.cancel();
|
||||
Message message = new Message();
|
||||
message.setContent("DifyChatAI error:" + bodyString);
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id("[ERROR]")
|
||||
.data(message));
|
||||
sseEmitter.send(SseEmitter.event()
|
||||
.id("[DONE]")
|
||||
.data("[DONE]"));
|
||||
sseEmitter.complete();
|
||||
} catch (Exception exception) {
|
||||
log.error("DifyChatAI 发送数据异常:", exception);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(@NotNull EventSource eventSource, @NotNull Response response) {
|
||||
log.info("DifyChatAI 建立sse连接...");
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.model;
|
||||
|
||||
public interface DifyChatConstant {
|
||||
|
||||
String CONVERSATION_CACHE_PREFIX="CONVERSATION:";
|
||||
|
||||
String EVENT_END_TAG="message_end";
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
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;
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class DifyChatStreamEvent {
|
||||
|
||||
private String event;
|
||||
@JsonProperty("task_id")
|
||||
private String taskId;
|
||||
private String id;
|
||||
private String answer;
|
||||
@JsonProperty("created_at")
|
||||
private long createdAt;
|
||||
@JsonProperty("conversation_id")
|
||||
private String conversationId;
|
||||
|
||||
}
|
@ -15,7 +15,6 @@ 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;
|
||||
@ -48,7 +47,7 @@ public class ConfigController {
|
||||
@PostMapping("/system_config")
|
||||
public ActionResult systemConfig(@RequestBody SystemConfigRequest request) {
|
||||
SystemConfigParam param = SystemConfigParam.builder().code(request.getCode()).content(request.getContent())
|
||||
.build();
|
||||
.build();
|
||||
configService.createOrUpdate(param);
|
||||
if (OpenAIClient.OPENAI_KEY.equals(request.getCode())) {
|
||||
OpenAIClient.refresh();
|
||||
@ -73,7 +72,7 @@ public class ConfigController {
|
||||
aiSqlSourceEnum = AiSqlSourceEnum.CHAT2DBAI;
|
||||
}
|
||||
SystemConfigParam param = SystemConfigParam.builder().code(RestAIClient.AI_SQL_SOURCE).content(sqlSource)
|
||||
.build();
|
||||
.build();
|
||||
configService.createOrUpdate(param);
|
||||
|
||||
switch (Objects.requireNonNull(aiSqlSourceEnum)) {
|
||||
@ -104,23 +103,10 @@ 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
|
||||
*
|
||||
@ -128,10 +114,10 @@ public class ConfigController {
|
||||
*/
|
||||
private void saveChat2dbAIConfig(AIConfigCreateRequest request) {
|
||||
SystemConfigParam param = SystemConfigParam.builder().code(Chat2dbAIClient.CHAT2DB_OPENAI_KEY).content(
|
||||
request.getApiKey()).build();
|
||||
request.getApiKey()).build();
|
||||
configService.createOrUpdate(param);
|
||||
SystemConfigParam hostParam = SystemConfigParam.builder().code(Chat2dbAIClient.CHAT2DB_OPENAI_HOST).content(
|
||||
request.getApiHost()).build();
|
||||
request.getApiHost()).build();
|
||||
configService.createOrUpdate(hostParam);
|
||||
SystemConfigParam modelParam = SystemConfigParam.builder().code(Chat2dbAIClient.CHAT2DB_OPENAI_MODEL).content(
|
||||
request.getModel()).build();
|
||||
@ -146,16 +132,16 @@ public class ConfigController {
|
||||
*/
|
||||
private void saveOpenAIConfig(AIConfigCreateRequest request) {
|
||||
SystemConfigParam param = SystemConfigParam.builder().code(OpenAIClient.OPENAI_KEY).content(
|
||||
request.getApiKey()).build();
|
||||
request.getApiKey()).build();
|
||||
configService.createOrUpdate(param);
|
||||
SystemConfigParam hostParam = SystemConfigParam.builder().code(OpenAIClient.OPENAI_HOST).content(
|
||||
request.getApiHost()).build();
|
||||
request.getApiHost()).build();
|
||||
configService.createOrUpdate(hostParam);
|
||||
SystemConfigParam httpProxyHostParam = SystemConfigParam.builder().code(OpenAIClient.PROXY_HOST).content(
|
||||
request.getHttpProxyHost()).build();
|
||||
request.getHttpProxyHost()).build();
|
||||
configService.createOrUpdate(httpProxyHostParam);
|
||||
SystemConfigParam httpProxyPortParam = SystemConfigParam.builder().code(OpenAIClient.PROXY_PORT).content(
|
||||
request.getHttpProxyPort()).build();
|
||||
request.getHttpProxyPort()).build();
|
||||
configService.createOrUpdate(httpProxyPortParam);
|
||||
OpenAIClient.refresh();
|
||||
}
|
||||
@ -167,10 +153,10 @@ public class ConfigController {
|
||||
*/
|
||||
private void saveRestAIConfig(AIConfigCreateRequest request) {
|
||||
SystemConfigParam restParam = SystemConfigParam.builder().code(RestAIClient.REST_AI_URL).content(
|
||||
request.getApiHost()).build();
|
||||
request.getApiHost()).build();
|
||||
configService.createOrUpdate(restParam);
|
||||
SystemConfigParam methodParam = SystemConfigParam.builder().code(RestAIClient.REST_AI_STREAM_OUT).content(
|
||||
request.getStream().toString()).build();
|
||||
request.getStream().toString()).build();
|
||||
configService.createOrUpdate(methodParam);
|
||||
RestAIClient.refresh();
|
||||
}
|
||||
@ -182,16 +168,16 @@ public class ConfigController {
|
||||
*/
|
||||
private void saveAzureAIConfig(AIConfigCreateRequest request) {
|
||||
SystemConfigParam apikeyParam = SystemConfigParam.builder().code(AzureOpenAIClient.AZURE_CHATGPT_API_KEY)
|
||||
.content(
|
||||
request.getApiKey()).build();
|
||||
.content(
|
||||
request.getApiKey()).build();
|
||||
configService.createOrUpdate(apikeyParam);
|
||||
SystemConfigParam endpointParam = SystemConfigParam.builder().code(AzureOpenAIClient.AZURE_CHATGPT_ENDPOINT)
|
||||
.content(
|
||||
request.getApiHost()).build();
|
||||
.content(
|
||||
request.getApiHost()).build();
|
||||
configService.createOrUpdate(endpointParam);
|
||||
SystemConfigParam modelParam = SystemConfigParam.builder().code(AzureOpenAIClient.AZURE_CHATGPT_DEPLOYMENT_ID)
|
||||
.content(
|
||||
request.getModel()).build();
|
||||
.content(
|
||||
request.getModel()).build();
|
||||
configService.createOrUpdate(modelParam);
|
||||
AzureOpenAIClient.refresh();
|
||||
}
|
||||
@ -322,9 +308,9 @@ public class ConfigController {
|
||||
config.setApiKey(Objects.nonNull(apiKey.getData()) ? apiKey.getData().getContent() : "");
|
||||
config.setApiHost(Objects.nonNull(apiHost.getData()) ? apiHost.getData().getContent() : "");
|
||||
config.setHttpProxyHost(
|
||||
Objects.nonNull(httpProxyHost.getData()) ? httpProxyHost.getData().getContent() : "");
|
||||
Objects.nonNull(httpProxyHost.getData()) ? httpProxyHost.getData().getContent() : "");
|
||||
config.setHttpProxyPort(
|
||||
Objects.nonNull(httpProxyPort.getData()) ? httpProxyPort.getData().getContent() : "");
|
||||
Objects.nonNull(httpProxyPort.getData()) ? httpProxyPort.getData().getContent() : "");
|
||||
break;
|
||||
case CHAT2DBAI:
|
||||
DataResult<Config> chat2dbApiKey = configService.find(Chat2dbAIClient.CHAT2DB_OPENAI_KEY);
|
||||
@ -332,7 +318,7 @@ public class ConfigController {
|
||||
DataResult<Config> chat2dbModel = configService.find(Chat2dbAIClient.CHAT2DB_OPENAI_MODEL);
|
||||
config.setApiKey(Objects.nonNull(chat2dbApiKey.getData()) ? chat2dbApiKey.getData().getContent() : "");
|
||||
config.setApiHost(
|
||||
Objects.nonNull(chat2dbApiHost.getData()) ? chat2dbApiHost.getData().getContent() : "");
|
||||
Objects.nonNull(chat2dbApiHost.getData()) ? chat2dbApiHost.getData().getContent() : "");
|
||||
config.setModel(Objects.nonNull(chat2dbModel.getData()) ? chat2dbModel.getData().getContent() : "");
|
||||
break;
|
||||
case AZUREAI:
|
||||
@ -348,7 +334,7 @@ public class ConfigController {
|
||||
DataResult<Config> restAiHttpMethod = configService.find(RestAIClient.REST_AI_STREAM_OUT);
|
||||
config.setApiHost(Objects.nonNull(restAiUrl.getData()) ? restAiUrl.getData().getContent() : "");
|
||||
config.setStream(Objects.nonNull(restAiHttpMethod.getData()) ? Boolean.valueOf(
|
||||
restAiHttpMethod.getData().getContent()) : Boolean.TRUE);
|
||||
restAiHttpMethod.getData().getContent()) : Boolean.TRUE);
|
||||
break;
|
||||
case FASTCHATAI:
|
||||
DataResult<Config> fastChatApiKey = configService.find(FastChatAIClient.FASTCHAT_API_KEY);
|
||||
@ -390,12 +376,6 @@ 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;
|
||||
}
|
||||
|
@ -126,8 +126,7 @@ public class RdbDmlController {
|
||||
if (DataSourceTypeEnum.MONGODB.getCode().equals(type)) {
|
||||
param.setSql("db." + request.getTableName() + ".find()");
|
||||
} else {
|
||||
// 拼接`tableName`,避免关键字被占用问题
|
||||
param.setSql("select * from " +"`"+ request.getTableName()+"`");
|
||||
param.setSql("select * from " + request.getTableName());
|
||||
}
|
||||
return dlTemplateService.execute(param)
|
||||
.map(rdbWebConverter::dto2vo);
|
||||
|
Reference in New Issue
Block a user