-
Notifications
You must be signed in to change notification settings - Fork 1k
New pattern - bedrock-agent-openai-cdk #3084
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
NithinChandranR-AWS
wants to merge
6
commits into
aws-samples:main
Choose a base branch
from
NithinChandranR-AWS:NithinChandranR-AWS-feature-bedrock-agent-openai-cdk
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7edf4c
feat(bedrock-agent-openai-cdk): Add Bedrock Agent with OpenAI GPT OSS…
NithinChandranR-AWS 043a3da
fix(readme): replace invalid invoke-agent CLI with Python SDK
NithinChandranR-AWS a3f1cd5
tagging
ellisms 0759a62
fix: address review comments on PR #3084
NithinChandranR-AWS 95a7b81
fix: replace Pillow diagram with proper AWS architecture diagram
NithinChandranR-AWS 6dac236
publishing file
ellisms 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,123 @@ | ||
| # Amazon Bedrock Agent with OpenAI model | ||
|
|
||
| This pattern deploys an Amazon Bedrock Agent powered by an OpenAI GPT OSS foundation model with an AWS Lambda action group that provides tool-use capabilities. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/bedrock-agent-openai-cdk | ||
|
|
||
| Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||
|
|
||
| ## Requirements | ||
|
|
||
| * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||
| * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
| * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
| * [Node.js 22+](https://nodejs.org/en/download/) installed | ||
| * [AWS CDK v2](https://docs.aws.amazon.com/cdk/v2/guide/getting-started.html) installed | ||
| * [Amazon Bedrock model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) enabled for OpenAI GPT OSS 20B in your target region | ||
|
|
||
| ## Architecture | ||
|
|
||
|  | ||
|
|
||
| *Figure 1: The user sends a natural language query to the Bedrock Agent. The agent uses the OpenAI GPT OSS model to reason about which tools to call, invokes the Lambda action group, and synthesizes the result.* | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. The Bedrock Agent receives a natural language query from the user. | ||
| 2. The agent uses the OpenAI GPT OSS foundation model to reason about the query and decide which tools to call. | ||
| 3. When the agent determines it needs weather or time data, it invokes the Lambda action group with the appropriate API path and parameters. | ||
| 4. The Lambda function returns structured data, which the agent incorporates into its final natural language response. | ||
|
|
||
| ## Deployment Instructions | ||
|
|
||
| 1. Clone the repository: | ||
| ```bash | ||
| git clone https://github.com/aws-samples/serverless-patterns | ||
| cd serverless-patterns/bedrock-agent-openai-cdk | ||
| ``` | ||
|
|
||
| 2. Install dependencies: | ||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| 3. Deploy the stack: | ||
| ```bash | ||
| cdk deploy | ||
| ``` | ||
|
|
||
| 4. Note the outputs printed after deployment. You will need `AgentId` and `AgentAliasId` for testing. | ||
| Example output: | ||
| ``` | ||
| Outputs: | ||
| BedrockAgentOpenaiStack.AgentId = 2VHQREVYJM | ||
| BedrockAgentOpenaiStack.AgentAliasId = WRP0JKNQFL | ||
| BedrockAgentOpenaiStack.FunctionName = BedrockAgentOpenaiStack-ActionGroupFn-AbCdEfGh | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| The Bedrock Agent `InvokeAgent` API returns a streaming response, so use the Python SDK (not the CLI). Replace `<AgentId>` and `<AgentAliasId>` with the values from the deploy output: | ||
|
|
||
| ```bash | ||
| python3 -c " | ||
| import boto3, json | ||
| client = boto3.client('bedrock-agent-runtime', region_name='us-east-1') | ||
| response = client.invoke_agent( | ||
| agentId='<AgentId>', | ||
| agentAliasId='<AgentAliasId>', | ||
| sessionId='test-session-1', | ||
| inputText='What is the weather in Tokyo?' | ||
| ) | ||
| for event in response['completion']: | ||
| if 'chunk' in event: | ||
| print(event['chunk']['bytes'].decode()) | ||
| " | ||
| ``` | ||
|
|
||
| For example, if your deploy output showed AgentId = `2VHQREVYJM` and AgentAliasId = `WRP0JKNQFL`: | ||
|
|
||
| ```bash | ||
| python3 -c " | ||
| import boto3 | ||
| client = boto3.client('bedrock-agent-runtime', region_name='us-east-1') | ||
| response = client.invoke_agent( | ||
| agentId='2VHQREVYJM', | ||
| agentAliasId='WRP0JKNQFL', | ||
| sessionId='test-session-1', | ||
| inputText='What is the weather in Tokyo?' | ||
| ) | ||
| for event in response['completion']: | ||
| if 'chunk' in event: | ||
| print(event['chunk']['bytes'].decode()) | ||
| " | ||
| ``` | ||
|
|
||
| Try a multi-tool query: | ||
|
|
||
| ```bash | ||
| python3 -c " | ||
| import boto3 | ||
| client = boto3.client('bedrock-agent-runtime', region_name='us-east-1') | ||
| response = client.invoke_agent( | ||
| agentId='<AgentId>', | ||
| agentAliasId='<AgentAliasId>', | ||
| sessionId='test-session-2', | ||
| inputText='What is the weather and current time in London?' | ||
| ) | ||
| for event in response['completion']: | ||
| if 'chunk' in event: | ||
| print(event['chunk']['bytes'].decode()) | ||
| " | ||
| ``` | ||
|
|
||
| ## Cleanup | ||
|
|
||
| ```bash | ||
| cdk destroy | ||
| ``` | ||
|
|
||
| ---- | ||
| Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-0 | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,83 @@ | ||
| { | ||
| "title": "Amazon Bedrock Agent with OpenAI model", | ||
| "description": "Deploy an Amazon Bedrock Agent powered by OpenAI GPT OSS with an AWS Lambda action group for tool use.", | ||
| "language": "TypeScript", | ||
| "level": "300", | ||
| "framework": "AWS CDK", | ||
| "services": { | ||
| "from": "bedrock", | ||
| "to": "lambda" | ||
| }, | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "This pattern deploys an Amazon Bedrock Agent that uses an OpenAI GPT OSS foundation model to orchestrate tool-use workflows. The agent reasons about user queries and invokes a Lambda action group to retrieve weather and time data.", | ||
| "OpenAI GPT OSS models are available on Amazon Bedrock with the same security, governance, and API experience as other Bedrock models. The agent uses the standard Bedrock Agent orchestration to plan and execute multi-step tool calls." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-agent-openai-cdk", | ||
| "templateURL": "serverless-patterns/bedrock-agent-openai-cdk", | ||
| "projectFolder": "bedrock-agent-openai-cdk", | ||
| "templateFile": "lib/bedrock-agent-openai-stack.ts" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { | ||
| "text": "Amazon Bedrock Agents", | ||
| "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html" | ||
| }, | ||
| { | ||
| "text": "OpenAI models on Amazon Bedrock", | ||
| "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-openai.html" | ||
| }, | ||
| { | ||
| "text": "Bedrock Agent action groups", | ||
| "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents-action-create.html" | ||
| } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": [ | ||
| "cdk deploy" | ||
| ], | ||
| "file": "lib/bedrock-agent-openai-stack.ts" | ||
| }, | ||
| "testing": { | ||
| "text": [ | ||
| "See the README for testing instructions." | ||
| ] | ||
| }, | ||
| "cleanup": { | ||
| "text": [ | ||
| "cdk destroy" | ||
| ] | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Nithin Chandran R", | ||
| "bio": "Technical Account Manager at AWS", | ||
| "linkedin": "nithin-chandran-r" | ||
| } | ||
| ], | ||
| "patternArch": { | ||
| "icon1": { | ||
| "x": 20, | ||
| "y": 50, | ||
| "service": "bedrock", | ||
| "label": "Bedrock Agent" | ||
| }, | ||
| "icon2": { | ||
| "x": 50, | ||
| "y": 50, | ||
| "service": "lambda", | ||
| "label": "Lambda" | ||
| }, | ||
| "line1": { | ||
| "from": "icon1", | ||
| "to": "icon2" | ||
| } | ||
| } | ||
| } |
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,8 @@ | ||
| #!/usr/bin/env node | ||
| import "source-map-support/register"; | ||
| import * as cdk from "aws-cdk-lib"; | ||
| import { BedrockAgentOpenaiStack } from "../lib/bedrock-agent-openai-stack"; | ||
|
|
||
| const description = "Sample app (uksb-1tthgi812) (tag:bedrock-agent-openai-cdk)" | ||
| const app = new cdk.App(); | ||
| new BedrockAgentOpenaiStack(app, "BedrockAgentOpenaiStack",{description}); |
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,3 @@ | ||
| { | ||
| "app": "npx ts-node --prefer-ts-exts bin/app.ts" | ||
| } |
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,50 @@ | ||
| { | ||
| "title": "Amazon Bedrock Agent with OpenAI model", | ||
| "description": "Deploy an Amazon Bedrock Agent powered by OpenAI GPT OSS with a Lambda action group for tool use.", | ||
| "language": "TypeScript", | ||
| "level": "300", | ||
| "framework": "AWS CDK", | ||
| "services": { | ||
| "from": "bedrock", | ||
| "to": "lambda" | ||
| }, | ||
| "introBox": { | ||
| "headline": "How it works", | ||
| "text": [ | ||
| "This pattern deploys an Amazon Bedrock Agent that uses an OpenAI GPT OSS foundation model to orchestrate tool-use workflows. The agent reasons about user queries and invokes a Lambda action group to retrieve weather and time data.", | ||
| "OpenAI GPT OSS models are available on Amazon Bedrock with the same security, governance, and API experience as other Bedrock models. The agent uses the standard Bedrock Agent orchestration to plan and execute multi-step tool calls." | ||
| ] | ||
| }, | ||
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-agent-openai-cdk", | ||
| "templateURL": "serverless-patterns/bedrock-agent-openai-cdk", | ||
| "projectFolder": "bedrock-agent-openai-cdk", | ||
| "templateFile": "lib/bedrock-agent-openai-stack.ts" | ||
| } | ||
| }, | ||
| "resources": { | ||
| "bullets": [ | ||
| { "text": "Amazon Bedrock Agents", "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html" }, | ||
| { "text": "OpenAI models on Amazon Bedrock", "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-openai.html" }, | ||
| { "text": "Bedrock Agent action groups", "link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents-action-create.html" } | ||
| ] | ||
| }, | ||
| "deploy": { | ||
| "text": ["cdk deploy"], | ||
| "file": "lib/bedrock-agent-openai-stack.ts" | ||
| }, | ||
| "testing": { | ||
| "text": ["See the README for testing instructions."] | ||
| }, | ||
| "cleanup": { | ||
| "text": ["cdk destroy"] | ||
| }, | ||
| "authors": [ | ||
| { | ||
| "name": "Nithin Chandran R", | ||
| "bio": "Technical Account Manager at AWS", | ||
| "linkedin": "nithin-chandran-r" | ||
| } | ||
| ] | ||
| } |
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.
Convert this to an image, rather than ASCII.
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.
Fixed in 0759a62. Thank you!