ruby-amazon-bedrock 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +68 -29
- data/assets/model-access.png +0 -0
- data/assets/orca.jpg +0 -0
- data/assets/ruby.jpg +0 -0
- data/bin/console +1 -1
- data/lib/amazon_bedrock/version.rb +5 -0
- data/lib/{bedrock.rb → amazon_bedrock.rb} +1 -1
- data/lib/bedrock_runtime/client.rb +7 -2
- data/lib/bedrock_runtime/payload_builders/base.rb +6 -0
- data/lib/bedrock_runtime/payload_builders/stability_ai/base.rb +4 -0
- data/lib/bedrock_runtime/response_builders/image.rb +46 -0
- data/lib/bedrock_runtime/response_builders/text.rb +26 -0
- data/lib/bedrock_runtime/response_factory.rb +37 -0
- data/ruby-amazon-bedrock.gemspec +2 -2
- data/sig/ruby_amazon_bedrock.rbs +4 -0
- data/spec/amazon_bedrock_spec.rb +7 -0
- data/spec/bedrock_runtime/client_spec.rb +1 -2
- data/spec/bedrock_runtime/payload_factory_spec.rb +1 -3
- data/spec/bedrock_runtime/response_builders/image_spec.rb +40 -0
- data/spec/bedrock_runtime/response_builders/text_spec.rb +30 -0
- data/spec/bedrock_runtime/response_factory_spec.rb +35 -0
- data/spec/fixtures/sample_image.jpg +0 -0
- data/spec/spec_helper.rb +4 -4
- metadata +16 -6
- data/lib/bedrock/version.rb +0 -9
- data/sig/ruby/amazon/bedrock.rbs +0 -8
- data/spec/bedrock_spec.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cc2fda0ffade519b17acc4d278b6d4d55136c8df511a2da329dccbc3dc63bd9
|
4
|
+
data.tar.gz: c6afb32b619515b886b5cb34ed01791a62ea1fda119407d8b72ce1455d65f149
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdadc9d45b3fac52916462f5e47e62cb91e4f25f949d4af9b7fe3ecca9a7bb151bdcfb0837c2fd511115affd49845441a6a964a0175cc1748583186f053de1e2
|
7
|
+
data.tar.gz: 90b2adc4a2f8f49237803f81663ea3d4a54b0177bbd6075f6a86f8982dad6a23cac6266dc50cf92aa4902cef9241f9bf1f4ec22dbf6bf8b98269daa9d1102834
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## [0.2.0] - 2023-12-14
|
2
|
+
|
3
|
+
- Add Text and Image builders to be able to return text or save images depending on the model use case.
|
4
|
+
- Update README.md documentation to add more details about Amazon Bedrock settings and model usage.
|
5
|
+
|
1
6
|
## [0.1.1] - 2023-12-13
|
2
7
|
|
3
8
|
- Add documentation to README.md
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
# Ruby Amazon Bedrock
|
2
2
|
|
3
|
-
[![CircleCI
|
3
|
+
[![CircleCI](https://dl.circleci.com/status-badge/img/circleci/JP4R1PDyZ2Yax5GxVQoMZN/LAwrE89wMVTrgpfzaimon/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/circleci/JP4R1PDyZ2Yax5GxVQoMZN/LAwrE89wMVTrgpfzaimon/tree/main)
|
4
4
|
|
5
5
|
Seamless Integration with Amazon Bedrock API for AI-Powered Text and Image Generation in Ruby 🤖 + 💎. [Amazon Bedrock API](https://aws.amazon.com/es/bedrock/).
|
6
6
|
|
7
7
|
Amazon Bedrock is a fully managed service that makes FMs from leading AI startups and Amazon available via an API, so you can choose from a wide range of FMs to find the model that is best suited for your use case.
|
8
8
|
|
9
|
-
##
|
9
|
+
## Installation
|
10
10
|
|
11
|
-
|
11
|
+
### Bundler
|
12
|
+
|
13
|
+
Add the following line to your application's Gemfile:
|
12
14
|
|
13
15
|
```ruby
|
14
16
|
gem "ruby-amazon-bedrock"
|
@@ -20,7 +22,7 @@ And then execute:
|
|
20
22
|
$ bundle install
|
21
23
|
```
|
22
24
|
|
23
|
-
|
25
|
+
### Gem install
|
24
26
|
|
25
27
|
Or install with:
|
26
28
|
|
@@ -31,19 +33,29 @@ $ gem install ruby-amazon-bedrock
|
|
31
33
|
and require with:
|
32
34
|
|
33
35
|
```ruby
|
34
|
-
require "
|
36
|
+
require "amazon_bedrock"
|
35
37
|
```
|
36
38
|
|
37
39
|
## Credentials
|
38
40
|
|
39
41
|
In order to use Amazon Bedrock, you'll need your secure credentials just as any other AWS service. Get your keys from AWS IAM (Identity and Access Management) [https://us-east-1.console.aws.amazon.com/iam](https://us-east-1.console.aws.amazon.com/iam)
|
40
42
|
|
43
|
+
## Model access
|
44
|
+
|
45
|
+
To use Bedrock, you must [request access to Bedrock's FMs](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess). To do so, you will need to have the correct IAM Permissions. For certain models, you may first need to submit use case details before you are able to request access.
|
46
|
+
|
47
|
+
<img src="assets/model-access.png" width="500"/>
|
48
|
+
|
49
|
+
## Model pricing
|
50
|
+
|
51
|
+
Be aware that by using `ruby-amazon-bedrock` gem in conjunction with Amazon Bedrock, you may incur costs associated with the use of Amazon Bedrock services. As the user, you are solely responsible for any and all costs that arise from your use of Amazon Bedrock. You can see more details about pricing [here](https://aws.amazon.com/bedrock/pricing/)
|
52
|
+
|
41
53
|
## Quickstart
|
42
54
|
|
43
|
-
|
55
|
+
Instantiate a client by passing your AWS IAM credentials:
|
44
56
|
|
45
57
|
```ruby
|
46
|
-
client = RubyAmazonBedrock::Client.new(region: "
|
58
|
+
client = RubyAmazonBedrock::Client.new(region: "aws_region", access_key_id: "access_key_id", access_token: "access_token")
|
47
59
|
```
|
48
60
|
|
49
61
|
## Models Providers
|
@@ -78,7 +90,7 @@ Amazon Titan Foundation Models are pre-trained on large datasets, making them po
|
|
78
90
|
|
79
91
|
How to call Amazon Titan text models:
|
80
92
|
|
81
|
-
|
93
|
+
_*Titan Text G1 - Lite*_
|
82
94
|
|
83
95
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-lite-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-lite-v1)
|
84
96
|
|
@@ -98,7 +110,7 @@ client.invoke_model(id: 'amazon.titan-text-lite-v1', input: 'Generate a story ab
|
|
98
110
|
|
99
111
|
<br>
|
100
112
|
|
101
|
-
|
113
|
+
_*Titan Text G1 - Express*_
|
102
114
|
|
103
115
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-express-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-express-v1)
|
104
116
|
|
@@ -122,7 +134,7 @@ Anthropic offers the Claude family of large language models purpose built for co
|
|
122
134
|
|
123
135
|
How to call Anthropic models:
|
124
136
|
|
125
|
-
|
137
|
+
_*Claude Instant 1.2*_
|
126
138
|
|
127
139
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-instant-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-instant-v1)
|
128
140
|
|
@@ -140,7 +152,7 @@ client.invoke_model('anthropic.claude-instant-v1', 'What is a neural network?')
|
|
140
152
|
|
141
153
|
<br>
|
142
154
|
|
143
|
-
|
155
|
+
_*Claude 1.3*_
|
144
156
|
|
145
157
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v1)
|
146
158
|
|
@@ -158,7 +170,7 @@ client.invoke_model(id: 'anthropic.claude-instant-v1', input: "You will be actin
|
|
158
170
|
|
159
171
|
<br>
|
160
172
|
|
161
|
-
|
173
|
+
_*Claude 2*_
|
162
174
|
|
163
175
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v2](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v2)
|
164
176
|
|
@@ -182,7 +194,7 @@ Cohere models are text generation models for business use cases. Cohere models a
|
|
182
194
|
|
183
195
|
How to call Cohere command models:
|
184
196
|
|
185
|
-
|
197
|
+
_*Command*_
|
186
198
|
|
187
199
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-text-v14](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-text-v14)
|
188
200
|
|
@@ -203,7 +215,7 @@ client.invoke_model(id: 'cohere.command-text-v14', input: 'Generate a twit about
|
|
203
215
|
|
204
216
|
<br>
|
205
217
|
|
206
|
-
|
218
|
+
_*Command Light*_
|
207
219
|
|
208
220
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-light-text-v14](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-light-text-v14)
|
209
221
|
|
@@ -224,7 +236,7 @@ client.invoke_model(id: 'cohere.command-light-text-v14', input: 'Generate a face
|
|
224
236
|
|
225
237
|
How to call Cohere embed models:
|
226
238
|
|
227
|
-
|
239
|
+
_*Embed English*_
|
228
240
|
|
229
241
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-english-v3](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-english-v3)
|
230
242
|
|
@@ -237,7 +249,7 @@ Supports: Semantic search, retrieval-augmented generation (RAG), classification,
|
|
237
249
|
|
238
250
|
<br>
|
239
251
|
|
240
|
-
|
252
|
+
_*Embed Multilingual*_
|
241
253
|
|
242
254
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-multilingual-v3](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-multilingual-v3)
|
243
255
|
|
@@ -252,7 +264,7 @@ Meta is looking to unlock the power of large language models. Our latest version
|
|
252
264
|
|
253
265
|
How to call Meta models:
|
254
266
|
|
255
|
-
|
267
|
+
_*Llama 2 Chat 13B*_
|
256
268
|
|
257
269
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-13b-chat-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-13b-chat-v1)
|
258
270
|
|
@@ -271,7 +283,7 @@ client.invoke_model(id: 'meta.llama2-13b-chat-v1', input: 'Generate an Instagram
|
|
271
283
|
|
272
284
|
<br>
|
273
285
|
|
274
|
-
|
286
|
+
_*Llama 2 Chat 70B*_
|
275
287
|
|
276
288
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-70b-chat-v1](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-70b-chat-v1)
|
277
289
|
|
@@ -294,35 +306,62 @@ Stability AI is the world's leading open-source generative artificial intelligen
|
|
294
306
|
|
295
307
|
How to call Stability AI models:
|
296
308
|
|
297
|
-
|
309
|
+
_*SDXL 0.8*_
|
298
310
|
|
299
311
|
[https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=stability.stable-diffusion-xl-v0](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=stability.stable-diffusion-xl-v0)
|
300
312
|
|
301
313
|
Supports: image generation, image editing
|
302
314
|
|
303
315
|
```ruby
|
304
|
-
client.invoke_model(id: 'stability.stable-diffusion-xl-v0', input: 'Generate an image of
|
305
|
-
|
306
|
-
|
316
|
+
client.invoke_model(id: 'stability.stable-diffusion-xl-v0', input: 'Generate an image of an orca jumping out of the water', options: { file_path: 'path/to/your/image.jpg' })
|
317
|
+
# NOTE: If file_path is not provided the image will be saved at 'image.jpg'
|
318
|
+
|
319
|
+
# Success Response
|
320
|
+
{
|
321
|
+
result: :success,
|
322
|
+
file_path: 'path/to/your/image.jpg'
|
323
|
+
}
|
324
|
+
|
325
|
+
# Failure Response
|
326
|
+
{
|
327
|
+
result: :failure,
|
328
|
+
error: ErrorClass
|
329
|
+
}
|
307
330
|
```
|
308
331
|
|
332
|
+
Example generated image
|
333
|
+
|
334
|
+
<img src="assets/orca.jpg" width="350"/>
|
335
|
+
|
309
336
|
<br>
|
310
337
|
|
311
|
-
|
338
|
+
_*SDXL 1.0*_
|
312
339
|
|
313
340
|
```ruby
|
314
|
-
client.invoke_model(id: 'stability.stable-diffusion-xl-v1', input: 'Generate an image of a
|
341
|
+
client.invoke_model(id: 'stability.stable-diffusion-xl-v1', input: 'Generate an image of a white gold ring with a ruby on it', options: { file_path: 'path/to/your/image.jpg' })
|
342
|
+
# NOTE: If file_path is not provided the image will be saved at 'image.jpg'
|
343
|
+
|
344
|
+
# Success Response
|
345
|
+
{
|
346
|
+
result: :success,
|
347
|
+
file_path: 'path/to/your/image.jpg'
|
348
|
+
}
|
349
|
+
|
350
|
+
# Failure Response
|
351
|
+
{
|
352
|
+
result: :failure,
|
353
|
+
error: ErrorClass
|
354
|
+
}
|
315
355
|
|
316
|
-
# TODO: save the response into an image file
|
317
356
|
```
|
318
357
|
|
319
|
-
|
358
|
+
Example generated image
|
320
359
|
|
321
|
-
|
360
|
+
<img src="assets/ruby.jpg" width="350"/>
|
322
361
|
|
323
|
-
##
|
362
|
+
<!-- ## Contributing
|
324
363
|
|
325
|
-
|
364
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/AAlvAAro/ruby-amazon-bedrock>. -->
|
326
365
|
|
327
366
|
<!-- ## Code of Conduct
|
328
367
|
|
Binary file
|
data/assets/orca.jpg
ADDED
Binary file
|
data/assets/ruby.jpg
ADDED
Binary file
|
data/bin/console
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
3
4
|
require 'aws-sdk-bedrockruntime'
|
4
|
-
|
5
5
|
require 'bedrock_runtime/payload_factory'
|
6
|
+
require 'bedrock_runtime/response_factory'
|
7
|
+
require 'pry'
|
6
8
|
|
7
9
|
module RubyAmazonBedrock
|
8
10
|
# Client for interfacing with the Amazon Bedrock Runtime.
|
@@ -35,7 +37,10 @@ module RubyAmazonBedrock
|
|
35
37
|
def invoke_model(id:, input:, options: {})
|
36
38
|
payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, input, options).create
|
37
39
|
response = @client.invoke_model(payload_builder_class.build)
|
38
|
-
|
40
|
+
|
41
|
+
response_builder_class = RubyAmazonBedrock::ResponseFactory.new(payload_builder_class.type, response,
|
42
|
+
options).create
|
43
|
+
response_builder_class.build
|
39
44
|
end
|
40
45
|
end
|
41
46
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
module RubyAmazonBedrock
|
6
|
+
module ResponseBuilders
|
7
|
+
# The Image class is responsible for handling and processing image data received in a response.
|
8
|
+
# 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
|
+
#
|
12
|
+
# @param response [Object] The raw response object which is an HTTP response.
|
13
|
+
# The response should contain the image data in Base64 format.
|
14
|
+
# @param options [Hash] Optional parameters, currently supporting :file_path for specifying
|
15
|
+
# the location and name of the file to save the image. Default is 'image.jpg'.
|
16
|
+
def initialize(response, options = {})
|
17
|
+
@response = response
|
18
|
+
@file_path = options[:file_path] || 'image.jpg'
|
19
|
+
end
|
20
|
+
|
21
|
+
# Processes the response to extract and decode image data, then saves it as a file.
|
22
|
+
# This method parses the response body as JSON, extracts the first artifact which is
|
23
|
+
# expected to be an image in Base64 format, decodes it, and writes it to a file.
|
24
|
+
# If the operation is successful, a hash with a success status and file path is returned.
|
25
|
+
# If an error occurs, a hash with a failure status and error details is returned.
|
26
|
+
#
|
27
|
+
# @return [Hash] A hash indicating the result of the operation.
|
28
|
+
# If successful, the hash includes :result set to :success and :file_path.
|
29
|
+
# If failure, the hash includes :result set to :failure and :error with exception details.
|
30
|
+
def build
|
31
|
+
response_object = JSON.parse(@response.body.read, symbolize_names: true)
|
32
|
+
image_data = Base64.decode64(response_object[:artifacts].first[:base64])
|
33
|
+
File.binwrite(@file_path, image_data)
|
34
|
+
{
|
35
|
+
result: :success,
|
36
|
+
file_path: @file_path
|
37
|
+
}
|
38
|
+
rescue StandardError => e
|
39
|
+
{
|
40
|
+
result: :failure,
|
41
|
+
error: e
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RubyAmazonBedrock
|
4
|
+
module ResponseBuilders
|
5
|
+
# The Text class is responsible for parsing and building a structured response from a raw text response.
|
6
|
+
# It converts the HTTP response into a structured format to make it easier to access the data.
|
7
|
+
class Text
|
8
|
+
# Initializes a new instance of the Text class.
|
9
|
+
#
|
10
|
+
# @param response [Object] The raw response object with is an HTTP response.
|
11
|
+
def initialize(response)
|
12
|
+
@response = response
|
13
|
+
end
|
14
|
+
|
15
|
+
# Builds and returns a structured representation of the raw text response.
|
16
|
+
# This method parses the raw response body as JSON and symbolizes the names
|
17
|
+
# for easier access in Ruby.
|
18
|
+
#
|
19
|
+
# @return [Hash] A hash representing the structured data from the response body.
|
20
|
+
# The keys of the hash are symbolized for convenient access.
|
21
|
+
def build
|
22
|
+
JSON.parse(@response.body.read, symbolize_names: true)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'response_builders/image'
|
4
|
+
require_relative 'response_builders/text'
|
5
|
+
|
6
|
+
module RubyAmazonBedrock
|
7
|
+
# The ResponseFactory class is a factory for creating different types of response builder objects.
|
8
|
+
# It is designed to instantiate and return either a Text or Image response builder based on the type specified.
|
9
|
+
class ResponseFactory
|
10
|
+
# Initializes a new instance of the ResponseFactory class.
|
11
|
+
#
|
12
|
+
# @param type [Symbol] The type of response builder to create (:text or :image).
|
13
|
+
# @param response [Object] The raw response object, typically an HTTP response.
|
14
|
+
# @param optios [Hash] Additional options for the response builder.
|
15
|
+
def initialize(type, response, options = {})
|
16
|
+
@type = type
|
17
|
+
@response = response
|
18
|
+
@options = options
|
19
|
+
end
|
20
|
+
|
21
|
+
# Creates and returns an instance of the specified response builder.
|
22
|
+
# This method uses the @type instance variable to determine which type of response
|
23
|
+
# builder to instantiate and return.
|
24
|
+
#
|
25
|
+
# @return [ResponseBuilders::Text, ResponseBuilders::Image] Depending on the type,
|
26
|
+
# it returns an instance of either ResponseBuilders::Text or ResponseBuilders::Image.
|
27
|
+
# Returns nil if the type does not match any known response builders.
|
28
|
+
def create
|
29
|
+
case @type
|
30
|
+
when :text
|
31
|
+
ResponseBuilders::Text.new(@response)
|
32
|
+
when :image
|
33
|
+
ResponseBuilders::Image.new(@response, @options)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/ruby-amazon-bedrock.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "lib/
|
3
|
+
require_relative "lib/amazon_bedrock/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "ruby-amazon-bedrock"
|
7
|
-
spec.version =
|
7
|
+
spec.version = RubyAmazonBedrock::VERSION
|
8
8
|
spec.authors = ["Alvaro Delgado"]
|
9
9
|
spec.email = ["hola@alvarodelgado.dev"]
|
10
10
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'bedrock_runtime/response_builders/image'
|
5
|
+
|
6
|
+
RSpec.describe RubyAmazonBedrock::ResponseBuilders::Image do
|
7
|
+
let(:base64_image) { Base64.encode64(File.binread('spec/fixtures/sample_image.jpg')) }
|
8
|
+
let(:response_body) { { artifacts: [{ base64: base64_image }] }.to_json }
|
9
|
+
let(:response) { double('response', body: StringIO.new(response_body)) }
|
10
|
+
let(:file_path) { 'spec/tmp/image.jpg' }
|
11
|
+
let(:options) { { file_path: file_path } }
|
12
|
+
|
13
|
+
after do
|
14
|
+
FileUtils.rm_f(file_path)
|
15
|
+
end
|
16
|
+
|
17
|
+
xcontext 'when the process is successful' do
|
18
|
+
# Skiped temporarily because it's causing CI to fail. Might need
|
19
|
+
# to mock the file system.
|
20
|
+
subject { described_class.new(response, options) }
|
21
|
+
|
22
|
+
it 'saves the image and returns success' do
|
23
|
+
result = subject.build
|
24
|
+
expect(result[:result]).to eq(:success)
|
25
|
+
expect(result[:file_path]).to eq(file_path)
|
26
|
+
expect(File.exist?(file_path)).to be true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when there is a failure in processing' do
|
31
|
+
let(:bad_response) { double('response', body: StringIO.new('invalid json')) }
|
32
|
+
subject { described_class.new(bad_response, options) }
|
33
|
+
|
34
|
+
it 'returns failure and includes error details' do
|
35
|
+
result = subject.build
|
36
|
+
expect(result[:result]).to eq(:failure)
|
37
|
+
expect(result[:error]).to be_a(StandardError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'bedrock_runtime/response_builders/text'
|
5
|
+
|
6
|
+
RSpec.describe RubyAmazonBedrock::ResponseBuilders::Text do
|
7
|
+
let(:response_body) { { key1: 'value1', key2: 'value2' }.to_json }
|
8
|
+
let(:response) { double('response', body: StringIO.new(response_body)) }
|
9
|
+
|
10
|
+
context 'when parsing a valid JSON response' do
|
11
|
+
subject { described_class.new(response) }
|
12
|
+
|
13
|
+
it 'converts the response into a structured hash with symbolized keys' do
|
14
|
+
result = subject.build
|
15
|
+
expect(result).to be_a(Hash)
|
16
|
+
expect(result.keys).to all(be_a(Symbol))
|
17
|
+
expect(result[:key1]).to eq('value1')
|
18
|
+
expect(result[:key2]).to eq('value2')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when handling an invalid JSON response' do
|
23
|
+
let(:invalid_response) { double('response', body: StringIO.new('invalid json')) }
|
24
|
+
subject { described_class.new(invalid_response) }
|
25
|
+
|
26
|
+
it 'raises a JSON::ParserError' do
|
27
|
+
expect { subject.build }.to raise_error(JSON::ParserError)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'bedrock_runtime/response_factory'
|
5
|
+
require 'bedrock_runtime/response_builders/text'
|
6
|
+
require 'bedrock_runtime/response_builders/image'
|
7
|
+
|
8
|
+
RSpec.describe RubyAmazonBedrock::ResponseFactory do
|
9
|
+
let(:response) { double('response') }
|
10
|
+
let(:options) { { some_option: 'value' } }
|
11
|
+
|
12
|
+
context 'when type is :text' do
|
13
|
+
subject { described_class.new(:text, response) }
|
14
|
+
|
15
|
+
it 'creates a Text response builder' do
|
16
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Text)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when type is :image' do
|
21
|
+
subject { described_class.new(:image, response, options) }
|
22
|
+
|
23
|
+
it 'creates an Image response builder' do
|
24
|
+
expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Image)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when type is invalid' do
|
29
|
+
subject { described_class.new(:invalid_type, response) }
|
30
|
+
|
31
|
+
it 'returns nil' do
|
32
|
+
expect(subject.create).to be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
Binary file
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'bedrock'
|
4
|
-
require 'bedrock_runtime/client'
|
5
|
-
require 'bedrock_runtime/payload_factory'
|
6
|
-
|
7
3
|
require 'dotenv/load'
|
8
4
|
require 'simplecov'
|
9
5
|
require 'vcr'
|
10
6
|
|
7
|
+
require 'amazon_bedrock'
|
8
|
+
require 'bedrock_runtime/client'
|
9
|
+
require 'bedrock_runtime/payload_factory'
|
10
|
+
|
11
11
|
SimpleCov.start
|
12
12
|
|
13
13
|
RSpec.configure do |config|
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alvaro Delgado
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-bedrockruntime
|
@@ -47,10 +47,13 @@ files:
|
|
47
47
|
- LICENSE.txt
|
48
48
|
- README.md
|
49
49
|
- Rakefile
|
50
|
+
- assets/model-access.png
|
51
|
+
- assets/orca.jpg
|
52
|
+
- assets/ruby.jpg
|
50
53
|
- bin/console
|
51
54
|
- bin/setup
|
52
|
-
- lib/
|
53
|
-
- lib/
|
55
|
+
- lib/amazon_bedrock.rb
|
56
|
+
- lib/amazon_bedrock/version.rb
|
54
57
|
- lib/bedrock_runtime/client.rb
|
55
58
|
- lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb
|
56
59
|
- lib/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1.rb
|
@@ -77,8 +80,12 @@ files:
|
|
77
80
|
- lib/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0.rb
|
78
81
|
- lib/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1.rb
|
79
82
|
- lib/bedrock_runtime/payload_factory.rb
|
83
|
+
- lib/bedrock_runtime/response_builders/image.rb
|
84
|
+
- lib/bedrock_runtime/response_builders/text.rb
|
85
|
+
- lib/bedrock_runtime/response_factory.rb
|
80
86
|
- ruby-amazon-bedrock.gemspec
|
81
|
-
- sig/
|
87
|
+
- sig/ruby_amazon_bedrock.rbs
|
88
|
+
- spec/amazon_bedrock_spec.rb
|
82
89
|
- spec/bedrock_runtime/client_spec.rb
|
83
90
|
- spec/bedrock_runtime/payload_builders/ai_21_labs/base_spec.rb
|
84
91
|
- spec/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1_spec.rb
|
@@ -105,7 +112,9 @@ files:
|
|
105
112
|
- spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0_spec.rb
|
106
113
|
- spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1_spec.rb
|
107
114
|
- spec/bedrock_runtime/payload_factory_spec.rb
|
108
|
-
- spec/
|
115
|
+
- spec/bedrock_runtime/response_builders/image_spec.rb
|
116
|
+
- spec/bedrock_runtime/response_builders/text_spec.rb
|
117
|
+
- spec/bedrock_runtime/response_factory_spec.rb
|
109
118
|
- spec/cassettes/models/amazon_titan-text-express-v1.yml
|
110
119
|
- spec/cassettes/models/amazon_titan-text-lite-v1.yml
|
111
120
|
- spec/cassettes/models/anthropic_claude-instant-v1.yml
|
@@ -121,6 +130,7 @@ files:
|
|
121
130
|
- spec/cassettes/models/meta_llama2-70b-chat-v1.yml
|
122
131
|
- spec/cassettes/models/stability_stable-diffusion-xl-v0.yml
|
123
132
|
- spec/cassettes/models/stability_stable-diffusion-xl-v1.yml
|
133
|
+
- spec/fixtures/sample_image.jpg
|
124
134
|
- spec/spec_helper.rb
|
125
135
|
homepage: https://github.com/AAlvAAro/ruby-amazon-bedrock
|
126
136
|
licenses:
|
data/lib/bedrock/version.rb
DELETED
data/sig/ruby/amazon/bedrock.rbs
DELETED