-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add model-output chaos end-to-end example #3827
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
Open
venkatkrish543re
wants to merge
1
commit into
strands-agents:main
Choose a base branch
from
venkatkrish543re:feat/model-chaos-example
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.
+182
−8
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from strands import Agent | ||
|
|
||
| from strands_evals import Case | ||
| from strands_evals.chaos import ( | ||
| ChaosCase, | ||
| ChaosExperiment, | ||
| ChaosPlugin, | ||
| Confabulation, | ||
| EmptyResponse, | ||
| FullRefusal, | ||
| MalformedJson, | ||
| SuccessFraming, | ||
| ) | ||
| from strands_evals.eval_task_handler import TracedHandler, eval_task | ||
| from strands_evals.evaluators import GoalSuccessRateEvaluator | ||
|
|
||
| logging.basicConfig(level=logging.INFO, format="%(message)s") | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # 1. Create the ChaosPlugin | ||
| chaos_plugin = ChaosPlugin() | ||
|
|
||
| # 2. Define named effect maps. | ||
| # model_effects is keyed by "*" (applies to all models; per-model targeting is a | ||
| # future extension). At most one pre-model-call effect per case, since a | ||
| # pre-model-call effect cancels the model call and only one can take effect. | ||
| effect_maps = { | ||
| # Pre-model-call: the model call is cancelled; the refusal text becomes the turn. | ||
| "full_refusal": { | ||
| "model_effects": {"*": [FullRefusal()]}, | ||
| }, | ||
| # Pre-model-call: the model call is cancelled with a blank turn, | ||
| # simulating "the model returned nothing". | ||
| "empty_response": { | ||
| "model_effects": {"*": [EmptyResponse()]}, | ||
| }, | ||
| # Post-model-call: the real response is corrupted after the model runs. | ||
| "malformed_json": { | ||
| "model_effects": {"*": [MalformedJson()]}, | ||
| }, | ||
| # Post-model-call, composed: fabricated citations wrapped in confident framing | ||
| # (SuccessFraming is always applied last). | ||
| "confabulation_framed": { | ||
| "model_effects": {"*": [Confabulation(), SuccessFraming()]}, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| # 3. Define the task function | ||
| @eval_task(TracedHandler()) | ||
| def travel_agent_task(case: ChaosCase): | ||
| """Run the travel assistant with a single user query.""" | ||
| logger.info(f"\n{'─'*60}") | ||
| logger.info(f" Case: {case.name}") | ||
| logger.info(f" User: {case.input}") | ||
| logger.info(f"{'─'*60}") | ||
| return Agent( | ||
| system_prompt=( | ||
| "You are a travel planning assistant. Answer the user's question " | ||
| "directly and honestly. Today's date is May 18, 2025." | ||
| ), | ||
| plugins=[chaos_plugin], | ||
| callback_handler=None, | ||
| trace_attributes={"gen_ai.conversation.id": case.session_id, "session.id": case.session_id}, | ||
| ) | ||
|
|
||
|
|
||
| # 4. Define test cases and expand with effect maps | ||
| test_cases = [ | ||
| Case( | ||
| name="trip_planning", | ||
| input="What is the best way to travel from SFO to JFK on May 20? Summarize your recommendation.", | ||
| expected_assertion="The agent should provide a travel recommendation for getting from SFO to JFK and summarize it.", | ||
| ), | ||
| ] | ||
|
|
||
| # Expand: 1 case x (4 effect maps + 1 baseline) = 5 ChaosCase objects | ||
| chaos_cases = ChaosCase.expand(test_cases, effect_maps, include_no_effect_baseline=True) | ||
|
|
||
| # 5. Create and run the ChaosExperiment | ||
| experiment = ChaosExperiment( | ||
| cases=chaos_cases, | ||
| evaluators=[GoalSuccessRateEvaluator()], | ||
| ) | ||
|
|
||
|
|
||
| async def main(): | ||
| report = await experiment.run_evaluations_async(task=travel_agent_task, max_workers=1) | ||
| report.run_display() | ||
|
|
||
|
|
||
| asyncio.run(main()) |
File renamed without changes.
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
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.
Uh oh!
There was an error while loading. Please reload this page.