Changesets · Branches · Tags · Compare
| Changeset | 825624809c0913161adf5de1d6cb4e2bf583bc97b8bf3a48552a91a65ed8eeeb |
|---|---|
| Author | Ting-Wai <40179554+tingwai@users.noreply.github.com> · 2025-09-05 10:13 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-05 10:13 -07:00 |
| Parents | 5e7595cfd516, b350229dfa06 |
6 files changed
modified packages/openai-adapters/src/apis/Gemini.ts +33 −2
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | Completion, |
| 11 | 11 | CompletionCreateParamsNonStreaming, |
| 12 | 12 | CompletionCreateParamsStreaming, |
| 13 | + CompletionUsage, | |
| 13 | 14 | CreateEmbeddingResponse, |
| 14 | 15 | EmbeddingCreateParams, |
| 15 | 16 | Model, |
| @@ -22,6 +23,7 @@ | ||
| 22 | 23 | chatChunkFromDelta, |
| 23 | 24 | customFetch, |
| 24 | 25 | embedding, |
| 26 | + usageChatChunk, | |
| 25 | 27 | } from "../util.js"; |
| 26 | 28 | import { |
| 27 | 29 | convertOpenAIToolToGeminiFunction, |
| @@ -37,6 +39,11 @@ | ||
| 37 | 39 | RerankCreateParams, |
| 38 | 40 | } from "./base.js"; |
| 39 | 41 | |
| 42 | +type UsageInfo = Pick< | |
| 43 | + CompletionUsage, | |
| 44 | + "total_tokens" | "completion_tokens" | "prompt_tokens" | |
| 45 | +>; | |
| 46 | + | |
| 40 | 47 | export class GeminiApi implements BaseLlmApi { |
| 41 | 48 | apiBase: string = "https://generativelanguage.googleapis.com/v1beta/"; |
| 42 | 49 | |
| @@ -240,6 +247,7 @@ | ||
| 240 | 247 | signal: AbortSignal, |
| 241 | 248 | ): Promise<ChatCompletion> { |
| 242 | 249 | let completion = ""; |
| 250 | + let usage: UsageInfo | undefined = undefined; | |
| 243 | 251 | for await (const chunk of this.chatCompletionStream( |
| 244 | 252 | { |
| 245 | 253 | ...body, |
| @@ -247,7 +255,12 @@ | ||
| 247 | 255 | }, |
| 248 | 256 | signal, |
| 249 | 257 | )) { |
| 250 | - completion += chunk.choices[0].delta.content; | |
| 258 | + if (chunk.choices.length > 0) { | |
| 259 | + completion += chunk.choices[0].delta.content || ""; | |
| 260 | + } | |
| 261 | + if (chunk.usage) { | |
| 262 | + usage = chunk.usage; | |
| 263 | + } | |
| 251 | 264 | } |
| 252 | 265 | return { |
| 253 | 266 | id: "", |
| @@ -266,12 +279,13 @@ | ||
| 266 | 279 | }, |
| 267 | 280 | }, |
| 268 | 281 | ], |
| 269 | - usage: undefined, | |
| 282 | + usage, | |
| 270 | 283 | }; |
| 271 | 284 | } |
| 272 | 285 | |
| 273 | 286 | async *handleStreamResponse(response: any, model: string) { |
| 274 | 287 | let buffer = ""; |
| 288 | + let usage: UsageInfo | undefined = undefined; | |
| 275 | 289 | for await (const chunk of streamResponse(response as any)) { |
| 276 | 290 | buffer += chunk; |
| 277 | 291 | if (buffer.startsWith("[")) { |
| @@ -300,6 +314,15 @@ | ||
| 300 | 314 | throw new Error(data.error.message); |
| 301 | 315 | } |
| 302 | 316 | |
| 317 | + // Check for usage metadata | |
| 318 | + if (data.usageMetadata) { | |
| 319 | + usage = { | |
| 320 | + prompt_tokens: data.usageMetadata.promptTokenCount || 0, | |
| 321 | + completion_tokens: data.usageMetadata.candidatesTokenCount || 0, | |
| 322 | + total_tokens: data.usageMetadata.totalTokenCount || 0, | |
| 323 | + }; | |
| 324 | + } | |
| 325 | + | |
| 303 | 326 | // In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec |
| 304 | 327 | const contentParts = data?.candidates?.[0]?.content?.parts; |
| 305 | 328 | if (contentParts) { |
| @@ -338,6 +361,14 @@ | ||
| 338 | 361 | buffer = ""; |
| 339 | 362 | } |
| 340 | 363 | } |
| 364 | + | |
| 365 | + // Emit usage at the end if we have it | |
| 366 | + if (usage) { | |
| 367 | + yield usageChatChunk({ | |
| 368 | + model, | |
| 369 | + usage, | |
| 370 | + }); | |
| 371 | + } | |
| 341 | 372 | } |
| 342 | 373 | |
| 343 | 374 | async *chatCompletionStream( |
modified packages/openai-adapters/src/apis/OpenAI.ts +5 −0
| @@ -35,6 +35,11 @@ | ||
| 35 | 35 | }); |
| 36 | 36 | } |
| 37 | 37 | modifyChatBody<T extends ChatCompletionCreateParams>(body: T): T { |
| 38 | + // Add stream_options to include usage in streaming responses | |
| 39 | + if (body.stream) { | |
| 40 | + (body as any).stream_options = { include_usage: true }; | |
| 41 | + } | |
| 42 | + | |
| 38 | 43 | // o-series models - only apply for official OpenAI API |
| 39 | 44 | const isOfficialOpenAIAPI = this.apiBase === "https://api.openai.com/v1/"; |
| 40 | 45 | if (isOfficialOpenAIAPI) { |
modified packages/openai-adapters/src/apis/Relace.ts +29 −2
| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | Completion, |
| 3 | 3 | CompletionCreateParamsNonStreaming, |
| 4 | 4 | CompletionCreateParamsStreaming, |
| 5 | + CompletionUsage, | |
| 5 | 6 | } from "openai/resources/completions.mjs"; |
| 6 | 7 | import { |
| 7 | 8 | CreateEmbeddingResponse, |
| @@ -16,7 +17,12 @@ | ||
| 16 | 17 | import { Model } from "openai/resources/models.mjs"; |
| 17 | 18 | import { z } from "zod"; |
| 18 | 19 | import { OpenAIConfigSchema } from "../types.js"; |
| 19 | -import { chatChunk, chatCompletion, customFetch } from "../util.js"; | |
| 20 | +import { | |
| 21 | + chatChunk, | |
| 22 | + chatCompletion, | |
| 23 | + customFetch, | |
| 24 | + usageChatChunk, | |
| 25 | +} from "../util.js"; | |
| 20 | 26 | import { |
| 21 | 27 | BaseLlmApi, |
| 22 | 28 | CreateRerankResponse, |
| @@ -24,6 +30,11 @@ | ||
| 24 | 30 | RerankCreateParams, |
| 25 | 31 | } from "./base.js"; |
| 26 | 32 | |
| 33 | +type UsageInfo = Pick< | |
| 34 | + CompletionUsage, | |
| 35 | + "total_tokens" | "completion_tokens" | "prompt_tokens" | |
| 36 | +>; | |
| 37 | + | |
| 27 | 38 | // Relace only supports apply through a /v1/apply endpoint |
| 28 | 39 | export class RelaceApi implements BaseLlmApi { |
| 29 | 40 | private apiBase = "https://instantapply.endpoint.relace.run/v1/"; |
| @@ -41,6 +52,7 @@ | ||
| 41 | 52 | signal: AbortSignal, |
| 42 | 53 | ): Promise<ChatCompletion> { |
| 43 | 54 | let content = ""; |
| 55 | + let usage: UsageInfo | undefined = undefined; | |
| 44 | 56 | |
| 45 | 57 | // Convert the non-streaming params to streaming params |
| 46 | 58 | const streamingBody: ChatCompletionCreateParamsStreaming = { |
| @@ -52,12 +64,18 @@ | ||
| 52 | 64 | streamingBody, |
| 53 | 65 | signal, |
| 54 | 66 | )) { |
| 55 | - content += chunk.choices[0]?.delta?.content || ""; | |
| 67 | + if (chunk.choices.length > 0) { | |
| 68 | + content += chunk.choices[0]?.delta?.content || ""; | |
| 69 | + } | |
| 70 | + if (chunk.usage) { | |
| 71 | + usage = chunk.usage; | |
| 72 | + } | |
| 56 | 73 | } |
| 57 | 74 | |
| 58 | 75 | return chatCompletion({ |
| 59 | 76 | content, |
| 60 | 77 | model: body.model, |
| 78 | + usage, | |
| 61 | 79 | }); |
| 62 | 80 | } |
| 63 | 81 | |
| @@ -115,6 +133,15 @@ | ||
| 115 | 133 | content: mergedCode, |
| 116 | 134 | model: body.model, |
| 117 | 135 | }); |
| 136 | + | |
| 137 | + yield usageChatChunk({ | |
| 138 | + model: body.model, | |
| 139 | + usage: { | |
| 140 | + prompt_tokens: result.usage.prompt_tokens || 0, | |
| 141 | + completion_tokens: result.usage.completion_tokens || 0, | |
| 142 | + total_tokens: result.usage.total_tokens, | |
| 143 | + }, | |
| 144 | + }); | |
| 118 | 145 | } |
| 119 | 146 | |
| 120 | 147 | completionNonStream( |
modified packages/openai-adapters/src/test/adapter-test-utils.ts +1 −0
| @@ -258,6 +258,7 @@ | ||
| 258 | 258 | model: "gpt-4", |
| 259 | 259 | messages: [{ role: "user", content: "hello" }], |
| 260 | 260 | stream: true, |
| 261 | + stream_options: { include_usage: true }, | |
| 261 | 262 | ...testConfig.customBodyOptions, |
| 262 | 263 | }, |
| 263 | 264 | }, |
modified packages/openai-adapters/src/test/main.test.ts +58 −7
| @@ -51,6 +51,20 @@ | ||
| 51 | 51 | model: "gpt-4o", |
| 52 | 52 | apiKey: process.env.OPENAI_API_KEY!, |
| 53 | 53 | roles: ["chat"], |
| 54 | + options: { | |
| 55 | + skipTools: false, | |
| 56 | + expectUsage: true, | |
| 57 | + }, | |
| 58 | + }, | |
| 59 | + { | |
| 60 | + provider: "openai", | |
| 61 | + model: "gpt-4o-mini", | |
| 62 | + apiKey: process.env.OPENAI_API_KEY!, | |
| 63 | + roles: ["chat"], | |
| 64 | + options: { | |
| 65 | + skipTools: false, | |
| 66 | + expectUsage: true, | |
| 67 | + }, | |
| 54 | 68 | }, |
| 55 | 69 | { |
| 56 | 70 | provider: "anthropic", |
| @@ -67,15 +81,32 @@ | ||
| 67 | 81 | model: "gemini-1.5-flash-latest", |
| 68 | 82 | apiKey: process.env.GEMINI_API_KEY!, |
| 69 | 83 | roles: ["chat"], |
| 84 | + options: { | |
| 85 | + skipTools: false, | |
| 86 | + expectUsage: true, | |
| 87 | + }, | |
| 88 | + }, | |
| 89 | + { | |
| 90 | + provider: "gemini", | |
| 91 | + model: "gemini-2.5-flash", | |
| 92 | + apiKey: process.env.GEMINI_API_KEY!, | |
| 93 | + roles: ["chat"], | |
| 94 | + options: { | |
| 95 | + skipTools: false, | |
| 96 | + expectUsage: true, | |
| 97 | + }, | |
| 98 | + }, | |
| 99 | + { | |
| 100 | + provider: "mistral", | |
| 101 | + model: "codestral", | |
| 102 | + apiKey: process.env.MISTRAL_API_KEY!, | |
| 103 | + roles: ["chat"], | |
| 104 | + options: { | |
| 105 | + skipTools: false, | |
| 106 | + expectUsage: true, | |
| 107 | + }, | |
| 70 | 108 | }, |
| 71 | 109 | // { |
| 72 | - // provider: "mistral", | |
| 73 | - // model: "codestral-latest", | |
| 74 | - // apiKey: process.env.MISTRAL_API_KEY!, | |
| 75 | - // apiBase: "https://api.mistral.ai/v1", | |
| 76 | - // roles: ["autocomplete"], | |
| 77 | - // }, | |
| 78 | - // { | |
| 79 | 110 | // provider: "deepseek", |
| 80 | 111 | // model: "deepseek-coder", |
| 81 | 112 | // apiKey: process.env.DEEPSEEK_API_KEY!, |
| @@ -111,6 +142,26 @@ | ||
| 111 | 142 | apiKey: process.env.VOYAGE_API_KEY!, |
| 112 | 143 | roles: ["rerank"], |
| 113 | 144 | }, |
| 145 | + { | |
| 146 | + provider: "relace", | |
| 147 | + model: "instant-apply", | |
| 148 | + apiKey: process.env.RELACE_API_KEY!, | |
| 149 | + roles: ["chat"], | |
| 150 | + options: { | |
| 151 | + skipTools: true, | |
| 152 | + expectUsage: true, | |
| 153 | + }, | |
| 154 | + }, | |
| 155 | + { | |
| 156 | + provider: "inception", | |
| 157 | + model: "mercury-coder", | |
| 158 | + apiKey: process.env.INCEPTION_API_KEY!, | |
| 159 | + roles: ["chat"], | |
| 160 | + options: { | |
| 161 | + skipTools: false, | |
| 162 | + expectUsage: true, | |
| 163 | + }, | |
| 164 | + }, | |
| 114 | 165 | // { |
| 115 | 166 | // provider: "cohere", |
| 116 | 167 | // model: "rerank-v3.5", |
modified packages/openai-adapters/src/test/util.ts +11 −3
| @@ -106,9 +106,17 @@ | ||
| 106 | 106 | expect(usage).toBeDefined(); |
| 107 | 107 | expect(usage!.completion_tokens).toBeGreaterThan(0); |
| 108 | 108 | expect(usage!.prompt_tokens).toBeGreaterThan(0); |
| 109 | - expect(usage!.total_tokens).toEqual( | |
| 110 | - usage!.prompt_tokens + usage!.completion_tokens, | |
| 111 | - ); | |
| 109 | + // Gemini 2.5 models have thinking tokens, so total_tokens >= prompt + completion | |
| 110 | + // Other models should have total_tokens = prompt + completion | |
| 111 | + if (model.includes("gemini-2.5") || model.includes("gemini-2.0")) { | |
| 112 | + expect(usage!.total_tokens).toBeGreaterThanOrEqual( | |
| 113 | + usage!.prompt_tokens + usage!.completion_tokens, | |
| 114 | + ); | |
| 115 | + } else { | |
| 116 | + expect(usage!.total_tokens).toEqual( | |
| 117 | + usage!.prompt_tokens + usage!.completion_tokens, | |
| 118 | + ); | |
| 119 | + } | |
| 112 | 120 | } |
| 113 | 121 | }); |
| 114 | 122 |