ruby-amazon-bedrock 0.1.1

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 (89) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +57 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +28 -0
  6. data/CHANGELOG.md +9 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/CONTRIBUTING.md +3 -0
  9. data/Gemfile +16 -0
  10. data/Gemfile.lock +109 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +329 -0
  13. data/Rakefile +12 -0
  14. data/bin/console +12 -0
  15. data/bin/setup +8 -0
  16. data/lib/bedrock/version.rb +9 -0
  17. data/lib/bedrock.rb +10 -0
  18. data/lib/bedrock_runtime/client.rb +41 -0
  19. data/lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb +55 -0
  20. data/lib/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1.rb +22 -0
  21. data/lib/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1.rb +22 -0
  22. data/lib/bedrock_runtime/payload_builders/amazon/base.rb +51 -0
  23. data/lib/bedrock_runtime/payload_builders/amazon/titan_image_generator_v1.rb +45 -0
  24. data/lib/bedrock_runtime/payload_builders/amazon/titan_text_express_v1.rb +23 -0
  25. data/lib/bedrock_runtime/payload_builders/amazon/titan_text_lite_v1.rb +31 -0
  26. data/lib/bedrock_runtime/payload_builders/anthropic/base.rb +55 -0
  27. data/lib/bedrock_runtime/payload_builders/anthropic/claude_instant_v1.rb +21 -0
  28. data/lib/bedrock_runtime/payload_builders/anthropic/claude_v1.rb +23 -0
  29. data/lib/bedrock_runtime/payload_builders/anthropic/claude_v2.rb +21 -0
  30. data/lib/bedrock_runtime/payload_builders/base.rb +33 -0
  31. data/lib/bedrock_runtime/payload_builders/cohere/command_base.rb +46 -0
  32. data/lib/bedrock_runtime/payload_builders/cohere/command_light_text_v14.rb +23 -0
  33. data/lib/bedrock_runtime/payload_builders/cohere/command_text_v14.rb +21 -0
  34. data/lib/bedrock_runtime/payload_builders/cohere/embed_base.rb +41 -0
  35. data/lib/bedrock_runtime/payload_builders/cohere/embed_english_v3.rb +23 -0
  36. data/lib/bedrock_runtime/payload_builders/cohere/embed_multilingual_v3.rb +21 -0
  37. data/lib/bedrock_runtime/payload_builders/meta/base.rb +45 -0
  38. data/lib/bedrock_runtime/payload_builders/meta/llama213b_chat_v1.rb +23 -0
  39. data/lib/bedrock_runtime/payload_builders/meta/llama270b_chat_v1.rb +21 -0
  40. data/lib/bedrock_runtime/payload_builders/stability_ai/base.rb +48 -0
  41. data/lib/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0.rb +23 -0
  42. data/lib/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1.rb +23 -0
  43. data/lib/bedrock_runtime/payload_factory.rb +72 -0
  44. data/ruby-amazon-bedrock.gemspec +39 -0
  45. data/sig/ruby/amazon/bedrock.rbs +8 -0
  46. data/spec/bedrock_runtime/client_spec.rb +51 -0
  47. data/spec/bedrock_runtime/payload_builders/ai_21_labs/base_spec.rb +32 -0
  48. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1_spec.rb +33 -0
  49. data/spec/bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1_spec.rb +33 -0
  50. data/spec/bedrock_runtime/payload_builders/amazon/base_spec.rb +31 -0
  51. data/spec/bedrock_runtime/payload_builders/amazon/titan_image_generator_v1_spec.rb +38 -0
  52. data/spec/bedrock_runtime/payload_builders/amazon/titan_text_express_v1_spec.rb +32 -0
  53. data/spec/bedrock_runtime/payload_builders/amazon/titan_text_lite_v1_spec.rb +32 -0
  54. data/spec/bedrock_runtime/payload_builders/anthropic/base_spec.rb +33 -0
  55. data/spec/bedrock_runtime/payload_builders/anthropic/claude_instant_v1_spec.rb +34 -0
  56. data/spec/bedrock_runtime/payload_builders/anthropic/claude_v1_spec.rb +34 -0
  57. data/spec/bedrock_runtime/payload_builders/anthropic/claude_v2_spec.rb +34 -0
  58. data/spec/bedrock_runtime/payload_builders/base_spec.rb +34 -0
  59. data/spec/bedrock_runtime/payload_builders/cohere/command_base_spec.rb +27 -0
  60. data/spec/bedrock_runtime/payload_builders/cohere/command_light_text_v14_spec.rb +28 -0
  61. data/spec/bedrock_runtime/payload_builders/cohere/command_text_v14_spec.rb +28 -0
  62. data/spec/bedrock_runtime/payload_builders/cohere/embed_base_spec.rb +26 -0
  63. data/spec/bedrock_runtime/payload_builders/cohere/embed_english_v3_spec.rb +27 -0
  64. data/spec/bedrock_runtime/payload_builders/cohere/embed_multilingual_v3_spec.rb +27 -0
  65. data/spec/bedrock_runtime/payload_builders/meta/base_spec.rb +28 -0
  66. data/spec/bedrock_runtime/payload_builders/meta/llama213b_chat_v1_spec.rb +29 -0
  67. data/spec/bedrock_runtime/payload_builders/meta/llama270b_chat_v1_spec.rb +29 -0
  68. data/spec/bedrock_runtime/payload_builders/stability_ai/base_spec.rb +30 -0
  69. data/spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0_spec.rb +31 -0
  70. data/spec/bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1_spec.rb +31 -0
  71. data/spec/bedrock_runtime/payload_factory_spec.rb +56 -0
  72. data/spec/bedrock_spec.rb +7 -0
  73. data/spec/cassettes/models/amazon_titan-text-express-v1.yml +59 -0
  74. data/spec/cassettes/models/amazon_titan-text-lite-v1.yml +54 -0
  75. data/spec/cassettes/models/anthropic_claude-instant-v1.yml +62 -0
  76. data/spec/cassettes/models/anthropic_claude-v1.yml +61 -0
  77. data/spec/cassettes/models/anthropic_claude-v2.yml +59 -0
  78. data/spec/cassettes/models/cohere_command-light-text-v14.yml +54 -0
  79. data/spec/cassettes/models/cohere_command-text-v14.yml +55 -0
  80. data/spec/cassettes/models/cohere_embed-english-v3.yml +47 -0
  81. data/spec/cassettes/models/cohere_embed-multilingual-v3.yml +47 -0
  82. data/spec/cassettes/models/errors/bad_request.yml +45 -0
  83. data/spec/cassettes/models/errors/not_found.yml +45 -0
  84. data/spec/cassettes/models/meta_llama2-13b-chat-v1.yml +73 -0
  85. data/spec/cassettes/models/meta_llama2-70b-chat-v1.yml +55 -0
  86. data/spec/cassettes/models/stability_stable-diffusion-xl-v0.yml +51 -0
  87. data/spec/cassettes/models/stability_stable-diffusion-xl-v1.yml +45 -0
  88. data/spec/spec_helper.rb +29 -0
  89. metadata +153 -0
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/anthropic/base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Anthropic::Base do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "\n\nHuman: #{input}\n\nAssistant:",
12
+ max_tokens_to_sample: 300,
13
+ temperature: 0.5,
14
+ top_k: 250,
15
+ top_p: 1,
16
+ stop_sequences: [
17
+ '\n\nHuman'
18
+ ],
19
+ anthropic_version: 'bedrock-2023-05-31'
20
+ }.to_json
21
+ end
22
+
23
+ describe '#build' do
24
+ it 'returns a hash with the expected structure' do
25
+ payload_builder = described_class.new(input, options)
26
+ payload = payload_builder.build
27
+
28
+ expect(payload[:content_type]).to eq('application/json')
29
+ expect(payload[:accept]).to eq('*/*')
30
+ expect(payload[:body]).to eq(body)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/anthropic/claude_instant_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Anthropic::ClaudeInstantV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "\n\nHuman: #{input}\n\nAssistant:",
12
+ max_tokens_to_sample: 300,
13
+ temperature: 0.5,
14
+ top_k: 250,
15
+ top_p: 1,
16
+ stop_sequences: [
17
+ '\n\nHuman'
18
+ ],
19
+ anthropic_version: 'bedrock-2023-05-31'
20
+ }.to_json
21
+ end
22
+
23
+ describe '#build' do
24
+ it 'returns a hash with the expected structure' do
25
+ payload_builder = described_class.new(input, options)
26
+ payload = payload_builder.build
27
+
28
+ expect(payload[:model_id]).to eq('anthropic.claude-instant-v1')
29
+ expect(payload[:content_type]).to eq('application/json')
30
+ expect(payload[:accept]).to eq('*/*')
31
+ expect(payload[:body]).to eq(body)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/anthropic/claude_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Anthropic::ClaudeV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "\n\nHuman: #{input}\n\nAssistant:",
12
+ max_tokens_to_sample: 300,
13
+ temperature: 0.5,
14
+ top_k: 250,
15
+ top_p: 1,
16
+ stop_sequences: [
17
+ '\n\nHuman'
18
+ ],
19
+ anthropic_version: 'bedrock-2023-05-31'
20
+ }.to_json
21
+ end
22
+
23
+ describe '#build' do
24
+ it 'returns a hash with the expected structure' do
25
+ payload_builder = described_class.new(input, options)
26
+ payload = payload_builder.build
27
+
28
+ expect(payload[:model_id]).to eq('anthropic.claude-v1')
29
+ expect(payload[:content_type]).to eq('application/json')
30
+ expect(payload[:accept]).to eq('*/*')
31
+ expect(payload[:body]).to eq(body)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/anthropic/claude_v2'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Anthropic::ClaudeV2 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "\n\nHuman: #{input}\n\nAssistant:",
12
+ max_tokens_to_sample: 300,
13
+ temperature: 0.5,
14
+ top_k: 250,
15
+ top_p: 1,
16
+ stop_sequences: [
17
+ '\n\nHuman'
18
+ ],
19
+ anthropic_version: 'bedrock-2023-05-31'
20
+ }.to_json
21
+ end
22
+
23
+ describe '#build' do
24
+ it 'returns a hash with the expected structure' do
25
+ payload_builder = described_class.new(input, options)
26
+ payload = payload_builder.build
27
+
28
+ expect(payload[:model_id]).to eq('anthropic.claude-v2')
29
+ expect(payload[:content_type]).to eq('application/json')
30
+ expect(payload[:accept]).to eq('*/*')
31
+ expect(payload[:body]).to eq(body)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Base do
7
+ describe '#initialize' do
8
+ it 'creates a new instance with input and options' do
9
+ input = 'example_input'
10
+ options = { key: 'value' }
11
+
12
+ payload_builder = described_class.new(input, options)
13
+
14
+ expect(payload_builder.instance_variable_get(:@input)).to eq(input)
15
+ expect(payload_builder.instance_variable_get(:@options)).to eq(options)
16
+ end
17
+ end
18
+
19
+ describe '#build' do
20
+ it 'raises NotImplementedError' do
21
+ payload_builder = described_class.new('example_input')
22
+
23
+ expect { payload_builder.build }.to raise_error(NotImplementedError)
24
+ end
25
+ end
26
+
27
+ describe '#model_id' do
28
+ it 'raises NotImplementedError' do
29
+ payload_builder = described_class.new('example_input')
30
+
31
+ expect { payload_builder.model_id }.to raise_error(NotImplementedError)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/cohere/command_base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Cohere::CommandBase do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "#{input}:",
12
+ max_tokens: 100,
13
+ temperature: 0.8
14
+ }.to_json
15
+ end
16
+
17
+ describe '#build' do
18
+ it 'returns a hash with the expected structure' do
19
+ payload_builder = described_class.new(input, options)
20
+ payload = payload_builder.build
21
+
22
+ expect(payload[:content_type]).to eq('application/json')
23
+ expect(payload[:accept]).to eq('*/*')
24
+ expect(payload[:body]).to eq(body)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/cohere/command_light_text_v14'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Cohere::CommandLightTextV14 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "#{input}:",
12
+ max_tokens: 100,
13
+ temperature: 0.8
14
+ }.to_json
15
+ end
16
+
17
+ describe '#build' do
18
+ it 'returns a hash with the expected structure' do
19
+ payload_builder = described_class.new(input, options)
20
+ payload = payload_builder.build
21
+
22
+ expect(payload[:model_id]).to eq('cohere.command-light-text-v14')
23
+ expect(payload[:content_type]).to eq('application/json')
24
+ expect(payload[:accept]).to eq('*/*')
25
+ expect(payload[:body]).to eq(body)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/cohere/command_text_v14'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Cohere::CommandTextV14 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: "#{input}:",
12
+ max_tokens: 100,
13
+ temperature: 0.8
14
+ }.to_json
15
+ end
16
+
17
+ describe '#build' do
18
+ it 'returns a hash with the expected structure' do
19
+ payload_builder = described_class.new(input, options)
20
+ payload = payload_builder.build
21
+
22
+ expect(payload[:model_id]).to eq('cohere.command-text-v14')
23
+ expect(payload[:content_type]).to eq('application/json')
24
+ expect(payload[:accept]).to eq('*/*')
25
+ expect(payload[:body]).to eq(body)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/cohere/embed_base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Cohere::EmbedBase do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ texts: [input],
12
+ input_type: 'search_document'
13
+ }.to_json
14
+ end
15
+
16
+ describe '#build' do
17
+ it 'returns a hash with the expected structure' do
18
+ payload_builder = described_class.new(input, options)
19
+ payload = payload_builder.build
20
+
21
+ expect(payload[:content_type]).to eq('application/json')
22
+ expect(payload[:accept]).to eq('*/*')
23
+ expect(payload[:body]).to eq(body)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/cohere/embed_english_v3'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Cohere::EmbedEnglishV3 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ texts: [input],
12
+ input_type: 'search_document'
13
+ }.to_json
14
+ end
15
+
16
+ describe '#build' do
17
+ it 'returns a hash with the expected structure' do
18
+ payload_builder = described_class.new(input, options)
19
+ payload = payload_builder.build
20
+
21
+ expect(payload[:model_id]).to eq('cohere.embed-english-v3')
22
+ expect(payload[:content_type]).to eq('application/json')
23
+ expect(payload[:accept]).to eq('*/*')
24
+ expect(payload[:body]).to eq(body)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/cohere/embed_multilingual_v3'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Cohere::EmbedMultilingualV3 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ texts: [input],
12
+ input_type: 'search_document'
13
+ }.to_json
14
+ end
15
+
16
+ describe '#build' do
17
+ it 'returns a hash with the expected structure' do
18
+ payload_builder = described_class.new(input, options)
19
+ payload = payload_builder.build
20
+
21
+ expect(payload[:model_id]).to eq('cohere.embed-multilingual-v3')
22
+ expect(payload[:content_type]).to eq('application/json')
23
+ expect(payload[:accept]).to eq('*/*')
24
+ expect(payload[:body]).to eq(body)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/meta/base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Meta::Base do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: input,
12
+ max_gen_len: 512,
13
+ temperature: 0.2,
14
+ top_p: 0.9
15
+ }.to_json
16
+ end
17
+
18
+ describe '#build' do
19
+ it 'returns a hash with the expected structure' do
20
+ payload_builder = described_class.new(input, options)
21
+ payload = payload_builder.build
22
+
23
+ expect(payload[:content_type]).to eq('application/json')
24
+ expect(payload[:accept]).to eq('*/*')
25
+ expect(payload[:body]).to eq(body)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/meta/llama213b_chat_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Meta::Llama213bChatV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: input,
12
+ max_gen_len: 512,
13
+ temperature: 0.2,
14
+ top_p: 0.9
15
+ }.to_json
16
+ end
17
+
18
+ describe '#build' do
19
+ it 'returns a hash with the expected structure' do
20
+ payload_builder = described_class.new(input, options)
21
+ payload = payload_builder.build
22
+
23
+ expect(payload[:model_id]).to eq('meta.llama2-13b-chat-v1')
24
+ expect(payload[:content_type]).to eq('application/json')
25
+ expect(payload[:accept]).to eq('*/*')
26
+ expect(payload[:body]).to eq(body)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/meta/llama270b_chat_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Meta::Llama270bChatV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: input,
12
+ max_gen_len: 512,
13
+ temperature: 0.2,
14
+ top_p: 0.9
15
+ }.to_json
16
+ end
17
+
18
+ describe '#build' do
19
+ it 'returns a hash with the expected structure' do
20
+ payload_builder = described_class.new(input, options)
21
+ payload = payload_builder.build
22
+
23
+ expect(payload[:model_id]).to eq('meta.llama2-70b-chat-v1')
24
+ expect(payload[:content_type]).to eq('application/json')
25
+ expect(payload[:accept]).to eq('*/*')
26
+ expect(payload[:body]).to eq(body)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/stability_ai/base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::StabilityAi::Base do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ text_prompts: [
12
+ { text: input }
13
+ ],
14
+ cfg_scale: 10,
15
+ seed: 0,
16
+ steps: 50
17
+ }.to_json
18
+ end
19
+
20
+ describe '#build' do
21
+ it 'returns a hash with the expected structure' do
22
+ payload_builder = described_class.new(input, options)
23
+ payload = payload_builder.build
24
+
25
+ expect(payload[:content_type]).to eq('application/json')
26
+ expect(payload[:accept]).to eq('*/*')
27
+ expect(payload[:body]).to eq(body)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v0'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::StabilityAi::StableDiffusionXlV0 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ text_prompts: [
12
+ { text: input }
13
+ ],
14
+ cfg_scale: 10,
15
+ seed: 0,
16
+ steps: 50
17
+ }.to_json
18
+ end
19
+
20
+ describe '#build' do
21
+ it 'returns a hash with the expected structure' do
22
+ payload_builder = described_class.new(input, options)
23
+ payload = payload_builder.build
24
+
25
+ expect(payload[:model_id]).to eq('stability.stable-diffusion-xl-v0')
26
+ expect(payload[:content_type]).to eq('application/json')
27
+ expect(payload[:accept]).to eq('*/*')
28
+ expect(payload[:body]).to eq(body)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/stability_ai/stable_diffusion_xl_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::StabilityAi::StableDiffusionXlV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ text_prompts: [
12
+ { text: input }
13
+ ],
14
+ cfg_scale: 10,
15
+ seed: 0,
16
+ steps: 50
17
+ }.to_json
18
+ end
19
+
20
+ describe '#build' do
21
+ it 'returns a hash with the expected structure' do
22
+ payload_builder = described_class.new(input, options)
23
+ payload = payload_builder.build
24
+
25
+ expect(payload[:model_id]).to eq('stability.stable-diffusion-xl-v1')
26
+ expect(payload[:content_type]).to eq('application/json')
27
+ expect(payload[:accept]).to eq('*/*')
28
+ expect(payload[:body]).to eq(body)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bedrock_runtime/payload_factory'
4
+ require 'spec_helper'
5
+
6
+ require 'pry'
7
+
8
+ RSpec.describe RubyAmazonBedrock::PayloadFactory do
9
+ let(:bedrock_models) do
10
+ ["ai21labs.j2-mid-v1",
11
+ "ai21labs.j2-ultra-v1",
12
+ "amazon.titan-image-generator-v1",
13
+ "amazon.titan-text-lite-v1",
14
+ "amazon.titan-text-express-v1",
15
+ "anthropic.claude-v1",
16
+ "anthropic.claude-instant-v1",
17
+ "anthropic.claude-v2",
18
+ "cohere.command-light-text-v14",
19
+ "cohere.command-text-v14",
20
+ "cohere.embed-english-v3",
21
+ "cohere.embed-multilingual-v3",
22
+ "meta.llama2-13b-chat-v1",
23
+ "meta.llama2-70b-chat-v1",
24
+ "stability.stable-diffusion-xl-v0",
25
+ "stability.stable-diffusion-xl-v1"]
26
+ end
27
+ let(:models_mapping) { described_class.new(nil, nil).models_to_builders }
28
+
29
+ describe '#create' do
30
+ context 'when model_id is a valid Bedrock model' do
31
+ it 'creates a payload using the appropriate builder' do
32
+ model_id = bedrock_models.sample
33
+ input = 'test input'
34
+ builder = described_class.new(model_id, input).create
35
+
36
+ expect(builder).to be_a(models_mapping[model_id])
37
+ end
38
+ end
39
+
40
+ context 'when model_id is not a valid Bedrock model' do
41
+ it 'raises an UnknownModelError' do
42
+ model_id = 'unknown_model'
43
+ input = 'test input'
44
+ payload_factory = described_class.new(model_id, input)
45
+
46
+ expect { payload_factory.create }.to raise_error(RubyAmazonBedrock::UnknownModelError, "Unknown modelId: #{model_id}")
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#models_to_builders' do
52
+ it 'defines a mapping of model identifiers to builder classes' do
53
+ expect(models_mapping.keys).to eq(bedrock_models)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Ruby::Amazon::Bedrock do
4
+ it "has a version number" do
5
+ expect(Ruby::Amazon::Bedrock::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,59 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-text-express-v1/invoke
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"inputText":"Give me a short list of steps for creating a Ruby gem","textGenerationConfig":{"maxTokenCount":4096,"stopSequences":[],"temperature":0,"topP":1}}'
9
+ headers:
10
+ Accept-Encoding:
11
+ - ''
12
+ Content-Type:
13
+ - application/json
14
+ Accept:
15
+ - "*/*"
16
+ User-Agent:
17
+ - aws-sdk-ruby3/3.189.0 ua/2.0 api/bedrock_runtime#1.4.0 os/macos#22 md/arm64
18
+ lang/ruby#3.2.2 md/3.2.2 cfg/retry-mode#legacy
19
+ Host:
20
+ - bedrock-runtime.us-east-1.amazonaws.com
21
+ Content-Length:
22
+ - '159'
23
+ response:
24
+ status:
25
+ code: 200
26
+ message: OK
27
+ headers:
28
+ Date:
29
+ - Wed, 06 Dec 2023 05:48:40 GMT
30
+ Content-Type:
31
+ - application/json
32
+ Content-Length:
33
+ - '936'
34
+ Connection:
35
+ - keep-alive
36
+ X-Amzn-Requestid:
37
+ - bfa4ea73-8745-4e6f-857c-cb568c567f0c
38
+ X-Amzn-Bedrock-Invocation-Latency:
39
+ - '7087'
40
+ X-Amzn-Bedrock-Output-Token-Count:
41
+ - '234'
42
+ X-Amzn-Bedrock-Input-Token-Count:
43
+ - '12'
44
+ body:
45
+ encoding: UTF-8
46
+ string: '{"inputTextTokenCount":12,"results":[{"tokenCount":234,"outputText":"\nHere
47
+ is a short list of steps for creating a Ruby gem:\n1. Create a new directory
48
+ for your gem.\n2. Initialize a new Ruby gem using the ''gem'' command.\n3.
49
+ Add a ''Gemfile'' to your gem directory.\n4. Write the code for your gem in
50
+ the ''lib/your_gem_name'' directory.\n5. Add a ''Rakefile'' to your gem directory.\n6.
51
+ Use the ''gem build'' command to build your gem.\n7. Use the ''gem install''
52
+ command to install your gem on your system.\n8. Publish your gem to the RubyGems
53
+ repository using the ''gem push'' command.\n9. Use the ''gem'' command to
54
+ install your gem from the RubyGems repository.\n10. Test your gem using the
55
+ ''rake'' command.\n11. Document your gem using the ''rdoc'' command.\n12.
56
+ Release a new version of your gem using the ''gem release'' command.\n13.
57
+ Update your gem with new features and bug fixes using the ''gem update'' command.","completionReason":"FINISH"}]}'
58
+ recorded_at: Wed, 06 Dec 2023 05:48:40 GMT
59
+ recorded_with: VCR 6.2.0