-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_agentcore_agent.py
More file actions
75 lines (55 loc) · 2.52 KB
/
Copy pathtest_agentcore_agent.py
File metadata and controls
75 lines (55 loc) · 2.52 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
"""test_agentcore_agent.py — pytest analog of ``test_pydantic_agent.py``
for the AgentCore × Strands integration.
Run with::
deepeval test run test_agentcore_agent.py
Same shape as ``test_pydantic_agent.py``: pull a dataset by alias,
instrument the agent at import time, wrap the agent invocation in
``next_agent_span(metrics=[...])`` for a span-level metric, and pass
the trace-level metric to ``assert_test``. The deepeval pytest plugin
wraps each test in an eval session so the agent's OTel spans route
through REST (``ContextAwareSpanProcessor`` flips routing because
``trace_manager.is_evaluating`` is True under ``deepeval test run``).
Requirements:
- ``CONFIDENT_API_KEY`` in env (or ``deepeval login``)
- ``OPENAI_API_KEY`` in env (the AnswerRelevancy scorer)
- AWS credentials (``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``,
optionally ``AWS_REGION``) — Strands invokes Bedrock under the hood.
- ``pip install bedrock-agentcore strands-agents pytest``
"""
import uuid
from pathlib import Path
import pytest
# from strands import Agent
from deepeval import assert_test
from deepeval.dataset import EvaluationDataset, Golden
from deepeval.integrations.agentcore import instrument_agentcore
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.tracing.context import next_agent_span
RUN_ID = f"{Path(__file__).stem}-{uuid.uuid4().hex[:8]}"
# Wire the deepeval OTel pipeline at import time. Trace-level kwargs
# only — span-level fields belong on per-call ``with next_*_span(...)``
# blocks below.
# instrument_agentcore(
# name="agentcore-pytest-agent",
# tags=["agentcore", "pytest"],
# metadata={"run_id": RUN_ID, "script": Path(__file__).stem},
# )
# # Module-scope agent so spans share the same instrumented TracerProvider.
# agent = Agent(
# model="amazon.nova-lite-v1:0",
# system_prompt="Be concise. Reply with one short sentence.",
# )
async def run_agent(prompt: str) -> str:
"""Wrap the Strands invocation in ``next_agent_span(metrics=[...])``
so the AnswerRelevancyMetric attaches to the agent span via the
``stash_pending_metrics`` overlay (carried across OTel transport
into ``ConfidentSpanExporter``). Mirrors the ``run_agent`` in
``test_pydantic_agent.py``.
"""
return "output"
dataset = EvaluationDataset()
dataset.pull(alias="Single Turn QA")
@pytest.mark.parametrize("golden", dataset.goldens)
async def test_agentcore_agent(golden: Golden):
await run_agent(golden.input)
assert_test(golden=golden, metrics=[AnswerRelevancyMetric(threshold=0.8)])