Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void streamCompletions(List<FastChatMessage> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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<TongyiChatChoice> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;


@Data
public class TongyiChatCompletions {

/*
* A unique identifier associated with this chat completions response.
*/
@JsonProperty(value = "request_id")
private String id;

/*
* The collection of completions choices associated with this completions response.
* 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<TongyiChatChoice> choices;

/*
* Usage information for tokens processed and generated as part of this completions operation.
Expand All @@ -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<TongyiChatChoice> choices,
@JsonProperty(value = "usage") TongyiChatCompletionsUsage usage) {
this.id = id;
this.output = choices;
this.choices = choices;
this.usage = usage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<FastChatMessage> messages;

/*
* A value indicating whether chat completions should be streamed for this request.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}