-
Notifications
You must be signed in to change notification settings - Fork 86
feat(project): add project add evaluator code-based
#2144
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
jariy17
wants to merge
7
commits into
refactor
Choose a base branch
from
feat/project-add-evaluator-code-based
base: refactor
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b130b9
feat(project): add `project add evaluator code-based`
bb281d8
fix(project): guard against app/<name> collisions when scaffolding ev…
8acc19f
fix(project): validate --metric class and require a Bedrock --model f…
a9e41a9
fix(project): echo inferred mode caveats for code-based evaluators
6763a26
refactor(project): share toPythonPackageName via fsUtils; DEFAULT_TIM…
6f3034a
refactor(project): move code-based evaluator template knowledge into …
ef9f5ca
fix(project): drop stale 'pending CDK/L3 support' note for managed ev…
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
15 changes: 15 additions & 0 deletions
15
src/assets/evaluators/autoevals-lambda/execution-role-policy.json
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,15 @@ | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"], | ||
| "Resource": "arn:*:logs:*:*:log-group:/aws/lambda/*" | ||
| }, | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": ["bedrock:InvokeModel"], | ||
| "Resource": "*" | ||
| } | ||
| ] | ||
| } | ||
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,37 @@ | ||
| {{#if ModelProviderBedrock}} | ||
| import os | ||
|
|
||
| # litellm's Bedrock provider reads AWS_REGION_NAME; Lambda only sets AWS_REGION/AWS_DEFAULT_REGION. | ||
| os.environ.setdefault("AWS_REGION_NAME", os.environ.get("AWS_REGION", "us-west-2")) | ||
|
|
||
| from autoevals import {{ EvaluatorClass }}, init | ||
| from autoevals.litellm import LiteLLMClient | ||
|
|
||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators import ( | ||
| EvaluatorInput, | ||
| EvaluatorOutput, | ||
| custom_code_based_evaluator, | ||
| ) | ||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators.third_party.autoevals import AutoEvalsAdapter | ||
|
|
||
| client = LiteLLMClient() | ||
| init(client=client, default_model="bedrock/{{ Model }}") | ||
|
|
||
| adapter = AutoEvalsAdapter(metric={{ EvaluatorClass }}(client=client, model="bedrock/{{ Model }}"){{#if EvaluatorParams}}, {{{ EvaluatorParams }}}{{/if}}) | ||
| {{else}} | ||
| from autoevals import {{ EvaluatorClass }} | ||
|
|
||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators import ( | ||
| EvaluatorInput, | ||
| EvaluatorOutput, | ||
| custom_code_based_evaluator, | ||
| ) | ||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators.third_party.autoevals import AutoEvalsAdapter | ||
|
|
||
| adapter = AutoEvalsAdapter(metric={{ EvaluatorClass }}({{#if Model}}model="{{ Model }}"{{/if}}){{#if EvaluatorParams}}, {{{ EvaluatorParams }}}{{/if}}) | ||
| {{/if}} | ||
|
|
||
|
|
||
| @custom_code_based_evaluator() | ||
| def handler(evaluator_input: EvaluatorInput, context) -> EvaluatorOutput: | ||
| return adapter(evaluator_input, context) |
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,22 @@ | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [project] | ||
| name = "{{ Name }}" | ||
| version = "0.1.0" | ||
| description = "AgentCore Code-Based Evaluator (Autoevals)" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "bedrock-agentcore[autoevals]", | ||
| "autoevals>=0.0.80,<1.0.0", | ||
| {{#if ModelProviderBedrock}} | ||
| # autoevals grades via LiteLLMClient -> Bedrock (Converse); litellm replaces the openai judge | ||
| "litellm>=1.60,<1.85", | ||
| {{else}} | ||
| "openai>=1.0.0", | ||
| {{/if}} | ||
| ] | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["."] |
15 changes: 15 additions & 0 deletions
15
src/assets/evaluators/deepeval-lambda/execution-role-policy.json
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,15 @@ | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"], | ||
| "Resource": "arn:*:logs:*:*:log-group:/aws/lambda/*" | ||
| }, | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": ["bedrock:InvokeModel"], | ||
| "Resource": "*" | ||
| } | ||
| ] | ||
| } |
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,29 @@ | ||
| import os | ||
|
|
||
| os.environ.setdefault("DEEPEVAL_RESULTS_FOLDER", "/tmp/.deepeval") | ||
| os.environ.setdefault("DEEPEVAL_TELEMETRY_OPT_OUT", "YES") | ||
| os.chdir("/tmp") | ||
|
|
||
| {{#if ModelProviderBedrock}} | ||
| from deepeval.models import AmazonBedrockModel | ||
| {{/if}} | ||
| from deepeval.metrics import {{ EvaluatorClass }} | ||
|
|
||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators import ( | ||
| EvaluatorInput, | ||
| EvaluatorOutput, | ||
| custom_code_based_evaluator, | ||
| ) | ||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators.third_party.deepeval import DeepEvalAdapter | ||
|
|
||
| {{#if ModelProviderBedrock}} | ||
| model = AmazonBedrockModel(model="{{ Model }}", region=os.environ.get("AWS_REGION", "us-west-2")) | ||
| adapter = DeepEvalAdapter(metric={{ EvaluatorClass }}(model=model{{#if EvaluatorParams}}, {{{ EvaluatorParams }}}{{/if}})) | ||
| {{else}} | ||
| adapter = DeepEvalAdapter(metric={{ EvaluatorClass }}({{{ EvaluatorParams }}})) | ||
| {{/if}} | ||
|
|
||
|
|
||
| @custom_code_based_evaluator() | ||
| def handler(evaluator_input: EvaluatorInput, context) -> EvaluatorOutput: | ||
| return adapter(evaluator_input, context) |
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,19 @@ | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [project] | ||
| name = "{{ Name }}" | ||
| version = "0.1.0" | ||
| description = "AgentCore Code-Based Evaluator (DeepEval)" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "bedrock-agentcore[deepeval]", | ||
| "deepeval>=2.0.0,<3.0.0", | ||
| {{#if ModelProviderBedrock}} | ||
| "aiobotocore>=2.13.0", | ||
| {{/if}} | ||
| ] | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["."] |
10 changes: 10 additions & 0 deletions
10
src/assets/evaluators/python-lambda/execution-role-policy.json
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,10 @@ | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"], | ||
| "Resource": "arn:*:logs:*:*:log-group:/aws/lambda/*" | ||
| } | ||
| ] | ||
| } |
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,19 @@ | ||
| from bedrock_agentcore.evaluation.custom_code_based_evaluators import ( | ||
| custom_code_based_evaluator, | ||
| EvaluatorInput, | ||
| EvaluatorOutput, | ||
| ) | ||
|
|
||
|
|
||
| @custom_code_based_evaluator() | ||
| def handler(input: EvaluatorInput, context) -> EvaluatorOutput: | ||
| """Evaluate agent behavior with custom logic. | ||
|
|
||
| Args: | ||
| input: Contains evaluation_level, session_spans, target_trace_id, target_span_id | ||
|
|
||
| Returns: | ||
| EvaluatorOutput with value/label for success, or errorCode/errorMessage for failure. | ||
| """ | ||
| # TODO: Replace with your evaluation logic | ||
| return EvaluatorOutput(value=1.0, label="Pass", explanation="Evaluation passed") |
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,15 @@ | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [project] | ||
| name = "{{ Name }}" | ||
| version = "0.1.0" | ||
| description = "AgentCore Code-Based Evaluator" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "bedrock-agentcore>=1.6.0", | ||
| ] | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["."] |
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
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,89 @@ | ||
| import { FsTreeNode } from "./fsTree"; | ||
| import type { AssetSource } from "../source"; | ||
| import type { Evaluator, EvaluationLevel } from "../../../projectSchemas/evaluator"; | ||
| import type { TemplateRenderer, TemplateResolver } from "./types"; | ||
| import { toPythonPackageName } from "../fsUtils"; | ||
|
|
||
| const DEFAULT_TIMEOUT = 60; | ||
|
|
||
| export const EVALUATOR_LIBRARIES = { | ||
| deepeval: { assetDir: "evaluators/deepeval-lambda", defaultTimeoutSeconds: 300 }, | ||
| autoevals: { assetDir: "evaluators/autoevals-lambda", defaultTimeoutSeconds: DEFAULT_TIMEOUT }, | ||
| } as const; | ||
|
|
||
| export type EvaluatorLibrary = keyof typeof EVALUATOR_LIBRARIES; | ||
|
|
||
| const EMPTY_ASSET_DIR = "evaluators/python-lambda"; | ||
|
|
||
| export type ManagedEvaluatorScaffoldInput = { | ||
| name: string; | ||
| level: EvaluationLevel; | ||
| description?: string; | ||
| kmsKeyArn?: string; | ||
| tags?: Record<string, string>; | ||
| metric?: { library: EvaluatorLibrary; metricClass: string }; | ||
| model?: string; | ||
| timeoutSeconds?: number; | ||
| }; | ||
|
|
||
| function buildManagedEvaluatorSpec(input: ManagedEvaluatorScaffoldInput): Evaluator { | ||
| const timeoutSeconds = | ||
| input.timeoutSeconds ?? | ||
| (input.metric | ||
| ? EVALUATOR_LIBRARIES[input.metric.library].defaultTimeoutSeconds | ||
| : DEFAULT_TIMEOUT); | ||
| return { | ||
| name: input.name, | ||
| level: input.level, | ||
| ...(input.description && { description: input.description }), | ||
| config: { | ||
| codeBased: { | ||
| managed: { | ||
| codeLocation: `app/${input.name}`, | ||
| entrypoint: "lambda_function.handler", | ||
| timeoutSeconds, | ||
| additionalPolicies: ["execution-role-policy.json"], | ||
| }, | ||
| }, | ||
| }, | ||
| ...(input.kmsKeyArn && { kmsKeyArn: input.kmsKeyArn }), | ||
| ...(input.tags && { tags: input.tags }), | ||
| }; | ||
| } | ||
|
|
||
| function buildRenderContext(input: ManagedEvaluatorScaffoldInput): Record<string, unknown> { | ||
| const context: Record<string, unknown> = { Name: toPythonPackageName(input.name) }; | ||
| if (input.metric) { | ||
| context["EvaluatorClass"] = input.metric.metricClass; | ||
| context["Model"] = input.model ?? ""; | ||
| context["ModelProviderBedrock"] = input.model !== undefined; | ||
| context["EvaluatorParams"] = ""; | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| type GetEvaluatorTemplateResolverConfig = { | ||
| assetSource: AssetSource; | ||
| templateRenderer: TemplateRenderer; | ||
| }; | ||
|
|
||
| export function getEvaluatorTemplateResolver( | ||
| config: GetEvaluatorTemplateResolverConfig, | ||
| ): TemplateResolver<ManagedEvaluatorScaffoldInput> { | ||
| return { | ||
| async resolve(input) { | ||
| const assetDir = input.metric | ||
| ? EVALUATOR_LIBRARIES[input.metric.library].assetDir | ||
| : EMPTY_ASSET_DIR; | ||
| const tree = await FsTreeNode.fromAssetSource( | ||
| { assetSource: config.assetSource }, | ||
| { assetDir }, | ||
| { | ||
| rootDirName: input.name, | ||
| transformContent: (raw) => config.templateRenderer.render(raw, buildRenderContext(input)), | ||
| }, | ||
| ); | ||
| return { tree, spec: { evaluators: [buildManagedEvaluatorSpec(input)] } }; | ||
| }, | ||
| }; | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I didn't want add an abstraction to generate common assets like this one due time constraints. We can look for this in the future.