上传备份

master
王兵 1 month ago
parent ff46e6e846
commit 259343497c

@ -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<Choice> 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<Choice> choices) {
this.choices = choices;
return this;
}
// 使用lambda表达式构建choices列表优化代码可读性
public Builder withChoices(java.util.function.Consumer<List<Choice>> 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<ToolCall> 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<List<ToolCall>> 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;
}

@ -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;

@ -1,7 +1,5 @@
package xyz.wbsite.achat.chat.tool;
import cn.hutool.core.util.RandomUtil;
/**
* - OpenAIAPI
*
@ -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;
}

Loading…
Cancel
Save

Powered by TurnKey Linux.