From 259343497c6db399853193061d59d58041240385 Mon Sep 17 00:00:00 2001 From: wangbing Date: Fri, 12 Sep 2025 14:10:36 +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 --- .../achat/chat/ChatCompletionResponse.java | 27 +++++++++++++------ .../wbsite/achat/chat/tool/FunctionCall.java | 20 ++++++++++++++ .../xyz/wbsite/achat/chat/tool/ToolCall.java | 12 ++++++--- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/wbsite/achat/chat/ChatCompletionResponse.java b/src/main/java/xyz/wbsite/achat/chat/ChatCompletionResponse.java index e8e0fdc..ef118fe 100644 --- a/src/main/java/xyz/wbsite/achat/chat/ChatCompletionResponse.java +++ b/src/main/java/xyz/wbsite/achat/chat/ChatCompletionResponse.java @@ -1,5 +1,7 @@ package xyz.wbsite.achat.chat; +import xyz.wbsite.achat.chat.tool.ToolCall; + import java.util.ArrayList; import java.util.List; @@ -18,11 +20,9 @@ public class ChatCompletionResponse { private List choices; private Usage usage; - // 无参构造函数 public ChatCompletionResponse() { } - // 私有构造函数,用于Builder模式 private ChatCompletionResponse(Builder builder) { this.id = builder.id; this.object = builder.object; @@ -32,12 +32,10 @@ public class ChatCompletionResponse { this.usage = builder.usage; } - // 静态builder方法,返回Builder实例 public static Builder builder() { return new Builder(); } - // Getters and Setters public String getId() { return id; } @@ -86,7 +84,6 @@ public class ChatCompletionResponse { this.usage = usage; } - // Builder内部类 public static class Builder { private String id; private String object; @@ -115,13 +112,11 @@ public class ChatCompletionResponse { 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; @@ -177,6 +172,7 @@ public class ChatCompletionResponse { private String content; private String name; private String finish_reason; + private List toolCalls = new ArrayList<>(); public ChoiceBuilder index(Integer index) { this.index = index; @@ -203,10 +199,25 @@ public class ChatCompletionResponse { return this; } + public ChoiceBuilder withToolCalls(java.util.function.Consumer> choicesConsumer) { + choicesConsumer.accept(this.toolCalls); + return this; + } + + public ChoiceBuilder addToolCall(ToolCall toolCalls) { + this.toolCalls.add(toolCalls); + return this; + } + public Choice build() { Choice choice = new Choice(); choice.setIndex(index); - choice.setMessage(Message.builder().role(role).content(content).name(name).build()); + choice.setMessage(Message.builder() + .role(role) + .content(content) + .name(name) + .tool_calls(toolCalls) + .build()); choice.setFinish_reason(finish_reason); return choice; } diff --git a/src/main/java/xyz/wbsite/achat/chat/tool/FunctionCall.java b/src/main/java/xyz/wbsite/achat/chat/tool/FunctionCall.java index c15d9f8..7b35d18 100644 --- a/src/main/java/xyz/wbsite/achat/chat/tool/FunctionCall.java +++ b/src/main/java/xyz/wbsite/achat/chat/tool/FunctionCall.java @@ -9,6 +9,11 @@ package xyz.wbsite.achat.chat.tool; */ public class FunctionCall { + /** + * 函数调用ID + */ + private String id; + /** * 函数名称 */ @@ -25,10 +30,19 @@ public class FunctionCall { } private FunctionCall(Builder builder) { + this.id = builder.id; this.name = builder.name; this.arguments = builder.arguments; } + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + public String getName() { return name; } @@ -50,9 +64,15 @@ public class FunctionCall { } public static class Builder { + private String id; private String name; private String arguments; + public Builder id(String id) { + this.id = id; + return this; + } + public Builder name(String name) { this.name = name; return this; diff --git a/src/main/java/xyz/wbsite/achat/chat/tool/ToolCall.java b/src/main/java/xyz/wbsite/achat/chat/tool/ToolCall.java index 6931c46..dc0f2d3 100644 --- a/src/main/java/xyz/wbsite/achat/chat/tool/ToolCall.java +++ b/src/main/java/xyz/wbsite/achat/chat/tool/ToolCall.java @@ -1,7 +1,5 @@ package xyz.wbsite.achat.chat.tool; -import cn.hutool.core.util.RandomUtil; - /** * 工具调用请求 - 符合OpenAI官方API规范 * @@ -10,11 +8,19 @@ import cn.hutool.core.util.RandomUtil; * @since 1.8 */ public class ToolCall { - private final String id = "call_" + RandomUtil.randomNumbers(8); + private String id; private int index; private String type; private FunctionCall function; + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + public int getIndex() { return index; }