From 9bfbd3674dca84776f374d7977418e84652070c2 Mon Sep 17 00:00:00 2001 From: wangbing Date: Wed, 3 Sep 2025 13:52:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xyz/wbsite/achat/ChatController.java | 2 +- .../xyz/wbsite/achat/OpenAiController.java | 11 +- .../achat/core/chat/ChatCompletionChunk.java | 131 ++++++----- .../core/chat/ChatCompletionRequest.java | 2 - .../core/chat/ChatCompletionResponse.java | 151 +++++++------ .../achat/core/chat/CompletionResponse.java | 149 +++++++------ .../achat/core/chat/EmbeddingsRequest.java | 12 ++ .../achat/core/chat/EmbeddingsResponse.java | 204 ++++++++++++++++++ .../achat/core/{message => chat}/Message.java | 2 +- .../achat/core/{message => chat}/Role.java | 2 +- .../achat/core/{message => chat}/Status.java | 2 +- .../core/{message => chat}/StreamEmitter.java | 5 +- .../xyz/wbsite/achat/core/chat/Usage.java | 88 ++++++++ .../wbsite/achat/core/message/AiMessage.java | 13 -- .../achat/core/message/UserMessage.java | 41 ---- .../achat/core/prompt/MessagePrompt.java | 2 +- .../core/service/ChatCompletionGenerator.java | 2 +- .../achat/core/service/ChatService.java | 6 +- .../achat/core/service/SessionService.java | 2 +- .../service/impl/ChatServiceSampleImpl.java | 83 +++---- .../impl/SessionServiceMemoryImpl.java | 2 +- 21 files changed, 606 insertions(+), 306 deletions(-) create mode 100644 src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsRequest.java create mode 100644 src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsResponse.java rename src/main/java/xyz/wbsite/achat/core/{message => chat}/Message.java (97%) rename src/main/java/xyz/wbsite/achat/core/{message => chat}/Role.java (95%) rename src/main/java/xyz/wbsite/achat/core/{message => chat}/Status.java (85%) rename src/main/java/xyz/wbsite/achat/core/{message => chat}/StreamEmitter.java (96%) create mode 100644 src/main/java/xyz/wbsite/achat/core/chat/Usage.java delete mode 100644 src/main/java/xyz/wbsite/achat/core/message/AiMessage.java delete mode 100644 src/main/java/xyz/wbsite/achat/core/message/UserMessage.java diff --git a/src/main/java/xyz/wbsite/achat/ChatController.java b/src/main/java/xyz/wbsite/achat/ChatController.java index 0b0c3be..3a07edd 100644 --- a/src/main/java/xyz/wbsite/achat/ChatController.java +++ b/src/main/java/xyz/wbsite/achat/ChatController.java @@ -8,7 +8,7 @@ //import org.springframework.web.bind.annotation.ResponseBody; //import org.springframework.web.bind.annotation.RestController; //import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; -//import xyz.wbsite.achat.core.message.Message; +//import xyz.wbsite.achat.core.chat.Message; //import xyz.wbsite.achat.core.Result; //import xyz.wbsite.achat.core.Session; //import xyz.wbsite.achat.core.prompt.MessagePrompt; diff --git a/src/main/java/xyz/wbsite/achat/OpenAiController.java b/src/main/java/xyz/wbsite/achat/OpenAiController.java index 10165e1..8545578 100644 --- a/src/main/java/xyz/wbsite/achat/OpenAiController.java +++ b/src/main/java/xyz/wbsite/achat/OpenAiController.java @@ -1,6 +1,5 @@ package xyz.wbsite.achat; -import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -10,6 +9,8 @@ import org.springframework.web.bind.annotation.RestController; import xyz.wbsite.achat.core.chat.ChatCompletionRequest; import xyz.wbsite.achat.core.chat.CompletionRequest; import xyz.wbsite.achat.core.chat.CompletionResponse; +import xyz.wbsite.achat.core.chat.EmbeddingsRequest; +import xyz.wbsite.achat.core.chat.EmbeddingsResponse; import xyz.wbsite.achat.core.model.ModelListResponse; import xyz.wbsite.achat.core.service.ChatService; @@ -31,8 +32,6 @@ public class OpenAiController { @Resource private ChatService chatService; - private static final ObjectMapper objectMapper = new ObjectMapper(); - @RequestMapping() public String info() { return "OpenAI API is support"; @@ -82,4 +81,10 @@ public class OpenAiController { return chatService.chat(request); } } + + @RequestMapping("/embeddings") + public EmbeddingsResponse embeddings(@RequestBody EmbeddingsRequest request) { + return chatService.embeddings(request); + } + } diff --git a/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionChunk.java b/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionChunk.java index bc239e7..bca4c3e 100644 --- a/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionChunk.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionChunk.java @@ -30,6 +30,46 @@ public class ChatCompletionChunk { return new Builder(); } + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + public long getCreated() { + return created; + } + + public void setCreated(long created) { + this.created = created; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public List getChoices() { + return choices; + } + + public void setChoices(List choices) { + this.choices = choices; + } + public static class Builder { private String id; private String object; @@ -74,49 +114,9 @@ public class ChatCompletionChunk { } } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getObject() { - return object; - } - - public void setObject(String object) { - this.object = object; - } - - public long getCreated() { - return created; - } - - public void setCreated(long created) { - this.created = created; - } - - public String getModel() { - return model; - } - - public void setModel(String model) { - this.model = model; - } - - public List getChoices() { - return choices; - } - - public void setChoices(List choices) { - this.choices = choices; - } - public static class Choice { private int index = 0; - private Delta delta; + private Message delta; private String finish_reason; public Integer getIndex() { @@ -127,11 +127,11 @@ public class ChatCompletionChunk { this.index = index; } - public Delta getDelta() { + public Message getDelta() { return delta; } - public void setDelta(Delta delta) { + public void setDelta(Message delta) { this.delta = delta; } @@ -144,27 +144,44 @@ public class ChatCompletionChunk { } } - /** - * 增量内容对象 - */ - public static class Delta { - private String role; + public static ChoiceBuilder choiceBuilder() { + return new ChoiceBuilder(); + } + + public static class ChoiceBuilder { + private Integer index; + private Role role; private String content; + private String name; + private String finish_reason; - public String getRole() { - return role; + public ChoiceBuilder index(Integer index) { + this.index = index; + return this; } - - public void setRole(String role) { + public ChoiceBuilder role(Role role) { this.role = role; + return this; } - - public String getContent() { - return content; + public ChoiceBuilder content(String content) { + this.content = content; + return this; + } + public ChoiceBuilder name(String name) { + this.name = name; + return this; + } + public ChoiceBuilder finish_reason(String finish_reason) { + this.finish_reason = finish_reason; + return this; } - public void setContent(String content) { - this.content = content; + public Choice build() { + Choice choice = new Choice(); + choice.setIndex(index); + choice.setDelta(Message.builder().role(role).content(content).name(name).build()); + choice.setFinish_reason(finish_reason); + return choice; } } } \ No newline at end of file diff --git a/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionRequest.java b/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionRequest.java index 75d740f..2977a2e 100644 --- a/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionRequest.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionRequest.java @@ -1,7 +1,5 @@ package xyz.wbsite.achat.core.chat; -import xyz.wbsite.achat.core.message.Message; - import java.util.List; /** diff --git a/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionResponse.java b/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionResponse.java index e62983b..738668d 100644 --- a/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionResponse.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/ChatCompletionResponse.java @@ -1,7 +1,5 @@ package xyz.wbsite.achat.core.chat; -import xyz.wbsite.achat.core.message.Message; - import java.util.ArrayList; import java.util.List; @@ -39,57 +37,6 @@ public class ChatCompletionResponse { return new Builder(); } - // Builder内部类 - public static class Builder { - private String id; - private String object; - private long created; - private String model; - private List choices = new ArrayList<>(); - private Usage usage; - - public Builder id(String id) { - this.id = id; - return this; - } - - public Builder object(String object) { - this.object = object; - return this; - } - - public Builder created(long created) { - this.created = created; - return this; - } - - public Builder model(String model) { - this.model = model; - return this; - } - - // 设置整个choices列表 - public Builder choices(List choices) { - this.choices = choices; - return this; - } - - // 使用lambda表达式构建choices列表,优化代码可读性 - public Builder withChoices(java.util.function.Consumer> choicesConsumer) { - choicesConsumer.accept(this.choices); - return this; - } - - public Builder usage(Usage usage) { - this.usage = usage; - return this; - } - - public ChatCompletionResponse build() { - return new ChatCompletionResponse(this); - } - } - // Getters and Setters public String getId() { return id; @@ -139,6 +86,57 @@ public class ChatCompletionResponse { this.usage = usage; } + // Builder内部类 + public static class Builder { + private String id; + private String object; + private long created; + private String model; + private List choices = new ArrayList<>(); + private Usage usage; + + public Builder id(String id) { + this.id = id; + return this; + } + + public Builder object(String object) { + this.object = object; + return this; + } + + public Builder created(long created) { + this.created = created; + return this; + } + + public Builder model(String model) { + this.model = model; + return this; + } + + // 设置整个choices列表 + public Builder choices(List choices) { + this.choices = choices; + return this; + } + + // 使用lambda表达式构建choices列表,优化代码可读性 + public Builder withChoices(java.util.function.Consumer> choicesConsumer) { + choicesConsumer.accept(this.choices); + return this; + } + + public Builder usage(Usage usage) { + this.usage = usage; + return this; + } + + public ChatCompletionResponse build() { + return new ChatCompletionResponse(this); + } + } + public static class Choice { private int index = 0; private Message message; @@ -169,33 +167,48 @@ public class ChatCompletionResponse { } } - public static class Usage { - private int prompt_tokens; - private int completion_tokens; - private int total_tokens; + public static ChoiceBuilder choiceBuilder() { + return new ChoiceBuilder(); + } - public int getPrompt_tokens() { - return prompt_tokens; + public static class ChoiceBuilder { + private Integer index; + private Role role; + private String content; + private String name; + private String finish_reason; + + public ChoiceBuilder index(Integer index) { + this.index = index; + return this; } - public void setPrompt_tokens(int prompt_tokens) { - this.prompt_tokens = prompt_tokens; + public ChoiceBuilder role(Role role) { + this.role = role; + return this; } - public int getCompletion_tokens() { - return completion_tokens; + public ChoiceBuilder content(String content) { + this.content = content; + return this; } - public void setCompletion_tokens(int completion_tokens) { - this.completion_tokens = completion_tokens; + public ChoiceBuilder name(String name) { + this.name = name; + return this; } - public int getTotal_tokens() { - return total_tokens; + public ChoiceBuilder finish_reason(String finish_reason) { + this.finish_reason = finish_reason; + return this; } - public void setTotal_tokens(int total_tokens) { - this.total_tokens = total_tokens; + public Choice build() { + Choice choice = new Choice(); + choice.setIndex(index); + choice.setMessage(Message.builder().role(role).content(content).name(name).build()); + choice.setFinish_reason(finish_reason); + return choice; } } } \ No newline at end of file diff --git a/src/main/java/xyz/wbsite/achat/core/chat/CompletionResponse.java b/src/main/java/xyz/wbsite/achat/core/chat/CompletionResponse.java index cd49dd7..23318ca 100644 --- a/src/main/java/xyz/wbsite/achat/core/chat/CompletionResponse.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/CompletionResponse.java @@ -1,7 +1,5 @@ package xyz.wbsite.achat.core.chat; -import xyz.wbsite.achat.core.message.Message; - import java.util.ArrayList; import java.util.List; @@ -29,63 +27,10 @@ public class CompletionResponse { this.usage = builder.usage; } - // 静态builder方法,返回Builder实例 public static Builder builder() { return new Builder(); } - // Builder内部类 - public static class Builder { - private String id; - private String object; - private long created; - private String model; - private List choices = new ArrayList<>(); - private Usage usage; - - public Builder id(String id) { - this.id = id; - return this; - } - - public Builder object(String object) { - this.object = object; - return this; - } - - public Builder created(long created) { - this.created = created; - return this; - } - - public Builder model(String model) { - this.model = model; - return this; - } - - // 设置整个choices列表 - public Builder choices(List choices) { - this.choices = choices; - return this; - } - - // 使用lambda表达式构建choices列表,优化代码可读性 - public Builder withChoices(java.util.function.Consumer> choicesConsumer) { - choicesConsumer.accept(this.choices); - return this; - } - - public Builder usage(Usage usage) { - this.usage = usage; - return this; - } - - // 构建CompletionResponse对象 - public CompletionResponse build() { - return new CompletionResponse(this); - } - } - public String getId() { return id; } @@ -134,6 +79,53 @@ public class CompletionResponse { this.usage = usage; } + public static class Builder { + private String id; + private String object; + private long created; + private String model; + private List choices = new ArrayList<>(); + private Usage usage; + + public Builder id(String id) { + this.id = id; + return this; + } + + public Builder object(String object) { + this.object = object; + return this; + } + + public Builder created(long created) { + this.created = created; + return this; + } + + public Builder model(String model) { + this.model = model; + return this; + } + + public Builder choices(List choices) { + this.choices = choices; + return this; + } + + public Builder withChoices(java.util.function.Consumer> choicesConsumer) { + choicesConsumer.accept(this.choices); + return this; + } + + public Builder usage(Usage usage) { + this.usage = usage; + return this; + } + + public CompletionResponse build() { + return new CompletionResponse(this); + } + } public static class Choice { private int index = 0; @@ -165,33 +157,48 @@ public class CompletionResponse { } } - public static class Usage { - private int prompt_tokens; - private int completion_tokens; - private int total_tokens; + public static ChoiceBuilder choiceBuilder() { + return new ChoiceBuilder(); + } - public int getPrompt_tokens() { - return prompt_tokens; + public static class ChoiceBuilder { + private Integer index; + private Role role; + private String content; + private String name; + private String finish_reason; + + public ChoiceBuilder index(Integer index) { + this.index = index; + return this; } - public void setPrompt_tokens(int prompt_tokens) { - this.prompt_tokens = prompt_tokens; + public ChoiceBuilder role(Role role) { + this.role = role; + return this; } - public int getCompletion_tokens() { - return completion_tokens; + public ChoiceBuilder content(String content) { + this.content = content; + return this; } - public void setCompletion_tokens(int completion_tokens) { - this.completion_tokens = completion_tokens; + public ChoiceBuilder name(String name) { + this.name = name; + return this; } - public int getTotal_tokens() { - return total_tokens; + public ChoiceBuilder finish_reason(String finish_reason) { + this.finish_reason = finish_reason; + return this; } - public void setTotal_tokens(int total_tokens) { - this.total_tokens = total_tokens; + public Choice build() { + Choice choice = new Choice(); + choice.setIndex(index); + choice.setMessage(Message.builder().role(role).content(content).name(name).build()); + choice.setFinish_reason(finish_reason); + return choice; } } } \ No newline at end of file diff --git a/src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsRequest.java b/src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsRequest.java new file mode 100644 index 0000000..faf5d26 --- /dev/null +++ b/src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsRequest.java @@ -0,0 +1,12 @@ +package xyz.wbsite.achat.core.chat; + +/** + * 嵌入请求 + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 + */ +public class EmbeddingsRequest { + +} \ No newline at end of file diff --git a/src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsResponse.java b/src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsResponse.java new file mode 100644 index 0000000..aa80fd8 --- /dev/null +++ b/src/main/java/xyz/wbsite/achat/core/chat/EmbeddingsResponse.java @@ -0,0 +1,204 @@ +package xyz.wbsite.achat.core.chat; + +import java.util.ArrayList; +import java.util.List; + +/** + * OpenAI聊天完成响应 - 符合OpenAI官方API规范 + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 + */ +public class EmbeddingsResponse { + private String id; + private String object; + private long created; + private String model; + private List choices; + private Usage usage; + + private EmbeddingsResponse(Builder builder) { + this.id = builder.id; + this.object = builder.object; + this.created = builder.created; + this.model = builder.model; + this.choices = builder.choices; + this.usage = builder.usage; + } + + public static Builder builder() { + return new Builder(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + public long getCreated() { + return created; + } + + public void setCreated(long created) { + this.created = created; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public List getChoices() { + return choices; + } + + public void setChoices(List choices) { + this.choices = choices; + } + + public Usage getUsage() { + return usage; + } + + public void setUsage(Usage usage) { + this.usage = usage; + } + + public static class Builder { + private String id; + private String object; + private long created; + private String model; + private List choices = new ArrayList<>(); + private Usage usage; + + public Builder id(String id) { + this.id = id; + return this; + } + + public Builder object(String object) { + this.object = object; + return this; + } + + public Builder created(long created) { + this.created = created; + return this; + } + + public Builder model(String model) { + this.model = model; + return this; + } + + public Builder choices(List choices) { + this.choices = choices; + return this; + } + + public Builder withChoices(java.util.function.Consumer> choicesConsumer) { + choicesConsumer.accept(this.choices); + return this; + } + + public Builder usage(Usage usage) { + this.usage = usage; + return this; + } + + public EmbeddingsResponse build() { + return new EmbeddingsResponse(this); + } + } + + public static class Choice { + private int index = 0; + private Message message; + private String finish_reason; + + public Integer getIndex() { + return index; + } + + public void setIndex(Integer index) { + this.index = index; + } + + public Message getMessage() { + return message; + } + + public void setMessage(Message message) { + this.message = message; + } + + public String getFinish_reason() { + return finish_reason; + } + + public void setFinish_reason(String finish_reason) { + this.finish_reason = finish_reason; + } + } + + public static ChoiceBuilder choiceBuilder() { + return new ChoiceBuilder(); + } + + public static class ChoiceBuilder { + private Integer index; + private Role role; + private String content; + private String name; + private String finish_reason; + + public ChoiceBuilder index(Integer index) { + this.index = index; + return this; + } + + public ChoiceBuilder role(Role role) { + this.role = role; + return this; + } + + public ChoiceBuilder content(String content) { + this.content = content; + return this; + } + + public ChoiceBuilder name(String name) { + this.name = name; + return this; + } + + public ChoiceBuilder finish_reason(String finish_reason) { + this.finish_reason = finish_reason; + return this; + } + + public Choice build() { + Choice choice = new Choice(); + choice.setIndex(index); + choice.setMessage(Message.builder().role(role).content(content).name(name).build()); + choice.setFinish_reason(finish_reason); + return choice; + } + } +} \ No newline at end of file diff --git a/src/main/java/xyz/wbsite/achat/core/message/Message.java b/src/main/java/xyz/wbsite/achat/core/chat/Message.java similarity index 97% rename from src/main/java/xyz/wbsite/achat/core/message/Message.java rename to src/main/java/xyz/wbsite/achat/core/chat/Message.java index c0c746b..6da9f16 100644 --- a/src/main/java/xyz/wbsite/achat/core/message/Message.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/Message.java @@ -1,4 +1,4 @@ -package xyz.wbsite.achat.core.message; +package xyz.wbsite.achat.core.chat; /** * 消息 diff --git a/src/main/java/xyz/wbsite/achat/core/message/Role.java b/src/main/java/xyz/wbsite/achat/core/chat/Role.java similarity index 95% rename from src/main/java/xyz/wbsite/achat/core/message/Role.java rename to src/main/java/xyz/wbsite/achat/core/chat/Role.java index beabf57..af6ae0e 100644 --- a/src/main/java/xyz/wbsite/achat/core/message/Role.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/Role.java @@ -1,4 +1,4 @@ -package xyz.wbsite.achat.core.message; +package xyz.wbsite.achat.core.chat; import com.fasterxml.jackson.annotation.JsonValue; diff --git a/src/main/java/xyz/wbsite/achat/core/message/Status.java b/src/main/java/xyz/wbsite/achat/core/chat/Status.java similarity index 85% rename from src/main/java/xyz/wbsite/achat/core/message/Status.java rename to src/main/java/xyz/wbsite/achat/core/chat/Status.java index b314e8e..2fbe72e 100644 --- a/src/main/java/xyz/wbsite/achat/core/message/Status.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/Status.java @@ -1,4 +1,4 @@ -package xyz.wbsite.achat.core.message; +package xyz.wbsite.achat.core.chat; /** * 消息状态枚举 diff --git a/src/main/java/xyz/wbsite/achat/core/message/StreamEmitter.java b/src/main/java/xyz/wbsite/achat/core/chat/StreamEmitter.java similarity index 96% rename from src/main/java/xyz/wbsite/achat/core/message/StreamEmitter.java rename to src/main/java/xyz/wbsite/achat/core/chat/StreamEmitter.java index 65150c3..df5d4bc 100644 --- a/src/main/java/xyz/wbsite/achat/core/message/StreamEmitter.java +++ b/src/main/java/xyz/wbsite/achat/core/chat/StreamEmitter.java @@ -1,7 +1,6 @@ -package xyz.wbsite.achat.core.message; +package xyz.wbsite.achat.core.chat; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; -import xyz.wbsite.achat.core.chat.ChatCompletionRequest; import xyz.wbsite.achat.core.service.ChatCompletionGenerator; import java.util.concurrent.ExecutorService; @@ -69,7 +68,6 @@ public class StreamEmitter extends SseEmitter { } } - // 静态builder方法,返回Builder实例 public static Builder builder() { return new Builder(); } @@ -95,7 +93,6 @@ public class StreamEmitter extends SseEmitter { } } - // 保留原有的构造函数以保持向后兼容 public StreamEmitter(ChatCompletionRequest chatCompletionRequest, ChatCompletionGenerator chatCompletionGenerator) { super(Long.MAX_VALUE); this.chatCompletionRequest = chatCompletionRequest; diff --git a/src/main/java/xyz/wbsite/achat/core/chat/Usage.java b/src/main/java/xyz/wbsite/achat/core/chat/Usage.java new file mode 100644 index 0000000..447d54c --- /dev/null +++ b/src/main/java/xyz/wbsite/achat/core/chat/Usage.java @@ -0,0 +1,88 @@ +package xyz.wbsite.achat.core.chat; + +/** + * 令牌使用统计 + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 + */ +public class Usage { + + /** + * 提示词 tokens 数量 + */ + private int prompt_tokens; + + /** + * 补全 tokens 数量 + */ + private int completion_tokens; + + /** + * 总 tokens 数量 + */ + private int total_tokens; + + public Usage() { + } + + private Usage(Builder builder) { + this.prompt_tokens = builder.prompt_tokens; + this.completion_tokens = builder.completion_tokens; + this.total_tokens = builder.total_tokens; + } + + public static Builder builder() { + return new Builder(); + } + + public int getPrompt_tokens() { + return prompt_tokens; + } + + public void setPrompt_tokens(int prompt_tokens) { + this.prompt_tokens = prompt_tokens; + } + + public int getCompletion_tokens() { + return completion_tokens; + } + + public void setCompletion_tokens(int completion_tokens) { + this.completion_tokens = completion_tokens; + } + + public int getTotal_tokens() { + return total_tokens; + } + + public void setTotal_tokens(int total_tokens) { + this.total_tokens = total_tokens; + } + + public static class Builder { + private int prompt_tokens; + private int completion_tokens; + private int total_tokens; + + public Builder prompt_tokens(int prompt_tokens) { + this.prompt_tokens = prompt_tokens; + return this; + } + + public Builder completion_tokens(int completion_tokens) { + this.completion_tokens = completion_tokens; + return this; + } + + public Builder total_tokens(int total_tokens) { + this.total_tokens = total_tokens; + return this; + } + + public Usage build() { + return new Usage(this); + } + } +} diff --git a/src/main/java/xyz/wbsite/achat/core/message/AiMessage.java b/src/main/java/xyz/wbsite/achat/core/message/AiMessage.java deleted file mode 100644 index 1909116..0000000 --- a/src/main/java/xyz/wbsite/achat/core/message/AiMessage.java +++ /dev/null @@ -1,13 +0,0 @@ -//package xyz.wbsite.achat.core.message; -// -// -///** -// * 用户消息 -// * -// * @author wangbing -// * @version 0.0.1 -// * @since 1.8 -// */ -//public class AiMessage extends Message { -// -//} diff --git a/src/main/java/xyz/wbsite/achat/core/message/UserMessage.java b/src/main/java/xyz/wbsite/achat/core/message/UserMessage.java deleted file mode 100644 index 10f8962..0000000 --- a/src/main/java/xyz/wbsite/achat/core/message/UserMessage.java +++ /dev/null @@ -1,41 +0,0 @@ -//package xyz.wbsite.achat.core.message; -// -//import xyz.wbsite.achat.core.Attachment; -// -//import java.util.List; -// -///** -// * 用户消息 -// * -// * @author wangbing -// * @version 0.0.1 -// * @since 1.8 -// */ -//public class UserMessage extends Message { -// -// /** -// * 用户ID -// */ -// private String uid; -// -// /** -// * 附件 -// */ -// private List attachments; -// -// public String getUid() { -// return uid; -// } -// -// public void setUid(String uid) { -// this.uid = uid; -// } -// -// public List getAttachments() { -// return attachments; -// } -// -// public void setAttachments(List attachments) { -// this.attachments = attachments; -// } -//} diff --git a/src/main/java/xyz/wbsite/achat/core/prompt/MessagePrompt.java b/src/main/java/xyz/wbsite/achat/core/prompt/MessagePrompt.java index 22c627c..009a9be 100644 --- a/src/main/java/xyz/wbsite/achat/core/prompt/MessagePrompt.java +++ b/src/main/java/xyz/wbsite/achat/core/prompt/MessagePrompt.java @@ -1,6 +1,6 @@ //package xyz.wbsite.achat.core.prompt; // -//import xyz.wbsite.achat.core.message.Message; +//import xyz.wbsite.achat.core.chat.Message; //import xyz.wbsite.achat.core.Prompt; //import xyz.wbsite.achat.core.message.UserMessage; // diff --git a/src/main/java/xyz/wbsite/achat/core/service/ChatCompletionGenerator.java b/src/main/java/xyz/wbsite/achat/core/service/ChatCompletionGenerator.java index 894568e..690e107 100644 --- a/src/main/java/xyz/wbsite/achat/core/service/ChatCompletionGenerator.java +++ b/src/main/java/xyz/wbsite/achat/core/service/ChatCompletionGenerator.java @@ -1,7 +1,7 @@ package xyz.wbsite.achat.core.service; import xyz.wbsite.achat.core.chat.ChatCompletionRequest; -import xyz.wbsite.achat.core.message.StreamEmitter; +import xyz.wbsite.achat.core.chat.StreamEmitter; /** * 推理生成器 diff --git a/src/main/java/xyz/wbsite/achat/core/service/ChatService.java b/src/main/java/xyz/wbsite/achat/core/service/ChatService.java index 583eade..b44250f 100644 --- a/src/main/java/xyz/wbsite/achat/core/service/ChatService.java +++ b/src/main/java/xyz/wbsite/achat/core/service/ChatService.java @@ -4,7 +4,9 @@ import xyz.wbsite.achat.core.chat.ChatCompletionRequest; import xyz.wbsite.achat.core.chat.ChatCompletionResponse; import xyz.wbsite.achat.core.chat.CompletionRequest; import xyz.wbsite.achat.core.chat.CompletionResponse; -import xyz.wbsite.achat.core.message.StreamEmitter; +import xyz.wbsite.achat.core.chat.EmbeddingsRequest; +import xyz.wbsite.achat.core.chat.EmbeddingsResponse; +import xyz.wbsite.achat.core.chat.StreamEmitter; public interface ChatService { @@ -13,4 +15,6 @@ public interface ChatService { ChatCompletionResponse chat(ChatCompletionRequest request); StreamEmitter streamChat(ChatCompletionRequest request); + + EmbeddingsResponse embeddings(EmbeddingsRequest request); } diff --git a/src/main/java/xyz/wbsite/achat/core/service/SessionService.java b/src/main/java/xyz/wbsite/achat/core/service/SessionService.java index 2ed3806..13b97c7 100644 --- a/src/main/java/xyz/wbsite/achat/core/service/SessionService.java +++ b/src/main/java/xyz/wbsite/achat/core/service/SessionService.java @@ -1,7 +1,7 @@ //package xyz.wbsite.achat.core.service; // //import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; -//import xyz.wbsite.achat.core.message.Message; +//import xyz.wbsite.achat.core.chat.Message; //import xyz.wbsite.achat.core.Result; //import xyz.wbsite.achat.core.Session; //import xyz.wbsite.achat.core.prompt.MessagePrompt; diff --git a/src/main/java/xyz/wbsite/achat/core/service/impl/ChatServiceSampleImpl.java b/src/main/java/xyz/wbsite/achat/core/service/impl/ChatServiceSampleImpl.java index 0c5ce4d..12a5c99 100644 --- a/src/main/java/xyz/wbsite/achat/core/service/impl/ChatServiceSampleImpl.java +++ b/src/main/java/xyz/wbsite/achat/core/service/impl/ChatServiceSampleImpl.java @@ -4,22 +4,20 @@ import xyz.wbsite.achat.core.chat.ChatCompletionRequest; import xyz.wbsite.achat.core.chat.ChatCompletionResponse; import xyz.wbsite.achat.core.chat.CompletionRequest; import xyz.wbsite.achat.core.chat.CompletionResponse; -import xyz.wbsite.achat.core.message.Message; -import xyz.wbsite.achat.core.message.Role; -import xyz.wbsite.achat.core.message.StreamEmitter; +import xyz.wbsite.achat.core.chat.EmbeddingsRequest; +import xyz.wbsite.achat.core.chat.EmbeddingsResponse; +import xyz.wbsite.achat.core.chat.Role; +import xyz.wbsite.achat.core.chat.StreamEmitter; +import xyz.wbsite.achat.core.chat.Usage; import xyz.wbsite.achat.core.service.ChatService; -import java.util.ArrayList; -import java.util.List; - public class ChatServiceSampleImpl implements ChatService { @Override public CompletionResponse prompt(CompletionRequest request) { - CompletionResponse response = new CompletionResponse(); - response.setObject("chat.completion"); - response.setCreated(System.currentTimeMillis() / 1000); - response.setModel(request.getModel()); - +// CompletionResponse response = new CompletionResponse(); +// response.setObject("chat.completion"); +// response.setCreated(System.currentTimeMillis() / 1000); +// response.setModel(request.getModel()); // List choices = new ArrayList<>(); // choices.add(Choice.builder().index(0).message(Message.builder().role(Role.ASSISTANT).content("您好,我还没有接入AI,请接入后再试!").build()).finish_reason("stop").build()); // response.setChoices(choices); @@ -31,37 +29,41 @@ public class ChatServiceSampleImpl implements ChatService { .created(System.currentTimeMillis() / 1000) .model(request.getModel()) .withChoices(choices -> { -// Choice choice1 = new Choice(); - CompletionResponse.Choice - choice1.setIndex(0); - choice1.setMessage(new Message()); - choice1.setFinish_reason("stop"); - choices.add(choice1); - - Choice choice2 = new Choice(); - choice2.setIndex(1); - choice2.setMessage(new Message()); - choice2.setFinish_reason("stop"); - choices.add(choice2); + choices.add(CompletionResponse.choiceBuilder() + .index(0) + .role(Role.ASSISTANT) + .content("您好,我还没有接入AI,请接入后再试!") + .finish_reason("stop").build()); }) - - .usage(CompletionResponse.UsageBuilder().prompt_tokens(10).completion_tokens(10).total_tokens(10).build()) + .usage(Usage.builder() + .prompt_tokens(10) + .completion_tokens(10) + .total_tokens(10) + .build()) .build(); } @Override public ChatCompletionResponse chat(ChatCompletionRequest request) { - ChatCompletionResponse response = new ChatCompletionResponse(); - response.setId("chatcmpl-" + System.currentTimeMillis()); - response.setObject("chat.completion"); - response.setCreated(System.currentTimeMillis() / 1000); - response.setModel(request.getModel()); - - List choices = new ArrayList<>(); - choices.add(Choice.builder().index(0).message(Message.builder().role(Role.ASSISTANT).content("您好,我还没有接入AI,请接入后再试!").build()).finish_reason("stop").build()); - response.setChoices(choices); - response.setUsage(Usage.builder().prompt_tokens(10).completion_tokens(20).total_tokens(30).build()); - return response; + return ChatCompletionResponse.builder() + .id("chatcmpl-" + System.currentTimeMillis()) + .object("chat.completion") + .created(System.currentTimeMillis() / 1000) + .model(request.getModel()) + .withChoices(choices -> { + choices.add(ChatCompletionResponse.choiceBuilder() + .index(0) + .role(Role.ASSISTANT) + .content("您好,我还没有接入AI,请接入后再试!") + .finish_reason("stop") + .build()); + }) + .usage(Usage.builder() + .prompt_tokens(10) + .completion_tokens(10) + .total_tokens(10) + .build()) + .build(); } @Override @@ -73,10 +75,12 @@ public class ChatServiceSampleImpl implements ChatService { if (request.getMessages() == null || request.getMessages().isEmpty()) { throw new IllegalArgumentException("消息不能为空"); } - return StreamEmitter.builder() + StreamEmitter streamEmitter = StreamEmitter.builder() .chatCompletionRequest(request) .build(); + return streamEmitter; + // SseEmitter emitter = new SseEmitter(Long.MAX_VALUE); // 在单独的线程中处理流式响应 // new Thread(() -> { @@ -107,4 +111,9 @@ public class ChatServiceSampleImpl implements ChatService { // // return emitter; } + + @Override + public EmbeddingsResponse embeddings(EmbeddingsRequest request) { + return null; + } } diff --git a/src/main/java/xyz/wbsite/achat/core/service/impl/SessionServiceMemoryImpl.java b/src/main/java/xyz/wbsite/achat/core/service/impl/SessionServiceMemoryImpl.java index d5fc745..9a2f1a1 100644 --- a/src/main/java/xyz/wbsite/achat/core/service/impl/SessionServiceMemoryImpl.java +++ b/src/main/java/xyz/wbsite/achat/core/service/impl/SessionServiceMemoryImpl.java @@ -3,7 +3,7 @@ //import org.springframework.stereotype.Service; //import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; //import xyz.wbsite.achat.core.message.MessageSseEmitter; -//import xyz.wbsite.achat.core.message.Message; +//import xyz.wbsite.achat.core.chat.Message; //import xyz.wbsite.achat.core.Result; //import xyz.wbsite.achat.core.Session; //import xyz.wbsite.achat.core.prompt.MessagePrompt;