From 427161221cd661a8d9160d6ef9a76c788721b427 Mon Sep 17 00:00:00 2001 From: magic chen <418379149@qq.com> Date: Wed, 15 May 2024 17:27:43 +0800 Subject: [PATCH] ### 1.5.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 升级gmp到1.9.7 - 支持batch相关api - 优化 function模块下的 GmpFunction ,方法 handleToolMessage 中不再默认指定模型,而是使用 HandleResult 中指定的模型 - 调整 function模块下的 HandleResult ,使用builder的方式来创建对象,新增了gptModel来指定 使用的模型 --- README.md | 7 ++ pom.xml | 4 +- .../WhiteMagic2014/function/DrawFunction.java | 4 +- .../WhiteMagic2014/function/GmpFunction.java | 3 +- .../WhiteMagic2014/function/HandleResult.java | 64 +++++++++++++++---- .../github/WhiteMagic2014/gmpm/GmpMemory.java | 6 +- 6 files changed, 69 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c559b43..d1ef55e 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,13 @@ gpt-magic-plus-memory, 携带上下文且能够归纳对话作为记忆的chat ## Version +### 1.5.2 + +- 升级gmp到1.9.7 +- 支持batch相关api +- 优化 function模块下的 GmpFunction ,方法 handleToolMessage 中不再默认指定模型,而是使用 HandleResult 中指定的模型 +- 调整 function模块下的 HandleResult ,使用builder的方式来创建对象,新增了gptModel来指定 使用的模型 + ### 1.5.1 - 升级gmp到1.9.6,支持gpt-4o diff --git a/pom.xml b/pom.xml index 45d3cf4..ff11a9d 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ https://github.com/WhiteMagic2014/gpt-magic-plus.git io.github.whitemagic2014 gpt-magic-plus - 1.5.1 + 1.5.2 @@ -53,7 +53,7 @@ io.github.whitemagic2014 gpt-magic - 1.9.6 + 1.9.7 org.apache.commons diff --git a/src/main/java/com/github/WhiteMagic2014/function/DrawFunction.java b/src/main/java/com/github/WhiteMagic2014/function/DrawFunction.java index ecf6e33..08f0469 100644 --- a/src/main/java/com/github/WhiteMagic2014/function/DrawFunction.java +++ b/src/main/java/com/github/WhiteMagic2014/function/DrawFunction.java @@ -48,6 +48,8 @@ public HandleResult handle(JSONObject arguments) { } catch (Exception e) { result = "很抱歉,作图的时候出了一点问题,可以稍后再次尝试"; } - return new HandleResult(true, result); + // 如果不需要额外处理 + // HandleResult.builder().result(result).gptProcess(false).build(); + return HandleResult.builder().result(result).gptProcess(true).gptModel(GptModel.gpt_4o).build(); } } diff --git a/src/main/java/com/github/WhiteMagic2014/function/GmpFunction.java b/src/main/java/com/github/WhiteMagic2014/function/GmpFunction.java index e55501c..ff74014 100644 --- a/src/main/java/com/github/WhiteMagic2014/function/GmpFunction.java +++ b/src/main/java/com/github/WhiteMagic2014/function/GmpFunction.java @@ -3,7 +3,6 @@ import com.alibaba.fastjson.JSONObject; import com.github.WhiteMagic2014.gptApi.Chat.CreateChatCompletionRequest; import com.github.WhiteMagic2014.gptApi.Chat.pojo.ChatMessage; -import com.github.WhiteMagic2014.gptApi.GptModel; import com.github.WhiteMagic2014.tool.FunctionTool; import com.github.WhiteMagic2014.util.RequestUtil; @@ -52,7 +51,7 @@ public String handleToolMessage(ChatMessage userMessage, ChatMessage assistantTe if (handleResult.getGptProcess()) { CreateChatCompletionRequest request = new CreateChatCompletionRequest() .addTool(getFunctionTool()) - .model(GptModel.gpt_4_function) + .model(handleResult.getGptModel()) .addMessage(userMessage) .addMessage(assistantTempMessage)// gpt result .addMessage(ChatMessage.toolMessage(callId, handleResult.getResult())); // send a function message with function_name and custom result diff --git a/src/main/java/com/github/WhiteMagic2014/function/HandleResult.java b/src/main/java/com/github/WhiteMagic2014/function/HandleResult.java index 38f8867..c0d9d25 100644 --- a/src/main/java/com/github/WhiteMagic2014/function/HandleResult.java +++ b/src/main/java/com/github/WhiteMagic2014/function/HandleResult.java @@ -12,37 +12,79 @@ public class HandleResult { */ private Boolean gptProcess; + /** + * 如果需要gpt再次处理,指定所使用的gpt模型 + */ + private String gptModel; + /** * 内部处理结果 */ private String result; - public HandleResult(Boolean gptProcess, String result) { - this.gptProcess = gptProcess; - this.result = result; + + protected HandleResult(Builder builder) { + this.gptProcess = builder.gptProcess; + this.gptModel = builder.gptModel; + this.result = builder.result; } - public Boolean getGptProcess() { - return gptProcess; + public static Builder builder() { + return new Builder(); } - public void setGptProcess(Boolean gptProcess) { - this.gptProcess = gptProcess; + public String getGptModel() { + return gptModel; } - public String getResult() { - return result; + public Boolean getGptProcess() { + return gptProcess; } - public void setResult(String result) { - this.result = result; + public String getResult() { + return result; } @Override public String toString() { return "HandleResult{" + "gptProcess=" + gptProcess + + ", gptModel='" + gptModel + '\'' + ", result='" + result + '\'' + '}'; } + + + public static class Builder { + private Boolean gptProcess; + private String gptModel; + private String result; + + public Builder() { + } + + public Builder gptProcess(Boolean gptProcess) { + this.gptProcess = gptProcess; + return this; + } + + public Builder gptModel(String gptModel) { + this.gptModel = gptModel; + return this; + } + + public Builder result(String result) { + this.result = result; + return this; + } + + public HandleResult build() { + if (gptProcess && (gptModel == null || gptModel.isEmpty())) { + throw new RuntimeException("gpt再次处理,请指定模型"); + } + return new HandleResult(this); + } + + } + } diff --git a/src/main/java/com/github/WhiteMagic2014/gmpm/GmpMemory.java b/src/main/java/com/github/WhiteMagic2014/gmpm/GmpMemory.java index 4d0c1df..27c4056 100644 --- a/src/main/java/com/github/WhiteMagic2014/gmpm/GmpMemory.java +++ b/src/main/java/com/github/WhiteMagic2014/gmpm/GmpMemory.java @@ -19,7 +19,7 @@ */ public class GmpMemory { - private String model = GptModel.gpt_3p5_turbo; + private String model = GptModel.gpt_4o; private int maxTokens = 500;// 回答问题限制的 token数量 private boolean stream = false;// 是否使用流式请求 private float temperature = 1.0f; // 拘束 0~2 自由 @@ -128,7 +128,7 @@ private boolean check(String session, String prompt) { } sb.append("\nuser:").append(prompt); String checkResult = originChat(Collections.singletonList(ChatMessage.userMessage(sb.toString()))); - System.out.println("记忆联想:" + !checkResult.contains("不需要")); +// System.out.println("记忆联想:" + !checkResult.contains("不需要")); return !checkResult.contains("不需要"); } @@ -142,7 +142,7 @@ private boolean check(String session, String prompt) { */ public void addChatMemory(String session, String user, String assistant) { String topic = originChat(Collections.singletonList(ChatMessage.userMessage("请概括以下user和assistant的对话:\n" + "user:\n" + user + "\nassistant:\n" + assistant))); - System.out.println("对话归纳:" + topic); +// System.out.println("对话归纳:" + topic); ChatMemory memory = new ChatMemory(); memory.setUser(user);