Skip to content

feat: show token usage on alerts table #216

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

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
23 changes: 23 additions & 0 deletions src/api/generated/@tanstack/react-query.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
v1SetWorkspaceMuxes,
v1StreamSse,
v1VersionCheck,
v1GetWorkspaceTokenUsage,
} from "../sdk.gen";
import type {
V1ListProviderEndpointsData,
Expand Down Expand Up @@ -71,6 +72,7 @@ import type {
V1SetWorkspaceMuxesData,
V1SetWorkspaceMuxesError,
V1SetWorkspaceMuxesResponse,
V1GetWorkspaceTokenUsageData,
} from "../types.gen";

type QueryKey<TOptions extends OptionsLegacyParser> = [
Expand Down Expand Up @@ -698,3 +700,24 @@ export const v1VersionCheckOptions = (options?: OptionsLegacyParser) => {
queryKey: v1VersionCheckQueryKey(options),
});
};

export const v1GetWorkspaceTokenUsageQueryKey = (
options: OptionsLegacyParser<V1GetWorkspaceTokenUsageData>,
) => [createQueryKey("v1GetWorkspaceTokenUsage", options)];

export const v1GetWorkspaceTokenUsageOptions = (
options: OptionsLegacyParser<V1GetWorkspaceTokenUsageData>,
) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await v1GetWorkspaceTokenUsage({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: v1GetWorkspaceTokenUsageQueryKey(options),
});
};
20 changes: 20 additions & 0 deletions src/api/generated/sdk.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ import type {
V1StreamSseResponse,
V1VersionCheckError,
V1VersionCheckResponse,
V1GetWorkspaceTokenUsageData,
V1GetWorkspaceTokenUsageError,
V1GetWorkspaceTokenUsageResponse,
} from "./types.gen";

export const client = createClient(createConfig());
Expand Down Expand Up @@ -521,3 +524,20 @@ export const v1VersionCheck = <ThrowOnError extends boolean = false>(
url: "/api/v1/version",
});
};

/**
* Get Workspace Token Usage
* Get the token usage of a workspace.
*/
export const v1GetWorkspaceTokenUsage = <ThrowOnError extends boolean = false>(
options: OptionsLegacyParser<V1GetWorkspaceTokenUsageData, ThrowOnError>,
) => {
return (options?.client ?? client).get<
V1GetWorkspaceTokenUsageResponse,
V1GetWorkspaceTokenUsageError,
ThrowOnError
>({
...options,
url: "/api/v1/workspaces/{workspace_name}/token-usage",
});
};
44 changes: 44 additions & 0 deletions src/api/generated/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type Conversation = {
type: QuestionType;
chat_id: string;
conversation_timestamp: string;
token_usage_agg: TokenUsageAggregate | null;
};

export type CreateOrRenameWorkspaceRequest = {
Expand Down Expand Up @@ -134,6 +135,8 @@ export enum ProviderType {
OPENAI = "openai",
ANTHROPIC = "anthropic",
VLLM = "vllm",
LLAMACPP = "llamacpp",
OLLAMA = "ollama",
}

/**
Expand All @@ -149,6 +152,37 @@ export enum QuestionType {
FIM = "fim",
}

/**
* TokenUsage it's not a table, it's a model to represent the token usage.
* The data is stored in the outputs table.
*/
export type TokenUsage = {
input_tokens?: number;
output_tokens?: number;
input_cost?: number;
output_cost?: number;
};

/**
* Represents the tokens used. Includes the information of the tokens used by model.
* `used_tokens` are the total tokens used in the `tokens_by_model` list.
*/
export type TokenUsageAggregate = {
tokens_by_model: {
[key: string]: TokenUsageByModel;
};
token_usage: TokenUsage;
};

/**
* Represents the tokens used by a model.
*/
export type TokenUsageByModel = {
provider_type: ProviderType;
model: string;
token_usage: TokenUsage;
};

export type ValidationError = {
loc: Array<string | number>;
msg: string;
Expand Down Expand Up @@ -367,3 +401,13 @@ export type V1StreamSseError = unknown;
export type V1VersionCheckResponse = unknown;

export type V1VersionCheckError = unknown;

export type V1GetWorkspaceTokenUsageData = {
path: {
workspace_name: string;
};
};

export type V1GetWorkspaceTokenUsageResponse = TokenUsageAggregate;

export type V1GetWorkspaceTokenUsageError = HTTPValidationError;
Loading
Loading