-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconftest.py
More file actions
135 lines (108 loc) · 4.32 KB
/
conftest.py
File metadata and controls
135 lines (108 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Fixtures for CrewAI Websearch agent evals."""
from __future__ import annotations
import asyncio
import logging
import os
import time
import warnings
from pathlib import Path
from typing import Any, AsyncGenerator, Callable, Coroutine
import httpx
import pytest
import yaml
from harness.runner import TaskConfig, TaskResult, run_task
try:
from harness.mlflow_client import MLflowTraceClient
except ImportError:
MLflowTraceClient = None # type: ignore[misc,assignment]
@pytest.fixture
def agent_url() -> str:
"""CrewAI Websearch agent URL from env var or default localhost:8000."""
return os.environ.get("CREWAI_WEBSEARCH_AGENT_URL", "http://localhost:8000")
@pytest.fixture
async def http_client() -> AsyncGenerator[httpx.AsyncClient, None]:
"""Provide an async httpx client that is closed after the test."""
async with httpx.AsyncClient() as client:
yield client
def _find_repo_root() -> Path:
"""Walk up from this file to find the repository root."""
path = Path(__file__).resolve().parent
while path.parent != path:
if (path / "tests" / "behavioral" / "configs" / "thresholds.yaml").is_file():
return path
path = path.parent
raise FileNotFoundError(
"Could not find repo root (no tests/behavioral/configs/thresholds.yaml)"
)
@pytest.fixture
def eval_config() -> dict[str, Any]:
"""Load threshold configuration from the shared configs directory."""
config_path = (
_find_repo_root() / "tests" / "behavioral" / "configs" / "thresholds.yaml"
)
with open(config_path, encoding="utf-8") as f:
return yaml.safe_load(f)
SEARCH_EVIDENCE = ["openshift ai"]
def load_golden(category: str | None = None) -> list[dict[str, Any]]:
"""Load golden queries from the fixtures directory, optionally filtering by category."""
path = Path(__file__).parent / "fixtures" / "golden_queries.yaml"
with open(path, encoding="utf-8") as f:
data = yaml.safe_load(f)
queries = data.get("queries", [])
if category:
queries = [q for q in queries if q.get("category") == category]
return queries
@pytest.fixture
def known_tools() -> list[str]:
"""Tools available on the CrewAI Websearch agent."""
return ["web_search"]
@pytest.fixture
def crewai_websearch_thresholds(eval_config: dict[str, Any]) -> dict[str, Any]:
"""Load the crewai_websearch section from the shared thresholds config."""
return eval_config["crewai_websearch"]
@pytest.fixture
def run_eval(
agent_url: str, http_client: httpx.AsyncClient
) -> Callable[..., Coroutine[Any, Any, TaskResult]]:
"""Run eval with automatic MLflow enrichment when available.
MLflow trace enrichment is the primary mechanism for extracting
tool_calls — CrewAI does not expose them in the HTTP response body.
The MLflowTraceClient pulls SpanType.TOOL spans from traces into
TaskResult.tool_calls, enabling full scorer coverage.
"""
mlflow = None
if MLflowTraceClient is not None:
tracking_uri = os.environ.get("MLFLOW_TRACKING_URI")
experiment = os.environ.get("MLFLOW_EXPERIMENT_NAME")
if tracking_uri and experiment:
mlflow = MLflowTraceClient(tracking_uri, experiment)
async def _run(
query: str,
expected_tools: list[str] | None = None,
timeout_seconds: float = 30.0,
max_tokens_budget: int | None = None,
model: str | None = None,
stream: bool = False,
) -> TaskResult:
config = TaskConfig(
agent_url=agent_url,
query=query,
expected_tools=expected_tools,
timeout_seconds=timeout_seconds,
max_tokens_budget=max_tokens_budget,
model=model,
stream=stream,
)
request_start_ms = int(time.time() * 1000)
result = await run_task(config, client=http_client)
if mlflow is not None and result.success:
try:
await asyncio.to_thread(
mlflow.enrich_eval_result, result, since_ms=request_start_ms
)
except Exception:
msg = "MLflow enrichment failed — tool scoring will degrade to content heuristics"
logging.getLogger(__name__).warning(msg, exc_info=True)
warnings.warn(msg, stacklevel=2)
return result
return _run