feat: add ai cost tracking foundation

This commit is contained in:
2026-07-31 16:24:35 +08:00
parent ec9f8a015a
commit a84650bb9e
14 changed files with 327 additions and 2 deletions

View File

@@ -231,7 +231,9 @@ def ai_logs(
AiRequestLog.id, AiRequestLog.session_id, AiRequestLog.message_id, AiRequestLog.user_id,
AiRequestLog.model_name, AiRequestLog.knowledge_ids, AiRequestLog.retrieve_count,
AiRequestLog.input_token, AiRequestLog.output_token, AiRequestLog.total_token,
AiRequestLog.cost_ms, AiRequestLog.status, AiRequestLog.error_message, AiRequestLog.created_at,
AiRequestLog.cost_ms, AiRequestLog.estimated_cost, AiRequestLog.currency,
AiRequestLog.route_reason, AiRequestLog.question_type, AiRequestLog.knowledge_hit,
AiRequestLog.status, AiRequestLog.error_message, AiRequestLog.created_at,
)).order_by(AiRequestLog.created_at.desc()).offset((page - 1) * pageSize).limit(pageSize)
).all()
return api_success(page_result([_ai_log_dict(item, include_chunks=False) for item in logs], total=total, page=page, page_size=pageSize))
@@ -373,6 +375,11 @@ def _ai_log_dict(log: AiRequestLog, *, include_prompt: bool = False, include_chu
"outputToken": log.output_token,
"totalToken": log.total_token,
"costMs": log.cost_ms,
"estimatedCost": float(log.estimated_cost) if log.estimated_cost is not None else None,
"currency": log.currency,
"routeReason": log.route_reason,
"questionType": log.question_type,
"knowledgeHit": bool(log.knowledge_hit),
"status": log.status,
"errorMessage": log.error_message,
"createdAt": log.created_at,

View File

@@ -280,6 +280,14 @@ def create_model(
extra_params=payload.extraParams,
remark=payload.remark,
timeout_second=payload.timeoutSecond,
input_price_per_1k=payload.inputPricePer1k,
output_price_per_1k=payload.outputPricePer1k,
currency=payload.currency or "CNY",
usage_scenarios=payload.usageScenarios,
allow_summary=payload.allowSummary,
allow_report=payload.allowReport,
allow_fixed_info=payload.allowFixedInfo,
allow_deep_chat=payload.allowDeepChat,
enabled=0,
)
db.add(model)
@@ -323,6 +331,14 @@ def update_model(
model.extra_params = payload.extraParams
model.remark = payload.remark
model.timeout_second = payload.timeoutSecond
model.input_price_per_1k = payload.inputPricePer1k
model.output_price_per_1k = payload.outputPricePer1k
model.currency = payload.currency or "CNY"
model.usage_scenarios = payload.usageScenarios
model.allow_summary = payload.allowSummary
model.allow_report = payload.allowReport
model.allow_fixed_info = payload.allowFixedInfo
model.allow_deep_chat = payload.allowDeepChat
db.add(model)
OperationLogService.write(db, admin_id=current_admin.id, module="model", action="update", target_id=model.id)
db.commit()
@@ -433,6 +449,14 @@ def _model_dict(model: ModelConfig) -> dict:
"remark": model.remark,
"timeoutSecond": model.timeout_second,
"enabled": model.enabled,
"inputPricePer1k": float(model.input_price_per_1k) if model.input_price_per_1k is not None else None,
"outputPricePer1k": float(model.output_price_per_1k) if model.output_price_per_1k is not None else None,
"currency": model.currency,
"usageScenarios": model.usage_scenarios,
"allowSummary": model.allow_summary,
"allowReport": model.allow_report,
"allowFixedInfo": model.allow_fixed_info,
"allowDeepChat": model.allow_deep_chat,
}