# /// script # requires-python = ">=3.10" # dependencies = [ # "arize-phoenix-client>=3.7,<4", # "arize-phoenix-evals>=3.10,<4", # "openai>=2", # ] # /// """Score the hello-agent project's recent answers with an LLM judge. Fetches the last hour's agent spans from Phoenix, with the tool results from the same trace, and asks a GPT model behind exe.dev's LLM gateway whether each answer sticks to what the tools returned (Phoenix's built-in faithfulness evaluator). The verdicts go back to Phoenix as "faithfulness" annotations, which show up on each trace in the UI. Reads PHOENIX_COLLECTOR_ENDPOINT, PHOENIX_API_KEY, and optionally PHOENIX_CLIENT_HEADERS from the environment. """ import os from datetime import datetime, timedelta, timezone from phoenix.client import Client from phoenix.evals import LLM from phoenix.evals.metrics import FaithfulnessEvaluator PROJECT = os.environ.get("PROJECT", "hello-agent") judge = LLM( provider="openai", model=os.environ.get("JUDGE_MODEL", "gpt-5.4-mini"), base_url=os.environ.get("LLM_GATEWAY", "https://llm.int.exe.xyz/v1"), api_key="exe-llm-gateway", ) faithfulness = FaithfulnessEvaluator(llm=judge) phoenix = Client() spans = phoenix.spans.get_spans( project_identifier=PROJECT, span_kind="AGENT", start_time=datetime.now(timezone.utc) - timedelta(hours=1), ) for span in spans: trace_id = span["context"]["trace_id"] tools = phoenix.spans.get_spans( project_identifier=PROJECT, trace_ids=[trace_id], span_kind="TOOL" ) score = faithfulness.evaluate({ "input": span["attributes"].get("input.value", ""), "output": span["attributes"].get("output.value", ""), "context": "\n".join(t["attributes"].get("output.value", "") for t in tools), })[0] phoenix.spans.add_span_annotation( span_id=span["context"]["span_id"], annotation_name="faithfulness", annotator_kind="LLM", label=score.label, score=score.score, explanation=score.explanation, sync=True, ) print(f"{trace_id}: {score.label} ({score.score})") print(f"Scored {len(spans)} answers.")