Skip to content

Commit

Permalink
Merge pull request #56 from Scale3-Labs/dylan/s3en-2120-add-support-f…
Browse files Browse the repository at this point in the history
…or-calculating-the-cost-for-perplexity-apis

adding perplexity cost calculator
  • Loading branch information
dylanzuber-scale3 authored Apr 10, 2024
2 parents 6f62fbe + 502bfe3 commit f217db2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
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

0 comments on commit f217db2

Please sign in to comment.