Refactor out file helpers into fs.ts

This commit is contained in:
Saoud Rizwan
2024-09-29 02:25:22 -04:00
parent ebead1b1fa
commit 2b63b91bfb
6 changed files with 107 additions and 117 deletions

24
src/utils/cost.ts Normal file
View File

@@ -0,0 +1,24 @@
import { ModelInfo } from "../shared/api"
export function calculateApiCost(
modelInfo: ModelInfo,
inputTokens: number,
outputTokens: number,
cacheCreationInputTokens?: number,
cacheReadInputTokens?: number
): number {
const modelCacheWritesPrice = modelInfo.cacheWritesPrice
let cacheWritesCost = 0
if (cacheCreationInputTokens && modelCacheWritesPrice) {
cacheWritesCost = (modelCacheWritesPrice / 1_000_000) * cacheCreationInputTokens
}
const modelCacheReadsPrice = modelInfo.cacheReadsPrice
let cacheReadsCost = 0
if (cacheReadInputTokens && modelCacheReadsPrice) {
cacheReadsCost = (modelCacheReadsPrice / 1_000_000) * cacheReadInputTokens
}
const baseInputCost = (modelInfo.inputPrice / 1_000_000) * inputTokens
const outputCost = (modelInfo.outputPrice / 1_000_000) * outputTokens
const totalCost = cacheWritesCost + cacheReadsCost + baseInputCost + outputCost
return totalCost
}