上传备份

master
王兵 1 month ago
parent ff46e6e846
commit 259343497c

@ -1,5 +1,7 @@
package xyz.wbsite.achat.chat; package xyz.wbsite.achat.chat;
import xyz.wbsite.achat.chat.tool.ToolCall;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -18,11 +20,9 @@ public class ChatCompletionResponse {
private List<Choice> choices; private List<Choice> choices;
private Usage usage; private Usage usage;
// 无参构造函数
public ChatCompletionResponse() { public ChatCompletionResponse() {
} }
// 私有构造函数用于Builder模式
private ChatCompletionResponse(Builder builder) { private ChatCompletionResponse(Builder builder) {
this.id = builder.id; this.id = builder.id;
this.object = builder.object; this.object = builder.object;
@ -32,12 +32,10 @@ public class ChatCompletionResponse {
this.usage = builder.usage; this.usage = builder.usage;
} }
// 静态builder方法返回Builder实例
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
// Getters and Setters
public String getId() { public String getId() {
return id; return id;
} }
@ -86,7 +84,6 @@ public class ChatCompletionResponse {
this.usage = usage; this.usage = usage;
} }
// Builder内部类
public static class Builder { public static class Builder {
private String id; private String id;
private String object; private String object;
@ -115,13 +112,11 @@ public class ChatCompletionResponse {
return this; return this;
} }
// 设置整个choices列表
public Builder choices(List<Choice> choices) { public Builder choices(List<Choice> choices) {
this.choices = choices; this.choices = choices;
return this; return this;
} }
// 使用lambda表达式构建choices列表优化代码可读性
public Builder withChoices(java.util.function.Consumer<List<Choice>> choicesConsumer) { public Builder withChoices(java.util.function.Consumer<List<Choice>> choicesConsumer) {
choicesConsumer.accept(this.choices); choicesConsumer.accept(this.choices);
return this; return this;
@ -177,6 +172,7 @@ public class ChatCompletionResponse {
private String content; private String content;
private String name; private String name;
private String finish_reason; private String finish_reason;
private List<ToolCall> toolCalls = new ArrayList<>();
public ChoiceBuilder index(Integer index) { public ChoiceBuilder index(Integer index) {
this.index = index; this.index = index;
@ -203,10 +199,25 @@ public class ChatCompletionResponse {
return this; return this;
} }
public ChoiceBuilder withToolCalls(java.util.function.Consumer<List<ToolCall>> choicesConsumer) {
choicesConsumer.accept(this.toolCalls);
return this;
}
public ChoiceBuilder addToolCall(ToolCall toolCalls) {
this.toolCalls.add(toolCalls);
return this;
}
public Choice build() { public Choice build() {
Choice choice = new Choice(); Choice choice = new Choice();
choice.setIndex(index); 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); choice.setFinish_reason(finish_reason);
return choice; return choice;
} }

@ -9,6 +9,11 @@ package xyz.wbsite.achat.chat.tool;
*/ */
public class FunctionCall { public class FunctionCall {
/**
* ID
*/
private String id;
/** /**
* *
*/ */
@ -25,10 +30,19 @@ public class FunctionCall {
} }
private FunctionCall(Builder builder) { private FunctionCall(Builder builder) {
this.id = builder.id;
this.name = builder.name; this.name = builder.name;
this.arguments = builder.arguments; this.arguments = builder.arguments;
} }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -50,9 +64,15 @@ public class FunctionCall {
} }
public static class Builder { public static class Builder {
private String id;
private String name; private String name;
private String arguments; private String arguments;
public Builder id(String id) {
this.id = id;
return this;
}
public Builder name(String name) { public Builder name(String name) {
this.name = name; this.name = name;
return this; return this;

@ -1,7 +1,5 @@
package xyz.wbsite.achat.chat.tool; package xyz.wbsite.achat.chat.tool;
import cn.hutool.core.util.RandomUtil;
/** /**
* - OpenAIAPI * - OpenAIAPI
* *
@ -10,11 +8,19 @@ import cn.hutool.core.util.RandomUtil;
* @since 1.8 * @since 1.8
*/ */
public class ToolCall { public class ToolCall {
private final String id = "call_" + RandomUtil.randomNumbers(8); private String id;
private int index; private int index;
private String type; private String type;
private FunctionCall function; private FunctionCall function;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getIndex() { public int getIndex() {
return index; return index;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.