return usage from Anthropic OpenAI adapter
This commit is contained in:
parent
e6406de3b9
commit
aa7944b625
4 changed files with 68 additions and 12 deletions
|
|
@ -8,10 +8,16 @@ import {
|
|||
Completion,
|
||||
CompletionCreateParamsNonStreaming,
|
||||
CompletionCreateParamsStreaming,
|
||||
CompletionUsage,
|
||||
} from "openai/resources/index";
|
||||
import { ChatCompletionCreateParams } from "openai/src/resources/index.js";
|
||||
import { AnthropicConfig } from "../types.js";
|
||||
import { chatChunk, chatChunkFromDelta, customFetch } from "../util.js";
|
||||
import {
|
||||
chatChunk,
|
||||
chatChunkFromDelta,
|
||||
customFetch,
|
||||
usageChatChunk,
|
||||
} from "../util.js";
|
||||
import { EMPTY_CHAT_COMPLETION } from "../util/emptyChatCompletion.js";
|
||||
import { safeParseArgs } from "../util/parseArgs.js";
|
||||
import {
|
||||
|
|
@ -214,6 +220,12 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
|
||||
let lastToolUseId: string | undefined;
|
||||
let lastToolUseName: string | undefined;
|
||||
|
||||
const usage: CompletionUsage = {
|
||||
completion_tokens: 0,
|
||||
prompt_tokens: 0,
|
||||
total_tokens: 0,
|
||||
};
|
||||
for await (const value of streamSse(response as any)) {
|
||||
// https://docs.anthropic.com/en/api/messages-streaming#event-types
|
||||
switch (value.type) {
|
||||
|
|
@ -223,6 +235,15 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
lastToolUseName = value.content_block.name;
|
||||
}
|
||||
break;
|
||||
case "message_start":
|
||||
usage.prompt_tokens = value.message.usage.input_tokens;
|
||||
usage.prompt_tokens_details = {
|
||||
cached_tokens: value.message.usage.cache_read_input_tokens,
|
||||
};
|
||||
break;
|
||||
case "message_delta":
|
||||
usage.completion_tokens = value.usage.output_tokens;
|
||||
break;
|
||||
case "content_block_delta":
|
||||
// https://docs.anthropic.com/en/api/messages-streaming#delta-types
|
||||
switch (value.delta.type) {
|
||||
|
|
@ -263,6 +284,14 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
yield usageChatChunk({
|
||||
model: body.model,
|
||||
usage: {
|
||||
...usage,
|
||||
total_tokens: usage.completion_tokens + usage.prompt_tokens,
|
||||
},
|
||||
});
|
||||
}
|
||||
async completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ dotenv.config();
|
|||
|
||||
export interface TestConfigOptions {
|
||||
skipTools: boolean;
|
||||
expectUsage?: boolean;
|
||||
}
|
||||
|
||||
function testConfig(_config: ModelConfig & { options?: TestConfigOptions }) {
|
||||
|
|
@ -51,6 +52,10 @@ const TESTS: Omit<ModelConfig & { options?: TestConfigOptions }, "name">[] = [
|
|||
model: "claude-3-5-haiku-latest",
|
||||
apiKey: process.env.ANTHROPIC_API_KEY!,
|
||||
roles: ["chat"],
|
||||
options: {
|
||||
skipTools: false,
|
||||
expectUsage: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
provider: "gemini",
|
||||
|
|
@ -117,15 +122,6 @@ const TESTS: Omit<ModelConfig & { options?: TestConfigOptions }, "name">[] = [
|
|||
apiVersion: "2024-02-15-preview",
|
||||
apiType: "azure-openai",
|
||||
},
|
||||
roles: ["chat"],
|
||||
},
|
||||
{
|
||||
provider: "azure",
|
||||
model: "mistral-small-2503",
|
||||
apiBase: "https://nate-0276-resource.services.ai.azure.com/models",
|
||||
apiKey: process.env.AZURE_FOUNDRY_MISTRAL_SMALL_API_KEY,
|
||||
roles: ["chat"],
|
||||
options: { skipTools: true },
|
||||
},
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { CompletionUsage } from "openai/resources/completions.mjs";
|
||||
import { BaseLlmApi, constructLlmApi } from "../index.js";
|
||||
import { LLMConfig } from "../types.js";
|
||||
import { TestConfigOptions } from "./main.test.js";
|
||||
|
|
@ -88,12 +89,27 @@ export function testChat(
|
|||
new AbortController().signal,
|
||||
);
|
||||
let completion = "";
|
||||
let usage: CompletionUsage | undefined = undefined;
|
||||
for await (const result of stream) {
|
||||
completion += result.choices[0].delta.content ?? "";
|
||||
completion += result.choices[0]?.delta.content ?? "";
|
||||
|
||||
expect(result.choices.length).toBeGreaterThan(0);
|
||||
if (result.usage) {
|
||||
usage = result.usage;
|
||||
} else {
|
||||
// At the end we expect a final message without choices that shares usage
|
||||
expect(result.choices.length).toBeGreaterThan(0);
|
||||
}
|
||||
}
|
||||
expect(completion.length).toBeGreaterThan(0);
|
||||
|
||||
if (options?.expectUsage === true) {
|
||||
expect(usage).toBeDefined();
|
||||
expect(usage!.completion_tokens).toBeGreaterThan(0);
|
||||
expect(usage!.prompt_tokens).toBeGreaterThan(0);
|
||||
expect(usage!.total_tokens).toEqual(
|
||||
usage!.prompt_tokens + usage!.completion_tokens,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("should successfully stream multi-part chat with empty text", async () => {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,21 @@ export function chatChunk(options: {
|
|||
};
|
||||
}
|
||||
|
||||
export function usageChatChunk(options: {
|
||||
model: string;
|
||||
id?: string | null;
|
||||
usage?: CompletionUsage;
|
||||
}): ChatCompletionChunk {
|
||||
return {
|
||||
choices: [],
|
||||
usage: options.usage,
|
||||
created: Date.now(),
|
||||
id: options.id ?? "",
|
||||
model: options.model,
|
||||
object: "chat.completion.chunk",
|
||||
};
|
||||
}
|
||||
|
||||
export function chatChunkFromDelta(options: {
|
||||
delta: ChatCompletionChunk.Choice["delta"];
|
||||
model: string;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue