feat: pricing stat.
This commit is contained in:
@@ -4,6 +4,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"context" // 在此处添加 context 导入
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -390,11 +391,35 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 计算费用
|
||||
var pricing models.AIPricingConfig
|
||||
var costJSON string
|
||||
var totalCost float64
|
||||
if err := config.DB.First(&pricing).Error; err == nil {
|
||||
inputCost := float64(analysisResult.InputTokens) * pricing.InputPricePerMillion / 1_000_000
|
||||
outputCost := float64(analysisResult.OutputTokens) * pricing.OutputPricePerMillion / 1_000_000
|
||||
totalCost = inputCost + outputCost
|
||||
|
||||
costInfo := map[string]interface{}{
|
||||
"pricingName": pricing.Name,
|
||||
"provider": pricing.Provider,
|
||||
"inputPricePerMillion": pricing.InputPricePerMillion,
|
||||
"outputPricePerMillion": pricing.OutputPricePerMillion,
|
||||
"inputCost": inputCost,
|
||||
"outputCost": outputCost,
|
||||
}
|
||||
if b, err := json.Marshal(costInfo); err == nil {
|
||||
costJSON = string(b)
|
||||
}
|
||||
}
|
||||
|
||||
record := models.AIAnalysisRecord{
|
||||
RegionID: regionID,
|
||||
SourceType: teachingPlanSource,
|
||||
AnalysisType: analysisType,
|
||||
AnalysisResult: analysisResult.Content,
|
||||
CostJSON: costJSON,
|
||||
TotalCost: totalCost,
|
||||
InputTokens: analysisResult.InputTokens,
|
||||
OutputTokens: analysisResult.OutputTokens,
|
||||
InputSizeBytes: analysisResult.InputSizeBytes,
|
||||
|
||||
Reference in New Issue
Block a user