diff --git a/src/main/java/cn/bugstack/chatglm/model/ChatCompletionRequest.java b/src/main/java/cn/bugstack/chatglm/model/ChatCompletionRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..af9302f919802120d70b0aee7b2b4ba547959138 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/model/ChatCompletionRequest.java @@ -0,0 +1,87 @@ +package cn.bugstack.chatglm.model; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.*; +import lombok.extern.slf4j.Slf4j; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author 小傅哥,微信:fustack + * @description 请求参数 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +@Data +@Slf4j +@JsonInclude(JsonInclude.Include.NON_NULL) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ChatCompletionRequest { + + /** + * 模型 + */ + private Model model = Model.CHATGLM_6B_SSE; + + /** + * 请求ID + */ + @JsonProperty("request_id") + private String requestId = String.format("xfg-%d", System.currentTimeMillis()); + /** + * 控制温度【随机性】 + */ + private float temperature = 0.9f; + /** + * 多样性控制; + */ + @JsonProperty("top_p") + private float topP = 0.7f; + /** + * 输入给模型的会话信息 + * 用户输入的内容;role=user + * 挟带历史的内容;role=assistant + */ + private List prompt; + /** + * 智普AI sse 固定参数 incremental = true 【增量返回】 + */ + private boolean incremental = true; + /** + * sseformat, 用于兼容解决sse增量模式okhttpsse截取data:后面空格问题, [data: hello]。只在增量模式下使用sseFormat。 + */ + private String sseFormat = "data"; + + @Data + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class Prompt { + private String role; + private String content; + } + + @Override + public String toString() { + Map paramsMap = new HashMap<>(); + paramsMap.put("request_id", requestId); + paramsMap.put("prompt", prompt); + paramsMap.put("incremental", incremental); + paramsMap.put("temperature", temperature); + paramsMap.put("top_p", topP); + paramsMap.put("sseFormat", sseFormat); + try { + return new ObjectMapper().writeValueAsString(paramsMap); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/main/java/cn/bugstack/chatglm/model/ChatCompletionResponse.java b/src/main/java/cn/bugstack/chatglm/model/ChatCompletionResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..6245d800a94172551558b173ea7f3eb800cc92f1 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/model/ChatCompletionResponse.java @@ -0,0 +1,32 @@ +package cn.bugstack.chatglm.model; + +import lombok.Data; + +/** + * @author 小傅哥,微信:fustack + * @description 返回结果 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +@Data +public class ChatCompletionResponse { + + private String data; + private String meta; + + @Data + public static class Meta { + private String task_status; + private Usage usage; + private String task_id; + private String request_id; + } + + @Data + public static class Usage { + private int completion_tokens; + private int prompt_tokens; + private int total_tokens; + } + +} diff --git a/src/main/java/cn/bugstack/chatglm/model/EventType.java b/src/main/java/cn/bugstack/chatglm/model/EventType.java new file mode 100644 index 0000000000000000000000000000000000000000..46fa9a0bdee3ace4e721aa42d1b4ec8d3ec15fd4 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/model/EventType.java @@ -0,0 +1,25 @@ +package cn.bugstack.chatglm.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author 小傅哥,微信:fustack + * @description 消息类型 chatglm_lite + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +@Getter +@AllArgsConstructor +public enum EventType { + + add("add", "增量"), + finish("finish", "结束"), + error("error", "错误"), + interrupted("interrupted", "中断"), + + ; + private final String code; + private final String info; + +} diff --git a/src/main/java/cn/bugstack/chatglm/model/Model.java b/src/main/java/cn/bugstack/chatglm/model/Model.java new file mode 100644 index 0000000000000000000000000000000000000000..5dc351f4250635c50c00edec922d5effcef4e6f4 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/model/Model.java @@ -0,0 +1,25 @@ +package cn.bugstack.chatglm.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author 小傅哥,微信:fustack + * @description 会话模型 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +@Getter +@AllArgsConstructor +public enum Model { + + CHATGLM_6B_SSE("chatGLM_6b_SSE", "ChatGLM-6B 测试模型"), + CHATGLM_LITE("chatglm_lite", "轻量版模型,适用对推理速度和成本敏感的场景"), + CHATGLM_LITE_32K("chatglm_lite_32k", "标准版模型,适用兼顾效果和成本的场景"), + CHATGLM_STD("chatglm_std", "适用于对知识量、推理能力、创造力要求较高的场景"), + CHATGLM_PRO("chatglm_pro", "适用于对知识量、推理能力、创造力要求较高的场景"), + + ; + private final String code; + private final String info; +} diff --git a/src/main/java/cn/bugstack/chatglm/model/Role.java b/src/main/java/cn/bugstack/chatglm/model/Role.java new file mode 100644 index 0000000000000000000000000000000000000000..e66540346502134c5efe75b6edc0f980c69d3351 --- /dev/null +++ b/src/main/java/cn/bugstack/chatglm/model/Role.java @@ -0,0 +1,26 @@ +package cn.bugstack.chatglm.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author 小傅哥,微信:fustack + * @description 角色 + * @github https://github.com/fuzhengwei + * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! + */ +@Getter +@AllArgsConstructor +public enum Role { + /** + * user 用户输入的内容,role位user + */ + user("user"), + /** + * 模型生成的内容,role位assistant + */ + assistant("assistant"), + ; + private final String code; + +}