-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix: allow Bedrock provider to use AWS SDK default credential chain #3708
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
majiayu000
wants to merge
4
commits into
simstudioai:main
Choose a base branch
from
majiayu000:fix/issue-3694-bedrock-default-credential-chain
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.
+138
−19
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
845a19a
fix: allow Bedrock provider to use AWS SDK default credential chain
majiayu000 bd1aab6
fix: add partial credential guard for Bedrock provider
majiayu000 0418a45
fix: clean up bedrock test lint and dead code
majiayu000 e67b9df
fix: address greptile review feedback on PR #3708
majiayu000 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,111 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mockSend = vi.fn() | ||
|
|
||
| vi.mock('@aws-sdk/client-bedrock-runtime', () => ({ | ||
| BedrockRuntimeClient: vi.fn().mockImplementation(() => { | ||
| return { send: mockSend } | ||
| }), | ||
| ConverseCommand: vi.fn(), | ||
| ConverseStreamCommand: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/providers/bedrock/utils', () => ({ | ||
| getBedrockInferenceProfileId: vi | ||
| .fn() | ||
| .mockReturnValue('us.anthropic.claude-3-5-sonnet-20241022-v2:0'), | ||
| checkForForcedToolUsage: vi.fn(), | ||
| createReadableStreamFromBedrockStream: vi.fn(), | ||
| generateToolUseId: vi.fn().mockReturnValue('tool-1'), | ||
| })) | ||
|
|
||
| vi.mock('@/providers/models', () => ({ | ||
| getProviderModels: vi.fn().mockReturnValue([]), | ||
| getProviderDefaultModel: vi.fn().mockReturnValue('us.anthropic.claude-3-5-sonnet-20241022-v2:0'), | ||
| })) | ||
|
|
||
| vi.mock('@/providers/utils', () => ({ | ||
| calculateCost: vi.fn().mockReturnValue({ input: 0, output: 0, total: 0, pricing: null }), | ||
| prepareToolExecution: vi.fn(), | ||
| prepareToolsWithUsageControl: vi.fn(), | ||
| sumToolCosts: vi.fn().mockReturnValue(0), | ||
| })) | ||
|
|
||
| vi.mock('@/tools', () => ({ | ||
| executeTool: vi.fn(), | ||
| })) | ||
|
|
||
| import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime' | ||
| import { bedrockProvider } from '@/providers/bedrock/index' | ||
|
|
||
| describe('bedrockProvider credential handling', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockSend.mockResolvedValue({ | ||
| output: { message: { content: [{ text: 'response' }] } }, | ||
| usage: { inputTokens: 10, outputTokens: 5 }, | ||
| }) | ||
| }) | ||
|
|
||
| const baseRequest = { | ||
| model: 'us.anthropic.claude-3-5-sonnet-20241022-v2:0', | ||
| systemPrompt: 'You are helpful.', | ||
| messages: [{ role: 'user' as const, content: 'Hello' }], | ||
| } | ||
|
|
||
| it('throws when only bedrockAccessKeyId is provided', async () => { | ||
| await expect( | ||
| bedrockProvider.executeRequest({ | ||
| ...baseRequest, | ||
| bedrockAccessKeyId: 'AKIAIOSFODNN7EXAMPLE', | ||
| }) | ||
| ).rejects.toThrow('Both bedrockAccessKeyId and bedrockSecretKey must be provided together') | ||
| }) | ||
|
|
||
| it('throws when only bedrockSecretKey is provided', async () => { | ||
| await expect( | ||
| bedrockProvider.executeRequest({ | ||
| ...baseRequest, | ||
| bedrockSecretKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', | ||
| }) | ||
| ).rejects.toThrow('Both bedrockAccessKeyId and bedrockSecretKey must be provided together') | ||
| }) | ||
|
|
||
| it('creates client with explicit credentials when both are provided', async () => { | ||
| await bedrockProvider.executeRequest({ | ||
| ...baseRequest, | ||
| bedrockAccessKeyId: 'AKIAIOSFODNN7EXAMPLE', | ||
| bedrockSecretKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', | ||
| }) | ||
|
|
||
| expect(BedrockRuntimeClient).toHaveBeenCalledWith({ | ||
| region: 'us-east-1', | ||
| credentials: { | ||
| accessKeyId: 'AKIAIOSFODNN7EXAMPLE', | ||
| secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('creates client without credentials when neither is provided', async () => { | ||
| await bedrockProvider.executeRequest(baseRequest) | ||
|
|
||
| expect(BedrockRuntimeClient).toHaveBeenCalledWith({ | ||
| region: 'us-east-1', | ||
| }) | ||
| }) | ||
|
|
||
| it('uses custom region when provided', async () => { | ||
| await bedrockProvider.executeRequest({ | ||
| ...baseRequest, | ||
| bedrockRegion: 'eu-west-1', | ||
| }) | ||
|
|
||
| expect(BedrockRuntimeClient).toHaveBeenCalledWith({ | ||
| region: 'eu-west-1', | ||
| }) | ||
| }) | ||
| }) | ||
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.
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.