chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* Cost calculation functions for tokenization
|
||||
*/
|
||||
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { toError } from '@sim/utils/errors'
|
||||
import { createTokenizationError } from '@/lib/tokenization/errors'
|
||||
import {
|
||||
estimateInputTokens,
|
||||
estimateOutputTokens,
|
||||
estimateTokenCount,
|
||||
} from '@/lib/tokenization/estimators'
|
||||
import type {
|
||||
CostBreakdown,
|
||||
StreamingCostResult,
|
||||
TokenizationInput,
|
||||
TokenUsage,
|
||||
} from '@/lib/tokenization/types'
|
||||
import {
|
||||
getProviderForTokenization,
|
||||
logTokenizationDetails,
|
||||
validateTokenizationInput,
|
||||
} from '@/lib/tokenization/utils'
|
||||
import { calculateCost } from '@/providers/utils'
|
||||
|
||||
const logger = createLogger('TokenizationCalculators')
|
||||
|
||||
/**
|
||||
* Calculates cost estimate for streaming execution using token estimation
|
||||
*/
|
||||
export function calculateStreamingCost(
|
||||
model: string,
|
||||
inputText: string,
|
||||
outputText: string,
|
||||
systemPrompt?: string,
|
||||
context?: string,
|
||||
messages?: Array<{ role: string; content: string }>
|
||||
): StreamingCostResult {
|
||||
try {
|
||||
// Validate inputs
|
||||
validateTokenizationInput(model, inputText, outputText)
|
||||
|
||||
const providerId = getProviderForTokenization(model)
|
||||
|
||||
// Estimate input tokens (combine all input sources)
|
||||
const inputEstimate = estimateInputTokens(systemPrompt, context, messages, providerId)
|
||||
|
||||
// Add the main input text to the estimation
|
||||
const mainInputEstimate = estimateTokenCount(inputText, providerId)
|
||||
const totalPromptTokens = inputEstimate.count + mainInputEstimate.count
|
||||
|
||||
// Estimate output tokens
|
||||
const outputEstimate = estimateOutputTokens(outputText, providerId)
|
||||
const completionTokens = outputEstimate.count
|
||||
|
||||
// Calculate total tokens
|
||||
const totalTokens = totalPromptTokens + completionTokens
|
||||
|
||||
// Create token usage object
|
||||
const tokens: TokenUsage = {
|
||||
input: totalPromptTokens,
|
||||
output: completionTokens,
|
||||
total: totalTokens,
|
||||
}
|
||||
|
||||
// Calculate cost using provider pricing
|
||||
const costResult = calculateCost(model, totalPromptTokens, completionTokens, false)
|
||||
|
||||
const cost: CostBreakdown = {
|
||||
input: costResult.input,
|
||||
output: costResult.output,
|
||||
total: costResult.total,
|
||||
}
|
||||
|
||||
const result: StreamingCostResult = {
|
||||
tokens,
|
||||
cost,
|
||||
model,
|
||||
provider: providerId,
|
||||
method: 'tokenization',
|
||||
}
|
||||
|
||||
logTokenizationDetails('Streaming cost calculation completed', {
|
||||
model,
|
||||
provider: providerId,
|
||||
inputLength: inputText.length,
|
||||
outputLength: outputText.length,
|
||||
tokens,
|
||||
cost,
|
||||
method: 'tokenization',
|
||||
})
|
||||
|
||||
return result
|
||||
} catch (error) {
|
||||
logger.error('Streaming cost calculation failed', {
|
||||
model,
|
||||
inputLength: inputText?.length || 0,
|
||||
outputLength: outputText?.length || 0,
|
||||
error: toError(error).message,
|
||||
})
|
||||
|
||||
if (error instanceof Error && error.name === 'TokenizationError') {
|
||||
throw error
|
||||
}
|
||||
|
||||
throw createTokenizationError(
|
||||
'CALCULATION_FAILED',
|
||||
`Failed to calculate streaming cost: ${toError(error).message}`,
|
||||
{ model, inputLength: inputText?.length || 0, outputLength: outputText?.length || 0 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates cost for tokenization input object
|
||||
*/
|
||||
export function calculateTokenizationCost(input: TokenizationInput): StreamingCostResult {
|
||||
return calculateStreamingCost(
|
||||
input.model,
|
||||
input.inputText,
|
||||
input.outputText,
|
||||
input.systemPrompt,
|
||||
input.context,
|
||||
input.messages
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a streaming cost result from existing provider response data
|
||||
*/
|
||||
export function createCostResultFromProviderData(
|
||||
model: string,
|
||||
providerTokens: TokenUsage,
|
||||
providerCost: CostBreakdown
|
||||
): StreamingCostResult {
|
||||
const providerId = getProviderForTokenization(model)
|
||||
|
||||
return {
|
||||
tokens: providerTokens,
|
||||
cost: providerCost,
|
||||
model,
|
||||
provider: providerId,
|
||||
method: 'provider_response',
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user