ruby-amazon-bedrock 0.2.2 → 0.2.3
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +9 -1
- data/README.md +2 -2
- data/lib/amazon_bedrock/version.rb +1 -1
- data/lib/bedrock_runtime/client.rb +1 -2
- data/lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb +2 -2
- data/lib/bedrock_runtime/payload_builders/amazon/base.rb +1 -1
- data/lib/bedrock_runtime/response_builders/ai_21_labs.rb +32 -0
- data/lib/bedrock_runtime/response_builders/{image.rb → amazon_image.rb} +42 -7
- data/lib/bedrock_runtime/response_builders/amazon_text.rb +32 -0
- data/lib/bedrock_runtime/response_builders/anthropic.rb +32 -0
- data/lib/bedrock_runtime/response_builders/cohere_command.rb +32 -0
- data/lib/bedrock_runtime/response_builders/cohere_embed.rb +32 -0
- data/lib/bedrock_runtime/response_builders/{text.rb → meta.rb} +11 -5
- data/lib/bedrock_runtime/response_builders/stability_ai.rb +85 -0
- data/lib/bedrock_runtime/response_factory.rb +42 -16
- data/ruby-amazon-bedrock.gemspec +1 -0
- data/spec/bedrock_runtime/payload_builders/ai_21_labs/base_spec.rb +1 -1
- data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1_spec.rb +1 -1
- data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1_spec.rb +1 -1
- data/spec/bedrock_runtime/response_builders/{text_spec.rb → ai_21_labs_spec.rb} +3 -4
- data/spec/bedrock_runtime/response_builders/{image_spec.rb → stability_ai_spec.rb} +2 -2
- data/spec/bedrock_runtime/response_factory_spec.rb +74 -14
- data/spec/support/helpers.rb +1 -3
- metadata +26 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81db5e2aa4d8b27014d6f2f24bc79083ddae2401a60a2d6d120fbc27e9facd62
|
4
|
+
data.tar.gz: f8f3ae19c0756142df41ef49e9d1d66d33f480013d375b6086cdb70e5365a3f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cd2bdd5a70da7ddd8f92dfed38cee2432b3a1800d9d774918fd967f6d623df68afef0b1e250866a90a013a0ff3f4226146cf1e9684c1e28bd8ebdd4dad444bc
|
7
|
+
data.tar.gz: e2b2f8a61ab61967e7d79ce0762002fbee9ce3fdc60086b5a6cb7f0c825caa91835fe07c632380cabcf8d0cadd237f609b104d3335af7bd9ffb08224bdf84d7f
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-amazon-bedrock (0.2.
|
4
|
+
ruby-amazon-bedrock (0.2.3)
|
5
5
|
aws-sdk-bedrockruntime (~> 1.0)
|
6
|
+
aws-sdk-s3 (~> 1.0)
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
@@ -20,6 +21,13 @@ GEM
|
|
20
21
|
aws-partitions (~> 1, >= 1.651.0)
|
21
22
|
aws-sigv4 (~> 1.8)
|
22
23
|
jmespath (~> 1, >= 1.6.1)
|
24
|
+
aws-sdk-kms (1.71.0)
|
25
|
+
aws-sdk-core (~> 3, >= 3.177.0)
|
26
|
+
aws-sigv4 (~> 1.1)
|
27
|
+
aws-sdk-s3 (1.132.0)
|
28
|
+
aws-sdk-core (~> 3, >= 3.179.0)
|
29
|
+
aws-sdk-kms (~> 1)
|
30
|
+
aws-sigv4 (~> 1.6)
|
23
31
|
aws-sigv4 (1.8.0)
|
24
32
|
aws-eventstream (~> 1, >= 1.0.2)
|
25
33
|
bundler-audit (0.9.1)
|
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.
|
17
|
+
gem "ruby-amazon-bedrock", "~> 0.2.3"
|
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.
|
31
|
+
$ gem install ruby-amazon-bedrock -v 0.2.3
|
32
32
|
```
|
33
33
|
|
34
34
|
and require with:
|
@@ -39,8 +39,7 @@ module RubyAmazonBedrock
|
|
39
39
|
payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, prompt, options).create
|
40
40
|
response = @client.invoke_model(payload_builder_class.build)
|
41
41
|
|
42
|
-
response_builder_class = RubyAmazonBedrock::ResponseFactory.new(
|
43
|
-
options).create
|
42
|
+
response_builder_class = RubyAmazonBedrock::ResponseFactory.new(id, response, options).create
|
44
43
|
response_builder_class.build
|
45
44
|
end
|
46
45
|
end
|
@@ -35,7 +35,7 @@ module RubyAmazonBedrock
|
|
35
35
|
accept: '*/*',
|
36
36
|
body: {
|
37
37
|
prompt: @prompt,
|
38
|
-
|
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
|
-
|
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
|
-
# - :
|
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,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
|
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
|
10
|
-
# Initializes a new instance of the
|
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
|
-
|
46
|
-
|
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
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyAmazonBedrock
|
4
|
+
module ResponseBuilders
|
5
|
+
# The CohereEmbed class is responsible for parsing and building a structured response from a raw text response.
|
6
|
+
# It converts the HTTP response for an Cohere Embed model into a structured format to make it easier to access
|
7
|
+
# the response data.
|
8
|
+
class CohereEmbed
|
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[:texts].first,
|
27
|
+
full_response: response
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -2,13 +2,14 @@
|
|
2
2
|
|
3
3
|
module RubyAmazonBedrock
|
4
4
|
module ResponseBuilders
|
5
|
-
# The
|
6
|
-
# It converts the HTTP response into a structured format to make it easier to access
|
7
|
-
|
5
|
+
# The Meta class is responsible for parsing and building a structured response from a raw text response.
|
6
|
+
# It converts the HTTP response for an Meta model into a structured format to make it easier to access
|
7
|
+
# the response data.
|
8
|
+
class Meta
|
8
9
|
# Initializes a new instance of the Text class.
|
9
10
|
#
|
10
11
|
# @param response [Object] The raw response object with is an HTTP response.
|
11
|
-
def initialize(response)
|
12
|
+
def initialize(response, _options = {})
|
12
13
|
@response = response
|
13
14
|
end
|
14
15
|
|
@@ -19,7 +20,12 @@ module RubyAmazonBedrock
|
|
19
20
|
# @return [Hash] A hash representing the structured data from the response body.
|
20
21
|
# The keys of the hash are symbolized for convenient access.
|
21
22
|
def build
|
22
|
-
JSON.parse(@response.body.read, symbolize_names: true)
|
23
|
+
response = JSON.parse(@response.body.read, symbolize_names: true)
|
24
|
+
|
25
|
+
{
|
26
|
+
text: response[:generation],
|
27
|
+
full_response: response
|
28
|
+
}
|
23
29
|
end
|
24
30
|
end
|
25
31
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws-sdk-s3'
|
4
|
+
require 'base64'
|
5
|
+
require 'digest'
|
6
|
+
|
7
|
+
module RubyAmazonBedrock
|
8
|
+
module ResponseBuilders
|
9
|
+
# The StabilityAi class is responsible for handling and processing image data received in a response.
|
10
|
+
# It parses the response, extracts the image data, decodes it from Base64, and saves it as a file.
|
11
|
+
class StabilityAi
|
12
|
+
# Initializes a new instance of the StabilityAi class.
|
13
|
+
#
|
14
|
+
# @param response [Object] The raw response object which is an HTTP response.
|
15
|
+
# The response should contain the image data in Base64 format.
|
16
|
+
# @param options [Hash] Optional parameters, currently supporting :file_path for specifying
|
17
|
+
# the location and name of the file to save the image. Default is 'image.jpg'.
|
18
|
+
def initialize(response, options = {})
|
19
|
+
@response = response
|
20
|
+
@file_path = options[:file_path] || 'image.jpg'
|
21
|
+
@upload = options[:upload] || false
|
22
|
+
end
|
23
|
+
|
24
|
+
# Processes the response to extract and decode image data, then saves it as a file.
|
25
|
+
# This method parses the response body as JSON, extracts the first artifact which is
|
26
|
+
# expected to be an image in Base64 format, decodes it, and writes it to a file.
|
27
|
+
# If the operation is successful, a hash with a success status and file path is returned.
|
28
|
+
# If an error occurs, a hash with a failure status and error details is returned.
|
29
|
+
#
|
30
|
+
# @return [Hash] A hash indicating the result of the operation.
|
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.
|
33
|
+
# If failure, the hash includes :result set to :failure and :error with exception details.
|
34
|
+
def build
|
35
|
+
response_object = JSON.parse(@response.body.read, symbolize_names: true)
|
36
|
+
image_data = Base64.decode64(response_object[:artifacts].first[:base64])
|
37
|
+
File.binwrite(@file_path, image_data)
|
38
|
+
|
39
|
+
upload_image if can_upload?
|
40
|
+
|
41
|
+
{
|
42
|
+
result: :success,
|
43
|
+
file_path: @file_path,
|
44
|
+
s3_url: s3_url
|
45
|
+
}.compact
|
46
|
+
rescue StandardError => e
|
47
|
+
{
|
48
|
+
result: :failure,
|
49
|
+
error: e
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
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}")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -1,19 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'response_builders/
|
4
|
-
require_relative 'response_builders/
|
3
|
+
require_relative 'response_builders/ai_21_labs'
|
4
|
+
require_relative 'response_builders/amazon_text'
|
5
|
+
require_relative 'response_builders/amazon_image'
|
6
|
+
require_relative 'response_builders/anthropic'
|
7
|
+
require_relative 'response_builders/cohere_command'
|
8
|
+
require_relative 'response_builders/cohere_embed'
|
9
|
+
require_relative 'response_builders/meta'
|
10
|
+
require_relative 'response_builders/stability_ai'
|
5
11
|
|
6
12
|
module RubyAmazonBedrock
|
7
13
|
# The ResponseFactory class is a factory for creating different types of response builder objects.
|
8
|
-
# It is designed to instantiate and return
|
14
|
+
# It is designed to instantiate and return an object of a specific response builder class
|
9
15
|
class ResponseFactory
|
10
16
|
# Initializes a new instance of the ResponseFactory class.
|
11
17
|
#
|
12
|
-
# @param
|
18
|
+
# @param model_id [String] The model_id of response builder to create (Amazon Bedrock model id).
|
13
19
|
# @param response [Object] The raw response object, typically an HTTP response.
|
14
|
-
# @param
|
15
|
-
def initialize(
|
16
|
-
@
|
20
|
+
# @param options [Object] optional attributes to customize the response.
|
21
|
+
def initialize(model_id, response, options = {})
|
22
|
+
@model_id = model_id
|
17
23
|
@response = response
|
18
24
|
@options = options
|
19
25
|
end
|
@@ -22,16 +28,36 @@ module RubyAmazonBedrock
|
|
22
28
|
# This method uses the @type instance variable to determine which type of response
|
23
29
|
# builder to instantiate and return.
|
24
30
|
#
|
25
|
-
# @return [ResponseBuilders::
|
26
|
-
# it returns an instance of
|
27
|
-
# Returns nil if the
|
31
|
+
# @return [ResponseBuilders::Class] Depending on the model_id,
|
32
|
+
# it returns an instance of any of the response builder classes.
|
33
|
+
# Returns nil if the model_id does not match any known response builders.
|
28
34
|
def create
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
+
builder_class = models_to_builders[@model_id]
|
36
|
+
builder_class.new(@response, @options)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Defines a mapping from model identifiers to their respective builder classes.
|
40
|
+
#
|
41
|
+
# @return [Hash] The mapping of model identifiers to builder classes.
|
42
|
+
def models_to_builders
|
43
|
+
{
|
44
|
+
'ai21.j2-mid-v1' => ResponseBuilders::Ai21Labs,
|
45
|
+
'ai21.j2-ultra-v1' => ResponseBuilders::Ai21Labs,
|
46
|
+
'amazon.titan-image-generator-v1' => ResponseBuilders::AmazonImage,
|
47
|
+
'amazon.titan-text-lite-v1' => ResponseBuilders::AmazonText,
|
48
|
+
'amazon.titan-text-express-v1' => ResponseBuilders::AmazonText,
|
49
|
+
'anthropic.claude-instant-v1' => ResponseBuilders::Anthropic,
|
50
|
+
'anthropic.claude-v1' => ResponseBuilders::Anthropic,
|
51
|
+
'anthropic.claude-v2' => ResponseBuilders::Anthropic,
|
52
|
+
'cohere.command-light-text-v14' => ResponseBuilders::CohereCommand,
|
53
|
+
'cohere.command-text-v14' => ResponseBuilders::CohereCommand,
|
54
|
+
'cohere.embed-english-v3' => ResponseBuilders::CohereEmbed,
|
55
|
+
'cohere.embed-multilingual-v3' => ResponseBuilders::CohereEmbed,
|
56
|
+
'meta.llama2-13b-chat-v1' => ResponseBuilders::Meta,
|
57
|
+
'meta.llama2-70b-chat-v1' => ResponseBuilders::Meta,
|
58
|
+
'stability.stable-diffusion-xl-v0' => ResponseBuilders::StabilityAi,
|
59
|
+
'stability.stable-diffusion-xl-v1' => ResponseBuilders::StabilityAi
|
60
|
+
}
|
35
61
|
end
|
36
62
|
end
|
37
63
|
end
|
data/ruby-amazon-bedrock.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
|
-
require 'bedrock_runtime/response_builders/
|
4
|
+
require 'bedrock_runtime/response_builders/ai_21_labs'
|
5
5
|
|
6
|
-
RSpec.describe RubyAmazonBedrock::ResponseBuilders::
|
6
|
+
RSpec.describe RubyAmazonBedrock::ResponseBuilders::Ai21Labs do
|
7
7
|
let(:response_body) { { key1: 'value1', key2: 'value2' }.to_json }
|
8
8
|
let(:response) { double('response', body: StringIO.new(response_body)) }
|
9
9
|
|
@@ -14,8 +14,7 @@ RSpec.describe RubyAmazonBedrock::ResponseBuilders::Text do
|
|
14
14
|
result = subject.build
|
15
15
|
expect(result).to be_a(Hash)
|
16
16
|
expect(result.keys).to all(be_a(Symbol))
|
17
|
-
expect(result[:
|
18
|
-
expect(result[:key2]).to eq('value2')
|
17
|
+
expect(result[:full_response]).to eq(JSON.parse(response_body, symbolize_names: true))
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
|
-
require 'bedrock_runtime/response_builders/
|
4
|
+
require 'bedrock_runtime/response_builders/stability_ai'
|
5
5
|
|
6
|
-
RSpec.describe RubyAmazonBedrock::ResponseBuilders::
|
6
|
+
RSpec.describe RubyAmazonBedrock::ResponseBuilders::StabilityAi do
|
7
7
|
let(:base64_image) { Base64.encode64(File.binread('spec/fixtures/sample_image.jpg')) }
|
8
8
|
let(:response_body) { { artifacts: [{ base64: base64_image }] }.to_json }
|
9
9
|
let(:response) { double('response', body: StringIO.new(response_body)) }
|
@@ -2,34 +2,94 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
require 'bedrock_runtime/response_factory'
|
5
|
-
require 'bedrock_runtime/response_builders/
|
6
|
-
require 'bedrock_runtime/response_builders/
|
5
|
+
require 'bedrock_runtime/response_builders/ai_21_labs'
|
6
|
+
require 'bedrock_runtime/response_builders/amazon_text'
|
7
|
+
require 'bedrock_runtime/response_builders/amazon_image'
|
8
|
+
require 'bedrock_runtime/response_builders/anthropic'
|
9
|
+
require 'bedrock_runtime/response_builders/cohere_command'
|
10
|
+
require 'bedrock_runtime/response_builders/cohere_embed'
|
11
|
+
require 'bedrock_runtime/response_builders/meta'
|
12
|
+
require 'bedrock_runtime/response_builders/stability_ai'
|
7
13
|
|
8
14
|
RSpec.describe RubyAmazonBedrock::ResponseFactory do
|
9
15
|
let(:response) { double('response') }
|
10
16
|
let(:options) { { some_option: 'value' } }
|
11
17
|
|
12
|
-
context 'when
|
13
|
-
|
18
|
+
context 'when model_id is from ai_21_labs' do
|
19
|
+
['ai21.j2-mid-v1', 'ai21.j2-ultra-v1'].each do |model_id|
|
20
|
+
subject { described_class.new(model_id, response) }
|
14
21
|
|
15
|
-
|
16
|
-
|
22
|
+
it 'creates an Ai21Labs response builder' do
|
23
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Ai21Labs)
|
24
|
+
end
|
17
25
|
end
|
18
26
|
end
|
19
27
|
|
20
|
-
context 'when
|
21
|
-
|
28
|
+
context 'when model_id is from Amazon text generators' do
|
29
|
+
['amazon.titan-text-lite-v1', 'amazon.titan-text-express-v1'].each do |model_id|
|
30
|
+
subject { described_class.new(model_id, response) }
|
22
31
|
|
23
|
-
|
24
|
-
|
32
|
+
it 'creates an AmazonText response builder' do
|
33
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::AmazonText)
|
34
|
+
end
|
25
35
|
end
|
26
36
|
end
|
27
37
|
|
28
|
-
context 'when
|
29
|
-
subject { described_class.new(
|
38
|
+
context 'when model_id is from Amazon image generators' do
|
39
|
+
subject { described_class.new('amazon.titan-image-generator-v1', response) }
|
30
40
|
|
31
|
-
it '
|
32
|
-
expect(subject.create).to
|
41
|
+
it 'creates an AmazonText response builder' do
|
42
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::AmazonImage)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when model_id is from Anthropic generators' do
|
47
|
+
['anthropic.claude-instant-v1', 'anthropic.claude-v1', 'anthropic.claude-v2'].each do |model_id|
|
48
|
+
subject { described_class.new(model_id, response) }
|
49
|
+
|
50
|
+
it 'creates an AmazonText response builder' do
|
51
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Anthropic)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when model_id is from Cohere commnand generators' do
|
57
|
+
['cohere.command-light-text-v14', 'cohere.command-text-v14'].each do |model_id|
|
58
|
+
subject { described_class.new(model_id, response) }
|
59
|
+
|
60
|
+
it 'creates an AmazonText response builder' do
|
61
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::CohereCommand)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when model_id is from Cohere embed generators' do
|
67
|
+
['cohere.embed-english-v3', 'cohere.embed-multilingual-v3'].each do |model_id|
|
68
|
+
subject { described_class.new(model_id, response) }
|
69
|
+
|
70
|
+
it 'creates an AmazonText response builder' do
|
71
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::CohereEmbed)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when model_id is from Meta generators' do
|
77
|
+
['meta.llama2-13b-chat-v1', 'meta.llama2-70b-chat-v1'].each do |model_id|
|
78
|
+
subject { described_class.new(model_id, response) }
|
79
|
+
|
80
|
+
it 'creates an AmazonText response builder' do
|
81
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Meta)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when model_id is from Stability AI generators' do
|
87
|
+
['stability.stable-diffusion-xl-v0', 'stability.stable-diffusion-xl-v0'].each do |model_id|
|
88
|
+
subject { described_class.new(model_id, response) }
|
89
|
+
|
90
|
+
it 'creates an AmazonText response builder' do
|
91
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::StabilityAi)
|
92
|
+
end
|
33
93
|
end
|
34
94
|
end
|
35
95
|
end
|
data/spec/support/helpers.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'pry'
|
4
|
-
|
5
3
|
RSpec.shared_examples 'a payload builder' do
|
6
4
|
it 'validates the payload structure' do
|
7
5
|
payload_builder = described_class.new(prompt, options)
|
@@ -28,7 +26,7 @@ RSpec.shared_context 'a121 labs parameters' do
|
|
28
26
|
let(:body) do
|
29
27
|
{
|
30
28
|
prompt: prompt,
|
31
|
-
|
29
|
+
maxTokens: 100,
|
32
30
|
temperature: 0.5,
|
33
31
|
topP: 2,
|
34
32
|
stopSequences: %w[stop1 stop2],
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-amazon-bedrock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alvaro Delgado
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-bedrockruntime
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk-s3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
description: The ruby-amazon-bedrock gem offers Ruby developers an efficient and user-friendly
|
28
42
|
interface to Amazon Bedrock, a powerful library for AI-driven text and image
|
29
43
|
generation. This gem simplifies the process of connecting to Amazon Bedrock's
|
@@ -80,8 +94,14 @@ files:
|
|
80
94
|
- lib/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0.rb
|
81
95
|
- lib/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1.rb
|
82
96
|
- lib/bedrock_runtime/payload_factory.rb
|
83
|
-
- lib/bedrock_runtime/response_builders/
|
84
|
-
- lib/bedrock_runtime/response_builders/
|
97
|
+
- lib/bedrock_runtime/response_builders/ai_21_labs.rb
|
98
|
+
- lib/bedrock_runtime/response_builders/amazon_image.rb
|
99
|
+
- lib/bedrock_runtime/response_builders/amazon_text.rb
|
100
|
+
- lib/bedrock_runtime/response_builders/anthropic.rb
|
101
|
+
- lib/bedrock_runtime/response_builders/cohere_command.rb
|
102
|
+
- lib/bedrock_runtime/response_builders/cohere_embed.rb
|
103
|
+
- lib/bedrock_runtime/response_builders/meta.rb
|
104
|
+
- lib/bedrock_runtime/response_builders/stability_ai.rb
|
85
105
|
- lib/bedrock_runtime/response_factory.rb
|
86
106
|
- ruby-amazon-bedrock.gemspec
|
87
107
|
- sig/ruby_amazon_bedrock.rbs
|
@@ -112,8 +132,8 @@ files:
|
|
112
132
|
- spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0_spec.rb
|
113
133
|
- spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1_spec.rb
|
114
134
|
- spec/bedrock_runtime/payload_factory_spec.rb
|
115
|
-
- spec/bedrock_runtime/response_builders/
|
116
|
-
- spec/bedrock_runtime/response_builders/
|
135
|
+
- spec/bedrock_runtime/response_builders/ai_21_labs_spec.rb
|
136
|
+
- spec/bedrock_runtime/response_builders/stability_ai_spec.rb
|
117
137
|
- spec/bedrock_runtime/response_factory_spec.rb
|
118
138
|
- spec/cassettes/models/amazon_titan-text-express-v1.yml
|
119
139
|
- spec/cassettes/models/amazon_titan-text-lite-v1.yml
|