|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.models; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 20 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 21 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +/** |
| 26 | + * Data Transfer Objects for Chat Completion and Chat Completion Chunk API responses. |
| 27 | + * |
| 28 | + * <p>These classes are used for deserializing JSON responses from the `/chat/completions` endpoint. |
| 29 | + */ |
| 30 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 31 | +final class ChatCompletionsResponse { |
| 32 | + |
| 33 | + private ChatCompletionsResponse() {} |
| 34 | + |
| 35 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 36 | + static class ChatCompletion { |
| 37 | + public String id; |
| 38 | + public List<Choice> choices; |
| 39 | + public Long created; |
| 40 | + public String model; |
| 41 | + public String object; |
| 42 | + |
| 43 | + @JsonProperty("service_tier") |
| 44 | + public String serviceTier; |
| 45 | + |
| 46 | + @JsonProperty("system_fingerprint") |
| 47 | + public String systemFingerprint; |
| 48 | + |
| 49 | + public Usage usage; |
| 50 | + } |
| 51 | + |
| 52 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 53 | + static class Choice { |
| 54 | + @JsonProperty("finish_reason") |
| 55 | + public String finishReason; |
| 56 | + |
| 57 | + public Integer index; |
| 58 | + public Logprobs logprobs; |
| 59 | + public Message message; |
| 60 | + } |
| 61 | + |
| 62 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 63 | + static class ChatCompletionChunk { |
| 64 | + public String id; |
| 65 | + public List<ChunkChoice> choices; |
| 66 | + public Long created; |
| 67 | + public String model; |
| 68 | + public String object; |
| 69 | + |
| 70 | + @JsonProperty("service_tier") |
| 71 | + public String serviceTier; |
| 72 | + |
| 73 | + @JsonProperty("system_fingerprint") |
| 74 | + public String systemFingerprint; |
| 75 | + |
| 76 | + public Usage usage; |
| 77 | + } |
| 78 | + |
| 79 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 80 | + static class ChunkChoice { |
| 81 | + @JsonProperty("finish_reason") |
| 82 | + public String finishReason; |
| 83 | + |
| 84 | + public Integer index; |
| 85 | + public Logprobs logprobs; |
| 86 | + public Message delta; |
| 87 | + } |
| 88 | + |
| 89 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 90 | + static class Message { |
| 91 | + public String content; |
| 92 | + public String refusal; |
| 93 | + public String role; |
| 94 | + |
| 95 | + @JsonProperty("tool_calls") |
| 96 | + public List<ToolCall> toolCalls; |
| 97 | + |
| 98 | + // function_call is not supported in ChatCompletionChunk and ChatCompletion support is |
| 99 | + // deprecated. |
| 100 | + @JsonProperty("function_call") |
| 101 | + public Function functionCall; // Fallback for deprecated top-level function calls |
| 102 | + |
| 103 | + public List<Annotation> annotations; |
| 104 | + public Audio audio; |
| 105 | + } |
| 106 | + |
| 107 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 108 | + static class ToolCall { |
| 109 | + // Index is only used in ChatCompletionChunk. |
| 110 | + public Integer index; |
| 111 | + public String id; |
| 112 | + public String type; |
| 113 | + public Function function; |
| 114 | + public Custom custom; |
| 115 | + |
| 116 | + @JsonProperty("extra_content") |
| 117 | + public Map<String, Object> extraContent; |
| 118 | + } |
| 119 | + |
| 120 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 121 | + static class Function { |
| 122 | + public String name; |
| 123 | + public String arguments; // JSON string |
| 124 | + } |
| 125 | + |
| 126 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 127 | + static class Custom { |
| 128 | + public String input; |
| 129 | + public String name; |
| 130 | + } |
| 131 | + |
| 132 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 133 | + static class Logprobs { |
| 134 | + public List<TokenLogprob> content; |
| 135 | + public List<TokenLogprob> refusal; |
| 136 | + } |
| 137 | + |
| 138 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 139 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 140 | + static class TokenLogprob { |
| 141 | + public String token; |
| 142 | + public List<Integer> bytes; |
| 143 | + public Double logprob; |
| 144 | + |
| 145 | + @JsonProperty("top_logprobs") |
| 146 | + public List<TokenLogprob> topLogprobs; |
| 147 | + } |
| 148 | + |
| 149 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 150 | + static class Usage { |
| 151 | + @JsonProperty("completion_tokens") |
| 152 | + public Integer completionTokens; |
| 153 | + |
| 154 | + @JsonProperty("prompt_tokens") |
| 155 | + public Integer promptTokens; |
| 156 | + |
| 157 | + @JsonProperty("total_tokens") |
| 158 | + public Integer totalTokens; |
| 159 | + |
| 160 | + @JsonProperty("thoughts_token_count") |
| 161 | + public Integer thoughtsTokenCount; // Gemini-specific extension |
| 162 | + |
| 163 | + @JsonProperty("completion_tokens_details") |
| 164 | + public CompletionTokensDetails completionTokensDetails; |
| 165 | + |
| 166 | + @JsonProperty("prompt_tokens_details") |
| 167 | + public PromptTokensDetails promptTokensDetails; |
| 168 | + } |
| 169 | + |
| 170 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 171 | + static class CompletionTokensDetails { |
| 172 | + @JsonProperty("accepted_prediction_tokens") |
| 173 | + public Integer acceptedPredictionTokens; |
| 174 | + |
| 175 | + @JsonProperty("audio_tokens") |
| 176 | + public Integer audioTokens; |
| 177 | + |
| 178 | + @JsonProperty("reasoning_tokens") |
| 179 | + public Integer reasoningTokens; |
| 180 | + |
| 181 | + @JsonProperty("rejected_prediction_tokens") |
| 182 | + public Integer rejectedPredictionTokens; |
| 183 | + } |
| 184 | + |
| 185 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 186 | + static class PromptTokensDetails { |
| 187 | + @JsonProperty("audio_tokens") |
| 188 | + public Integer audioTokens; |
| 189 | + |
| 190 | + @JsonProperty("cached_tokens") |
| 191 | + public Integer cachedTokens; |
| 192 | + } |
| 193 | + |
| 194 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 195 | + static class Annotation { |
| 196 | + public String type; |
| 197 | + |
| 198 | + @JsonProperty("url_citation") |
| 199 | + public UrlCitation urlCitation; |
| 200 | + } |
| 201 | + |
| 202 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 203 | + static class UrlCitation { |
| 204 | + @JsonProperty("end_index") |
| 205 | + public Integer endIndex; |
| 206 | + |
| 207 | + @JsonProperty("start_index") |
| 208 | + public Integer startIndex; |
| 209 | + |
| 210 | + public String title; |
| 211 | + public String url; |
| 212 | + } |
| 213 | + |
| 214 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 215 | + static class Audio { |
| 216 | + public String id; |
| 217 | + public String data; |
| 218 | + |
| 219 | + @JsonProperty("expires_at") |
| 220 | + public Long expiresAt; |
| 221 | + |
| 222 | + public String transcript; |
| 223 | + } |
| 224 | +} |
0 commit comments