vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.33k stars 1.36k forks source link

Don't require AWS credentials with Amazon Bedrock Provider #2493

Closed amir-ziaei closed 2 weeks ago

amir-ziaei commented 1 month ago

Context

The createAmazonBedrock function from the @ai-sdk/amazon-bedrock package currently requires AWS credentials (access key ID and secret access key) to be provided explicitly via the options object or through environment variables. This implementation necessitates duplicating credentials in the project's .env file, rather than leveraging the AWS profile configuration at the system level.

Reproduction Steps

  1. Set up your AWS credentials at the system level via the AWS default profile.
  2. Install the @ai-sdk/amazon-bedrock package.
  3. Make sure the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are NOT defined.
  4. Use the createAmazonBedrock function to create a Bedrock instance, without passing a config object.
  5. Observe the crash.

Expected Behavior

createAmazonBedrock to respect the AWS credentials set at the profile level.

Actual Behavior

The BedrockRuntimeClient does not resolve credentials from the AWS profile configuration. Instead, it mandates the credentials to be provided either through the options object or environment variables, forcing users to duplicate their credentials in the .env file of their project. Failure to comply results in an error:

AWS access key ID setting is missing. Pass it using the 'accessKeyId' parameter or the AWS_ACCESS_KEY_ID environment variable.: AI_LoadSettingError: AWS access key ID setting is missing. Pass it using the 'accessKeyId' parameter or the AWS_ACCESS_KEY_ID environment variable.
    at loadSetting (node_modules/@ai-sdk/provider-utils/src/load-setting.ts:33:11)
    at createBedrockRuntimeClient (node_modules/@ai-sdk/amazon-bedrock/src/bedrock-provider.ts:56:24)
    at createChatModel (node_modules/@ai-sdk/amazon-bedrock/src/bedrock-provider.ts:77:15)
    at provider (node_modules/@ai-sdk/amazon-bedrock/src/bedrock-provider.ts:91:12)
    ...

Proposed Change

Either:

  1. Allow consumers to supply their own BedrockRuntimeClient.
  2. Let BedrockRuntimeClient handle the credentials resolving.

Code example

import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';

process.env.AWS_ACCESS_KEY_ID = undefined;
process.env.AWS_SECRET_ACCESS_KEY = undefined;

const bedrock = createAmazonBedrock();
bedrock('meta.llama3-70b-instruct-v1:0');

Additional context

@ai-sdk/amazon-bedrock version 0.0.9

lgrammel commented 1 month ago

You can pass the bedrockOptions parameter if you want to configure the bedrock library: https://sdk.vercel.ai/providers/ai-sdk-providers/amazon-bedrock#provider-instance

lgrammel commented 1 month ago

Agree this should be easier. I'll leave the issue open since configuration has come up multiple times for Bedrock.

shoopapa commented 4 weeks ago

Maybe this relevant? https://github.com/vercel/ai/pull/2409

lgrammel commented 2 weeks ago

The bedrockOptions parameter solves this.