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.
- checksums.yaml +4 -4
- data/.circleci/config.yml +0 -2
- data/.env.sample +5 -0
- data/.gitignore +2 -2
- data/CHANGELOG.md +12 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +55 -41
- data/README.md +19 -11
- data/lib/amazon_bedrock/version.rb +1 -1
- data/lib/amazon_bedrock.rb +2 -1
- data/lib/bedrock_runtime/client.rb +13 -8
- 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/payload_builders/meta/llama370b_instruct_v1.rb +21 -0
- data/lib/bedrock_runtime/payload_builders/meta/llama38b_instruct_v1.rb +21 -0
- data/lib/bedrock_runtime/payload_factory.rb +4 -0
- 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 +44 -16
- data/ruby-amazon-bedrock.gemspec +2 -0
- data/spec/bedrock_runtime/client_spec.rb +4 -1
- 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/payload_builders/meta/llama370b_instruct_v1_spec.rb +26 -0
- data/spec/bedrock_runtime/payload_builders/meta/llama38b_instruct_v1_spec.rb +26 -0
- data/spec/bedrock_runtime/payload_factory_spec.rb +3 -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/cassettes/models/meta_llama3-70b-instruct-v1_0.yml +73 -0
- data/spec/cassettes/models/meta_llama3-8b-instruct-v1_0.yml +71 -0
- data/spec/support/helpers.rb +1 -3
- metadata +48 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06f4b8ff875c2d3d8b104c026621108f24e2ea60dd4ef07e85b4874b346b95af
|
4
|
+
data.tar.gz: 5fae3ad77abbab1f45ce187ade385da0ab0d3a64d7a8c38e22283a08ed23078b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd0bd9610262b0222d9680386cfcd46b070582c8f0053737020c35509504a31c2cac4e94668debed36fc9f3f5c4f45396f0082c57314277572124d24ce2bffb4
|
7
|
+
data.tar.gz: 44c080409f214e186841ff78a073a84749bb84cc978d2d206045f0a143b9533bf652356da17e4405bbdf3ae59337a0752f762af02ec2125ee1e84750a1c394a9
|
data/.circleci/config.yml
CHANGED
data/.env.sample
ADDED
data/.gitignore
CHANGED
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
data/Gemfile.lock
CHANGED
@@ -1,96 +1,110 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-amazon-bedrock (0.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.
|
11
|
-
public_suffix (>= 2.0.2, <
|
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.
|
15
|
-
aws-sdk-bedrockruntime (1.
|
16
|
-
aws-sdk-core (~> 3, >= 3.
|
17
|
-
aws-sigv4 (~> 1.
|
18
|
-
aws-sdk-core (3.
|
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-
|
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.
|
40
|
+
crack (1.0.0)
|
41
|
+
bigdecimal
|
30
42
|
rexml
|
31
|
-
diff-lcs (1.5.
|
43
|
+
diff-lcs (1.5.1)
|
32
44
|
dotenv (2.8.1)
|
33
|
-
hashdiff (1.
|
45
|
+
hashdiff (1.1.1)
|
34
46
|
jmespath (1.6.2)
|
35
|
-
json (2.
|
47
|
+
json (2.7.2)
|
36
48
|
language_server-protocol (3.17.0.3)
|
37
|
-
method_source (1.
|
38
|
-
parallel (1.
|
39
|
-
parser (3.
|
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.
|
46
|
-
racc (1.
|
57
|
+
public_suffix (5.1.1)
|
58
|
+
racc (1.8.1)
|
47
59
|
rainbow (3.1.1)
|
48
|
-
rake (13.1
|
49
|
-
regexp_parser (2.
|
50
|
-
rexml (3.
|
51
|
-
rspec (3.
|
52
|
-
rspec-core (~> 3.
|
53
|
-
rspec-expectations (~> 3.
|
54
|
-
rspec-mocks (~> 3.
|
55
|
-
rspec-core (3.
|
56
|
-
rspec-support (~> 3.
|
57
|
-
rspec-expectations (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.
|
60
|
-
rspec-mocks (3.
|
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.
|
63
|
-
rspec-support (3.
|
64
|
-
rubocop (1.
|
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.
|
80
|
+
parser (>= 3.3.0.2)
|
69
81
|
rainbow (>= 2.2.2, < 4.0)
|
70
|
-
regexp_parser (>=
|
82
|
+
regexp_parser (>= 2.4, < 3.0)
|
71
83
|
rexml (>= 3.2.5, < 4.0)
|
72
|
-
rubocop-ast (>= 1.
|
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.
|
76
|
-
parser (>= 3.
|
87
|
+
rubocop-ast (1.32.0)
|
88
|
+
parser (>= 3.3.1.0)
|
77
89
|
ruby-progressbar (1.13.0)
|
78
|
-
thor (1.3.
|
90
|
+
thor (1.3.1)
|
79
91
|
unicode-display_width (2.5.0)
|
80
92
|
vcr (6.2.0)
|
81
|
-
webmock (3.
|
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.
|
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.
|
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.
|
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
|
-
|
62
|
+
secret_access_key: "AWS_SECRET_ACCESS_KEY"
|
63
63
|
)
|
64
64
|
```
|
65
65
|
|
66
|
-
|
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.
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
-
|
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=>
|
data/lib/amazon_bedrock.rb
CHANGED
@@ -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 =
|
21
|
-
|
22
|
-
|
23
|
-
|
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(
|
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
|
-
|
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,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
|
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
|