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

fix: force decimal notiation for large floats #28252

Merged
merged 5 commits into from
Feb 5, 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
47 changes: 34 additions & 13 deletions plugin-server/scripts/update-ai-costs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import dotenv from 'dotenv'
import fs from 'fs'
import bigDecimal from 'js-big-decimal'
import path from 'path'

dotenv.config()

interface ModelRow {
model: string
cost: {
prompt_token: number
completion_token: number
prompt_token: string
completion_token: string
}
}

Expand All @@ -23,6 +24,28 @@ const supportedProviderList = [
'meta-llama',
]

function serializeModels(models: ModelRow[]): string {
let output = '[\n'
models.forEach((model, index) => {
output += ' {\n'
output += ` model: ${JSON.stringify(model.model)},\n`
output += ' cost: {\n'
output += ` prompt_token: ${new bigDecimal(model.cost.prompt_token)
.round(10)
.stripTrailingZero()
.getValue()},\n`
output += ` completion_token: ${new bigDecimal(model.cost.completion_token)
.round(10)
.stripTrailingZero()
.getValue()}\n`
output += ' }\n'
output += ' }'
output += index < models.length - 1 ? ',\n' : '\n'
})
output += ']'
return output
}

const main = async () => {
if (!process.env.OPENROUTER_API_KEY) {
console.error('OPENROUTER_API_KEY is not set')
Expand Down Expand Up @@ -68,27 +91,25 @@ const main = async () => {
providerModels.set(provider, [])
}

// Convert pricing values to numbers before using toFixed(10)
const promptPrice = new bigDecimal(model.pricing.prompt).getValue()
const completionPrice = new bigDecimal(model.pricing.completion).getValue()

const modelRow: ModelRow = {
model: modelParts.join('/'), // Only include the part after the provider
cost: {
prompt_token: parseFloat(model.pricing.prompt),
completion_token: parseFloat(model.pricing.completion),
prompt_token: promptPrice,
completion_token: completionPrice,
},
}

providerModels.get(provider)!.push(modelRow)
}

// Generate files for each provider
// Generate files for each provider using our custom serializer
for (const [provider, models] of providerModels.entries()) {
if (!fs.existsSync(baseDir)) {
fs.mkdirSync(baseDir)
}

const fileContent = `import type { ModelRow } from './types';\n\nexport const costs: ModelRow[] = ${JSON.stringify(
models,
null,
2
const fileContent = `import type { ModelRow } from './types';\n\nexport const costs: ModelRow[] = ${serializeModels(
models
)};\n`
fs.writeFileSync(path.join(baseDir, `${provider}.ts`), fileContent)
}
Expand Down
12 changes: 6 additions & 6 deletions plugin-server/src/utils/ai-costs/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ export const costs: ModelRow[] = [
{
model: 'claude-3.5-haiku-20241022:beta',
cost: {
prompt_token: 8e-7,
prompt_token: 0.0000008,
completion_token: 0.000004,
},
},
{
model: 'claude-3.5-haiku-20241022',
cost: {
prompt_token: 8e-7,
prompt_token: 0.0000008,
completion_token: 0.000004,
},
},
{
model: 'claude-3.5-haiku:beta',
cost: {
prompt_token: 8e-7,
prompt_token: 0.0000008,
completion_token: 0.000004,
},
},
{
model: 'claude-3.5-haiku',
cost: {
prompt_token: 8e-7,
prompt_token: 0.0000008,
completion_token: 0.000004,
},
Comment on lines 29 to 30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using TypeScript const assertions (as const) on the costs array to ensure type safety of the model names and prevent accidental modifications.

},
Expand Down Expand Up @@ -60,14 +60,14 @@ export const costs: ModelRow[] = [
{
model: 'claude-3-haiku:beta',
cost: {
prompt_token: 2.5e-7,
prompt_token: 0.00000025,
completion_token: 0.00000125,
},
},
{
model: 'claude-3-haiku',
cost: {
prompt_token: 2.5e-7,
prompt_token: 0.00000025,
completion_token: 0.00000125,
},
},
Expand Down
14 changes: 7 additions & 7 deletions plugin-server/src/utils/ai-costs/cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ export const costs: ModelRow[] = [
{
model: 'command-r7b-12-2024',
cost: {
prompt_token: 3.75e-8,
completion_token: 1.5e-7,
prompt_token: 0.0000000375,
completion_token: 0.00000015,
},
},
{
model: 'command-r-08-2024',
cost: {
prompt_token: 1.425e-7,
completion_token: 5.7e-7,
prompt_token: 0.0000001425,
completion_token: 0.00000057,
},
},
{
Expand All @@ -39,21 +39,21 @@ export const costs: ModelRow[] = [
{
model: 'command',
cost: {
prompt_token: 9.5e-7,
prompt_token: 0.00000095,
completion_token: 0.0000019,
},
},
{
model: 'command-r',
cost: {
prompt_token: 4.75e-7,
prompt_token: 0.000000475,
completion_token: 0.000001425,
},
},
{
model: 'command-r-03-2024',
cost: {
prompt_token: 4.75e-7,
prompt_token: 0.000000475,
completion_token: 0.000001425,
},
},
Expand Down
25 changes: 16 additions & 9 deletions plugin-server/src/utils/ai-costs/deepseek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ export const costs: ModelRow[] = [
{
model: 'deepseek-r1-distill-qwen-1.5b',
cost: {
prompt_token: 1.8e-7,
completion_token: 1.8e-7,
prompt_token: 0.00000018,
completion_token: 0.00000018,
},
},
{
model: 'deepseek-r1-distill-qwen-32b',
cost: {
prompt_token: 5e-7,
completion_token: 0.00000488,
prompt_token: 0.00000012,
completion_token: 0.00000018,
},
},
{
Expand All @@ -22,11 +22,18 @@ export const costs: ModelRow[] = [
completion_token: 0.0000016,
},
},
{
model: 'deepseek-r1-distill-llama-70b:free',
cost: {
prompt_token: 0,
completion_token: 0,
},
},
{
model: 'deepseek-r1-distill-llama-70b',
cost: {
prompt_token: 2.3e-7,
completion_token: 6.9e-7,
prompt_token: 0.00000023,
completion_token: 0.00000069,
},
},
{
Expand All @@ -39,7 +46,7 @@ export const costs: ModelRow[] = [
{
model: 'deepseek-r1',
cost: {
prompt_token: 8e-7,
prompt_token: 0.00000075,
completion_token: 0.0000024,
},
},
Expand All @@ -53,8 +60,8 @@ export const costs: ModelRow[] = [
{
model: 'deepseek-chat',
cost: {
prompt_token: 5e-7,
completion_token: 0.0000015,
prompt_token: 0.00000049,
completion_token: 0.00000089,
},
},
{
Expand Down
31 changes: 19 additions & 12 deletions plugin-server/src/utils/ai-costs/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ export const costs: ModelRow[] = [
{
model: 'gemini-flash-1.5-8b',
cost: {
prompt_token: 3.75e-8,
completion_token: 1.5e-7,
prompt_token: 0.0000000375,
completion_token: 0.00000015,
},
},
{
model: 'gemini-flash-1.5-exp:free',
cost: {
prompt_token: 0,
completion_token: 0,
},
},
{
Expand All @@ -81,8 +88,8 @@ export const costs: ModelRow[] = [
{
model: 'gemma-2-27b-it',
cost: {
prompt_token: 2.7e-7,
completion_token: 2.7e-7,
prompt_token: 0.00000027,
completion_token: 0.00000027,
},
},
{
Expand All @@ -95,15 +102,15 @@ export const costs: ModelRow[] = [
{
model: 'gemma-2-9b-it',
cost: {
prompt_token: 3e-8,
completion_token: 6e-8,
prompt_token: 0.00000003,
completion_token: 0.00000006,
},
},
{
model: 'gemini-flash-1.5',
cost: {
prompt_token: 7.5e-8,
completion_token: 3e-7,
prompt_token: 0.000000075,
completion_token: 0.0000003,
},
},
{
Expand All @@ -116,21 +123,21 @@ export const costs: ModelRow[] = [
{
model: 'gemma-7b-it',
cost: {
prompt_token: 1.5e-7,
completion_token: 1.5e-7,
prompt_token: 0.00000015,
completion_token: 0.00000015,
},
},
{
model: 'gemini-pro-vision',
cost: {
prompt_token: 5e-7,
prompt_token: 0.0000005,
completion_token: 0.0000015,
},
},
{
model: 'gemini-pro',
cost: {
prompt_token: 5e-7,
prompt_token: 0.0000005,
completion_token: 0.0000015,
},
},
Expand Down
Loading