Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose deepseek cache usage #4333 #4359

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/deepseek/src/deepseek-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
LanguageModelV1,
NoSuchModelError,
ProviderV1,
LanguageModelV1ProviderMetadata,
} from '@ai-sdk/provider';
import {
FetchFunction,
Expand All @@ -13,6 +14,7 @@ import {
DeepSeekChatModelId,
DeepSeekChatSettings,
} from './deepseek-chat-settings';
import { z } from 'zod';

export interface DeepSeekProviderSettings {
/**
Expand Down Expand Up @@ -85,6 +87,29 @@ export function createDeepSeek(
headers: getHeaders,
fetch: options.fetch,
defaultObjectGenerationMode: 'json',
usageStructure: z
.object({
prompt_tokens: z.number().nullish(),
completion_tokens: z.number().nullish(),
prompt_cache_hit_tokens: z.number().nullish(),
prompt_cache_miss_tokens: z.number().nullish(),
})
.nullish(),
getProviderMetadata(
value: any,
_cur: LanguageModelV1ProviderMetadata | undefined,
) {
if (value?.usage?.prompt_cache_hit_tokens != null) {
return {
deepseek: {
promptCacheHitTokens: value.usage.prompt_cache_hit_tokens,
promptCacheMissTokens: value.usage.prompt_cache_miss_tokens,
},
};
} else {
return undefined;
}
},
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export type OpenAICompatibleChatConfig = {
fetch?: FetchFunction;
errorStructure?: ProviderErrorStructure<any>;

usageStructure?: z.ZodType;

getProviderMetadata?: (
value: any,
cur: LanguageModelV1ProviderMetadata | undefined,
) => LanguageModelV1ProviderMetadata | undefined;

/**
Default object generation mode that should be used with this model when
no mode is specified. Should be the mode with the best results for this
Expand Down Expand Up @@ -80,6 +87,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
config.errorStructure ?? defaultOpenAICompatibleErrorStructure;
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
errorStructure.errorSchema,
config.usageStructure,
);
this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);

Expand Down Expand Up @@ -360,6 +368,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
let isFirstChunk = true;

let providerMetadata: LanguageModelV1ProviderMetadata | undefined;
const getProviderMetadata = this.config.getProviderMetadata;
return {
stream: response.pipeThrough(
new TransformStream<
Expand Down Expand Up @@ -392,6 +401,10 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
});
}

if (typeof getProviderMetadata === 'function') {
providerMetadata = getProviderMetadata(value, providerMetadata);
}

if (value.usage != null) {
usage = {
promptTokens: value.usage.prompt_tokens ?? undefined,
Expand Down Expand Up @@ -590,6 +603,7 @@ const OpenAICompatibleChatResponseSchema = z.object({
// this approach limits breakages when the API changes and increases efficiency
const createOpenAICompatibleChatChunkSchema = <ERROR_SCHEMA extends z.ZodType>(
errorSchema: ERROR_SCHEMA,
usageSchema?: z.ZodType,
) =>
z.union([
z.object({
Expand Down Expand Up @@ -620,12 +634,14 @@ const createOpenAICompatibleChatChunkSchema = <ERROR_SCHEMA extends z.ZodType>(
finish_reason: z.string().nullish(),
}),
),
usage: z
.object({
prompt_tokens: z.number().nullish(),
completion_tokens: z.number().nullish(),
})
.nullish(),
usage:
usageSchema ??
z
.object({
prompt_tokens: z.number().nullish(),
completion_tokens: z.number().nullish(),
})
.nullish(),
}),
errorSchema,
]);
Loading