generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add OTel test semantic convention attributes to Experiment spans #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
anirudha
wants to merge
1
commit into
strands-agents:main
Choose a base branch
from
anirudha:feat/otel-test-semantic-conventions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import json | ||
| import logging | ||
| import os | ||
| import uuid | ||
| from collections.abc import Callable | ||
| from pathlib import Path | ||
| from typing import cast | ||
|
|
@@ -99,13 +100,23 @@ def __init__( | |
| self, | ||
| cases: list[Case[InputT, OutputT]] | None = None, | ||
| evaluators: list[Evaluator[InputT, OutputT]] | None = None, | ||
| name: str = "unnamed_experiment", | ||
| ): | ||
| self._name = name | ||
| self._cases = cases or [] | ||
| self._evaluators = evaluators or [Evaluator()] | ||
| self._tracer = get_tracer() | ||
| # self._logger = get_logger(__name__) | ||
|
|
||
| self._config_id = os.environ.get("EVALUATION_RESULTS_LOG_GROUP", "default-strands-evals") | ||
| @property | ||
| def name(self) -> str: | ||
| """Get the experiment name. | ||
|
|
||
| Returns: | ||
| The name of the experiment. | ||
| """ | ||
| return self._name | ||
|
|
||
| @property | ||
| def cases(self) -> list[Case[InputT, OutputT]]: | ||
|
|
@@ -260,14 +271,15 @@ async def _run_task_async( | |
|
|
||
| return evaluation_context | ||
|
|
||
| async def _worker(self, queue: asyncio.Queue, task: Callable, results: list): | ||
| async def _worker(self, queue: asyncio.Queue, task: Callable, results: list, run_id: str): | ||
| """ | ||
| Worker that processes cases from the queue. Run evaluation on the task. | ||
|
|
||
| Args: | ||
| queue: Queue containing cases to process | ||
| task: Task function to run on each case | ||
| results: List to store results | ||
| run_id: Unique identifier for this evaluation run | ||
| """ | ||
| while True: | ||
| try: | ||
|
|
@@ -305,6 +317,10 @@ async def _run_task_with_retry(task=task, case=case): | |
| "gen_ai.evaluation.data.has_interactions": ( | ||
| evaluation_context.actual_interactions is not None | ||
| ), | ||
| "test.suite.name": self._name, | ||
| "test.suite.run.id": run_id, | ||
| "test.case.name": case_name, | ||
| "test.case.id": case.session_id, | ||
| } | ||
| ) | ||
| trace_id = format_trace_id(case_span.get_span_context().trace_id) | ||
|
|
@@ -356,6 +372,7 @@ async def _evaluate_with_retry(evaluator=evaluator, evaluation_context=evaluatio | |
| "gen_ai.evaluation.score.value": str(aggregate_score), | ||
| "gen_ai.evaluation.test_pass": aggregate_pass, | ||
| "gen_ai.evaluation.explanation": aggregate_reason or "", | ||
| "test.case.result.status": "pass" if aggregate_pass else "fail", | ||
| } | ||
| ) | ||
|
|
||
|
|
@@ -483,6 +500,8 @@ def run_evaluations( | |
| A list of EvaluationReport objects, one for each evaluator, containing the overall score, | ||
| individual case results, and basic feedback for each test case. | ||
| """ | ||
| run_id = str(uuid.uuid4()) | ||
|
|
||
| evaluator_data: dict[str, dict[str, list]] = { | ||
| evaluator.get_type_name(): { | ||
| "scores": [], | ||
|
|
@@ -502,6 +521,10 @@ def run_evaluations( | |
| attributes={ | ||
| "gen_ai.evaluation.case.name": case_name, | ||
| "gen_ai.evaluation.case.input": serialize(case.input), | ||
| "test.suite.name": self._name, | ||
| "test.suite.run.id": run_id, | ||
| "test.case.name": case_name, | ||
| "test.case.id": case.session_id, | ||
| }, | ||
| ) as case_span: | ||
| # Task execution with retry logic | ||
|
|
@@ -605,6 +628,7 @@ def _evaluate_with_retry(evaluator=evaluator, evaluation_context=evaluation_cont | |
| "gen_ai.evaluation.score.value": aggregate_score, | ||
| "gen_ai.evaluation.test_pass": aggregate_pass, | ||
| "gen_ai.evaluation.explanation": aggregate_reason or "", | ||
| "test.case.result.status": "pass" if aggregate_pass else "fail", | ||
| } | ||
| ) | ||
|
|
||
|
|
@@ -679,13 +703,14 @@ async def run_evaluations_async(self, task: Callable, max_workers: int = 10) -> | |
| """ | ||
| queue: asyncio.Queue[Case[InputT, OutputT]] = asyncio.Queue() | ||
| results: list[Any] = [] | ||
| run_id = str(uuid.uuid4()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
|
||
| for case in self._cases: | ||
| queue.put_nowait(case) | ||
|
|
||
| num_workers = min(max_workers, len(self._cases)) | ||
|
|
||
| workers = [asyncio.create_task(self._worker(queue, task, results)) for _ in range(num_workers)] | ||
| workers = [asyncio.create_task(self._worker(queue, task, results, run_id)) for _ in range(num_workers)] | ||
|
|
||
| await queue.join() | ||
| for worker in workers: | ||
|
|
@@ -739,6 +764,7 @@ def to_dict(self) -> dict: | |
| A dictionary representation of the experiment. | ||
| """ | ||
| return { | ||
| "name": self._name, | ||
| "cases": [case.model_dump() for case in self._cases], | ||
| "evaluators": [evaluator.to_dict() for evaluator in self._evaluators], | ||
| } | ||
|
|
@@ -787,6 +813,7 @@ def from_dict(cls, data: dict, custom_evaluators: list[type[Evaluator]] | None = | |
| Return: | ||
| An Experiment object. | ||
| """ | ||
| name = data.get("name", "unnamed_experiment") | ||
| custom_evaluators = custom_evaluators or [] | ||
| cases: list[Case] = [Case.model_validate(case_data) for case_data in data["cases"]] | ||
| default_evaluators: dict[str, type[Evaluator]] = { | ||
|
|
@@ -817,7 +844,7 @@ def from_dict(cls, data: dict, custom_evaluators: list[type[Evaluator]] | None = | |
| f"all relevant custom evaluators are passed in." | ||
| ) | ||
|
|
||
| return cls(cases=cases, evaluators=evaluators) | ||
| return cls(cases=cases, evaluators=evaluators, name=name) | ||
|
|
||
| @classmethod | ||
| def from_file(cls, path: str, custom_evaluators: list[type[Evaluator]] | None = None): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the same as session.id?