ruby-amazon-bedrock 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +0 -2
  3. data/.env.sample +5 -0
  4. data/.gitignore +2 -2
  5. data/CHANGELOG.md +12 -0
  6. data/Gemfile +1 -0
  7. data/Gemfile.lock +55 -41
  8. data/README.md +19 -11
  9. data/lib/amazon_bedrock/version.rb +1 -1
  10. data/lib/amazon_bedrock.rb +2 -1
  11. data/lib/bedrock_runtime/client.rb +13 -8
  12. data/lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb +2 -2
  13. data/lib/bedrock_runtime/payload_builders/amazon/base.rb +1 -1
  14. data/lib/bedrock_runtime/payload_builders/meta/llama370b_instruct_v1.rb +21 -0
  15. data/lib/bedrock_runtime/payload_builders/meta/llama38b_instruct_v1.rb +21 -0
  16. data/lib/bedrock_runtime/payload_factory.rb +4 -0
  17. data/lib/bedrock_runtime/response_builders/ai_21_labs.rb +32 -0
  18. data/lib/bedrock_runtime/response_builders/{image.rb → amazon_image.rb} +42 -7
  19. data/lib/bedrock_runtime/response_builders/amazon_text.rb +32 -0
  20. data/lib/bedrock_runtime/response_builders/anthropic.rb +32 -0
  21. data/lib/bedrock_runtime/response_builders/cohere_command.rb +32 -0
  22. data/lib/bedrock_runtime/response_builders/cohere_embed.rb +32 -0
  23. data/lib/bedrock_runtime/response_builders/{text.rb → meta.rb} +11 -5
  24. data/lib/bedrock_runtime/response_builders/stability_ai.rb +85 -0
  25. data/lib/bedrock_runtime/response_factory.rb +44 -16
  26. data/ruby-amazon-bedrock.gemspec +2 -0
  27. data/spec/bedrock_runtime/client_spec.rb +4 -1
  28. data/spec/bedrock_runtime/payload_builders/ai_21_labs/base_spec.rb +1 -1
  29. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1_spec.rb +1 -1
  30. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1_spec.rb +1 -1
  31. data/spec/bedrock_runtime/payload_builders/meta/llama370b_instruct_v1_spec.rb +26 -0
  32. data/spec/bedrock_runtime/payload_builders/meta/llama38b_instruct_v1_spec.rb +26 -0
  33. data/spec/bedrock_runtime/payload_factory_spec.rb +3 -1
  34. data/spec/bedrock_runtime/response_builders/{text_spec.rb → ai_21_labs_spec.rb} +3 -4
  35. data/spec/bedrock_runtime/response_builders/{image_spec.rb → stability_ai_spec.rb} +2 -2
  36. data/spec/bedrock_runtime/response_factory_spec.rb +74 -14
  37. data/spec/cassettes/models/meta_llama3-70b-instruct-v1_0.yml +73 -0
  38. data/spec/cassettes/models/meta_llama3-8b-instruct-v1_0.yml +71 -0
  39. data/spec/support/helpers.rb +1 -3
  40. metadata +48 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e34184a2246b1054a64d6d29bc7bb1144487a508c6d27f717d8f92edb4cb32a2
4
- data.tar.gz: ae40e48b1107bf163bbb30e4c058d660784f1bf9cc6fcafa0bfa9843b526dd49
3
+ metadata.gz: 06f4b8ff875c2d3d8b104c026621108f24e2ea60dd4ef07e85b4874b346b95af
4
+ data.tar.gz: 5fae3ad77abbab1f45ce187ade385da0ab0d3a64d7a8c38e22283a08ed23078b
5
5
  SHA512:
6
- metadata.gz: fcdfeb1017d3bf2cefeb1bf77b68a85bd36bd3a7906651b1a56f8ff2544309ba98eee08fc1f04ce80ed22b738302fd38e7811087f56a976c6eeea1f7cfae0010
7
- data.tar.gz: f19b95ef5965317c53eda21c96c69e421abdb34ce441cc850731663aa8ed35bbc67000d02a34ebc2f192ce7fbe81e0d18afe3ab59d0dbea38e32c858625fcc89
6
+ metadata.gz: cd0bd9610262b0222d9680386cfcd46b070582c8f0053737020c35509504a31c2cac4e94668debed36fc9f3f5c4f45396f0082c57314277572124d24ce2bffb4
7
+ data.tar.gz: 44c080409f214e186841ff78a073a84749bb84cc978d2d206045f0a143b9533bf652356da17e4405bbdf3ae59337a0752f762af02ec2125ee1e84750a1c394a9
data/.circleci/config.yml CHANGED
@@ -50,8 +50,6 @@ workflows:
50
50
  matrix:
51
51
  parameters:
52
52
  ruby-image:
53
- - cimg/ruby:2.6-node
54
- - cimg/ruby:2.7-node
55
53
  - cimg/ruby:3.0-node
56
54
  - cimg/ruby:3.1-node
57
55
  - cimg/ruby:3.2-node
data/.env.sample ADDED
@@ -0,0 +1,5 @@
1
+ AWS_REGION: 'us-east-1'
2
+
3
+ # You don't need valid key data *unless you are creating new
4
+ AWS_ACCESS_KEY_ID: 'aaaaaaaaaaaaaaaaaaaa'
5
+ AWS_SECRET_ACCESS_KEY: 'aaaaaaaaaaaaaaaaaaaa'
data/.gitignore CHANGED
@@ -4,11 +4,11 @@
4
4
  /coverage/
5
5
  /doc/
6
6
  /pkg/
7
- /spec/reports/
7
+ /spec/reports/
8
8
  /tmp/
9
9
  .env
10
10
  *.gem
11
- image.jpg
11
+ *.jpg
12
12
 
13
13
  # rspec failure tracking
14
14
  .rspec_status
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [0.2.5] - 2024-09-17
2
+
3
+ - Support for llama38b_instruct_v1
4
+ - Support for llama370b_instruct_v1
5
+ - Explicitly add base64 gem to dependencies
6
+ - Support the use of AWS Named Profiles for authentication
7
+
8
+ ## [0.2.3] - 2024-01-12
9
+
10
+ - Fix A121 Labs bug for maxTokens parameter
11
+ - Allow Image generator to store images in S3 after being created
12
+
1
13
  ## [0.2.2] - 2024-01-10
2
14
 
3
15
  - Fix bug for Amazon Titan Text Lite
data/Gemfile CHANGED
@@ -9,6 +9,7 @@ gem "bundler-audit", "~> 0.8"
9
9
  gem "dotenv", "~> 2.7"
10
10
  gem "pry", "~> 0.13"
11
11
  gem "rake", "~> 13.0"
12
+ gem "rexml", ">= 3.3.6"
12
13
  gem "rspec", "~> 3.0"
13
14
  gem "rubocop", "~> 1.21"
14
15
  gem "vcr", "~> 6.0"
data/Gemfile.lock CHANGED
@@ -1,96 +1,110 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-amazon-bedrock (0.2.2)
4
+ ruby-amazon-bedrock (0.2.4)
5
5
  aws-sdk-bedrockruntime (~> 1.0)
6
+ aws-sdk-s3 (~> 1.0)
7
+ base64 (~> 0.2)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
9
11
  specs:
10
- addressable (2.8.5)
11
- public_suffix (>= 2.0.2, < 6.0)
12
+ addressable (2.8.7)
13
+ public_suffix (>= 2.0.2, < 7.0)
12
14
  ast (2.4.2)
13
15
  aws-eventstream (1.3.0)
14
- aws-partitions (1.860.0)
15
- aws-sdk-bedrockruntime (1.4.0)
16
- aws-sdk-core (~> 3, >= 3.188.0)
17
- aws-sigv4 (~> 1.1)
18
- aws-sdk-core (3.189.0)
16
+ aws-partitions (1.963.0)
17
+ aws-sdk-bedrockruntime (1.17.0)
18
+ aws-sdk-core (~> 3, >= 3.201.0)
19
+ aws-sigv4 (~> 1.5)
20
+ aws-sdk-core (3.201.4)
19
21
  aws-eventstream (~> 1, >= 1.3.0)
20
22
  aws-partitions (~> 1, >= 1.651.0)
21
23
  aws-sigv4 (~> 1.8)
22
24
  jmespath (~> 1, >= 1.6.1)
23
- aws-sigv4 (1.8.0)
25
+ aws-sdk-kms (1.88.0)
26
+ aws-sdk-core (~> 3, >= 3.201.0)
27
+ aws-sigv4 (~> 1.5)
28
+ aws-sdk-s3 (1.157.0)
29
+ aws-sdk-core (~> 3, >= 3.201.0)
30
+ aws-sdk-kms (~> 1)
31
+ aws-sigv4 (~> 1.5)
32
+ aws-sigv4 (1.9.1)
24
33
  aws-eventstream (~> 1, >= 1.0.2)
34
+ base64 (0.2.0)
35
+ bigdecimal (3.1.8)
25
36
  bundler-audit (0.9.1)
26
37
  bundler (>= 1.2.0, < 3)
27
38
  thor (~> 1.0)
28
39
  coderay (1.1.3)
29
- crack (0.4.5)
40
+ crack (1.0.0)
41
+ bigdecimal
30
42
  rexml
31
- diff-lcs (1.5.0)
43
+ diff-lcs (1.5.1)
32
44
  dotenv (2.8.1)
33
- hashdiff (1.0.1)
45
+ hashdiff (1.1.1)
34
46
  jmespath (1.6.2)
35
- json (2.6.3)
47
+ json (2.7.2)
36
48
  language_server-protocol (3.17.0.3)
37
- method_source (1.0.0)
38
- parallel (1.23.0)
39
- parser (3.2.2.4)
49
+ method_source (1.1.0)
50
+ parallel (1.26.1)
51
+ parser (3.3.4.2)
40
52
  ast (~> 2.4.1)
41
53
  racc
42
54
  pry (0.14.2)
43
55
  coderay (~> 1.1)
44
56
  method_source (~> 1.0)
45
- public_suffix (5.0.4)
46
- racc (1.7.3)
57
+ public_suffix (5.1.1)
58
+ racc (1.8.1)
47
59
  rainbow (3.1.1)
48
- rake (13.1.0)
49
- regexp_parser (2.8.2)
50
- rexml (3.2.6)
51
- rspec (3.12.0)
52
- rspec-core (~> 3.12.0)
53
- rspec-expectations (~> 3.12.0)
54
- rspec-mocks (~> 3.12.0)
55
- rspec-core (3.12.2)
56
- rspec-support (~> 3.12.0)
57
- rspec-expectations (3.12.3)
60
+ rake (13.2.1)
61
+ regexp_parser (2.9.2)
62
+ rexml (3.3.7)
63
+ rspec (3.13.0)
64
+ rspec-core (~> 3.13.0)
65
+ rspec-expectations (~> 3.13.0)
66
+ rspec-mocks (~> 3.13.0)
67
+ rspec-core (3.13.0)
68
+ rspec-support (~> 3.13.0)
69
+ rspec-expectations (3.13.1)
58
70
  diff-lcs (>= 1.2.0, < 2.0)
59
- rspec-support (~> 3.12.0)
60
- rspec-mocks (3.12.6)
71
+ rspec-support (~> 3.13.0)
72
+ rspec-mocks (3.13.1)
61
73
  diff-lcs (>= 1.2.0, < 2.0)
62
- rspec-support (~> 3.12.0)
63
- rspec-support (3.12.1)
64
- rubocop (1.57.2)
74
+ rspec-support (~> 3.13.0)
75
+ rspec-support (3.13.1)
76
+ rubocop (1.65.1)
65
77
  json (~> 2.3)
66
78
  language_server-protocol (>= 3.17.0)
67
79
  parallel (~> 1.10)
68
- parser (>= 3.2.2.4)
80
+ parser (>= 3.3.0.2)
69
81
  rainbow (>= 2.2.2, < 4.0)
70
- regexp_parser (>= 1.8, < 3.0)
82
+ regexp_parser (>= 2.4, < 3.0)
71
83
  rexml (>= 3.2.5, < 4.0)
72
- rubocop-ast (>= 1.28.1, < 2.0)
84
+ rubocop-ast (>= 1.31.1, < 2.0)
73
85
  ruby-progressbar (~> 1.7)
74
86
  unicode-display_width (>= 2.4.0, < 3.0)
75
- rubocop-ast (1.30.0)
76
- parser (>= 3.2.1.0)
87
+ rubocop-ast (1.32.0)
88
+ parser (>= 3.3.1.0)
77
89
  ruby-progressbar (1.13.0)
78
- thor (1.3.0)
90
+ thor (1.3.1)
79
91
  unicode-display_width (2.5.0)
80
92
  vcr (6.2.0)
81
- webmock (3.19.1)
93
+ webmock (3.23.1)
82
94
  addressable (>= 2.8.0)
83
95
  crack (>= 0.3.2)
84
96
  hashdiff (>= 0.4.0, < 2.0.0)
85
97
 
86
98
  PLATFORMS
87
99
  arm64-darwin-22
100
+ arm64-darwin-23
88
101
 
89
102
  DEPENDENCIES
90
103
  bundler-audit (~> 0.8)
91
104
  dotenv (~> 2.7)
92
105
  pry (~> 0.13)
93
106
  rake (~> 13.0)
107
+ rexml (>= 3.3.6)
94
108
  rspec (~> 3.0)
95
109
  rubocop (~> 1.21)
96
110
  ruby-amazon-bedrock!
@@ -98,4 +112,4 @@ DEPENDENCIES
98
112
  webmock (~> 3.12)
99
113
 
100
114
  BUNDLED WITH
101
- 2.4.22
115
+ 2.4.14
data/README.md CHANGED
@@ -14,7 +14,7 @@ Amazon Bedrock is a fully managed service that makes FMs from leading AI startup
14
14
  Add the following line to your application's Gemfile:
15
15
 
16
16
  ```ruby
17
- gem "ruby-amazon-bedrock", "~> 0.2.2"
17
+ gem "ruby-amazon-bedrock", "~> 0.2.4"
18
18
  ```
19
19
 
20
20
  And then execute:
@@ -28,7 +28,7 @@ $ bundle install
28
28
  Or install with:
29
29
 
30
30
  ```bash
31
- $ gem install ruby-amazon-bedrock -v 0.2.1
31
+ $ gem install ruby-amazon-bedrock -v 0.2.3
32
32
  ```
33
33
 
34
34
  and require with:
@@ -59,20 +59,28 @@ Instantiate a client by passing your AWS IAM credentials:
59
59
  client = RubyAmazonBedrock::Client.new(
60
60
  region: "AWS_REGION",
61
61
  access_key_id: "AWS_ACCESS_KEY_ID",
62
- access_token: "AWS_SECRET_ACCESS_KEY"
62
+ secret_access_key: "AWS_SECRET_ACCESS_KEY"
63
63
  )
64
64
  ```
65
65
 
66
- ## With Configuration
66
+ Or copy the `.env.sample` to a `.env` file with your account's credentials and use the `ENV` variable. NOTE: This step is REQUIRED in order to run the tests with `rspec`
67
67
 
68
68
  ```ruby
69
- RubyAmazonBedrock.configure do |config|
70
- config.region = ENV.fetch('AWS_REGION', nil)
71
- config.access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
72
- config.secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
73
- end
69
+ client = RubyAmazonBedrock::Client.new(
70
+ region: ENV["AWS_REGION"],
71
+ access_key_id: ENV["AWS_ACCESS_KEY_ID"],
72
+ secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
73
+ )
74
+ ```
75
+
76
+ ### AWS Named Profiles
74
77
 
75
- client = RubyAmazonBedrock::Client.new
78
+ You can also use [AWS Named Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-format-profile) by passing the `profile` keywoard argument. When using a named profile, specyfing the `region`, `access_key_id` and `access_token` won't be required.
79
+
80
+ ```ruby
81
+ client = RubyAmazonBedrock::Client.new(
82
+ profile: "AWS_PROFILE"
83
+ )
76
84
  ```
77
85
 
78
86
  ## Options
@@ -231,7 +239,7 @@ _*Claude Instant 1.2*_
231
239
  Supports: Question answering, information extraction, removing PII, content generation, multiple choice classification, Roleplay, comparing text, summarization, document Q&A with citation
232
240
 
233
241
  ```ruby
234
- client.invoke_model('anthropic.claude-instant-v1', 'What is a neural network?')
242
+ client.invoke_model(id: 'anthropic.claude-instant-v1', prompt: 'What is a neural network?')
235
243
 
236
244
  # Response
237
245
  {:completion=>
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyAmazonBedrock
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -13,7 +13,7 @@ module RubyAmazonBedrock
13
13
 
14
14
  # Configuration class for setting up AWS credentials and region.
15
15
  class Configuration
16
- attr_accessor :region, :access_key_id, :secret_access_key
16
+ attr_accessor :region, :access_key_id, :secret_access_key, :profile
17
17
 
18
18
  # Initializes a new Configuration instance, loading values from
19
19
  # environment variables or setting them to nil by default so the
@@ -22,6 +22,7 @@ module RubyAmazonBedrock
22
22
  @region = ENV.fetch('AWS_REGION', nil)
23
23
  @access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID', nil)
24
24
  @secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
25
+ @profile = ENV.fetch('AWS_PROFILE', nil)
25
26
  end
26
27
  end
27
28
 
@@ -14,14 +14,20 @@ module RubyAmazonBedrock
14
14
  # Initializes the AWS BedrockRuntime client.
15
15
  #
16
16
  # @note The AWS credentials and region are fetched from the environment variables.
17
- def initialize(region: nil, access_key_id: nil, secret_access_key: nil)
17
+ def initialize(region: nil, access_key_id: nil, secret_access_key: nil, profile: nil)
18
18
  config = RubyAmazonBedrock.configuration || RubyAmazonBedrock::Configuration.new
19
19
 
20
- @client = Aws::BedrockRuntime::Client.new(
21
- region: region || config.region,
22
- access_key_id: access_key_id || config.access_key_id,
23
- secret_access_key: secret_access_key || config.secret_access_key
24
- )
20
+ @client = if profile
21
+ Aws::BedrockRuntime::Client.new(
22
+ profile: profile
23
+ )
24
+ else
25
+ Aws::BedrockRuntime::Client.new(
26
+ region: region || config.region,
27
+ access_key_id: access_key_id || config.access_key_id,
28
+ secret_access_key: secret_access_key || config.secret_access_key
29
+ )
30
+ end
25
31
  end
26
32
 
27
33
  # Invokes a model using the Bedrock Runtime client.
@@ -39,8 +45,7 @@ module RubyAmazonBedrock
39
45
  payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, prompt, options).create
40
46
  response = @client.invoke_model(payload_builder_class.build)
41
47
 
42
- response_builder_class = RubyAmazonBedrock::ResponseFactory.new(payload_builder_class.type, response,
43
- options).create
48
+ response_builder_class = RubyAmazonBedrock::ResponseFactory.new(id, response, options).create
44
49
  response_builder_class.build
45
50
  end
46
51
  end
@@ -35,7 +35,7 @@ module RubyAmazonBedrock
35
35
  accept: '*/*',
36
36
  body: {
37
37
  prompt: @prompt,
38
- maxTokenCount: parameters[:maxTokenCount],
38
+ maxTokens: parameters[:maxTokens],
39
39
  temperature: parameters[:temperature],
40
40
  topP: parameters[:topP],
41
41
  stopSequences: parameters[:stopSequences],
@@ -52,7 +52,7 @@ module RubyAmazonBedrock
52
52
 
53
53
  def parameters # rubocop:disable Metrics/CyclomaticComplexity
54
54
  {
55
- maxTokenCount: @options[:max_tokens] || 200,
55
+ maxTokens: @options[:max_tokens] || 200,
56
56
  stopSequences: @options[:stop_sequences] || [],
57
57
  temperature: @options[:temperature] || 0,
58
58
  topP: @options[:top_p] || 1,
@@ -20,7 +20,7 @@ module RubyAmazonBedrock
20
20
  # - :body [String] A JSON string that includes the following details:
21
21
  # - :inputText [String] The input text to be processed by the model.
22
22
  # - :textGenerationConfig [Hash] A hash containing configuration parameters for text generation:
23
- # - :maxTokenCount [Integer] The maximum number of tokens to generate.
23
+ # - :maxTokens [Integer] The maximum number of tokens to generate.
24
24
  # - :stopSequences [Array<String>] An array of strings that, when encountered, will signal the end
25
25
  # of generation.
26
26
  # - :temperature [Float] A parameter controlling the randomness in the response generation.
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Meta
6
+ # Llama370bInstructV1 is a subclass of Base. It provides functionalities specific to the Meta
7
+ # Llama 3 70B Instruct model.
8
+ #
9
+ # @see https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=meta.llama3-70b-instruct-v1:0
10
+ # for more information about the Meta model.
11
+ class Llama370bInstructV1 < Base
12
+ # Returns the model ID for the Meta Llama 3 70B Instruct model.
13
+ #
14
+ # @return [String] 'meta.llama3-70b-chat-v1:0'
15
+ def model_id
16
+ 'meta.llama3-70b-instruct-v1:0'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Meta
6
+ # Llama370bInstructV1 is a subclass of Base. It provides functionalities specific to the Meta
7
+ # Llama 3 70B Instruct model.
8
+ #
9
+ # @see https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/providers?model=meta.llama3-8b-instruct-v1:0
10
+ # for more information about the Meta model.
11
+ class Llama38bInstructV1 < Base
12
+ # Returns the model ID for the Meta Llama 3 70B Instruct model.
13
+ #
14
+ # @return [String] 'meta.llama3-8b-instruct-v1:0'
15
+ def model_id
16
+ 'meta.llama3-8b-instruct-v1:0'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -14,6 +14,8 @@ require_relative 'payload_builders/cohere/embed_english_v3'
14
14
  require_relative 'payload_builders/cohere/embed_multilingual_v3'
15
15
  require_relative 'payload_builders/meta/llama213b_chat_v1'
16
16
  require_relative 'payload_builders/meta/llama270b_chat_v1'
17
+ require_relative 'payload_builders/meta/llama370b_instruct_v1'
18
+ require_relative 'payload_builders/meta/llama38b_instruct_v1'
17
19
  require_relative 'payload_builders/stability_ai/stable_diffusion_xl_v0'
18
20
  require_relative 'payload_builders/stability_ai/stable_diffusion_xl_v1'
19
21
 
@@ -64,6 +66,8 @@ module RubyAmazonBedrock
64
66
  'cohere.embed-multilingual-v3' => PayloadBuilders::Cohere::EmbedMultilingualV3,
65
67
  'meta.llama2-13b-chat-v1' => PayloadBuilders::Meta::Llama213bChatV1,
66
68
  'meta.llama2-70b-chat-v1' => PayloadBuilders::Meta::Llama270bChatV1,
69
+ 'meta.llama3-70b-instruct-v1:0' => PayloadBuilders::Meta::Llama370bInstructV1,
70
+ 'meta.llama3-8b-instruct-v1:0' => PayloadBuilders::Meta::Llama38bInstructV1,
67
71
  'stability.stable-diffusion-xl-v0' => PayloadBuilders::StabilityAi::StableDiffusionXlV0,
68
72
  'stability.stable-diffusion-xl-v1' => PayloadBuilders::StabilityAi::StableDiffusionXlV1
69
73
  }
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module ResponseBuilders
5
+ # The Ai21Labs class is responsible for parsing and building a structured response from a raw text response.
6
+ # It converts the HTTP response for an AI21 Labs model into a structured format to make it easier to access
7
+ # the response data.
8
+ class Ai21Labs
9
+ # Initializes a new instance of the Text class.
10
+ #
11
+ # @param response [Object] The raw response object with is an HTTP response.
12
+ def initialize(response, _options = {})
13
+ @response = response
14
+ end
15
+
16
+ # Builds and returns a structured representation of the raw text response.
17
+ # This method parses the raw response body as JSON and symbolizes the names
18
+ # for easier access in Ruby.
19
+ #
20
+ # @return [Hash] A hash representing the structured data from the response body.
21
+ # The keys of the hash are symbolized for convenient access.
22
+ def build
23
+ response = JSON.parse(@response.body.read, symbolize_names: true)
24
+
25
+ {
26
+ text: response[:completions]&.first&.dig(:data, :text),
27
+ full_response: response
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'aws-sdk-s3'
3
4
  require 'base64'
5
+ require 'digest'
4
6
 
5
7
  module RubyAmazonBedrock
6
8
  module ResponseBuilders
7
- # The Image class is responsible for handling and processing image data received in a response.
9
+ # The AmazonImage class is responsible for handling and processing image data received in a response.
8
10
  # It parses the response, extracts the image data, decodes it from Base64, and saves it as a file.
9
- class Image
10
- # Initializes a new instance of the Image class.
11
+ class AmazonImage
12
+ # Initializes a new instance of the AmazonImage class.
11
13
  #
12
14
  # @param response [Object] The raw response object which is an HTTP response.
13
15
  # The response should contain the image data in Base64 format.
@@ -16,6 +18,7 @@ module RubyAmazonBedrock
16
18
  def initialize(response, options = {})
17
19
  @response = response
18
20
  @file_path = options[:file_path] || 'image.jpg'
21
+ @upload = options[:upload] || false
19
22
  end
20
23
 
21
24
  # Processes the response to extract and decode image data, then saves it as a file.
@@ -26,15 +29,20 @@ module RubyAmazonBedrock
26
29
  #
27
30
  # @return [Hash] A hash indicating the result of the operation.
28
31
  # If successful, the hash includes :result set to :success and :file_path.
32
+ # If upload option is provided it will also include :s3_url after saving the image in S3.
29
33
  # If failure, the hash includes :result set to :failure and :error with exception details.
30
34
  def build
31
35
  response_object = JSON.parse(@response.body.read, symbolize_names: true)
32
36
  image_data = Base64.decode64(response_object[:artifacts].first[:base64])
33
37
  File.binwrite(@file_path, image_data)
38
+
39
+ upload_image if can_upload?
40
+
34
41
  {
35
42
  result: :success,
36
- file_path: @file_path
37
- }
43
+ file_path: @file_path,
44
+ s3_url: s3_url
45
+ }.compact
38
46
  rescue StandardError => e
39
47
  {
40
48
  result: :failure,
@@ -42,8 +50,35 @@ module RubyAmazonBedrock
42
50
  }
43
51
  end
44
52
 
45
- def model_id
46
- # noop
53
+ private
54
+
55
+ def bucket
56
+ @bucket ||= ENV.fetch('AWS_S3_BUCKET')
57
+ end
58
+
59
+ def key
60
+ @key ||= Digest::SHA1.hexdigest(Time.now.to_s)[0..11]
61
+ end
62
+
63
+ def can_upload?
64
+ @upload && bucket && File.exist?(@file_path)
65
+ end
66
+
67
+ def s3_url
68
+ s3_resource = Aws::S3::Resource.new
69
+ object = s3_resource.bucket(bucket).object(key)
70
+ object.presigned_url(:get)
71
+ end
72
+
73
+ def upload_image
74
+ s3_client = Aws::S3::Client.new
75
+ s3_client.put_object(
76
+ bucket: bucket,
77
+ key: key,
78
+ body: File.read(@file_path)
79
+ )
80
+ rescue StandardError => e
81
+ Rails.logger.error("Error uploading image to S3: #{e}")
47
82
  end
48
83
  end
49
84
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module ResponseBuilders
5
+ # The AmazonText class is responsible for parsing and building a structured response from a raw text response.
6
+ # It converts the HTTP response for an Amazon Titan model into a structured format to make it easier to access
7
+ # the response data.
8
+ class AmazonText
9
+ # Initializes a new instance of the Text class.
10
+ #
11
+ # @param response [Object] The raw response object with is an HTTP response.
12
+ def initialize(response, _options = {})
13
+ @response = response
14
+ end
15
+
16
+ # Builds and returns a structured representation of the raw text response.
17
+ # This method parses the raw response body as JSON and symbolizes the names
18
+ # for easier access in Ruby.
19
+ #
20
+ # @return [Hash] A hash representing the structured data from the response body.
21
+ # The keys of the hash are symbolized for convenient access.
22
+ def build
23
+ response = JSON.parse(@response.body.read, symbolize_names: true)
24
+
25
+ {
26
+ text: response[:results]&.first&.dig(:outputText),
27
+ full_response: response
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module ResponseBuilders
5
+ # The Anthropic class is responsible for parsing and building a structured response from a raw text response.
6
+ # It converts the HTTP response for an Anthropic model into a structured format to make it easier to access
7
+ # the response data.
8
+ class Anthropic
9
+ # Initializes a new instance of the Text class.
10
+ #
11
+ # @param response [Object] The raw response object with is an HTTP response.
12
+ def initialize(response, _options = {})
13
+ @response = response
14
+ end
15
+
16
+ # Builds and returns a structured representation of the raw text response.
17
+ # This method parses the raw response body as JSON and symbolizes the names
18
+ # for easier access in Ruby.
19
+ #
20
+ # @return [Hash] A hash representing the structured data from the response body.
21
+ # The keys of the hash are symbolized for convenient access.
22
+ def build
23
+ response = JSON.parse(@response.body.read, symbolize_names: true)
24
+
25
+ {
26
+ text: response[:completion],
27
+ full_response: response
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module ResponseBuilders
5
+ # The CohereCommand class is responsible for parsing and building a structured response from a raw text response.
6
+ # It converts the HTTP response for an Cohere Command model into a structured format to make it easier to access
7
+ # the response data.
8
+ class CohereCommand
9
+ # Initializes a new instance of the Text class.
10
+ #
11
+ # @param response [Object] The raw response object with is an HTTP response.
12
+ def initialize(response, _options = {})
13
+ @response = response
14
+ end
15
+
16
+ # Builds and returns a structured representation of the raw text response.
17
+ # This method parses the raw response body as JSON and symbolizes the names
18
+ # for easier access in Ruby.
19
+ #
20
+ # @return [Hash] A hash representing the structured data from the response body.
21
+ # The keys of the hash are symbolized for convenient access.
22
+ def build
23
+ response = JSON.parse(@response.body.read, symbolize_names: true)
24
+
25
+ {
26
+ text: response[:generations]&.first&.[](:text),
27
+ full_response: response
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end