From 22b623b1d31eda5175bcc6efdf172df9c59fe94b Mon Sep 17 00:00:00 2001 From: xianyu120 Date: Mon, 9 Oct 2023 11:18:00 +0800 Subject: [PATCH] Mon Oct 9 11:18:00 CST 2023 inscode --- .../java/org/enthusa/askdata/Application.java | 22 +++++++-- .../askdata/ext/inscode/GptClient.java | 26 +++++++++-- .../askdata/ext/inscode/GptRequest.java | 45 +++++++++++++++++-- 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/enthusa/askdata/Application.java b/src/main/java/org/enthusa/askdata/Application.java index 6422796..b3fc6c0 100644 --- a/src/main/java/org/enthusa/askdata/Application.java +++ b/src/main/java/org/enthusa/askdata/Application.java @@ -11,19 +11,35 @@ import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; /** - * @author henry - * @date 2023/6/22 + * Application类是应用程序的入口类 + * 包含了main方法和处理主页请求的方法 + * 该类还注入了GlobalSetting对象 + * + * author henry + * date 2023/6/22 */ @Controller @SpringBootApplication public class Application { + /** + * main方法是应用程序的入口方法 + * @param args 命令行参数 + */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } + /** + * globalSetting属性表示全局设置对象 + */ @Resource private GlobalSetting globalSetting; + /** + * 处理主页请求的方法 + * @param model 视图模型对象 + * @return ModelAndView对象 + */ @GetMapping("/") public ModelAndView home(ModelAndView model) { String url = String.format("http://xiyu.zhiyuanbiji.cn/api/askdata?env=%s", globalSetting.getEnv()); @@ -32,4 +48,4 @@ public class Application { model.setViewName("main"); return model; } -} +} \ No newline at end of file diff --git a/src/main/java/org/enthusa/askdata/ext/inscode/GptClient.java b/src/main/java/org/enthusa/askdata/ext/inscode/GptClient.java index 7acc227..4890424 100644 --- a/src/main/java/org/enthusa/askdata/ext/inscode/GptClient.java +++ b/src/main/java/org/enthusa/askdata/ext/inscode/GptClient.java @@ -13,13 +13,17 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** - * @author henry - * @date 2023/7/1 + * GptClient类用于与GPT模型进行交互 + * 通过发送请求与GPT模型进行对话完成 */ @Slf4j public class GptClient { private OkHttpClient httpClient; + /** + * GptClient的构造函数 + * @param builder GptClient的构造器 + */ private GptClient(Builder builder) { httpClient = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) @@ -28,16 +32,32 @@ public class GptClient { .build(); } + /** + * 创建GptClient的构造器 + * @return GptClient的构造器 + */ public static Builder builder() { return new Builder(); } + /** + * GptClient的构造器类 + */ public static final class Builder { + /** + * 构建GptClient对象 + * @return GptClient对象 + */ public GptClient build() { return new GptClient(this); } } + /** + * 与GPT模型进行对话完成 + * @param completion GptRequest对象,包含对话的请求信息 + * @return 对话完成后的文本 + */ public String chatCompletion(GptRequest completion) { String json = JSON.toJSONString(completion); log.debug("GptRequest: {}", json); @@ -65,4 +85,4 @@ public class GptClient { } return ""; } -} +} \ No newline at end of file diff --git a/src/main/java/org/enthusa/askdata/ext/inscode/GptRequest.java b/src/main/java/org/enthusa/askdata/ext/inscode/GptRequest.java index 5494ddd..cfec5c2 100644 --- a/src/main/java/org/enthusa/askdata/ext/inscode/GptRequest.java +++ b/src/main/java/org/enthusa/askdata/ext/inscode/GptRequest.java @@ -10,11 +10,18 @@ import java.util.ArrayList; import java.util.List; /** + * GptRequest类用于表示GPT请求 + * 包含了消息列表、温度、停止列表和API密钥等属性 + * 该类还包含了添加用户消息和助手消息的方法,以及创建新请求的静态方法 + * * @author henry * @date 2023/7/1 */ @Data public class GptRequest { + /** + * Role枚举表示消息的角色,包括System、Assistant和User + */ private enum Role { System, Assistant, User } @@ -23,25 +30,40 @@ public class GptRequest { private List messages; /** - * 在0和2之间使用什么采样温度?较高的值如0.8会使输出更随机,而较低的值如0.2会使其更加集中和确定性。 - * 默认值: 1 + * temperature属性表示在0和2之间使用的采样温度 + * 较高的值如0.8会使输出更随机,而较低的值如0.2会使其更加集中和确定性 + * 默认值为1 */ @Setter @Getter private Double temperature; + /** + * stop属性表示停止列表,用于指定生成文本的终止条件 + */ @Setter @Getter private List stop; + /** + * apiKey属性表示API密钥 + */ @JsonProperty("apikey") @JSONField(name = "apikey") private String apiKey; + /** + * 构造方法,初始化消息列表 + */ private GptRequest() { messages = new ArrayList<>(); } + /** + * 根据角色和消息内容添加消息到消息列表中 + * @param role 消息的角色 + * @param query 消息的内容 + */ private void addQueryWithRole(Role role, String query) { Message msg = new Message(); msg.setRole(role.name().toLowerCase()); @@ -49,23 +71,40 @@ public class GptRequest { messages.add(msg); } + /** + * 添加用户消息到消息列表中 + * @param msg 用户消息内容 + */ public void addUserMsg(String msg) { addQueryWithRole(Role.User, msg); } + /** + * 添加助手消息到消息列表中 + * @param msg 助手消息内容 + */ public void addAssistantMsg(String msg) { addQueryWithRole(Role.Assistant, msg); } + /** + * 创建新的GptRequest对象,并添加系统描述消息到消息列表中 + * @param systemDesc 系统描述消息内容 + * @return 新的GptRequest对象 + */ public static GptRequest newRequest(String systemDesc) { GptRequest request = new GptRequest(); request.addQueryWithRole(Role.System, systemDesc); return request; } + /** + * Message类用于表示消息 + * 包含了角色和内容属性 + */ @Data public static class Message { private String role; private String content; } -} +} \ No newline at end of file -- GitLab