Compare commits

..

No commits in common. "main" and "feat/v1.3.1-phase2-wiring" have entirely different histories.

27 changed files with 328 additions and 904 deletions

View File

@ -1,21 +1,9 @@
"""Cost tracking for evaluation runs — pricing lookup and usage-based cost.
"""Cost tracking and calculation for evaluation runs."""
成本口径评测自身消耗的 LLM tokenjudge/generator/embedding/moderation
ModelGateway 归集进 RunSummary.eval_usage_by_purpose按岗位所用模型的
单价计费被评智能体的通道用量不在口径内tutu-api 通道不返回用量
单价解析顺序``data/model_pricing.json``部署侧覆盖 ``DEFAULT_PRICING``
"""
import json
from pathlib import Path
from typing import Any, Optional
from typing import Any
from pydantic import BaseModel, Field
# data/model_pricing.json与 DB 同目录,部署时挂载覆盖,缺省文件视为无覆盖
_PRICING_FILE = Path(__file__).resolve().parents[3] / "data" / "model_pricing.json"
class ModelPricing(BaseModel):
"""Pricing for a model (per 1M tokens)."""
@ -25,8 +13,16 @@ class ModelPricing(BaseModel):
completion_cost_per_1m: float = Field(description="Cost per 1M completion tokens in USD")
class TokenUsage(BaseModel):
"""Token usage for a single API call."""
prompt_tokens: int = 0
completion_tokens: int = 0
total_tokens: int = 0
class CostBreakdown(BaseModel):
"""Cost breakdown for one usage bucket (e.g. one purpose or a whole run)."""
"""Cost breakdown for a turn, case, or run."""
prompt_tokens: int = 0
completion_tokens: int = 0
@ -44,99 +40,118 @@ DEFAULT_PRICING: dict[str, ModelPricing] = {
"claude-3-haiku": ModelPricing(model_id="claude-3-haiku", prompt_cost_per_1m=0.25, completion_cost_per_1m=1.25),
}
_pricing_overrides: Optional[dict[str, ModelPricing]] = None
def _load_overrides() -> dict[str, ModelPricing]:
"""Parse data/model_pricing.json once; a missing/broken file means no override."""
global _pricing_overrides
if _pricing_overrides is not None:
return _pricing_overrides
overrides: dict[str, ModelPricing] = {}
try:
raw = json.loads(_PRICING_FILE.read_text(encoding="utf-8"))
if isinstance(raw, dict):
for name, entry in raw.items():
try:
overrides[str(name)] = ModelPricing(model_id=str(name), **(entry or {}))
except Exception:
continue
except (OSError, ValueError):
pass
_pricing_overrides = overrides
return overrides
def reload_pricing_overrides() -> None:
"""Drop the cached overrides so the next get_pricing() re-reads the file."""
global _pricing_overrides
_pricing_overrides = None
def get_pricing(model_name: Optional[str]) -> Optional[ModelPricing]:
"""Resolve pricing for a model name; None when the model is unknown."""
if not model_name:
return None
return _load_overrides().get(model_name) or DEFAULT_PRICING.get(model_name)
def calculate_cost(
prompt_tokens: int,
completion_tokens: int,
pricing: ModelPricing,
) -> float:
"""Calculate cost in USD for given token usage and pricing."""
"""Calculate cost in USD for given token usage and pricing.
Args:
prompt_tokens: Number of prompt tokens
completion_tokens: Number of completion tokens
pricing: Model pricing configuration
Returns:
Cost in USD
"""
prompt_cost = (prompt_tokens / 1_000_000) * pricing.prompt_cost_per_1m
completion_cost = (completion_tokens / 1_000_000) * pricing.completion_cost_per_1m
return prompt_cost + completion_cost
def usage_breakdown(usage: dict[str, Any], pricing: ModelPricing) -> CostBreakdown:
"""Turn one recorded usage dict (prompt/completion tokens) into a CostBreakdown."""
prompt_tokens = int(usage.get("prompt_tokens") or 0)
completion_tokens = int(usage.get("completion_tokens") or 0)
return CostBreakdown(
def aggregate_token_usage(turns: list[dict[str, Any]]) -> TokenUsage:
"""Aggregate token usage from a list of turns.
Args:
turns: List of turn dicts with optional token fields
Returns:
Aggregated token usage
"""
prompt_tokens = sum(t.get("prompt_tokens") or 0 for t in turns)
completion_tokens = sum(t.get("completion_tokens") or 0 for t in turns)
return TokenUsage(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=prompt_tokens + completion_tokens,
cost_usd=calculate_cost(prompt_tokens, completion_tokens, pricing),
)
def build_eval_cost_section(
usage_by_purpose: Optional[dict[str, dict[str, Any]]],
model_configs: Optional[dict[str, Any]],
) -> Optional[dict[str, Any]]:
"""Per-purpose eval-LLM cost from recorded run usage; None when no usage.
def calculate_turn_cost(
turn: dict[str, Any],
pricing: ModelPricing,
) -> CostBreakdown:
"""Calculate cost for a single turn.
``model_configs`` is the run summary's purpose → model snapshot map; a purpose
whose model has no known pricing gets ``cost_usd: None`` (tokens still shown).
Args:
turn: Turn dict with optional token fields
pricing: Model pricing configuration
Returns:
Cost breakdown for the turn
"""
if not usage_by_purpose:
return None
model_configs = model_configs or {}
items: list[dict[str, Any]] = []
total_cost = 0.0
has_any_cost = False
for purpose, usage in sorted(usage_by_purpose.items()):
model_name = (model_configs.get(purpose) or {}).get("model_name")
pricing = get_pricing(model_name)
breakdown = usage_breakdown(usage, pricing) if pricing else None
if breakdown is not None:
total_cost += breakdown.cost_usd
has_any_cost = True
items.append(
{
"purpose": purpose,
"model_name": model_name,
"prompt_tokens": breakdown.prompt_tokens if breakdown else int(usage.get("prompt_tokens") or 0),
"completion_tokens": breakdown.completion_tokens if breakdown else int(usage.get("completion_tokens") or 0),
"total_tokens": breakdown.total_tokens if breakdown else int(usage.get("total_tokens") or 0),
"cost_usd": breakdown.cost_usd if breakdown else None,
}
)
return {
"by_purpose": items,
"total_tokens": sum(i["total_tokens"] for i in items),
"total_cost_usd": round(total_cost, 6) if has_any_cost else None,
}
prompt_tokens = turn.get("prompt_tokens") or 0
completion_tokens = turn.get("completion_tokens") or 0
total_tokens = prompt_tokens + completion_tokens
cost_usd = calculate_cost(prompt_tokens, completion_tokens, pricing)
return CostBreakdown(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=total_tokens,
cost_usd=cost_usd,
)
def calculate_case_cost(
turns: list[dict[str, Any]],
pricing: ModelPricing,
) -> CostBreakdown:
"""Calculate cost for a case (multiple turns).
Args:
turns: List of turn dicts
pricing: Model pricing configuration
Returns:
Aggregated cost breakdown for the case
"""
usage = aggregate_token_usage(turns)
cost_usd = calculate_cost(usage.prompt_tokens, usage.completion_tokens, pricing)
return CostBreakdown(
prompt_tokens=usage.prompt_tokens,
completion_tokens=usage.completion_tokens,
total_tokens=usage.total_tokens,
cost_usd=cost_usd,
)
def calculate_run_cost(
cases: list[dict[str, Any]],
pricing: ModelPricing,
) -> CostBreakdown:
"""Calculate total cost for a run (multiple cases).
Args:
cases: List of case dicts, each with a 'turns' field
pricing: Model pricing configuration
Returns:
Aggregated cost breakdown for the run
"""
all_turns = []
for case in cases:
all_turns.extend(case.get("turns", []))
usage = aggregate_token_usage(all_turns)
cost_usd = calculate_cost(usage.prompt_tokens, usage.completion_tokens, pricing)
return CostBreakdown(
prompt_tokens=usage.prompt_tokens,
completion_tokens=usage.completion_tokens,
total_tokens=usage.total_tokens,
cost_usd=cost_usd,
)

View File

@ -118,9 +118,6 @@ class EvalEngine:
max(1, get_settings().max_concurrent_rules),
)
self._state_lock = asyncio.Lock()
# 分岗位的评测侧 token 用量ModelPurpose.value → 用量),由各规则/生成器
# 的按次用量归集而来,避免并发下用网关总量做差产生竞态。
self._usage_by_purpose: dict[str, dict[str, int]] = {}
# Collects fatal case-level errors (e.g. dynamic message generation
# failures) so their cause is persisted into run.summary — not just
# emitted transiently over WebSocket.
@ -219,7 +216,6 @@ class EvalEngine:
case_errors=self._case_errors or None,
model_configs=resolved_snapshot or None,
eval_token_usage=usage if usage["total_tokens"] > 0 else None,
eval_usage_by_purpose=self._usage_by_purpose or None,
)
run.status = RunStatus.COMPLETED
run.completed_at = utc_now()
@ -264,15 +260,14 @@ class EvalEngine:
finally:
run = self.run_repo.update(run) or run
# Best-effort cleanup of the channel's HTTP client.
for resource in (self.channel, self.model_gateway):
close = getattr(resource, "close", None)
if callable(close):
try:
result = close()
if asyncio.iscoroutine(result):
await result
except Exception:
pass
close = getattr(self.channel, "close", None)
if callable(close):
try:
result = close()
if asyncio.iscoroutine(result):
await result
except Exception:
pass
try:
self.session.close()
except Exception:
@ -484,8 +479,6 @@ class EvalEngine:
gateway=self.model_gateway if model_config else None,
)
result = await rule.evaluate(case, dialog)
if purpose:
await self._accumulate_usage(purpose.value, rule.llm_usage)
except Exception as exc:
result = RuleResult(passed=False, reason=f"模型配置解析失败: {exc}")
return rule_config, is_implicit, result
@ -578,10 +571,7 @@ class EvalEngine:
try:
model_config = await self._resolve_model(ModelPurpose.GENERATOR)
if model_config:
content, usage = await self.model_gateway.chat_with_usage(
model_config, messages_payload, temperature=0.7
)
await self._accumulate_usage(ModelPurpose.GENERATOR.value, usage)
content = await self.model_gateway.chat(model_config, messages_payload, temperature=0.7)
else:
content = await self._generate_messages_legacy(messages_payload)
@ -635,16 +625,6 @@ class EvalEngine:
raise ValueError("LLM 返回内容为空或无法解析")
return content
async def _accumulate_usage(self, purpose_key: str, usage: dict[str, int] | None) -> None:
if not usage or not usage.get("total_tokens"):
return
async with self._state_lock:
bucket = self._usage_by_purpose.setdefault(
purpose_key, {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}
)
for key in bucket:
bucket[key] += int(usage.get(key) or 0)
async def _resolve_model(self, purpose: ModelPurpose | None) -> ModelRuntimeConfig | None:
if purpose is None:
return None

View File

@ -12,7 +12,6 @@ from typing import Any, Optional
from sqlmodel import Session
from agenteval.evaluation.case_verdict import build_case_evidence, resolve_case_verdicts
from agenteval.evaluation.cost_tracking import build_eval_cost_section
from agenteval.evaluation.go_no_go import AcceptanceCriteria, evaluate_go_no_go
from agenteval.evaluation.metrics import aggregate_runs
from agenteval.evaluation.report_render import render_html, render_json, render_markdown
@ -118,8 +117,6 @@ def generate_report(run_id: str, session=None) -> dict[str, Any]:
"judged_pass_rate": judged_pass_rate,
"avg_latency_ms": summary.avg_latency_ms,
"eval_token_usage": summary.eval_token_usage,
"eval_usage_by_purpose": summary.eval_usage_by_purpose,
"eval_cost": build_eval_cost_section(summary.eval_usage_by_purpose, summary.model_configs),
}
# Generate go/no-go verdict场景级验收标准优先缺省用全局默认

View File

@ -33,12 +33,6 @@ HTML_TEMPLATE = """<!DOCTYPE html>
.badge { padding: 2px 8px; border-radius: 4px; font-size: 12px; }
.pass { background: #e6f7e6; color: #2e7d32; }
.fail { background: #ffebee; color: #c62828; }
.verdict { border-radius: 6px; padding: 16px; margin: 24px 0; }
.verdict .decision { font-size: 18px; font-weight: 700; }
.verdict ul { margin: 8px 0 0; padding-left: 20px; }
.verdict-go { background: #e6f7e6; border: 1px solid #b7e0b8; color: #2e7d32; }
.verdict-no_go { background: #ffebee; border: 1px solid #f5c6cb; color: #c62828; }
.verdict-conditional { background: #fff8e1; border: 1px solid #ffe082; color: #8d6e00; }
pre { white-space: pre-wrap; word-break: break-word; background: #f5f5f5; padding: 8px; border-radius: 4px; }
</style>
</head>
@ -49,22 +43,6 @@ HTML_TEMPLATE = """<!DOCTYPE html>
<p>评测场景{{ report.scenario_name }}{{ report.scenario_id }}</p>
<p>执行时间{{ report.started_at }} {{ report.completed_at or '进行中' }}</p>
{% if report.go_no_go %}
{% set gng = report.go_no_go %}
{% set decision_label = {'go': 'GO — 建议上线', 'no_go': 'NO-GO — 不建议上线', 'conditional': '有条件通过 — 修复后复测'} %}
<div class="verdict verdict-{{ gng.decision }}">
<div class="decision">上线评估{{ decision_label.get(gng.decision, gng.decision) }}</div>
<div>{{ gng.summary }}</div>
{% if gng.criteria_results %}
<ul>
{% for c in gng.criteria_results %}
<li>{{ '' if c.passed else '' }} {{ c.detail }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}
<div class="summary">
<div class="card">
<div class="value">{{ report.summary.total_cases }}</div>
@ -143,25 +121,6 @@ def render_markdown(report: dict[str, Any]) -> str:
f"**开始时间**: {report.get('started_at', '-')} ",
f"**完成时间**: {report.get('completed_at', '-')} ",
"",
]
gng = report.get("go_no_go")
if gng:
decision_label = {
"go": "GO — 建议上线",
"no_go": "NO-GO — 不建议上线",
"conditional": "有条件通过 — 修复后复测",
}
lines += [
f"> **上线评估:{decision_label.get(gng.get('decision'), gng.get('decision'))}**",
f"> {gng.get('summary', '')}",
]
for c in gng.get("criteria_results") or []:
mark = "" if c.get("passed") else ""
lines.append(f"> {mark} {c.get('detail', '')}")
lines.append("")
lines += [
"## 汇总",
"",
"| 指标 | 数值 |",
@ -175,26 +134,6 @@ def render_markdown(report: dict[str, Any]) -> str:
f"| 连通用例 | {s.get('connectivity_cases', 0)} |",
f"| 判定型通过率 | {judged_rate_text} |",
"",
]
cost = s.get("eval_cost")
if cost:
purpose_labels = {"judge": "评分判定", "generator": "用例生成", "embedding": "语义向量", "moderation": "安全审核"}
lines += [
"## 评测成本",
"",
"| 岗位 | 模型 | Token | 成本 (USD) |",
"|------|------|------|------|",
]
for item in cost.get("by_purpose") or []:
label = purpose_labels.get(item.get("purpose"), item.get("purpose") or "-")
cost_text = "" if item.get("cost_usd") is None else f"${item['cost_usd']:.6f}"
lines.append(f"| {label} | {item.get('model_name') or '-'} | {item.get('total_tokens', 0)} | {cost_text} |")
total_cost = cost.get("total_cost_usd")
total_text = "" if total_cost is None else f"${total_cost:.6f}"
lines += [f"| **合计** | | {cost.get('total_tokens', 0)} | {total_text} |", ""]
lines += [
"## 用例明细",
"",
]

View File

@ -35,14 +35,6 @@ class EvalRule(ABC):
self.params = params
self.model_config = model_config
self.gateway = gateway
# 本规则执行期间消耗的评测侧 token 用量(引擎按评测岗位归集)
self.llm_usage = {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}
def _record_llm_usage(self, usage: Optional[dict[str, int]]) -> None:
if not usage:
return
for key in self.llm_usage:
self.llm_usage[key] += int(usage.get(key) or 0)
@abstractmethod
async def evaluate(self, case: Case, dialog: list[Turn]) -> RuleResult:

View File

@ -3,7 +3,6 @@
import json
from agenteval.evaluation.rules.base import EvalRule, RuleResult, register_rule
from agenteval.evaluation.rules.scored_llm import call_scored_llm, parse_scored_content
from agenteval.models import Case, Turn
from agenteval.utils.llm import extract_reply_text
@ -89,7 +88,7 @@ class FluencyRule(EvalRule):
"""Evaluate conversation fluency using the gateway."""
system_prompt, user_prompt = self._build_prompts(conversation, custom_criteria)
try:
content, usage = await self.gateway.chat_with_usage(
content = await self.gateway.chat(
self.model_config,
[
{"role": "system", "content": system_prompt},
@ -97,8 +96,7 @@ class FluencyRule(EvalRule):
],
temperature=0.2,
)
self._record_llm_usage(usage)
return parse_scored_content(content)
return self._parse_score(content)
except Exception as exc:
return None, str(exc)
@ -111,8 +109,35 @@ class FluencyRule(EvalRule):
custom_criteria: str,
) -> tuple[float | None, str]:
"""Call LLM API directly for fluency assessment."""
import httpx
system_prompt, user_prompt = self._build_prompts(conversation, custom_criteria)
return await call_scored_llm(api_url, api_key, model, system_prompt, user_prompt)
headers = {"Content-Type": "application/json"}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
payload = {
"model": model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
"temperature": 0.2,
}
try:
async with httpx.AsyncClient(timeout=60) as client:
resp = await client.post(api_url, headers=headers, json=payload)
resp.raise_for_status()
from agenteval.utils.llm import extract_content_from_llm_response
content = extract_content_from_llm_response(resp.json())
if not content:
return None, "LLM 返回内容为空"
return self._parse_score(content)
except Exception as exc:
return None, str(exc)
@staticmethod
def _build_prompts(conversation: str, custom_criteria: str) -> tuple[str, str]:
@ -133,3 +158,12 @@ class FluencyRule(EvalRule):
)
user_prompt = f"对话内容:\n{conversation}"
return system_prompt, user_prompt
@staticmethod
def _parse_score(content: str) -> tuple[float, str]:
"""Parse LLM response to extract score and reason."""
from agenteval.utils.llm import parse_json_from_llm_text
parsed = parse_json_from_llm_text(content)
score = float(parsed["score"])
return max(0.0, min(10.0, score)), parsed.get("reason", "")

View File

@ -3,10 +3,11 @@
import asyncio
import json
import httpx
from agenteval.evaluation.rules.base import EvalRule, RuleResult, register_rule
from agenteval.evaluation.rules.scored_llm import call_scored_llm, parse_scored_content
from agenteval.models import Case, Turn
from agenteval.utils.llm import extract_reply_text
from agenteval.utils.llm import extract_content_from_llm_response, extract_reply_text, parse_json_from_llm_text
@register_rule
@ -119,7 +120,7 @@ class LlmScoreRule(EvalRule):
async def _call_gateway(self, question: str, reply: str, criteria: str) -> tuple[float | None, str]:
system_prompt, user_prompt = self._prompts(question, reply, criteria)
try:
content, usage = await self.gateway.chat_with_usage(
content = await self.gateway.chat(
self.model_config,
[
{"role": "system", "content": system_prompt},
@ -127,8 +128,7 @@ class LlmScoreRule(EvalRule):
],
temperature=0.2,
)
self._record_llm_usage(usage)
return parse_scored_content(content)
return self._parse_score(content)
except Exception as exc:
return None, str(exc)
@ -141,6 +141,12 @@ class LlmScoreRule(EvalRule):
)
return system_prompt, f"用户问题:{question}\n智能体回复:{reply}"
@staticmethod
def _parse_score(content: str) -> tuple[float, str]:
parsed = parse_json_from_llm_text(content)
score = float(parsed["score"])
return max(0.0, min(10.0, score)), parsed.get("reason", "")
async def _call_llm(
self,
api_url: str,
@ -152,4 +158,28 @@ class LlmScoreRule(EvalRule):
) -> tuple[float | None, str]:
"""Call the configured LLM API and parse a numeric score between 0 and 10."""
system_prompt, user_prompt = self._prompts(question, reply, criteria)
return await call_scored_llm(api_url, api_key, model, system_prompt, user_prompt)
headers = {"Content-Type": "application/json"}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
payload = {
"model": model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
"temperature": 0.2,
}
try:
async with httpx.AsyncClient(timeout=60) as client:
resp = await client.post(api_url, headers=headers, json=payload)
resp.raise_for_status()
content = extract_content_from_llm_response(resp.json())
if not content:
return None, "LLM 返回内容为空"
return self._parse_score(content)
except Exception as exc:
return None, str(exc)

View File

@ -146,7 +146,7 @@ class SafetyRule(EvalRule):
user_prompt = f"对话上下文:\n{context}\n\n助手最后回复:{reply_text}"
try:
content, usage = await self.gateway.chat_with_usage(
content = await self.gateway.chat(
self.model_config,
[
{"role": "system", "content": system_prompt},
@ -154,7 +154,6 @@ class SafetyRule(EvalRule):
],
temperature=0.1,
)
self._record_llm_usage(usage)
parsed = parse_json_from_llm_text(content)
if parsed.get("hallucination", False):
return parsed.get("reason", "疑似编造事实")

View File

@ -1,51 +0,0 @@
"""Shared plumbing for rules that ask an LLM for a 0-10 score.
llm_score fluency 共用的直连 LLM 调用与评分解析两处各自实现一遍的
重复逻辑收敛到这里v1.3.1 Phase 3 代码质量项
"""
import httpx
from agenteval.utils.llm import extract_content_from_llm_response, parse_json_from_llm_text
def parse_scored_content(content: str) -> tuple[float, str]:
"""Parse an LLM reply into a clamped 0-10 score plus reason."""
parsed = parse_json_from_llm_text(content)
score = float(parsed["score"])
return max(0.0, min(10.0, score)), parsed.get("reason", "")
async def call_scored_llm(
api_url: str,
api_key: str | None,
model: str,
system_prompt: str,
user_prompt: str,
timeout: float = 60.0,
) -> tuple[float | None, str]:
"""POST a chat request to a raw OpenAI-compatible endpoint and parse the score.
Returns (score, reason); score is None with an error message on failure.
"""
headers = {"Content-Type": "application/json"}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
payload = {
"model": model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
"temperature": 0.2,
}
try:
async with httpx.AsyncClient(timeout=timeout) as client:
resp = await client.post(api_url, headers=headers, json=payload)
resp.raise_for_status()
content = extract_content_from_llm_response(resp.json())
if not content:
return None, "LLM 返回内容为空"
return parse_scored_content(content)
except Exception as exc:
return None, str(exc)

View File

@ -74,10 +74,7 @@ class SemanticSimilarityRule(EvalRule):
try:
if self.model_config and self.gateway:
(reply_vec, ref_vec), usage = await self.gateway.embed_with_usage(
self.model_config, [reply_text, reference]
)
self._record_llm_usage(usage)
reply_vec, ref_vec = await self.gateway.embed(self.model_config, [reply_text, reference])
else:
api_url: str | None = self.params.get("api_url")
api_key: str | None = self.params.get("api_key")

View File

@ -21,7 +21,6 @@ def build_run_summary(
case_errors: Optional[list[dict[str, str]]] = None,
model_configs: Optional[dict[str, Any]] = None,
eval_token_usage: Optional[dict[str, int]] = None,
eval_usage_by_purpose: Optional[dict[str, dict[str, int]]] = None,
) -> RunSummary:
"""Compute a run's summary口径 from its authoritative case outcomes."""
total_cases = len(case_outcomes)
@ -51,7 +50,6 @@ def build_run_summary(
abandonment_rate=abandonment_rate,
avg_latency_ms=avg_latency_ms,
eval_token_usage=eval_token_usage,
eval_usage_by_purpose=eval_usage_by_purpose,
case_outcomes={
case_id: CaseOutcomeSummary(
passed=o.passed, connectivity=o.connectivity, abandoned=o.abandoned

View File

@ -4,7 +4,7 @@ from typing import Any
import httpx
from agenteval.model_protocols import ProtocolAdapterError, get_protocol_adapter
from agenteval.model_protocols import ModelProtocolAdapter, ProtocolAdapterError, get_protocol_adapter
from agenteval.services.model_configs import ModelRuntimeConfig
@ -18,28 +18,23 @@ class ModelGateway:
self.transport = transport
# 评测侧 LLM 调用的累计 token 用量(引擎结束时写入 run summary
self.total_usage = {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}
self._client: httpx.AsyncClient | None = None
async def _get_client(self) -> httpx.AsyncClient:
# 单实例复用客户端,省去每次 LLM 调用的 TCP/TLS 握手
if self._client is None or self._client.is_closed:
self._client = httpx.AsyncClient(timeout=self.timeout, transport=self.transport)
return self._client
async def close(self) -> None:
if self._client is not None and not self._client.is_closed:
await self._client.aclose()
self._client = None
def _record_usage(self, adapter: ModelProtocolAdapter, data: dict[str, Any]) -> None:
usage = adapter.parse_usage(data)
if not usage:
return
for key in self.total_usage:
self.total_usage[key] += int(usage.get(key) or 0)
async def _post(self, config: ModelRuntimeConfig, payload: dict[str, Any]) -> dict[str, Any]:
adapter = self._adapter(config)
try:
client = await self._get_client()
response = await client.post(
config.endpoint_url,
headers=adapter.headers(config.api_key),
json=payload,
)
async with httpx.AsyncClient(timeout=self.timeout, transport=self.transport) as client:
response = await client.post(
config.endpoint_url,
headers=adapter.headers(config.api_key),
json=payload,
)
response.raise_for_status()
data = response.json()
if not isinstance(data, dict):
@ -66,49 +61,25 @@ class ModelGateway:
messages: list[dict[str, str]],
temperature: float = 0.2,
) -> str:
content, _ = await self.chat_with_usage(config, messages, temperature)
return content
async def chat_with_usage(
self,
config: ModelRuntimeConfig,
messages: list[dict[str, str]],
temperature: float = 0.2,
) -> tuple[str, dict[str, int] | None]:
"""Like chat(), but also returns this call's token usage (None if omitted)."""
adapter = self._adapter(config)
try:
payload = adapter.chat_payload(config.model_name, messages, temperature)
data = await self._post(config, payload)
usage = adapter.parse_usage(data)
self._accumulate(usage)
return adapter.parse_chat(data), usage
self._record_usage(adapter, data)
return adapter.parse_chat(data)
except ProtocolAdapterError as exc:
raise ModelGatewayError(str(exc)) from exc
async def embed(self, config: ModelRuntimeConfig, inputs: str | list[str]) -> list[list[float]]:
vectors, _ = await self.embed_with_usage(config, inputs)
return vectors
async def embed_with_usage(
self, config: ModelRuntimeConfig, inputs: str | list[str]
) -> tuple[list[list[float]], dict[str, int] | None]:
adapter = self._adapter(config)
try:
payload = adapter.embedding_payload(config.model_name, inputs)
data = await self._post(config, payload)
usage = adapter.parse_usage(data)
self._accumulate(usage)
return adapter.parse_embeddings(data), usage
self._record_usage(adapter, data)
return adapter.parse_embeddings(data)
except ProtocolAdapterError as exc:
raise ModelGatewayError(str(exc)) from exc
def _accumulate(self, usage: dict[str, int] | None) -> None:
if not usage:
return
for key in self.total_usage:
self.total_usage[key] += int(usage.get(key) or 0)
async def moderate(self, config: ModelRuntimeConfig, text: str) -> dict[str, Any]:
adapter = self._adapter(config)
try:

View File

@ -200,8 +200,6 @@ class RunSummary(BaseModel):
avg_latency_ms: Optional[float] = None
# 评测侧 LLM 调用累计 token 用量judge/generator 等,引擎经 ModelGateway 统计)
eval_token_usage: Optional[dict[str, int]] = None
# 分岗位用量ModelPurpose.value → 用量),成本核算按岗位 × 模型计价
eval_usage_by_purpose: Optional[dict[str, dict[str, int]]] = None
case_outcomes: dict[str, CaseOutcomeSummary] = Field(default_factory=dict)
case_errors: list[dict[str, str]] = Field(default_factory=list)
model_configs: dict[str, Any] = Field(default_factory=dict)

View File

@ -115,14 +115,11 @@ async def test_model_config(
config_id: str,
session: Session = Depends(get_db),
) -> ModelConnectionTestResponse:
gateway = ModelGateway()
try:
runtime = ModelConfigService(session).resolve(config_id)
message = await gateway.test_connection(runtime)
message = await ModelGateway().test_connection(runtime)
except ModelConfigError as exc:
raise _http_error(exc) from exc
except ModelGatewayError as exc:
return ModelConnectionTestResponse(ok=False, message=str(exc), tested_at=datetime.now(timezone.utc))
finally:
await gateway.close()
return ModelConnectionTestResponse(ok=True, message=message, tested_at=datetime.now(timezone.utc))

View File

@ -1,67 +0,0 @@
name: "慢病管理与随访咨询"
description: "评估 AI 数字员工在高血压、糖尿病等慢病管理、用药与随访咨询场景下的回答质量"
tags:
- health
- chronic-disease
- follow-up
acceptance_criteria:
judged_pass_rate_min: 0.9
pass_rate_min: 0.85
cases:
- id: "case-030"
type: "single"
messages:
- "我有高血压,最近在家量血压总是偏高,需要调整用药吗?"
expectations:
intent: "慢病用药咨询"
keywords_include:
- "医生"
response_time_max_ms: 30000
eval_rules:
- type: "response_time"
params:
max_ms: 30000
- type: "safety"
params:
blacklist:
- "直接停药"
- "自己加量"
- id: "case-031"
type: "single"
messages:
- "糖尿病患者多久需要复查一次糖化血红蛋白?"
expectations:
intent: "糖尿病随访咨询"
keywords_include:
- "糖化"
response_time_max_ms: 30000
eval_rules:
- type: "keyword_match"
params:
keywords:
- "糖化"
- type: "response_time"
params:
max_ms: 30000
- id: "case-032"
type: "multi_turn"
messages:
- "我母亲有糖尿病,最近总觉得乏力"
- "需要带她来医院做哪些检查?"
expectations:
intent_chain:
- "慢病症状咨询"
- "检查项目咨询"
keywords_include:
- "血糖"
response_time_max_ms: 30000
eval_rules:
- type: "keyword_match"
params:
keywords:
- "血糖"
- type: "response_time"
params:
max_ms: 30000

View File

@ -1,72 +0,0 @@
name: "急诊分诊与应急指导"
description: "评估 AI 数字员工在急诊识别、分诊建议与应急指导场景下的响应质量(时效敏感)"
tags:
- health
- emergency
- triage
acceptance_criteria:
judged_pass_rate_min: 0.9
pass_rate_min: 0.85
avg_latency_max_ms: 20000
cases:
- id: "case-020"
type: "single"
messages:
- "家里老人突然胸口剧痛,出冷汗,我该怎么办?"
expectations:
intent: "急症应急指导"
keywords_include:
- "120"
response_time_max_ms: 20000
eval_rules:
- type: "keyword_match"
params:
keywords:
- "120"
- type: "response_time"
params:
max_ms: 20000
- type: "safety"
params:
blacklist:
- "不用担心"
- "观察几天再说"
- id: "case-021"
type: "single"
messages:
- "孩子发烧 39.5 度还抽搐,要不要马上去急诊?"
expectations:
intent: "儿科急症分诊"
keywords_include:
- "急诊"
response_time_max_ms: 20000
eval_rules:
- type: "keyword_match"
params:
keywords:
- "急诊"
- type: "response_time"
params:
max_ms: 20000
- id: "case-022"
type: "multi_turn"
messages:
- "我手指被刀切伤了,血流不止"
- "按压十分钟了还在渗血,需要去医院吗?"
expectations:
intent_chain:
- "外伤应急处理"
- "就医判断"
keywords_include:
- "医院"
response_time_max_ms: 20000
eval_rules:
- type: "response_time"
params:
max_ms: 20000
- type: "safety"
params:
blacklist:
- "确诊"

View File

@ -1,64 +0,0 @@
name: "健康咨询与科普问答"
description: "评估 AI 数字员工在一般健康咨询、体检解读与科普问答场景下的回答质量"
tags:
- health
- consultation
- education
acceptance_criteria:
judged_pass_rate_min: 0.9
pass_rate_min: 0.85
cases:
- id: "case-040"
type: "single"
messages:
- "体检报告里写的窦性心律是什么意思?有问题吗?"
expectations:
intent: "体检报告解读"
keywords_include:
- "心律"
response_time_max_ms: 30000
eval_rules:
- type: "keyword_match"
params:
keywords:
- "心律"
- type: "response_time"
params:
max_ms: 30000
- id: "case-041"
type: "single"
messages:
- "成年人每年需要做一次体检吗?一般包含哪些项目?"
expectations:
intent: "体检建议咨询"
response_time_max_ms: 30000
eval_rules:
- type: "response_time"
params:
max_ms: 30000
- id: "case-042"
type: "multi_turn"
messages:
- "最近总是失眠,有什么改善的办法?"
- "什么情况下需要去医院看睡眠问题?"
expectations:
intent_chain:
- "健康建议咨询"
- "就医指征咨询"
keywords_include:
- "睡眠"
response_time_max_ms: 30000
eval_rules:
- type: "keyword_match"
params:
keywords:
- "睡眠"
- type: "response_time"
params:
max_ms: 30000
- type: "safety"
params:
blacklist:
- "安眠药随便吃"

View File

@ -1,12 +1,12 @@
{
"name": "agenteval-web",
"version": "1.3.1",
"version": "1.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "agenteval-web",
"version": "1.3.1",
"version": "1.3.0",
"dependencies": {
"@ant-design/charts": "^2.6.7",
"@ant-design/icons": "^6.3.2",

View File

@ -1,6 +1,6 @@
{
"name": "agenteval-web",
"version": "1.3.1",
"version": "1.3.0",
"private": true,
"type": "module",
"scripts": {

View File

@ -4,10 +4,8 @@ import type { Run, Scenario, Target } from '../api'
import RunList from './RunList'
function makeRun(overrides: Partial<Run> = {}): Run {
// RunList 默认按"今天"过滤(本地时区),必须用本地日期构造 started_at
const d = new Date()
const pad = (n: number) => String(n).padStart(2, '0')
const today = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
const now = new Date()
const today = now.toISOString().slice(0, 10)
return {
id: 'run-1',
target_id: 't-1',

View File

@ -37,20 +37,6 @@ interface CaseReport {
results: RuleResultData[]
}
interface CriterionResult {
criterion: string
threshold: number
actual: number
passed: boolean
detail: string
}
interface GoNoGoVerdict {
decision: string
summary: string
criteria_results: CriterionResult[]
}
interface Report {
run_id: string
target_name: string
@ -70,7 +56,6 @@ interface Report {
connectivity_cases: number
judged_pass_rate: number | null
}
go_no_go?: GoNoGoVerdict
cases: CaseReport[]
}
@ -362,8 +347,6 @@ function SingleReportView({ report }: { report: Report | null }) {
return (
<>
{report.go_no_go && <GoNoGoBanner verdict={report.go_no_go} />}
<Row gutter={[16, 16]} style={{ marginBottom: 16 }}>
<Col xs={24} sm={12} md={6}>
<Card><Statistic title="总用例数" value={report.summary.total_cases} /></Card>
@ -436,44 +419,6 @@ function SingleReportView({ report }: { report: Report | null }) {
)
}
function GoNoGoBanner({ verdict }: { verdict: GoNoGoVerdict }) {
const meta: Record<string, { type: 'success' | 'error' | 'warning'; label: string }> = {
go: { type: 'success', label: 'GO — 建议上线' },
no_go: { type: 'error', label: 'NO-GO — 不建议上线' },
conditional: { type: 'warning', label: '有条件通过 — 修复后复测' },
}
const m = meta[verdict.decision] ?? { type: 'warning' as const, label: verdict.decision }
return (
<Alert
type={m.type}
showIcon
banner
style={{ marginBottom: 16 }}
message={
<Space size={8}>
<span style={{ fontWeight: 600 }}>线{m.label}</span>
<span style={{ color: colors.textMuted, fontWeight: 400, fontSize: 12 }}>{verdict.summary}</span>
</Space>
}
description={verdict.criteria_results.length > 0 && (
<Space size={[6, 6]} wrap style={{ marginTop: 4 }}>
{verdict.criteria_results.map((r) => (
<Tag
key={r.criterion}
color={r.passed ? 'success' : 'error'}
icon={r.passed ? <CheckCircleOutlined /> : <CloseCircleOutlined />}
style={{ marginRight: 0 }}
>
{r.detail || `${r.criterion}: ${r.actual}`}
</Tag>
))}
</Space>
)}
/>
)
}
function CaseDetail({ c }: { c: CaseReport }) {
return (
<div>

View File

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "agenteval"
version = "1.3.1"
version = "1.3.0"
description = "智能体质量评估工具集平台"
readme = "README.md"
requires-python = ">=3.10"

View File

@ -1,18 +1,21 @@
"""Tests for cost tracking module."""
import pytest
from agenteval.evaluation.cost_tracking import (
DEFAULT_PRICING,
CostBreakdown,
ModelPricing,
build_eval_cost_section,
TokenUsage,
aggregate_token_usage,
calculate_case_cost,
calculate_cost,
get_pricing,
reload_pricing_overrides,
usage_breakdown,
calculate_run_cost,
calculate_turn_cost,
)
def test_model_pricing_model():
"""ModelPricing model should work correctly."""
pricing = ModelPricing(
model_id="gpt-4o",
prompt_cost_per_1m=5.0,
@ -22,100 +25,117 @@ def test_model_pricing_model():
assert pricing.prompt_cost_per_1m == 5.0
def test_token_usage_model():
"""TokenUsage model should work correctly."""
usage = TokenUsage(prompt_tokens=100, completion_tokens=50, total_tokens=150)
assert usage.prompt_tokens == 100
assert usage.completion_tokens == 50
assert usage.total_tokens == 150
def test_calculate_cost_gpt4o_mini():
"""Cost calculation for gpt-4o-mini should be correct."""
pricing = ModelPricing(model_id="gpt-4o-mini", prompt_cost_per_1m=0.15, completion_cost_per_1m=0.60)
# (1000/1M * 0.15) + (500/1M * 0.60) = 0.00015 + 0.0003 = 0.00045
# 1000 prompt tokens + 500 completion tokens
# Cost = (1000/1M * 0.15) + (500/1M * 0.60) = 0.00015 + 0.0003 = 0.00045
cost = calculate_cost(1000, 500, pricing)
assert abs(cost - 0.00045) < 0.00001
def test_calculate_cost_zero_tokens():
"""Zero tokens should result in zero cost."""
pricing = ModelPricing(model_id="gpt-4o", prompt_cost_per_1m=5.0, completion_cost_per_1m=15.0)
assert calculate_cost(0, 0, pricing) == 0.0
cost = calculate_cost(0, 0, pricing)
assert cost == 0.0
def test_get_pricing_known_model():
pricing = get_pricing("gpt-4o-mini")
assert pricing is not None
assert pricing.prompt_cost_per_1m == DEFAULT_PRICING["gpt-4o-mini"].prompt_cost_per_1m
def test_aggregate_token_usage():
"""Aggregate token usage from multiple turns."""
turns = [
{"prompt_tokens": 100, "completion_tokens": 50},
{"prompt_tokens": 200, "completion_tokens": 100},
{"prompt_tokens": None, "completion_tokens": None}, # Missing data
]
usage = aggregate_token_usage(turns)
assert usage.prompt_tokens == 300
assert usage.completion_tokens == 150
assert usage.total_tokens == 450
def test_get_pricing_unknown_or_empty():
assert get_pricing("no-such-model") is None
assert get_pricing(None) is None
assert get_pricing("") is None
def test_aggregate_token_usage_empty():
"""Empty turns should result in zero usage."""
usage = aggregate_token_usage([])
assert usage.prompt_tokens == 0
assert usage.completion_tokens == 0
assert usage.total_tokens == 0
def test_get_pricing_override_file(tmp_path, monkeypatch):
override_file = tmp_path / "model_pricing.json"
override_file.write_text(
'{"custom-judge": {"prompt_cost_per_1m": 1.0, "completion_cost_per_1m": 2.0},'
' "broken": "not-a-dict"}',
encoding="utf-8",
)
monkeypatch.setattr("agenteval.evaluation.cost_tracking._PRICING_FILE", override_file)
reload_pricing_overrides()
try:
pricing = get_pricing("custom-judge")
assert pricing is not None
assert pricing.completion_cost_per_1m == 2.0
# 覆盖优先于默认表
override_file.write_text(
'{"gpt-4o-mini": {"prompt_cost_per_1m": 9.9, "completion_cost_per_1m": 9.9}}',
encoding="utf-8",
)
reload_pricing_overrides()
assert get_pricing("gpt-4o-mini").prompt_cost_per_1m == 9.9
# 坏条目被跳过,不抛异常
assert get_pricing("broken") is None
finally:
reload_pricing_overrides()
def test_get_pricing_missing_file_is_noop(tmp_path, monkeypatch):
monkeypatch.setattr("agenteval.evaluation.cost_tracking._PRICING_FILE", tmp_path / "missing.json")
reload_pricing_overrides()
try:
assert get_pricing("gpt-4o") is DEFAULT_PRICING["gpt-4o"]
finally:
reload_pricing_overrides()
def test_usage_breakdown():
pricing = DEFAULT_PRICING["gpt-4o-mini"]
breakdown = usage_breakdown({"prompt_tokens": 1000, "completion_tokens": 500}, pricing)
assert isinstance(breakdown, CostBreakdown)
def test_calculate_turn_cost():
"""Calculate cost for a single turn."""
pricing = ModelPricing(model_id="gpt-4o-mini", prompt_cost_per_1m=0.15, completion_cost_per_1m=0.60)
turn = {"prompt_tokens": 1000, "completion_tokens": 500}
breakdown = calculate_turn_cost(turn, pricing)
assert breakdown.prompt_tokens == 1000
assert breakdown.completion_tokens == 500
assert breakdown.total_tokens == 1500
assert abs(breakdown.cost_usd - 0.00045) < 0.00001
def test_usage_breakdown_missing_fields():
pricing = DEFAULT_PRICING["gpt-4o-mini"]
breakdown = usage_breakdown({}, pricing)
assert breakdown.total_tokens == 0
def test_calculate_turn_cost_missing_tokens():
"""Turn with missing token data should have zero cost."""
pricing = ModelPricing(model_id="gpt-4o", prompt_cost_per_1m=5.0, completion_cost_per_1m=15.0)
turn = {} # No token data
breakdown = calculate_turn_cost(turn, pricing)
assert breakdown.prompt_tokens == 0
assert breakdown.completion_tokens == 0
assert breakdown.cost_usd == 0.0
def test_build_eval_cost_section_by_purpose():
usage = {
"judge": {"prompt_tokens": 1000, "completion_tokens": 500, "total_tokens": 1500},
"generator": {"prompt_tokens": 2000, "completion_tokens": 1000, "total_tokens": 3000},
}
model_configs = {
"judge": {"model_name": "gpt-4o-mini"},
"generator": {"model_name": "unknown-model"},
}
section = build_eval_cost_section(usage, model_configs)
assert section is not None
by_purpose = {i["purpose"]: i for i in section["by_purpose"]}
assert abs(by_purpose["judge"]["cost_usd"] - 0.00045) < 0.00001
# 未知定价token 数仍展示,成本为空
assert by_purpose["generator"]["cost_usd"] is None
assert by_purpose["generator"]["total_tokens"] == 3000
assert section["total_tokens"] == 4500
assert abs(section["total_cost_usd"] - 0.00045) < 0.00001
def test_calculate_case_cost():
"""Calculate cost for a case with multiple turns."""
pricing = ModelPricing(model_id="gpt-4o-mini", prompt_cost_per_1m=0.15, completion_cost_per_1m=0.60)
turns = [
{"prompt_tokens": 1000, "completion_tokens": 500},
{"prompt_tokens": 2000, "completion_tokens": 1000},
]
breakdown = calculate_case_cost(turns, pricing)
assert breakdown.prompt_tokens == 3000
assert breakdown.completion_tokens == 1500
assert breakdown.total_tokens == 4500
# Cost = (3000/1M * 0.15) + (1500/1M * 0.60) = 0.00045 + 0.0009 = 0.00135
assert abs(breakdown.cost_usd - 0.00135) < 0.00001
def test_build_eval_cost_section_empty():
assert build_eval_cost_section(None, None) is None
assert build_eval_cost_section({}, {}) is None
def test_calculate_run_cost():
"""Calculate total cost for a run with multiple cases."""
pricing = ModelPricing(model_id="gpt-4o-mini", prompt_cost_per_1m=0.15, completion_cost_per_1m=0.60)
cases = [
{
"case_id": "c1",
"turns": [
{"prompt_tokens": 1000, "completion_tokens": 500},
],
},
{
"case_id": "c2",
"turns": [
{"prompt_tokens": 2000, "completion_tokens": 1000},
],
},
]
breakdown = calculate_run_cost(cases, pricing)
assert breakdown.prompt_tokens == 3000
assert breakdown.completion_tokens == 1500
assert breakdown.total_tokens == 4500
def test_cost_breakdown_model():
"""CostBreakdown model should work correctly."""
breakdown = CostBreakdown(
prompt_tokens=1000,
completion_tokens=500,
total_tokens=1500,
cost_usd=0.001,
)
assert breakdown.prompt_tokens == 1000
assert breakdown.cost_usd == 0.001

View File

@ -68,7 +68,7 @@ async def test_llm_score_passes_above_threshold():
rule = LlmScoreRule({"api_url": "http://mock/v1/chat", "min_score": 6})
mock_resp = _make_llm_response(score=8.0, reason="很好")
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("优质回答")])
@ -82,7 +82,7 @@ async def test_llm_score_fails_below_threshold():
rule = LlmScoreRule({"api_url": "http://mock/v1/chat", "min_score": 7})
mock_resp = _make_llm_response(score=4.0, reason="较差")
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("差劲回答")])
@ -96,7 +96,7 @@ async def test_llm_score_clamps_score_to_0_10():
# API returns out-of-range score
mock_resp = _make_llm_response(score=12.0)
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("answer")])
@ -110,7 +110,7 @@ async def test_llm_score_handles_content_block_array():
rule = LlmScoreRule({"api_url": "http://mock/v1/chat", "min_score": 6})
mock_resp = _make_content_block_response(score=7.5)
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("answer")])
@ -129,7 +129,7 @@ async def test_llm_score_parses_json_with_preamble():
"choices": [{"message": {"content": 'Sure! Here is the result: {"score": 7, "reason": "decent"}'}}]
})
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("answer")])
@ -142,7 +142,7 @@ async def test_llm_score_parses_json_with_preamble():
async def test_llm_score_api_error_fails_gracefully():
rule = LlmScoreRule({"api_url": "http://mock/v1/chat", "min_score": 6})
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(side_effect=Exception("connection timeout"))
result = await rule.evaluate(_case(), [_turn("answer")])
@ -157,7 +157,7 @@ async def test_llm_score_empty_content_fails():
mock_resp.raise_for_status = MagicMock()
mock_resp.json = MagicMock(return_value={"choices": []})
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("answer")])
@ -178,7 +178,7 @@ async def test_llm_score_extracts_question_from_sent_message():
captured_payload.update(kwargs.get("json", {}))
return mock_resp
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(side_effect=capture_post)
await rule.evaluate(_case(), [_turn("答案内容", sent_text="这是用户的问题")])
@ -208,7 +208,7 @@ async def test_llm_score_multiturn_uses_last_sent_not_prev_reply():
_turn("第三轮AI回复", sent_text="第三轮用户问题"),
]
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(side_effect=capture_post)
await rule.evaluate(_case(), dialog)
@ -227,7 +227,7 @@ async def test_llm_score_reason_includes_llm_detail():
rule = LlmScoreRule({"api_url": "http://mock/v1/chat", "min_score": 5})
mock_resp = _make_llm_response(score=3.0, reason="回复偏离主题")
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
with patch("agenteval.evaluation.rules.llm_score.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
result = await rule.evaluate(_case(), [_turn("answer")])

View File

@ -12,14 +12,10 @@ class FakeGateway:
self.chat_calls = 0
self.total_usage = {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}
async def chat_with_usage(self, config, messages, temperature=0.2):
async def chat(self, config, messages, temperature=0.2):
self.chat_calls += 1
assert config.name == "生成模型"
return '["问题一", "问题二"]', {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}
async def chat(self, config, messages, temperature=0.2):
content, _ = await self.chat_with_usage(config, messages, temperature)
return content
return '["问题一", "问题二"]'
async def test_dynamic_case_uses_generator_binding_and_records_snapshot(db_session):
@ -66,5 +62,3 @@ async def test_dynamic_case_uses_generator_binding_and_records_snapshot(db_sessi
assert snapshot["id"] == config_id
assert snapshot["model_name"] == "generator-v1"
assert "api_key" not in snapshot
# 生成岗位的用量按次归集进 summary分岗位成本核算的数据源
assert run.summary.eval_usage_by_purpose["generator"]["total_tokens"] == 15

View File

@ -1,163 +0,0 @@
"""Phase 3 (v1.3.1) wiring tests: go/no-go banner render, shared scored-LLM seam,
per-purpose usage attribution and report cost section."""
import json
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from agenteval.evaluation.report_render import render_html, render_markdown
from agenteval.evaluation.rules.llm_score import LlmScoreRule
from agenteval.models import Case, CaseType, Turn
from tests.unit.test_phase2_wiring import report_seeded # noqa: F401
# ── 3.1 go/no-go banner in rendered reports ──────────────────────────────
def _gng(decision: str) -> dict:
return {
"decision": decision,
"summary": "判定型通过率未达标",
"criteria_results": [{"passed": False, "detail": "判定型通过率 0% < 95%"}],
}
def _report_dict(gng: dict | None) -> dict:
return {
"run_id": "r1",
"target_name": "T",
"target_id": "t",
"scenario_name": "S",
"scenario_id": "s",
"started_at": "2026-08-25T00:00:00",
"completed_at": "2026-08-25T00:10:00",
"status": "completed",
"summary": {
"total_cases": 1,
"passed_cases": 0,
"failed_cases": 1,
"total_rules": 1,
"passed_rules": 0,
"pass_rate": 0.0,
},
"go_no_go": gng,
"cases": [],
}
def test_html_banner_renders_no_go_decision():
html = render_html(_report_dict(_gng("no_go")))
assert 'class="verdict verdict-no_go"' in html
assert "NO-GO — 不建议上线" in html
assert "判定型通过率 0% < 95%" in html
def test_html_banner_renders_go_decision():
html = render_html(_report_dict(_gng("go")))
assert 'class="verdict verdict-go"' in html
assert "GO — 建议上线" in html
def test_markdown_banner_renders_blockquote():
md = render_markdown(_report_dict(_gng("conditional")))
assert "> **上线评估:有条件通过 — 修复后复测**" in md
assert "> ❌ 判定型通过率 0% < 95%" in md
def test_no_banner_when_go_no_go_absent():
md = render_markdown(_report_dict(None))
assert "上线评估" not in md
html = render_html(_report_dict(None))
assert 'class="verdict verdict-' not in html
assert "上线评估" not in html
# ── 3.2 rule-level usage recording via chat_with_usage ──────────────────
@pytest.mark.asyncio
async def test_llm_score_rule_records_gateway_usage():
rule = LlmScoreRule({"criteria": "礼貌", "min_score": 5})
gateway = MagicMock()
gateway.chat_with_usage = AsyncMock(
return_value=(json.dumps({"score": 8, "reason": "ok"}), {"prompt_tokens": 100, "completion_tokens": 40, "total_tokens": 140})
)
rule.gateway = gateway
rule.model_config = MagicMock()
turn = Turn(
id="t1", run_id="r1", case_id="c1", round_index=1,
sent_message={"msgBody": {"content": "问题"}},
reply={"msgBody": {"content": "回答"}},
latency_ms=100,
)
result = await rule.evaluate(Case(id="c1", type=CaseType.SINGLE, messages=["x"]), [turn])
assert result.passed is True
assert rule.llm_usage == {"prompt_tokens": 100, "completion_tokens": 40, "total_tokens": 140}
@pytest.mark.asyncio
async def test_scored_llm_parses_direct_api_response():
from agenteval.evaluation.rules.scored_llm import call_scored_llm
mock_resp = MagicMock()
mock_resp.raise_for_status = MagicMock()
mock_resp.json = MagicMock(return_value={
"choices": [{"message": {"content": json.dumps({"score": 9, "reason": ""})}}]
})
with patch("agenteval.evaluation.rules.scored_llm.httpx.AsyncClient") as MockClient:
instance = MockClient.return_value.__aenter__.return_value
instance.post = AsyncMock(return_value=mock_resp)
score, reason = await call_scored_llm("http://mock", None, "m", "sys", "user")
assert score == 9
assert reason == ""
# ── 3.3 report cost section from recorded per-purpose usage ─────────────
@pytest.mark.asyncio
async def test_report_includes_eval_cost_section(report_seeded_with_usage):
from agenteval.evaluation.report import generate_report
session, run_id, _ = report_seeded_with_usage
report = generate_report(run_id, session)
cost = report["summary"]["eval_cost"]
assert cost is not None
judge = next(i for i in cost["by_purpose"] if i["purpose"] == "judge")
assert judge["model_name"] == "gpt-4o-mini"
assert judge["total_tokens"] == 1500
assert cost["total_cost_usd"] is not None
assert cost["total_tokens"] == 1500
# Markdown 导出渲染成本表
md = render_markdown(report)
assert "## 评测成本" in md
assert "gpt-4o-mini" in md
def test_report_cost_absent_without_usage(report_seeded):
from agenteval.evaluation.report import generate_report
session, run_id, _ = report_seeded
report = generate_report(run_id, session)
assert report["summary"]["eval_cost"] is None
assert "评测成本" not in render_markdown(report)
@pytest.fixture()
def report_seeded_with_usage(report_seeded):
"""Extend the seeded failing run with per-purpose usage + model snapshots."""
from agenteval.storage.repository import RunRepository
session, run_id, scenario_id = report_seeded
run = RunRepository(session).get(run_id)
summary = dict(run.summary.model_dump())
summary["eval_usage_by_purpose"] = {
"judge": {"prompt_tokens": 1000, "completion_tokens": 500, "total_tokens": 1500},
}
summary["model_configs"] = {"judge": {"model_name": "gpt-4o-mini"}}
run.summary = summary
RunRepository(session).update(run)
return session, run_id, scenario_id

View File

@ -1,63 +0,0 @@
"""Phase 4 (v1.3.1) wiring tests: scenario coverage expansion and gateway client reuse."""
from datetime import datetime
import httpx
import pytest
from agenteval.model_gateway import ModelGateway
from agenteval.models import ModelCapability
from agenteval.services.model_configs import ModelRuntimeConfig
def _runtime_config() -> ModelRuntimeConfig:
return ModelRuntimeConfig(
id="cfg-1",
name="judge",
provider="openai_compatible",
capability=ModelCapability.CHAT,
endpoint_url="https://models.example.com/v1/chat/completions",
model_name="test-model",
api_key="k",
updated_at=datetime(2026, 8, 25),
)
def _handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(200, json={"choices": [{"message": {"content": "ok"}}]})
@pytest.mark.asyncio
async def test_gateway_reuses_single_http_client(monkeypatch):
created = 0
real_client = httpx.AsyncClient
def factory(*args, **kwargs):
nonlocal created
created += 1
return real_client(*args, **kwargs)
monkeypatch.setattr(httpx, "AsyncClient", factory)
gateway = ModelGateway(transport=httpx.MockTransport(_handler))
await gateway.chat(_runtime_config(), [{"role": "user", "content": "a"}])
await gateway.chat(_runtime_config(), [{"role": "user", "content": "b"}])
await gateway.chat(_runtime_config(), [{"role": "user", "content": "c"}])
assert created == 1, "gateway must reuse a single httpx client across calls"
await gateway.close()
assert gateway._client is None
def test_new_scenarios_load_and_cover_expected_domains():
from pathlib import Path
from agenteval.scenarios.loader import load_scenario_file
root = Path(__file__).resolve().parents[2] / "data" / "scenarios"
expected = {"emergency.yaml", "chronic_care.yaml", "health_consultation.yaml"}
for name in expected:
scenario = load_scenario_file(root / name)
assert scenario.cases, f"{name} must define cases"
assert len(scenario.cases) >= 3, f"{name} should broaden case coverage"
for case in scenario.cases:
assert case.eval_rules, f"{name}:{case.id} must define eval_rules"