From 502bfe3a6adaaf58e887d1d9a4d4dfd6310de41c Mon Sep 17 00:00:00 2001 From: dylan Date: Wed, 10 Apr 2024 14:54:49 -0400 Subject: [PATCH] adding perplexity cost calculator: --- lib/constants.ts | 31 +++++++++++++++++++++++++++++++ lib/utils.ts | 18 ++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/lib/constants.ts b/lib/constants.ts index 791851c2..7f22b41c 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -107,6 +107,37 @@ export const ANTHROPIC_PRICING: Record = { }, }; +// https://docs.perplexity.ai/docs/pricing --> slightly unclear +// https://docs.perplexity.ai/changelog/api-updates-february-2024 --> diff prices +export const PERPLEXITY_PRICING: Record = { + "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[] = [ diff --git a/lib/utils.ts b/lib/utils.ts index e8cde4a6..f46e9cbb 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -9,6 +9,7 @@ import { ANTHROPIC_PRICING, LangTraceAttributes, OPENAI_PRICING, + PERPLEXITY_PRICING, SpanStatusCode, } from "./constants"; @@ -275,10 +276,7 @@ export function prepareForClickhouse(spans: Normalized[]): Span[] { }); } -export async function authApiKey( - api_key?: string, - project_id?: string -): Promise { +export async function authApiKey(api_key?: string): Promise { if (!api_key) { return NextResponse.json( { @@ -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 }; }