mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-30 19:22:58 +08:00
feature: dify chat stream client add builder
This commit is contained in:
@ -1,9 +1,16 @@
|
||||
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;
|
||||
|
||||
@ -21,15 +28,63 @@ public class DifyChatAIClient {
|
||||
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);
|
||||
}
|
||||
|
||||
public static DifyChatAIClient getInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void streamCompletions(List<Message> messages, DifyChatAIEventSourceListener eventSourceListener) {
|
||||
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,4 +1,30 @@
|
||||
package ai.chat2db.server.web.api.controller.ai.dify.client;
|
||||
|
||||
import ai.chat2db.server.web.api.controller.ai.dify.listener.DifyChatAIEventSourceListener;
|
||||
import com.unfbx.chatgpt.entity.chat.Message;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DifyChatAiStreamClient {
|
||||
|
||||
/**
|
||||
* apikey
|
||||
*/
|
||||
@Getter
|
||||
@NotNull
|
||||
private String apiKey;
|
||||
|
||||
/**
|
||||
* apiHost
|
||||
*/
|
||||
@Getter
|
||||
@NotNull
|
||||
private String apiHost;
|
||||
|
||||
|
||||
public void streamCompletions(List<Message> messages, DifyChatAIEventSourceListener eventSourceListener) {
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user