Skip to content

Commit f9461bc

Browse files
feat: show token usage on alerts table (#216)
* feat: show token usage on alerts table * chore: fix type errors
1 parent b683d56 commit f9461bc

14 files changed

+683
-208
lines changed

src/api/generated/@tanstack/react-query.gen.ts

+23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
v1SetWorkspaceMuxes,
3030
v1StreamSse,
3131
v1VersionCheck,
32+
v1GetWorkspaceTokenUsage,
3233
} from "../sdk.gen";
3334
import type {
3435
V1ListProviderEndpointsData,
@@ -71,6 +72,7 @@ import type {
7172
V1SetWorkspaceMuxesData,
7273
V1SetWorkspaceMuxesError,
7374
V1SetWorkspaceMuxesResponse,
75+
V1GetWorkspaceTokenUsageData,
7476
} from "../types.gen";
7577

7678
type QueryKey<TOptions extends OptionsLegacyParser> = [
@@ -698,3 +700,24 @@ export const v1VersionCheckOptions = (options?: OptionsLegacyParser) => {
698700
queryKey: v1VersionCheckQueryKey(options),
699701
});
700702
};
703+
704+
export const v1GetWorkspaceTokenUsageQueryKey = (
705+
options: OptionsLegacyParser<V1GetWorkspaceTokenUsageData>,
706+
) => [createQueryKey("v1GetWorkspaceTokenUsage", options)];
707+
708+
export const v1GetWorkspaceTokenUsageOptions = (
709+
options: OptionsLegacyParser<V1GetWorkspaceTokenUsageData>,
710+
) => {
711+
return queryOptions({
712+
queryFn: async ({ queryKey, signal }) => {
713+
const { data } = await v1GetWorkspaceTokenUsage({
714+
...options,
715+
...queryKey[0],
716+
signal,
717+
throwOnError: true,
718+
});
719+
return data;
720+
},
721+
queryKey: v1GetWorkspaceTokenUsageQueryKey(options),
722+
});
723+
};

src/api/generated/sdk.gen.ts

+20
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ import type {
7474
V1StreamSseResponse,
7575
V1VersionCheckError,
7676
V1VersionCheckResponse,
77+
V1GetWorkspaceTokenUsageData,
78+
V1GetWorkspaceTokenUsageError,
79+
V1GetWorkspaceTokenUsageResponse,
7780
} from "./types.gen";
7881

7982
export const client = createClient(createConfig());
@@ -521,3 +524,20 @@ export const v1VersionCheck = <ThrowOnError extends boolean = false>(
521524
url: "/api/v1/version",
522525
});
523526
};
527+
528+
/**
529+
* Get Workspace Token Usage
530+
* Get the token usage of a workspace.
531+
*/
532+
export const v1GetWorkspaceTokenUsage = <ThrowOnError extends boolean = false>(
533+
options: OptionsLegacyParser<V1GetWorkspaceTokenUsageData, ThrowOnError>,
534+
) => {
535+
return (options?.client ?? client).get<
536+
V1GetWorkspaceTokenUsageResponse,
537+
V1GetWorkspaceTokenUsageError,
538+
ThrowOnError
539+
>({
540+
...options,
541+
url: "/api/v1/workspaces/{workspace_name}/token-usage",
542+
});
543+
};

src/api/generated/types.gen.ts

+44
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type Conversation = {
5353
type: QuestionType;
5454
chat_id: string;
5555
conversation_timestamp: string;
56+
token_usage_agg: TokenUsageAggregate | null;
5657
};
5758

5859
export type CreateOrRenameWorkspaceRequest = {
@@ -134,6 +135,8 @@ export enum ProviderType {
134135
OPENAI = "openai",
135136
ANTHROPIC = "anthropic",
136137
VLLM = "vllm",
138+
LLAMACPP = "llamacpp",
139+
OLLAMA = "ollama",
137140
}
138141

139142
/**
@@ -149,6 +152,37 @@ export enum QuestionType {
149152
FIM = "fim",
150153
}
151154

155+
/**
156+
* TokenUsage it's not a table, it's a model to represent the token usage.
157+
* The data is stored in the outputs table.
158+
*/
159+
export type TokenUsage = {
160+
input_tokens?: number;
161+
output_tokens?: number;
162+
input_cost?: number;
163+
output_cost?: number;
164+
};
165+
166+
/**
167+
* Represents the tokens used. Includes the information of the tokens used by model.
168+
* `used_tokens` are the total tokens used in the `tokens_by_model` list.
169+
*/
170+
export type TokenUsageAggregate = {
171+
tokens_by_model: {
172+
[key: string]: TokenUsageByModel;
173+
};
174+
token_usage: TokenUsage;
175+
};
176+
177+
/**
178+
* Represents the tokens used by a model.
179+
*/
180+
export type TokenUsageByModel = {
181+
provider_type: ProviderType;
182+
model: string;
183+
token_usage: TokenUsage;
184+
};
185+
152186
export type ValidationError = {
153187
loc: Array<string | number>;
154188
msg: string;
@@ -367,3 +401,13 @@ export type V1StreamSseError = unknown;
367401
export type V1VersionCheckResponse = unknown;
368402

369403
export type V1VersionCheckError = unknown;
404+
405+
export type V1GetWorkspaceTokenUsageData = {
406+
path: {
407+
workspace_name: string;
408+
};
409+
};
410+
411+
export type V1GetWorkspaceTokenUsageResponse = TokenUsageAggregate;
412+
413+
export type V1GetWorkspaceTokenUsageError = HTTPValidationError;

0 commit comments

Comments
 (0)