-
Notifications
You must be signed in to change notification settings - Fork 187
feat: add bedrock as a new data source #730
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
AlexHladin
wants to merge
9
commits into
master
Choose a base branch
from
feature/add-bedrock-data-source-support
base: master
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
9 commits
Select commit
Hold shift + click to select a range
72b3d59
feat: add bedrock as a new data source
13ae025
feat: fix example
6c1d775
feat: fix example
bf54292
feat: update role assigning
b40da69
feat: fix comments
99f686e
feat: add schemaVersion to example
6aeab34
feat: update doc
cae5727
feat: update tests
27b9c6a
feat: fix comments
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { synthesize } from './helpers/synthesize'; | ||
| import { | ||
| expectDataSourceOfType, | ||
| findResourcesByType, | ||
| } from './helpers/assertions'; | ||
|
|
||
| describe('examples/datasource-bedrock', () => { | ||
| let result: ReturnType<typeof synthesize>; | ||
|
|
||
| beforeAll(() => { | ||
| result = synthesize('examples/datasource-bedrock'); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| result.cleanup(); | ||
| }); | ||
|
|
||
| it('creates an AMAZON_BEDROCK_RUNTIME data source', () => { | ||
| const ds = expectDataSourceOfType( | ||
| result.template, | ||
| 'AMAZON_BEDROCK_RUNTIME', | ||
| ); | ||
| expect(ds.resource.Properties?.Name).toBe('bedrock'); | ||
| }); | ||
|
|
||
| it('generates a service role with bedrock invoke permissions for the datasource', () => { | ||
| const ds = expectDataSourceOfType( | ||
| result.template, | ||
| 'AMAZON_BEDROCK_RUNTIME', | ||
| ); | ||
| const serviceRoleArn = ds.resource.Properties?.ServiceRoleArn as { | ||
| 'Fn::GetAtt'?: [string, string]; | ||
| }; | ||
| const roleLogicalId = serviceRoleArn?.['Fn::GetAtt']?.[0]; | ||
| expect(roleLogicalId).toBeDefined(); | ||
|
|
||
| const roles = findResourcesByType(result.template, 'AWS::IAM::Role'); | ||
| const bedrockRole = roles.find( | ||
| ({ logicalId }) => logicalId === roleLogicalId, | ||
| ); | ||
| expect(bedrockRole).toBeDefined(); | ||
|
|
||
| if (!bedrockRole) { | ||
| throw new Error('Expected Bedrock datasource role to be present'); | ||
| } | ||
|
|
||
| const policies = bedrockRole.resource.Properties?.Policies as Array<{ | ||
| PolicyName?: string; | ||
| PolicyDocument?: { Statement?: Array<{ Action?: string | string[] }> }; | ||
| }>; | ||
| expect( | ||
| policies?.some( | ||
| (policy) => policy.PolicyName === 'AppSync-Datasource-bedrock', | ||
| ), | ||
| ).toBe(true); | ||
|
|
||
| const actions = | ||
| policies?.flatMap( | ||
| (policy) => | ||
| policy.PolicyDocument?.Statement?.flatMap((statement) => | ||
| Array.isArray(statement.Action) | ||
| ? statement.Action | ||
| : statement.Action | ||
| ? [statement.Action] | ||
| : [], | ||
| ) ?? [], | ||
| ) ?? []; | ||
|
|
||
| expect(actions).toEqual( | ||
| expect.arrayContaining(['bedrock:InvokeModel', 'bedrock:Converse']), | ||
| ); | ||
| }); | ||
| }); |
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,20 @@ | ||
| import { invokeModel } from '@aws-appsync/utils/ai'; | ||
|
|
||
| export function request(ctx) { | ||
| return invokeModel({ | ||
| modelId: 'eu.amazon.nova-micro-v1:0', | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| body: { | ||
| schemaVersion: 'messages-v1', | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [{ text: ctx.args.text }], | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export function response(ctx) { | ||
| return ctx.result.output.message.content[0].text; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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 @@ | ||
| type Query { | ||
| summarize(text: String!): AWSJSON | ||
| } |
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 @@ | ||
| service: appsync-datasource-bedrock | ||
|
|
||
| provider: | ||
| name: aws | ||
| runtime: nodejs22.x | ||
|
|
||
| plugins: | ||
| - serverless-appsync-plugin | ||
|
|
||
| appSync: | ||
| name: datasource-bedrock | ||
| authentication: | ||
| type: API_KEY | ||
| apiKeys: | ||
| - name: default | ||
|
|
||
| resolvers: | ||
| Query.summarize: | ||
| kind: UNIT | ||
| dataSource: bedrock | ||
| code: ./resolvers/summarize.js | ||
|
|
||
| dataSources: | ||
| bedrock: | ||
| type: AMAZON_BEDROCK_RUNTIME | ||
| description: Amazon Bedrock runtime for synchronous model invocations | ||
| config: | ||
| models: | ||
| - eu.amazon.nova-micro-v1:0 |
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.