Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit 4fb7f47

Browse files
authored
Added missing request and response fields (#49)
- `ChatCompletionRequest`: added `store`, `metadata` and `serviceTier` fields - `ChatCompletionResponse`: added `serviceTier` field - `Usage`: added `promptTokensDetails` field
1 parent 7aca342 commit 4fb7f47

File tree

6 files changed

+189
-2
lines changed

6 files changed

+189
-2
lines changed

src/main/java/dev/ai4j/openai4j/chat/ChatCompletionRequest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public final class ChatCompletionRequest {
6464
@JsonProperty
6565
private final Boolean parallelToolCalls;
6666
@JsonProperty
67+
private final Boolean store;
68+
@JsonProperty
69+
private final Map<String, String> metadata;
70+
@JsonProperty
71+
private final String serviceTier;
72+
@JsonProperty
6773
@Deprecated
6874
private final List<Function> functions;
6975
@JsonProperty
@@ -90,6 +96,9 @@ private ChatCompletionRequest(Builder builder) {
9096
this.tools = builder.tools;
9197
this.toolChoice = builder.toolChoice;
9298
this.parallelToolCalls = builder.parallelToolCalls;
99+
this.store = builder.store;
100+
this.metadata = builder.metadata;
101+
this.serviceTier = builder.serviceTier;
93102
this.functions = builder.functions;
94103
this.functionCall = builder.functionCall;
95104
}
@@ -170,6 +179,18 @@ public Boolean parallelToolCalls() {
170179
return parallelToolCalls;
171180
}
172181

182+
public Boolean store() {
183+
return store;
184+
}
185+
186+
public Map<String, String> metadata() {
187+
return metadata;
188+
}
189+
190+
public String serviceTier() {
191+
return serviceTier;
192+
}
193+
173194
@Deprecated
174195
public List<Function> functions() {
175196
return functions;
@@ -207,6 +228,9 @@ private boolean equalTo(ChatCompletionRequest another) {
207228
&& Objects.equals(tools, another.tools)
208229
&& Objects.equals(toolChoice, another.toolChoice)
209230
&& Objects.equals(parallelToolCalls, another.parallelToolCalls)
231+
&& Objects.equals(store, another.store)
232+
&& Objects.equals(metadata, another.metadata)
233+
&& Objects.equals(serviceTier, another.serviceTier)
210234
&& Objects.equals(functions, another.functions)
211235
&& Objects.equals(functionCall, another.functionCall);
212236
}
@@ -233,6 +257,9 @@ public int hashCode() {
233257
h += (h << 5) + Objects.hashCode(tools);
234258
h += (h << 5) + Objects.hashCode(toolChoice);
235259
h += (h << 5) + Objects.hashCode(parallelToolCalls);
260+
h += (h << 5) + Objects.hashCode(store);
261+
h += (h << 5) + Objects.hashCode(metadata);
262+
h += (h << 5) + Objects.hashCode(serviceTier);
236263
h += (h << 5) + Objects.hashCode(functions);
237264
h += (h << 5) + Objects.hashCode(functionCall);
238265
return h;
@@ -260,6 +287,9 @@ public String toString() {
260287
+ ", tools=" + tools
261288
+ ", toolChoice=" + toolChoice
262289
+ ", parallelToolCalls=" + parallelToolCalls
290+
+ ", store=" + store
291+
+ ", metadata=" + metadata
292+
+ ", serviceTier=" + serviceTier
263293
+ ", functions=" + functions
264294
+ ", functionCall=" + functionCall
265295
+ "}";
@@ -293,6 +323,9 @@ public static final class Builder {
293323
private List<Tool> tools;
294324
private Object toolChoice;
295325
private Boolean parallelToolCalls;
326+
private Boolean store;
327+
private Map<String, String> metadata;
328+
private String serviceTier;
296329
@Deprecated
297330
private List<Function> functions;
298331
@Deprecated
@@ -321,6 +354,9 @@ public Builder from(ChatCompletionRequest instance) {
321354
tools(instance.tools);
322355
toolChoice(instance.toolChoice);
323356
parallelToolCalls(instance.parallelToolCalls);
357+
store(instance.store);
358+
metadata(instance.metadata);
359+
serviceTier(instance.serviceTier);
324360
functions(instance.functions);
325361
functionCall(instance.functionCall);
326362
return this;
@@ -503,6 +539,23 @@ public Builder parallelToolCalls(Boolean parallelToolCalls) {
503539
return this;
504540
}
505541

542+
public Builder store(Boolean store) {
543+
this.store = store;
544+
return this;
545+
}
546+
547+
public Builder metadata(Map<String, String> metadata) {
548+
if (metadata != null) {
549+
this.metadata = unmodifiableMap(metadata);
550+
}
551+
return this;
552+
}
553+
554+
public Builder serviceTier(String serviceTier) {
555+
this.serviceTier = serviceTier;
556+
return this;
557+
}
558+
506559
@Deprecated
507560
public Builder functions(Function... functions) {
508561
return functions(asList(functions));

src/main/java/dev/ai4j/openai4j/chat/ChatCompletionResponse.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public final class ChatCompletionResponse {
3131
private final Usage usage;
3232
@JsonProperty
3333
private final String systemFingerprint;
34+
@JsonProperty
35+
private final String serviceTier;
3436

3537
private ChatCompletionResponse(Builder builder) {
3638
this.id = builder.id;
@@ -39,6 +41,7 @@ private ChatCompletionResponse(Builder builder) {
3941
this.choices = builder.choices;
4042
this.usage = builder.usage;
4143
this.systemFingerprint = builder.systemFingerprint;
44+
this.serviceTier = builder.serviceTier;
4245
}
4346

4447
public String id() {
@@ -65,6 +68,10 @@ public String systemFingerprint() {
6568
return systemFingerprint;
6669
}
6770

71+
public String serviceTier() {
72+
return serviceTier;
73+
}
74+
6875
/**
6976
* Convenience method to get the content of the message from the first choice.
7077
*/
@@ -85,7 +92,8 @@ private boolean equalTo(ChatCompletionResponse another) {
8592
&& Objects.equals(model, another.model)
8693
&& Objects.equals(choices, another.choices)
8794
&& Objects.equals(usage, another.usage)
88-
&& Objects.equals(systemFingerprint, another.systemFingerprint);
95+
&& Objects.equals(systemFingerprint, another.systemFingerprint)
96+
&& Objects.equals(serviceTier, another.serviceTier);
8997
}
9098

9199
@Override
@@ -97,6 +105,7 @@ public int hashCode() {
97105
h += (h << 5) + Objects.hashCode(choices);
98106
h += (h << 5) + Objects.hashCode(usage);
99107
h += (h << 5) + Objects.hashCode(systemFingerprint);
108+
h += (h << 5) + Objects.hashCode(serviceTier);
100109
return h;
101110
}
102111

@@ -109,6 +118,7 @@ public String toString() {
109118
+ ", choices=" + choices
110119
+ ", usage=" + usage
111120
+ ", systemFingerprint=" + systemFingerprint
121+
+ ", serviceTier=" + serviceTier
112122
+ "}";
113123
}
114124

@@ -127,6 +137,7 @@ public static final class Builder {
127137
private List<ChatCompletionChoice> choices;
128138
private Usage usage;
129139
private String systemFingerprint;
140+
private String serviceTier;
130141

131142
private Builder() {
132143
}
@@ -163,6 +174,11 @@ public Builder systemFingerprint(String systemFingerprint) {
163174
return this;
164175
}
165176

177+
public Builder serviceTier(String serviceTier) {
178+
this.serviceTier = serviceTier;
179+
return this;
180+
}
181+
166182
public ChatCompletionResponse build() {
167183
return new ChatCompletionResponse(this);
168184
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package dev.ai4j.openai4j.shared;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
7+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
8+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
9+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
10+
11+
import java.util.Objects;
12+
13+
@JsonDeserialize(builder = PromptTokensDetails.Builder.class)
14+
@JsonInclude(JsonInclude.Include.NON_NULL)
15+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
16+
public final class PromptTokensDetails {
17+
18+
@JsonProperty
19+
private final Integer cachedTokens;
20+
21+
private PromptTokensDetails(Builder builder) {
22+
this.cachedTokens = builder.cachedTokens;
23+
}
24+
25+
public Integer cachedTokens() {
26+
return cachedTokens;
27+
}
28+
29+
@Override
30+
public boolean equals(Object another) {
31+
if (this == another) return true;
32+
return another instanceof PromptTokensDetails
33+
&& equalTo((PromptTokensDetails) another);
34+
}
35+
36+
private boolean equalTo(PromptTokensDetails another) {
37+
return Objects.equals(cachedTokens, another.cachedTokens);
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
int h = 5381;
43+
h += (h << 5) + Objects.hashCode(cachedTokens);
44+
return h;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "PromptTokensDetails{"
50+
+ "cachedTokens=" + cachedTokens
51+
+ "}";
52+
}
53+
54+
public static Builder builder() {
55+
return new Builder();
56+
}
57+
58+
@JsonPOJOBuilder(withPrefix = "")
59+
@JsonIgnoreProperties(ignoreUnknown = true)
60+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
61+
public static final class Builder {
62+
63+
private Integer cachedTokens;
64+
65+
private Builder() {
66+
}
67+
68+
public Builder cachedTokens(Integer cachedTokens) {
69+
this.cachedTokens = cachedTokens;
70+
return this;
71+
}
72+
73+
public PromptTokensDetails build() {
74+
return new PromptTokensDetails(this);
75+
}
76+
}
77+
}

src/main/java/dev/ai4j/openai4j/shared/Usage.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ public final class Usage {
2020
@JsonProperty
2121
private final Integer promptTokens;
2222
@JsonProperty
23+
private final PromptTokensDetails promptTokensDetails;
24+
@JsonProperty
2325
private final Integer completionTokens;
2426
@JsonProperty
2527
private final CompletionTokensDetails completionTokensDetails;
2628

2729
private Usage(Builder builder) {
2830
this.totalTokens = builder.totalTokens;
2931
this.promptTokens = builder.promptTokens;
32+
this.promptTokensDetails = builder.promptTokensDetails;
3033
this.completionTokens = builder.completionTokens;
3134
this.completionTokensDetails = builder.completionTokensDetails;
3235
}
@@ -39,6 +42,10 @@ public Integer promptTokens() {
3942
return promptTokens;
4043
}
4144

45+
public PromptTokensDetails promptTokensDetails() {
46+
return promptTokensDetails;
47+
}
48+
4249
public Integer completionTokens() {
4350
return completionTokens;
4451
}
@@ -57,6 +64,7 @@ public boolean equals(Object another) {
5764
private boolean equalTo(Usage another) {
5865
return Objects.equals(totalTokens, another.totalTokens)
5966
&& Objects.equals(promptTokens, another.promptTokens)
67+
&& Objects.equals(promptTokensDetails, another.promptTokensDetails)
6068
&& Objects.equals(completionTokens, another.completionTokens)
6169
&& Objects.equals(completionTokensDetails, another.completionTokensDetails);
6270
}
@@ -66,6 +74,7 @@ public int hashCode() {
6674
int h = 5381;
6775
h += (h << 5) + Objects.hashCode(totalTokens);
6876
h += (h << 5) + Objects.hashCode(promptTokens);
77+
h += (h << 5) + Objects.hashCode(promptTokensDetails);
6978
h += (h << 5) + Objects.hashCode(completionTokens);
7079
h += (h << 5) + Objects.hashCode(completionTokensDetails);
7180
return h;
@@ -76,6 +85,7 @@ public String toString() {
7685
return "Usage{"
7786
+ "totalTokens=" + totalTokens
7887
+ ", promptTokens=" + promptTokens
88+
+ ", promptTokensDetails=" + promptTokensDetails
7989
+ ", completionTokens=" + completionTokens
8090
+ ", completionTokensDetails=" + completionTokensDetails
8191
+ "}";
@@ -92,6 +102,7 @@ public static final class Builder {
92102

93103
private Integer totalTokens;
94104
private Integer promptTokens;
105+
private PromptTokensDetails promptTokensDetails;
95106
private Integer completionTokens;
96107
private CompletionTokensDetails completionTokensDetails;
97108

@@ -108,6 +119,11 @@ public Builder promptTokens(Integer promptTokens) {
108119
return this;
109120
}
110121

122+
public Builder promptTokensDetails(PromptTokensDetails promptTokensDetails) {
123+
this.promptTokensDetails = promptTokensDetails;
124+
return this;
125+
}
126+
111127
public Builder completionTokens(Integer completionTokens) {
112128
this.completionTokens = completionTokens;
113129
return this;

src/main/resources/META-INF/native-image/dev.ai4j/openai4j/reflect-config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,15 @@
458458
"allDeclaredFields": true,
459459
"allPublicFields": true
460460
},
461+
{
462+
"name": "dev.ai4j.openai4j.shared.PromptTokensDetails",
463+
"allDeclaredConstructors": true,
464+
"allPublicConstructors": true,
465+
"allDeclaredMethods": true,
466+
"allPublicMethods": true,
467+
"allDeclaredFields": true,
468+
"allPublicFields": true
469+
},
461470
{
462471
"name": "dev.ai4j.openai4j.shared.StreamOptions",
463472
"allDeclaredConstructors": true,

0 commit comments

Comments
 (0)