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

adding perplexity cost calculator #56

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
31 changes: 31 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,37 @@ export const ANTHROPIC_PRICING: Record<string, any> = {
},
};

// https://docs.perplexity.ai/docs/pricing --> slightly unclear
// https://docs.perplexity.ai/changelog/api-updates-february-2024 --> diff prices
export const PERPLEXITY_PRICING: Record<string, any> = {
"sonar-small-chat": {
input: 0.0002,
output: 0.0002,
},
"sonar-small-online": {
// + $5/K requests
input: 0.0002,
output: 0.0002,
},
"sonar-medium-chat": {
input: 0.0006,
output: 0.0006,
},
"sonar-medium-online": {
// + $5/K requests
input: 0.0006,
output: 0.0006,
},
"mistral-7b-instruct": {
input: 0.0002,
output: 0.0002,
},
"mixtral-8x7b-instruct": {
input: 0.0006,
output: 0.0018,
},
};

export const PAGE_SIZE = 15;

export const DEFAULT_TESTS: Partial<Test>[] = [
Expand Down
18 changes: 14 additions & 4 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ANTHROPIC_PRICING,
LangTraceAttributes,
OPENAI_PRICING,
PERPLEXITY_PRICING,
SpanStatusCode,
} from "./constants";

Expand Down Expand Up @@ -275,10 +276,7 @@ export function prepareForClickhouse(spans: Normalized[]): Span[] {
});
}

export async function authApiKey(
api_key?: string,
project_id?: string
): Promise<NextResponse> {
export async function authApiKey(api_key?: string): Promise<NextResponse> {
if (!api_key) {
return NextResponse.json(
{
Expand Down Expand Up @@ -386,6 +384,18 @@ export function calculatePriceFromUsage(
return { total, input, output };
}
return { total: 0, input: 0, output: 0 };
} else if (vendor === "perplexity") {
const costTable = PERPLEXITY_PRICING[model];
if (costTable) {
const total =
(costTable.input * usage.input_tokens +
costTable.output * usage.output_tokens) /
1000;
const input = (costTable.input * usage.input_tokens) / 1000;
const output = (costTable.output * usage.output_tokens) / 1000;
return { total, input, output };
}
return { total: 0, input: 0, output: 0 };
}
return { total: 0, input: 0, output: 0 };
}
Expand Down