diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/client/TongyiChatAIStreamClient.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/client/TongyiChatAIStreamClient.java index 91635b353..1d08f27d6 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/client/TongyiChatAIStreamClient.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/client/TongyiChatAIStreamClient.java @@ -189,7 +189,7 @@ public void streamCompletions(List chatMessages, EventSourceLis chatCompletionsOptions.setParameters(parameters); TongyiChatMessage tongyiChatMessage = new TongyiChatMessage(); tongyiChatMessage.setMessages(chatMessages); - chatCompletionsOptions.setInput(tongyiChatMessage); + chatCompletionsOptions.setMessages(chatMessages); EventSource.Factory factory = EventSources.createFactory(this.okHttpClient); ObjectMapper mapper = new ObjectMapper(); diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/listener/TongyiChatAIEventSourceListener.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/listener/TongyiChatAIEventSourceListener.java index 388649a9e..787752bc1 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/listener/TongyiChatAIEventSourceListener.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/listener/TongyiChatAIEventSourceListener.java @@ -1,9 +1,6 @@ package ai.chat2db.server.web.api.controller.ai.tongyi.listener; -import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatChoice; -import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletions; -import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletionsUsage; -import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage; +import ai.chat2db.server.web.api.controller.ai.tongyi.model.TongyiChatChoice; import ai.chat2db.server.web.api.controller.ai.tongyi.model.TongyiChatCompletions; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -18,6 +15,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException; +import java.util.List; import java.util.Objects; /** @@ -63,15 +61,18 @@ public void onEvent(EventSource eventSource, String id, String type, String data } TongyiChatCompletions chatCompletions = mapper.readValue(data, TongyiChatCompletions.class); - String text = chatCompletions.getOutput().getText(); - log.info("id: {}, text: {}", chatCompletions.getId(), text); + List choices = chatCompletions.getChoices(); + for (TongyiChatChoice choice : choices) { + String text = choice.getDelta().getContent(); + log.info("id: {}, text: {}", chatCompletions.getId(), text); - Message message = new Message(); - message.setContent(text); - sseEmitter.send(SseEmitter.event() - .id(null) - .data(message) - .reconnectTime(3000)); + Message message = new Message(); + message.setContent(text); + sseEmitter.send(SseEmitter.event() + .id(null) + .data(message) + .reconnectTime(3000)); + } } @Override diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatOutput.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatChoice.java similarity index 76% rename from chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatOutput.java rename to chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatChoice.java index fabeefe52..8ae487685 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatOutput.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatChoice.java @@ -13,13 +13,17 @@ * choices generated. */ @Data -public final class TongyiChatOutput { +public final class TongyiChatChoice { /* * The generated text for a given completions prompt. */ - @JsonProperty(value = "text") - private String text; +// @JsonProperty(value = "text") +// private String text; + + private TongyiDelta delta; + +// private Integer index; /* * Reason for finishing @@ -30,14 +34,14 @@ public final class TongyiChatOutput { /** * Creates an instance of Choice class. * - * @param text the text value to set. + * @param delta the text value to set. * @param finishReason the finishReason value to set. */ @JsonCreator - private TongyiChatOutput( - @JsonProperty(value = "text") String text, + private TongyiChatChoice( + @JsonProperty(value = "delta") TongyiDelta delta, @JsonProperty(value = "finish_reason") String finishReason) { - this.text = text; + this.delta = delta; this.finishReason = finishReason; } } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletions.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletions.java index f0066b9f2..bfd5b8ccd 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletions.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletions.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; +import java.util.List; + @Data public class TongyiChatCompletions { @@ -11,7 +13,6 @@ public class TongyiChatCompletions { /* * A unique identifier associated with this chat completions response. */ - @JsonProperty(value = "request_id") private String id; /* @@ -19,7 +20,7 @@ public class TongyiChatCompletions { * Generally, `n` choices are generated per provided prompt with a default value of 1. * Token limits and other settings may limit the number of choices generated. */ - private TongyiChatOutput output; + private List choices; /* * Usage information for tokens processed and generated as part of this completions operation. @@ -35,11 +36,11 @@ public class TongyiChatCompletions { */ @JsonCreator private TongyiChatCompletions( - @JsonProperty(value = "request_id") String id, - @JsonProperty(value = "output") TongyiChatOutput choices, + @JsonProperty(value = "id") String id, + @JsonProperty(value = "choices") List choices, @JsonProperty(value = "usage") TongyiChatCompletionsUsage usage) { this.id = id; - this.output = choices; + this.choices = choices; this.usage = usage; } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletionsOptions.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletionsOptions.java index ee08a65c4..b70988af5 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletionsOptions.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiChatCompletionsOptions.java @@ -3,8 +3,10 @@ // Code generated by Microsoft (R) AutoRest Code Generator. package ai.chat2db.server.web.api.controller.ai.tongyi.model; +import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage; import lombok.Data; +import java.util.List; import java.util.Map; /** @@ -20,7 +22,7 @@ public final class TongyiChatCompletionsOptions { * the behavior of the assistant, followed by alternating messages between the User and * Assistant roles. */ - private TongyiChatMessage input; + private List messages; /* * A value indicating whether chat completions should be streamed for this request. diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiDelta.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiDelta.java new file mode 100644 index 000000000..127ee3b88 --- /dev/null +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/tongyi/model/TongyiDelta.java @@ -0,0 +1,9 @@ +package ai.chat2db.server.web.api.controller.ai.tongyi.model; + +import lombok.Data; + +@Data +public final class TongyiDelta { + private String content; + private String role; +}