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
@@ -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 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
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/image'
4
- require_relative 'response_builders/text'
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 either a Text or Image response builder based on the type specified.
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 type [Symbol] The type of response builder to create (:text or :image).
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 optios [Hash] Additional options for the response builder.
15
- def initialize(type, response, options = {})
16
- @type = type
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,38 @@ 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::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.
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
- case @type
30
- when :text
31
- ResponseBuilders::Text.new(@response)
32
- when :image
33
- ResponseBuilders::Image.new(@response, @options)
34
- end
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
+ 'meta.llama3-70b-instruct-v1:0' => ResponseBuilders::Meta,
59
+ 'meta.llama3-8b-instruct-v1:0' => ResponseBuilders::Meta,
60
+ 'stability.stable-diffusion-xl-v0' => ResponseBuilders::StabilityAi,
61
+ 'stability.stable-diffusion-xl-v1' => ResponseBuilders::StabilityAi
62
+ }
35
63
  end
36
64
  end
37
65
  end
@@ -36,4 +36,6 @@ Gem::Specification.new do |spec|
36
36
  spec.require_paths = ["lib"]
37
37
 
38
38
  spec.add_dependency "aws-sdk-bedrockruntime", "~> 1.0"
39
+ spec.add_dependency "aws-sdk-s3", "~> 1.0"
40
+ spec.add_dependency "base64", "~> 0.2"
39
41
  end
@@ -8,7 +8,8 @@ RSpec.describe RubyAmazonBedrock::Client do
8
8
  described_class.new(
9
9
  region: ENV.fetch('AWS_REGION', nil),
10
10
  access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID', nil),
11
- secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
11
+ secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY', nil),
12
+ profile: ENV.fetch('AWS_PROFILE', nil)
12
13
  )
13
14
  end
14
15
 
@@ -26,6 +27,8 @@ RSpec.describe RubyAmazonBedrock::Client do
26
27
  'cohere.embed-multilingual-v3',
27
28
  'meta.llama2-13b-chat-v1',
28
29
  'meta.llama2-70b-chat-v1',
30
+ 'meta.llama3-70b-instruct-v1:0',
31
+ 'meta.llama3-8b-instruct-v1:0',
29
32
  'stability.stable-diffusion-xl-v0',
30
33
  'stability.stable-diffusion-xl-v1'
31
34
  ]
@@ -9,7 +9,7 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::Base do
9
9
  let(:body) do
10
10
  {
11
11
  prompt: prompt,
12
- maxTokenCount: 200,
12
+ maxTokens: 200,
13
13
  temperature: 0,
14
14
  topP: 1,
15
15
  stopSequences: [],
@@ -9,7 +9,7 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2MidV1 do
9
9
  let(:body) do
10
10
  {
11
11
  prompt: prompt,
12
- maxTokenCount: 200,
12
+ maxTokens: 200,
13
13
  temperature: 0,
14
14
  topP: 1,
15
15
  stopSequences: [],
@@ -9,7 +9,7 @@ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2UltraV1 do
9
9
  let(:body) do
10
10
  {
11
11
  prompt: prompt,
12
- maxTokenCount: 200,
12
+ maxTokens: 200,
13
13
  temperature: 0,
14
14
  topP: 1,
15
15
  stopSequences: [],
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/meta/llama370b_instruct_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Meta::Llama370bInstructV1 do
7
+ let(:prompt) { 'example_prompt' }
8
+ let(:options) { {} }
9
+ let(:body) do
10
+ {
11
+ prompt: prompt,
12
+ max_gen_len: 512,
13
+ temperature: 0.5,
14
+ top_p: 0.9
15
+ }.to_json
16
+ end
17
+
18
+ describe '#build' do
19
+ it_should_behave_like 'a payload builder'
20
+
21
+ context 'with custom parameters' do
22
+ include_context 'meta parameters'
23
+ it_should_behave_like 'a payload builder'
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/meta/llama38b_instruct_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Meta::Llama38bInstructV1 do
7
+ let(:prompt) { 'example_prompt' }
8
+ let(:options) { {} }
9
+ let(:body) do
10
+ {
11
+ prompt: prompt,
12
+ max_gen_len: 512,
13
+ temperature: 0.5,
14
+ top_p: 0.9
15
+ }.to_json
16
+ end
17
+
18
+ describe '#build' do
19
+ it_should_behave_like 'a payload builder'
20
+
21
+ context 'with custom parameters' do
22
+ include_context 'meta parameters'
23
+ it_should_behave_like 'a payload builder'
24
+ end
25
+ end
26
+ end
@@ -19,6 +19,8 @@ RSpec.describe RubyAmazonBedrock::PayloadFactory do
19
19
  "cohere.embed-multilingual-v3",
20
20
  "meta.llama2-13b-chat-v1",
21
21
  "meta.llama2-70b-chat-v1",
22
+ "meta.llama3-70b-instruct-v1:0",
23
+ "meta.llama3-8b-instruct-v1:0",
22
24
  "stability.stable-diffusion-xl-v0",
23
25
  "stability.stable-diffusion-xl-v1"]
24
26
  end
@@ -48,7 +50,7 @@ RSpec.describe RubyAmazonBedrock::PayloadFactory do
48
50
 
49
51
  describe '#models_to_builders' do
50
52
  it 'defines a mapping of model identifiers to builder classes' do
51
- expect(models_mapping.keys).to eq(bedrock_models)
53
+ expect(models_mapping.keys).to contain_exactly(*bedrock_models)
52
54
  end
53
55
  end
54
56
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
- require 'bedrock_runtime/response_builders/text'
4
+ require 'bedrock_runtime/response_builders/ai_21_labs'
5
5
 
6
- RSpec.describe RubyAmazonBedrock::ResponseBuilders::Text do
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[:key1]).to eq('value1')
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/image'
4
+ require 'bedrock_runtime/response_builders/stability_ai'
5
5
 
6
- RSpec.describe RubyAmazonBedrock::ResponseBuilders::Image do
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/text'
6
- require 'bedrock_runtime/response_builders/image'
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 type is :text' do
13
- subject { described_class.new(:text, response) }
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
- it 'creates a Text response builder' do
16
- expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Text)
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 type is :image' do
21
- subject { described_class.new(:image, response, options) }
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
- it 'creates an Image response builder' do
24
- expect(subject.create).to be_a(RubyAmazonBedrock::ResponseBuilders::Image)
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 type is invalid' do
29
- subject { described_class.new(:invalid_type, response) }
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 'returns nil' do
32
- expect(subject.create).to be_nil
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
@@ -0,0 +1,73 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/meta.llama3-70b-instruct-v1:0/invoke
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"prompt":"Give me a short list of steps for creating a Ruby gem","max_gen_len":512,"temperature":0.5,"top_p":0.9}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - ''
12
+ Amz-Sdk-Invocation-Id:
13
+ - 32851ae6-1618-400a-9411-11d6d3d67636
14
+ Content-Type:
15
+ - application/json
16
+ Accept:
17
+ - "*/*"
18
+ User-Agent:
19
+ - aws-sdk-ruby3/3.201.4 ua/2.1 api/bedrock_runtime#1.17.0 os/macos#23 md/arm64
20
+ lang/ruby#3.0.0 md/3.0.0 m/D
21
+ Host:
22
+ - bedrock-runtime.us-east-1.amazonaws.com
23
+ X-Amz-Date:
24
+ - 20240917T163051Z
25
+ X-Amz-Content-Sha256:
26
+ - cf1ec2da83f5824b04de33922f5727968ac482454a9af56c2c3119c6711f2b1e
27
+ Authorization:
28
+ - AWS4-HMAC-SHA256 Credential=AKIASKU7DUX5LSIDI7FA/20240917/us-east-1/bedrock/aws4_request,
29
+ SignedHeaders=accept;amz-sdk-invocation-id;content-type;host;x-amz-content-sha256;x-amz-date,
30
+ Signature=c2858b5790f78f1428b84dace670018733257a68224824485146d1c0a695f0c6
31
+ Content-Length:
32
+ - '114'
33
+ response:
34
+ status:
35
+ code: 200
36
+ message: OK
37
+ headers:
38
+ Date:
39
+ - Tue, 17 Sep 2024 16:31:00 GMT
40
+ Content-Type:
41
+ - application/json
42
+ Content-Length:
43
+ - '1244'
44
+ Connection:
45
+ - keep-alive
46
+ X-Amzn-Requestid:
47
+ - 83c64727-212b-4d5d-a26e-681c991809d3
48
+ X-Amzn-Bedrock-Invocation-Latency:
49
+ - '7293'
50
+ X-Amzn-Bedrock-Output-Token-Count:
51
+ - '293'
52
+ X-Amzn-Bedrock-Input-Token-Count:
53
+ - '12'
54
+ body:
55
+ encoding: UTF-8
56
+ string: '{"generation":".\n\nHere is a short list of steps for creating a Ruby
57
+ gem:\n\n1. **Create a new gem**: Run `gem new my_gem` (replace \"my_gem\"
58
+ with your gem''s name) to create a new gem with the basic directory structure.\n2.
59
+ **Define your gem''s metadata**: Edit the `my_gem.gemspec` file to specify
60
+ your gem''s name, and other metadata such as version, description, and dependencies.\n3.
61
+ **Write your gem''s code**: Create a `lib` directory and add your Ruby code
62
+ to it. This is where you''ll implement the functionality of your gem.\n4.
63
+ **Test your gem**: Create a `test` directory and add test files to it. Use
64
+ a testing framework like RSpec or Minitest to write tests for your gem.\n5.
65
+ **Build and package your gem**: Run `gem build my_gem.gemspec` to build your
66
+ gem, and then `gem push my_gem-0.1.0.gem` (replace \"0.1.0\" with your gem''s
67
+ version) to push it to RubyGems.org.\n6. **Publish your gem**: Go to RubyGems.org
68
+ and create an account if you haven''t already. Then, upload your gem and make
69
+ it publicly available.\n\nThat''s it! Of course, there are many more details
70
+ to consider when creating a Ruby gem, but these steps should give you a good
71
+ starting point.","prompt_token_count":12,"generation_token_count":293,"stop_reason":"stop"}'
72
+ recorded_at: Tue, 17 Sep 2024 16:31:00 GMT
73
+ recorded_with: VCR 6.2.0
@@ -0,0 +1,71 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/meta.llama3-8b-instruct-v1:0/invoke
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"prompt":"Give me a short list of steps for creating a Ruby gem","max_gen_len":512,"temperature":0.5,"top_p":0.9}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - ''
12
+ Amz-Sdk-Invocation-Id:
13
+ - f256772e-a630-485d-b52d-e20311339125
14
+ Content-Type:
15
+ - application/json
16
+ Accept:
17
+ - "*/*"
18
+ User-Agent:
19
+ - aws-sdk-ruby3/3.201.4 ua/2.1 api/bedrock_runtime#1.17.0 os/macos#23 md/arm64
20
+ lang/ruby#3.0.0 md/3.0.0 m/D
21
+ Host:
22
+ - bedrock-runtime.us-east-1.amazonaws.com
23
+ X-Amz-Date:
24
+ - 20240917T163109Z
25
+ X-Amz-Content-Sha256:
26
+ - cf1ec2da83f5824b04de33922f5727968ac482454a9af56c2c3119c6711f2b1e
27
+ Authorization:
28
+ - AWS4-HMAC-SHA256 Credential=AKIASKU7DUX5LSIDI7FA/20240917/us-east-1/bedrock/aws4_request,
29
+ SignedHeaders=accept;amz-sdk-invocation-id;content-type;host;x-amz-content-sha256;x-amz-date,
30
+ Signature=036bf9fb08b5c0308655674b5bd9eec2eb8c347f3b2e712585be1def1c780f69
31
+ Content-Length:
32
+ - '114'
33
+ response:
34
+ status:
35
+ code: 200
36
+ message: OK
37
+ headers:
38
+ Date:
39
+ - Tue, 17 Sep 2024 16:31:13 GMT
40
+ Content-Type:
41
+ - application/json
42
+ Content-Length:
43
+ - '1170'
44
+ Connection:
45
+ - keep-alive
46
+ X-Amzn-Requestid:
47
+ - 1e88ebb3-f78f-4b57-8926-229f8d4dc625
48
+ X-Amzn-Bedrock-Invocation-Latency:
49
+ - '3536'
50
+ X-Amzn-Bedrock-Output-Token-Count:
51
+ - '275'
52
+ X-Amzn-Bedrock-Input-Token-Count:
53
+ - '12'
54
+ body:
55
+ encoding: UTF-8
56
+ string: '{"generation":".\n\nComment by Brian Hays on January 28, 2013 at 9:13am\n\nHere
57
+ are the basic steps for creating a Ruby gem:\n\n1. Create a new directory
58
+ for your gem and navigate to it in your terminal.\n2. Run `gem init` to create
59
+ a basic gem structure.\n3. Edit the `gemspec` file to add information about
60
+ your gem, such as its name, version, and dependencies.\n4. Write your gem''s
61
+ code in the `lib` directory.\n5. Run `gem build` to build your gem.\n6. Run
62
+ `gem push` to publish your gem to a gem server (such as RubyGems.org).\n\nThat''s
63
+ the basic process, but there are many other things you can do to customize
64
+ and improve your gem, such as adding tests, documentation, and a README file.\n\nComment
65
+ by Brian Hays on January 28, 2013 at 9:14am\n\nAlso, you may want to consider
66
+ using a gem template tool like Bundler''s `bundle gem` command or the `gem-skeleton`
67
+ gem to help you get started with creating a new gem. These tools can save
68
+ you some time and effort by creating a basic gem structure for you. Just run
69
+ `bundle gem my_gem` or `gem-skeleton my_gem` and you''ll be off and running!","prompt_token_count":12,"generation_token_count":275,"stop_reason":"stop"}'
70
+ recorded_at: Tue, 17 Sep 2024 16:31:13 GMT
71
+ recorded_with: VCR 6.2.0