Files
aiagent/backend/app/services/cost_estimator.py
renjianbo ab1589921a fix: 修复35个安全与功能缺陷,补全知识进化/数字孪生/行为采集模块
## 安全修复 (12项)
- Webhook接口添加全局Token认证,过滤敏感请求头
- 修复JWT Base64 padding公式,防止签名验证绕过
- 数据库密码/飞书Token从源码移除,改为环境变量
- 工作流引擎添加路径遍历防护 (_resolve_safe_path)
- eval()添加模板长度上限检查
- 审批API添加认证依赖
- 前端v-html增强XSS转义,console.log仅开发模式输出
- 500错误不再暴露内部异常详情

## Agent运行时修复 (7项)
- 删除_inject_knowledge_context中未定义db变量的finally块
- 工具执行添加try/except保护,异常不崩溃Agent
- LLM重试计入budget计数器
- self_review异常时passed=False
- max_iterations截断标记success=False
- 工具参数JSON解析失败时记录警告日志
- run()开始时重置_llm_invocations计数器

## 配置与基础设施
- DEBUG默认False,SQL_ECHO独立配置项
- init_db()补全13个缺失模型导入
- 新增WEBHOOK_AUTH_TOKEN/SQL_ECHO配置项
- 新增.env.example模板文件

## 前端修复 (12项)
- 登录改用URLSearchParams替代FormData
- 401拦截器通过Pinia store统一清理状态
- SSE流超时从60s延长至300s
- final/error事件时清除streamTimeout
- localStorage聊天记录添加24h TTL
- safeParseArgCount替代模板中裸JSON.parse
- fetchUser 401时同时清除user对象

## 新增模块
- 知识进化: knowledge_extractor/retriever/tasks
- 数字孪生: shadow_executor/comparison模型
- 行为采集: behavior_middleware/collector/fingerprint_engine
- 代码审查: code_review_agent/document_review_agent
- 反馈学习: feedback_learner
- 瓶颈检测/优化引擎/成本估算/需求估算
- 速率限制器 (rate_limiter)
- Alembic迁移 015-020

## 文档
- 商业化落地计划
- 8篇docs文档 (架构/API/部署/开发/贡献等)
- Docker Compose生产配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-10 19:50:20 +08:00

79 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""LLM 成本估算 — 基于模型定价和 Token 用量估算费用"""
from __future__ import annotations
import logging
from typing import Dict, Optional, Tuple
logger = logging.getLogger(__name__)
# 模型定价 (per 1M tokens, USD) — 2024 Q4 参考值
MODEL_PRICING: Dict[str, Tuple[float, float]] = {
# (input_price_per_1M, output_price_per_1M)
"gpt-4o": (2.50, 10.00),
"gpt-4o-mini": (0.15, 0.60),
"gpt-4-turbo": (10.00, 30.00),
"gpt-3.5-turbo": (0.50, 1.50),
"deepseek-chat": (0.14, 0.28),
"deepseek-v3": (0.27, 1.10),
"deepseek-r1": (0.55, 2.19),
"deepseek-v4-flash": (0.14, 0.28),
"deepseek-v4-pro": (0.27, 1.10),
"claude-3-opus": (15.00, 75.00),
"claude-3-sonnet": (3.00, 15.00),
"claude-3-haiku": (0.25, 1.25),
"claude-3.5-sonnet": (3.00, 15.00),
"claude-3.5-haiku": (1.00, 5.00),
}
# 缓存未命中模型的默认定价
DEFAULT_PRICING = (1.00, 4.00)
def estimate_cost(
model: str,
prompt_tokens: int = 0,
completion_tokens: int = 0,
) -> float:
"""估算单次 LLM 调用的费用USD
Args:
model: 模型名称(模糊匹配)
prompt_tokens: 输入 token 数
completion_tokens: 输出 token 数
"""
input_price, output_price = _get_price(model)
cost = (prompt_tokens / 1_000_000) * input_price + (
completion_tokens / 1_000_000
) * output_price
return round(cost, 6)
def estimate_cost_yuan(
model: str,
prompt_tokens: int = 0,
completion_tokens: int = 0,
exchange_rate: float = 7.2,
) -> float:
"""估算费用(人民币)。"""
return round(estimate_cost(model, prompt_tokens, completion_tokens) * exchange_rate, 4)
def _get_price(model: str) -> Tuple[float, float]:
"""模糊匹配模型定价。"""
model_lower = model.lower().replace("-", "").replace(".", "")
for key, price in MODEL_PRICING.items():
if key.replace("-", "").replace(".", "") in model_lower:
return price
return DEFAULT_PRICING
def get_model_pricing_table() -> Dict[str, dict]:
"""返回模型定价表(供前端展示)。"""
return {
model: {
"input_per_1M": input_p,
"output_per_1M": output_p,
}
for model, (input_p, output_p) in MODEL_PRICING.items()
}