- GET /settings/ai-token-usage: 목록 + 통계 조회 - GET /settings/ai-token-usage/pricing: 단가 설정 조회 (읽기 전용) - AiTokenHelper: Gemini/Claude/R2/STT 사용량 기록 헬퍼 - AiPricingConfig 캐시에 cloudflare-r2 provider 추가
134 lines
4.9 KiB
PHP
134 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\Tenants\AiPricingConfig;
|
|
use App\Models\Tenants\AiTokenUsage;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
class AiTokenHelper
|
|
{
|
|
/**
|
|
* Gemini API 응답에서 토큰 사용량 저장
|
|
*/
|
|
public static function saveGeminiUsage(array $apiResult, string $model, string $menuName): void
|
|
{
|
|
try {
|
|
$usage = $apiResult['usageMetadata'] ?? null;
|
|
if (! $usage) {
|
|
return;
|
|
}
|
|
|
|
$promptTokens = $usage['promptTokenCount'] ?? 0;
|
|
$completionTokens = $usage['candidatesTokenCount'] ?? 0;
|
|
$totalTokens = $usage['totalTokenCount'] ?? 0;
|
|
|
|
$pricing = AiPricingConfig::getActivePricing('gemini');
|
|
$inputPrice = $pricing ? (float) $pricing->input_price_per_million / 1_000_000 : 0.10 / 1_000_000;
|
|
$outputPrice = $pricing ? (float) $pricing->output_price_per_million / 1_000_000 : 0.40 / 1_000_000;
|
|
|
|
self::save($model, $menuName, $promptTokens, $completionTokens, $totalTokens, $inputPrice, $outputPrice);
|
|
} catch (\Exception $e) {
|
|
Log::warning('AI token usage save failed (Gemini)', ['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Claude API 응답에서 토큰 사용량 저장
|
|
*/
|
|
public static function saveClaudeUsage(array $apiResult, string $model, string $menuName): void
|
|
{
|
|
try {
|
|
$usage = $apiResult['usage'] ?? null;
|
|
if (! $usage) {
|
|
return;
|
|
}
|
|
|
|
$promptTokens = $usage['input_tokens'] ?? 0;
|
|
$completionTokens = $usage['output_tokens'] ?? 0;
|
|
$totalTokens = $promptTokens + $completionTokens;
|
|
|
|
$pricing = AiPricingConfig::getActivePricing('claude');
|
|
$inputPrice = $pricing ? (float) $pricing->input_price_per_million / 1_000_000 : 0.25 / 1_000_000;
|
|
$outputPrice = $pricing ? (float) $pricing->output_price_per_million / 1_000_000 : 1.25 / 1_000_000;
|
|
|
|
self::save($model, $menuName, $promptTokens, $completionTokens, $totalTokens, $inputPrice, $outputPrice);
|
|
} catch (\Exception $e) {
|
|
Log::warning('AI token usage save failed (Claude)', ['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cloudflare R2 Storage 업로드 사용량 저장
|
|
* Class A (PUT/POST): $0.0045 / 1,000,000건
|
|
* Storage: $0.015 / GB / 월
|
|
*/
|
|
public static function saveR2StorageUsage(string $menuName, int $fileSizeBytes): void
|
|
{
|
|
try {
|
|
$pricing = AiPricingConfig::getActivePricing('cloudflare-r2');
|
|
$unitPrice = $pricing ? (float) $pricing->unit_price : 0.0045;
|
|
|
|
$operationCost = $unitPrice / 1_000_000;
|
|
$fileSizeGB = $fileSizeBytes / (1024 * 1024 * 1024);
|
|
$storageCost = $fileSizeGB * 0.015;
|
|
$costUsd = $operationCost + $storageCost;
|
|
|
|
self::save('cloudflare-r2', $menuName, $fileSizeBytes, 0, $fileSizeBytes, $costUsd / max($fileSizeBytes, 1), 0);
|
|
} catch (\Exception $e) {
|
|
Log::warning('AI token usage save failed (R2)', ['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Speech-to-Text 사용량 저장
|
|
* STT latest_long 모델: $0.009 / 15초
|
|
*/
|
|
public static function saveSttUsage(string $menuName, int $durationSeconds): void
|
|
{
|
|
try {
|
|
$pricing = AiPricingConfig::getActivePricing('google-stt');
|
|
$sttUnitPrice = $pricing ? (float) $pricing->unit_price : 0.009;
|
|
$costUsd = ceil($durationSeconds / 15) * $sttUnitPrice;
|
|
|
|
self::save('google-speech-to-text', $menuName, $durationSeconds, 0, $durationSeconds, $costUsd / max($durationSeconds, 1), 0);
|
|
} catch (\Exception $e) {
|
|
Log::warning('AI token usage save failed (STT)', ['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 공통 저장 로직
|
|
*/
|
|
private static function save(
|
|
string $model,
|
|
string $menuName,
|
|
int $promptTokens,
|
|
int $completionTokens,
|
|
int $totalTokens,
|
|
float $inputPricePerToken,
|
|
float $outputPricePerToken,
|
|
): void {
|
|
$costUsd = ($promptTokens * $inputPricePerToken) + ($completionTokens * $outputPricePerToken);
|
|
$exchangeRate = AiPricingConfig::getExchangeRate();
|
|
$costKrw = $costUsd * $exchangeRate;
|
|
|
|
$tenantId = app('tenant_id');
|
|
$userId = app('api_user');
|
|
|
|
AiTokenUsage::create([
|
|
'tenant_id' => $tenantId ?: 1,
|
|
'model' => $model,
|
|
'menu_name' => $menuName,
|
|
'prompt_tokens' => $promptTokens,
|
|
'completion_tokens' => $completionTokens,
|
|
'total_tokens' => $totalTokens,
|
|
'cost_usd' => $costUsd,
|
|
'cost_krw' => $costKrw,
|
|
'request_id' => Str::uuid()->toString(),
|
|
'created_by' => $userId ?: null,
|
|
]);
|
|
}
|
|
}
|