Skip to content
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

Add Support for AWS Named Profiles #3

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.2.4] - 2024-04-25

- Support the use of AWS Named Profiles for authentication

## [0.2.3] - 2024-01-12

- Fix A121 Labs bug for maxTokens parameter
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby-amazon-bedrock (0.2.3)
ruby-amazon-bedrock (0.2.4)
aws-sdk-bedrockruntime (~> 1.0)
aws-sdk-s3 (~> 1.0)

Expand Down Expand Up @@ -106,4 +106,4 @@ DEPENDENCIES
webmock (~> 3.12)

BUNDLED WITH
2.4.22
2.4.14
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,33 @@ client = RubyAmazonBedrock::Client.new(
)
```

### AWS Named Profiles

You can also use [AWS Named Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-format-profile):

```ruby
client = RubyAmazonBedrock::Client.new(
profile: 'dev'
)
```

This will default to the value of the `$AWS_PROFILE` environment variable and will take precidence over key/token keys. If for some reason you have `$AWS_PROFILE` defined in your environment _and you do not want to use it_ then you can disable by passing an empty string and specifying your region/key/secret as usual:

```ruby
client = RubyAmazonBedrock::Client.new(
profile: "", # Disable named profile
region: "AWS_REGION",
access_key_id: "AWS_ACCESS_KEY_ID",
access_token: "AWS_SECRET_ACCESS_KEY"
)
```

## With Configuration

```ruby
RubyAmazonBedrock.configure do |config|
# You don't need to specify these lines to default to the environment variables, these lines are provided for illustrative purposes only
config.profile = ENV.fetch('AWS_PROFILE', nil)
config.region = ENV.fetch('AWS_REGION', nil)
config.access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
config.secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
Expand Down
3 changes: 2 additions & 1 deletion lib/amazon_bedrock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Error < StandardError; end

# Configuration class for setting up AWS credentials and region.
class Configuration
attr_accessor :region, :access_key_id, :secret_access_key
attr_accessor :region, :access_key_id, :secret_access_key, :profile

# Initializes a new Configuration instance, loading values from
# environment variables or setting them to nil by default so the
Expand All @@ -22,6 +22,7 @@ def initialize
@region = ENV.fetch('AWS_REGION', nil)
@access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
@secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
@profile = ENV.fetch('AWS_PROFILE', nil)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/amazon_bedrock/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RubyAmazonBedrock
VERSION = "0.2.3"
VERSION = "0.2.4"
end
19 changes: 13 additions & 6 deletions lib/bedrock_runtime/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ class Client
# Initializes the AWS BedrockRuntime client.
#
# @note The AWS credentials and region are fetched from the environment variables.
def initialize(region: nil, access_key_id: nil, secret_access_key: nil)
def initialize(region: nil, access_key_id: nil, secret_access_key: nil, profile: nil)
config = RubyAmazonBedrock.configuration || RubyAmazonBedrock::Configuration.new

@client = Aws::BedrockRuntime::Client.new(
region: region || config.region,
access_key_id: access_key_id || config.access_key_id,
secret_access_key: secret_access_key || config.secret_access_key
)
profile ||= config.profile
if profile.present?
@client = Aws::BedrockRuntime::Client.new(
profile: profile
)
else
@client = Aws::BedrockRuntime::Client.new(
region: region || config.region,
access_key_id: access_key_id || config.access_key_id,
secret_access_key: secret_access_key || config.secret_access_key
)
end
end

# Invokes a model using the Bedrock Runtime client.
Expand Down