feature: update DifyChatAIEventSourceListener

This commit is contained in:
leven.chen
2023-12-23 11:57:37 +08:00
parent 0d8b0891af
commit a4457edb64
4 changed files with 20 additions and 5 deletions

View File

@ -29,6 +29,7 @@ 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;
@ -273,8 +274,8 @@ public class ChatController {
messages.add(currentMessage);
buildSseEmitter(sseEmitter, uid);
DifyChatAIEventSourceListener eventSourceListener = new DifyChatAIEventSourceListener(sseEmitter);
String conversationId = uid + "-" + queryRequest.getDataSourceId();
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;

View File

@ -135,7 +135,7 @@ public class DifyChatAiStreamClient {
DifyChatCompletionsOptions chatCompletionsOptions = new DifyChatCompletionsOptions();
chatCompletionsOptions.setQuery(lastMessage);
chatCompletionsOptions.setResponseMode("streaming");
//chatCompletionsOptions.setConversationId(conversationId);
chatCompletionsOptions.setConversationId(conversationId);
chatCompletionsOptions.setUser(uid);
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);

View File

@ -5,6 +5,8 @@ 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;
@ -29,11 +31,14 @@ 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) {
public DifyChatAIEventSourceListener(SseEmitter sseEmitter, String uid) {
this.sseEmitter = sseEmitter;
this.uid = uid;
}
@Override
@ -54,7 +59,7 @@ public class DifyChatAIEventSourceListener extends EventSourceListener {
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 ("message_end".equals(event.getEvent())) {
if (DifyChatConstant.EVENT_END_TAG.equals(event.getEvent())) {
log.info("DifyChatAI返回数据结束了");
sseEmitter.send(SseEmitter.event()
.id("[DONE]")
@ -64,6 +69,7 @@ public class DifyChatAIEventSourceListener extends EventSourceListener {
}
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);

View File

@ -0,0 +1,8 @@
package ai.chat2db.server.web.api.controller.ai.dify.model;
public interface DifyChatConstant {
String CONVERSATION_CACHE_PREFIX="CONVERSATION:";
String EVENT_END_TAG="message_end";
}