Changesets · Branches · Tags · Compare
| Changeset | 3c52fb4450251184f0baf779742726179e8c0adeff27f889258b2e8d548d856e |
|---|---|
| Author | Ting-Wai To <40179554+tingwai@users.noreply.github.com> · 2025-08-28 11:46 -07:00 |
| Committer | Ting-Wai To <40179554+tingwai@users.noreply.github.com> · 2025-08-28 11:46 -07:00 |
| Parents | 7a6a62e41ecd |
3 files changed
modified packages/openai-adapters/src/apis/Gemini.ts +29 −2
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | chatChunkFromDelta, |
| 23 | 23 | customFetch, |
| 24 | 24 | embedding, |
| 25 | + usageChatChunk, | |
| 25 | 26 | } from "../util.js"; |
| 26 | 27 | import { |
| 27 | 28 | convertOpenAIToolToGeminiFunction, |
| @@ -37,6 +38,8 @@ | ||
| 37 | 38 | RerankCreateParams, |
| 38 | 39 | } from "./base.js"; |
| 39 | 40 | |
| 41 | +type UsageInfo = { total_tokens: number; completion_tokens: number; prompt_tokens: number }; | |
| 42 | + | |
| 40 | 43 | export class GeminiApi implements BaseLlmApi { |
| 41 | 44 | apiBase: string = "https://generativelanguage.googleapis.com/v1beta/"; |
| 42 | 45 | |
| @@ -240,6 +243,7 @@ | ||
| 240 | 243 | signal: AbortSignal, |
| 241 | 244 | ): Promise<ChatCompletion> { |
| 242 | 245 | let completion = ""; |
| 246 | + let usage: UsageInfo | undefined = undefined; | |
| 243 | 247 | for await (const chunk of this.chatCompletionStream( |
| 244 | 248 | { |
| 245 | 249 | ...body, |
| @@ -247,7 +251,12 @@ | ||
| 247 | 251 | }, |
| 248 | 252 | signal, |
| 249 | 253 | )) { |
| 250 | - completion += chunk.choices[0].delta.content; | |
| 254 | + if (chunk.choices.length > 0) { | |
| 255 | + completion += chunk.choices[0].delta.content || ""; | |
| 256 | + } | |
| 257 | + if (chunk.usage) { | |
| 258 | + usage = chunk.usage; | |
| 259 | + } | |
| 251 | 260 | } |
| 252 | 261 | return { |
| 253 | 262 | id: "", |
| @@ -266,12 +275,13 @@ | ||
| 266 | 275 | }, |
| 267 | 276 | }, |
| 268 | 277 | ], |
| 269 | - usage: undefined, | |
| 278 | + usage, | |
| 270 | 279 | }; |
| 271 | 280 | } |
| 272 | 281 | |
| 273 | 282 | async *handleStreamResponse(response: any, model: string) { |
| 274 | 283 | let buffer = ""; |
| 284 | + let usage: UsageInfo | undefined = undefined; | |
| 275 | 285 | for await (const chunk of streamResponse(response as any)) { |
| 276 | 286 | buffer += chunk; |
| 277 | 287 | if (buffer.startsWith("[")) { |
| @@ -300,6 +310,15 @@ | ||
| 300 | 310 | throw new Error(data.error.message); |
| 301 | 311 | } |
| 302 | 312 | |
| 313 | + // Check for usage metadata | |
| 314 | + if (data.usageMetadata) { | |
| 315 | + usage = { | |
| 316 | + prompt_tokens: data.usageMetadata.promptTokenCount || 0, | |
| 317 | + completion_tokens: data.usageMetadata.candidatesTokenCount || 0, | |
| 318 | + total_tokens: data.usageMetadata.totalTokenCount || 0, | |
| 319 | + }; | |
| 320 | + } | |
| 321 | + | |
| 303 | 322 | // In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec |
| 304 | 323 | const contentParts = data?.candidates?.[0]?.content?.parts; |
| 305 | 324 | if (contentParts) { |
| @@ -338,6 +357,14 @@ | ||
| 338 | 357 | buffer = ""; |
| 339 | 358 | } |
| 340 | 359 | } |
| 360 | + | |
| 361 | + // Emit usage at the end if we have it | |
| 362 | + if (usage) { | |
| 363 | + yield usageChatChunk({ | |
| 364 | + model, | |
| 365 | + usage, | |
| 366 | + }); | |
| 367 | + } | |
| 341 | 368 | } |
| 342 | 369 | |
| 343 | 370 | async *chatCompletionStream( |
modified packages/openai-adapters/src/apis/Relace.ts +25 −2
| @@ -16,7 +16,12 @@ | ||
| 16 | 16 | import { Model } from "openai/resources/models.mjs"; |
| 17 | 17 | import { z } from "zod"; |
| 18 | 18 | import { OpenAIConfigSchema } from "../types.js"; |
| 19 | -import { chatChunk, chatCompletion, customFetch } from "../util.js"; | |
| 19 | +import { | |
| 20 | + chatChunk, | |
| 21 | + chatCompletion, | |
| 22 | + customFetch, | |
| 23 | + usageChatChunk, | |
| 24 | +} from "../util.js"; | |
| 20 | 25 | import { |
| 21 | 26 | BaseLlmApi, |
| 22 | 27 | CreateRerankResponse, |
| @@ -24,6 +29,8 @@ | ||
| 24 | 29 | RerankCreateParams, |
| 25 | 30 | } from "./base.js"; |
| 26 | 31 | |
| 32 | +type UsageInfo = { total_tokens: number; completion_tokens: number; prompt_tokens: number }; | |
| 33 | + | |
| 27 | 34 | // Relace only supports apply through a /v1/apply endpoint |
| 28 | 35 | export class RelaceApi implements BaseLlmApi { |
| 29 | 36 | private apiBase = "https://instantapply.endpoint.relace.run/v1/"; |
| @@ -41,6 +48,7 @@ | ||
| 41 | 48 | signal: AbortSignal, |
| 42 | 49 | ): Promise<ChatCompletion> { |
| 43 | 50 | let content = ""; |
| 51 | + let usage: UsageInfo | undefined = undefined; | |
| 44 | 52 | |
| 45 | 53 | // Convert the non-streaming params to streaming params |
| 46 | 54 | const streamingBody: ChatCompletionCreateParamsStreaming = { |
| @@ -52,12 +60,18 @@ | ||
| 52 | 60 | streamingBody, |
| 53 | 61 | signal, |
| 54 | 62 | )) { |
| 55 | - content += chunk.choices[0]?.delta?.content || ""; | |
| 63 | + if (chunk.choices.length > 0) { | |
| 64 | + content += chunk.choices[0]?.delta?.content || ""; | |
| 65 | + } | |
| 66 | + if (chunk.usage) { | |
| 67 | + usage = chunk.usage; | |
| 68 | + } | |
| 56 | 69 | } |
| 57 | 70 | |
| 58 | 71 | return chatCompletion({ |
| 59 | 72 | content, |
| 60 | 73 | model: body.model, |
| 74 | + usage, | |
| 61 | 75 | }); |
| 62 | 76 | } |
| 63 | 77 | |
| @@ -115,6 +129,15 @@ | ||
| 115 | 129 | content: mergedCode, |
| 116 | 130 | model: body.model, |
| 117 | 131 | }); |
| 132 | + | |
| 133 | + yield usageChatChunk({ | |
| 134 | + model: body.model, | |
| 135 | + usage: { | |
| 136 | + prompt_tokens: result.usage.prompt_tokens || 0, | |
| 137 | + completion_tokens: result.usage.completion_tokens || 0, | |
| 138 | + total_tokens: result.usage.total_tokens, | |
| 139 | + }, | |
| 140 | + }); | |
| 118 | 141 | } |
| 119 | 142 | |
| 120 | 143 | completionNonStream( |
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", |