ruby-amazon-bedrock 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'embed_base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Cohere
8
+ # EmbedEnglishV3 is a subclass of Base. It provides functionalities specific to the Cohere
9
+ # Embed English model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-english-v3
12
+ # for more information about the Cohere model.
13
+ class EmbedEnglishV3 < EmbedBase
14
+ # Returns the model ID for the Cohere Embed English model.
15
+ #
16
+ # @return [String] 'cohere.embed-english-v3'
17
+ def model_id
18
+ 'cohere.embed-english-v3'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Cohere
6
+ # EmbedMultilingualV3 is a subclass of Base. It provides functionalities specific to the Cohere
7
+ # Embed Multilingual model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-multilingual-v3
10
+ # for more information about the Cohere model.
11
+ class EmbedMultilingualV3 < EmbedBase
12
+ # Returns the model ID for the Cohere Embed Multilingual model.
13
+ #
14
+ # @return [String] 'cohere.embed-multilingual-v3'
15
+ def model_id
16
+ 'cohere.embed-multilingual-v3'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Meta
6
+ # Builds and returns a payload hash suitable for the Meta model processing.
7
+ # This method constructs a payload with specific parameters like `model_id`,
8
+ # `content_type`, `accept`, and a `body` that includes various AI-related settings.
9
+ #
10
+ # @return [Hash] The constructed payload containing AI model parameters and settings.
11
+ class Base < RubyAmazonBedrock::PayloadBuilders::Base
12
+ # Constructs and returns a payload formatted for text generation requests.
13
+ # This method assembles the necessary data structure for processing text input through
14
+ # an AI model, with various parameters to guide the generation process.
15
+ #
16
+ # @return [Hash] A structured payload containing:
17
+ # - :model_id [String] Identifier for the AI model that will process the text generation request.
18
+ # - :content_type [String] Specifies the content type of the payload, set to 'application/json'.
19
+ # - :accept [String] Indicates the MIME type for the expected response.
20
+ # - :body [String] A JSON string encapsulating the following details:
21
+ # - :prompt [String] The input text for the model to generate content from.
22
+ # - :max_gen_len [Integer] Maximum length for the generated content, measured in tokens.
23
+ # - :temperature [Float] A parameter controlling the randomness in the generated content.
24
+ # - :top_p [Float] Nucleus sampling parameter controlling the diversity of the generated text.
25
+ def build
26
+ {
27
+ model_id: model_id,
28
+ content_type: 'application/json',
29
+ accept: '*/*',
30
+ body: {
31
+ prompt: @input,
32
+ max_gen_len: 512,
33
+ temperature: 0.2,
34
+ top_p: 0.9
35
+ }.to_json
36
+ }
37
+ end
38
+
39
+ def model_id
40
+ # noop
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Meta
8
+ # Llama213bChatV1 is a subclass of Base. It provides functionalities specific to the Meta
9
+ # Llama 2 Chat 13B model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-13b-chat-v1
12
+ # for more information about the Meta model.
13
+ class Llama213bChatV1 < Base
14
+ # Returns the model ID for the Meta Llama 2 Chat 13B model.
15
+ #
16
+ # @return [String] 'meta.llama2-13b-chat-v1'
17
+ def model_id
18
+ 'meta.llama2-13b-chat-v1'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Meta
6
+ # Llama270bChatV1 is a subclass of Base. It provides functionalities specific to the Meta
7
+ # Llama 2 Chat 70B model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-70b-chat-v1
10
+ # for more information about the Meta model.
11
+ class Llama270bChatV1 < Base
12
+ # Returns the model ID for the Meta Llama 2 Chat 70B model.
13
+ #
14
+ # @return [String] 'meta.llama2-70b-chat-v1'
15
+ def model_id
16
+ 'meta.llama2-70b-chat-v1'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module StabilityAi
6
+ # Builds and returns a payload hash suitable for the Stability AI model processing.
7
+ # This method constructs a payload with specific parameters like `model_id`,
8
+ # `content_type`, `accept`, and a `body` that includes various AI-related settings.
9
+ #
10
+ # @return [Hash] The constructed payload containing AI model parameters and settings.
11
+ class Base < RubyAmazonBedrock::PayloadBuilders::Base
12
+ # Constructs and returns a structured payload for processing by an AI model.
13
+ # This method assembles data in a format suitable for tasks requiring configurable prompts,
14
+ # such as text or image generation, with control over creativity and randomness.
15
+ #
16
+ # @return [Hash] A hash containing the necessary details for the AI model to process:
17
+ # - :model_id [String] Identifier for the AI model to which the request is directed.
18
+ # - :content_type [String] Specifies the content type of the payload, typically set to 'application/json'.
19
+ # - :accept [String] Indicates the MIME type that the response should conform to.
20
+ # - :body [String] A JSON string including:
21
+ # - :text_prompts [Array<Hash>] An array of hashes, each containing a 'text' key with a string value
22
+ # representing a prompt.
23
+ # - :cfg_scale [Integer] A parameter influencing the level of control versus freedom in content generation.
24
+ # - :seed [Integer] Seed value for deterministic outputs, enabling reproducibility of results.
25
+ # - :steps [Integer] Specifies the number of steps the model should take in generating the output.
26
+ def build
27
+ {
28
+ model_id: model_id,
29
+ content_type: 'application/json',
30
+ accept: '*/*',
31
+ body: {
32
+ text_prompts: [
33
+ { text: @input }
34
+ ],
35
+ cfg_scale: 10,
36
+ seed: 0,
37
+ steps: 50
38
+ }.to_json
39
+ }
40
+ end
41
+
42
+ def model_id
43
+ # noop
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module StabilityAi
8
+ # StableDifussionXlV0 is a subclass of Base. It provides functionalities specific to the Stability AI
9
+ # SDXL 0.8 model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=stability.stable-diffusion-xl-v0
12
+ # for more information about the Anthropic model.
13
+ class StableDiffusionXlV0 < Base
14
+ # Returns the model ID for the SDXL 0.8 model.
15
+ #
16
+ # @return [String] 'stability.stable-diffusion-xl-v0'
17
+ def model_id
18
+ 'stability.stable-diffusion-xl-v0'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module StabilityAi
8
+ # StableDifussionXlV1 is a subclass of Base. It provides functionalities specific to the Stability AI
9
+ # SDXL 1.0 model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=stability.stable-diffusion-xl-v1
12
+ # for more information about the Stability AI model.
13
+ class StableDiffusionXlV1 < Base
14
+ # Returns the model ID for the SDXL 1.0 model.
15
+ #
16
+ # @return [String] 'stability.stable-diffusion-xl-v1'
17
+ def model_id
18
+ 'stability.stable-diffusion-xl-v1'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'payload_builders/ai_21_labs/j2_mid_v1'
4
+ require_relative 'payload_builders/ai_21_labs/j2_ultra_v1'
5
+ require_relative 'payload_builders/amazon/titan_image_generator_v1'
6
+ require_relative 'payload_builders/amazon/titan_text_lite_v1'
7
+ require_relative 'payload_builders/amazon/titan_text_express_v1'
8
+ require_relative 'payload_builders/anthropic/claude_v1'
9
+ require_relative 'payload_builders/anthropic/claude_instant_v1'
10
+ require_relative 'payload_builders/anthropic/claude_v2'
11
+ require_relative 'payload_builders/cohere/command_light_text_v14'
12
+ require_relative 'payload_builders/cohere/command_text_v14'
13
+ require_relative 'payload_builders/cohere/embed_english_v3'
14
+ require_relative 'payload_builders/cohere/embed_multilingual_v3'
15
+ require_relative 'payload_builders/meta/llama213b_chat_v1'
16
+ require_relative 'payload_builders/meta/llama270b_chat_v1'
17
+ require_relative 'payload_builders/stability_ai/stable_diffusion_xl_v0'
18
+ require_relative 'payload_builders/stability_ai/stable_diffusion_xl_v1'
19
+
20
+ module RubyAmazonBedrock
21
+ # The PayloadFactory class is responsible for creating instances of payload builders
22
+ # based on the provided model identifier.
23
+ #
24
+ # Attributes:
25
+ # - model_id [String]: The identifier of the model.
26
+ # - input [String]: The input string for what needs to be generated.
27
+ # - options [Hash]: Additional options for payload creation.
28
+ #
29
+ class PayloadFactory
30
+ def initialize(model_id, input, options = {})
31
+ @model_id = model_id
32
+ @input = input
33
+ @options = options
34
+ end
35
+
36
+ # Creates a payload using the appropriate builder based on the model identifier.
37
+ #
38
+ # @return [Hash] The built payload.
39
+ # @raise [UnknownModelError] if the model identifier is not recognized.
40
+ def create
41
+ builder_class = models_to_builders[@model_id]
42
+
43
+ raise UnknownModelError, "Unknown modelId: #{@model_id}" unless builder_class
44
+
45
+ builder_class.new(@input, @options)
46
+ end
47
+
48
+ # Defines a mapping from model identifiers to their respective builder classes.
49
+ #
50
+ # @return [Hash] The mapping of model identifiers to builder classes.
51
+ def models_to_builders
52
+ {
53
+ 'ai21labs.j2-mid-v1' => PayloadBuilders::Ai21Labs::J2MidV1,
54
+ 'ai21labs.j2-ultra-v1' => PayloadBuilders::Ai21Labs::J2UltraV1,
55
+ 'amazon.titan-image-generator-v1' => PayloadBuilders::Amazon::TitanImageGeneratorV1,
56
+ 'amazon.titan-text-lite-v1' => PayloadBuilders::Amazon::TitanTextLiteV1,
57
+ 'amazon.titan-text-express-v1' => PayloadBuilders::Amazon::TitanTextExpressV1,
58
+ 'anthropic.claude-v1' => PayloadBuilders::Anthropic::ClaudeV1,
59
+ 'anthropic.claude-instant-v1' => PayloadBuilders::Anthropic::ClaudeInstantV1,
60
+ 'anthropic.claude-v2' => PayloadBuilders::Anthropic::ClaudeV2,
61
+ 'cohere.command-light-text-v14' => PayloadBuilders::Cohere::CommandLightTextV14,
62
+ 'cohere.command-text-v14' => PayloadBuilders::Cohere::CommandTextV14,
63
+ 'cohere.embed-english-v3' => PayloadBuilders::Cohere::EmbedEnglishV3,
64
+ 'cohere.embed-multilingual-v3' => PayloadBuilders::Cohere::EmbedMultilingualV3,
65
+ 'meta.llama2-13b-chat-v1' => PayloadBuilders::Meta::Llama213bChatV1,
66
+ 'meta.llama2-70b-chat-v1' => PayloadBuilders::Meta::Llama270bChatV1,
67
+ 'stability.stable-diffusion-xl-v0' => PayloadBuilders::StabilityAi::StableDiffusionXlV0,
68
+ 'stability.stable-diffusion-xl-v1' => PayloadBuilders::StabilityAi::StableDiffusionXlV1
69
+ }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bedrock/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruby-amazon-bedrock"
7
+ spec.version = Ruby::Amazon::Bedrock::VERSION
8
+ spec.authors = ["Alvaro Delgado"]
9
+ spec.email = ["hola@alvarodelgado.dev"]
10
+
11
+ spec.summary = "Seamless Integration with Amazon Bedrock for AI-Powered Text and Image Generation in Ruby."
12
+ spec.description = "The ruby-amazon-bedrock gem offers Ruby developers an efficient and user-friendly interface to \
13
+ Amazon Bedrock, a powerful library for AI-driven text and image generation. This gem simplifies the process of \
14
+ connecting to Amazon Bedrock's APIs, enabling developers to easily harness the capabilities of advanced machine \
15
+ learning models for generating high-quality text and images."
16
+ spec.homepage = "https://github.com/AAlvAAro/ruby-amazon-bedrock"
17
+ spec.license = "MIT"
18
+ spec.required_ruby_version = ">= 2.6.0"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/AAlvAAro/ruby-amazon-bedrock"
22
+ spec.metadata["changelog_uri"] = "https://github.com/AAlvAAro/ruby-amazon-bedrock/blob/main/CHANGELOG.md"
23
+ spec.metadata["rubygems_mfa_required"] = "true"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(__dir__) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ (File.expand_path(f) == __FILE__) ||
30
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
31
+ f.end_with?(*%w[.gem])
32
+ end
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "aws-sdk-bedrockruntime", "~> 1.0"
39
+ end
@@ -0,0 +1,8 @@
1
+ module Ruby
2
+ module Amazon
3
+ module Bedrock
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bedrock_runtime/client'
4
+ require 'spec_helper'
5
+ require 'webmock/rspec'
6
+
7
+ RSpec.describe RubyAmazonBedrock::Client do
8
+ let(:client) do
9
+ described_class.new(
10
+ region: ENV.fetch('AWS_REGION', nil),
11
+ access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID', nil),
12
+ secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)
13
+ )
14
+ end
15
+
16
+ describe '#invoke_model' do
17
+ models = [
18
+ # 'amazon.titan-image-generator-v1', This model is currently in preview release. Changes may be made to this model.
19
+ 'amazon.titan-text-lite-v1',
20
+ 'amazon.titan-text-express-v1',
21
+ 'anthropic.claude-instant-v1',
22
+ 'anthropic.claude-v1',
23
+ 'anthropic.claude-v2',
24
+ 'cohere.command-light-text-v14',
25
+ 'cohere.command-text-v14',
26
+ 'cohere.embed-english-v3',
27
+ 'cohere.embed-multilingual-v3',
28
+ 'meta.llama2-13b-chat-v1',
29
+ 'meta.llama2-70b-chat-v1',
30
+ 'stability.stable-diffusion-xl-v0',
31
+ 'stability.stable-diffusion-xl-v1'
32
+ ]
33
+
34
+ models.each do |model|
35
+ it "invokes #{model} model with the given payload" do
36
+ VCR.use_cassette("models/#{model}") do
37
+ response = client.invoke_model(id: model, input: 'Give me a short list of steps for creating a Ruby gem', options: {})
38
+ expect(response).to be_a(Hash)
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'when the model is not found' do
44
+ it 'raises an UnkownModelError' do
45
+ VCR.use_cassette('models/not_found') do
46
+ expect { client.invoke_model(id: 'unknown-model', input: 'Hello World', options: {}) }.to raise_error(RubyAmazonBedrock::UnknownModelError)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/ai_21_labs/base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::Base do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: input,
12
+ maxTokens: 200,
13
+ temperature: 0,
14
+ topP: 250,
15
+ stop_sequences: [],
16
+ countPenalty: { scale: 0 },
17
+ presencePenalty: { scale: 0 },
18
+ frequencyPenalty: { scale: 0 }
19
+ }.to_json
20
+ end
21
+
22
+ describe '#build' do
23
+ it 'returns a hash with the expected structure' do
24
+ payload_builder = described_class.new(input, options)
25
+ payload = payload_builder.build
26
+
27
+ expect(payload[:content_type]).to eq('application/json')
28
+ expect(payload[:accept]).to eq('*/*')
29
+ expect(payload[:body]).to eq(body)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/ai_21_labs/j2_mid_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2MidV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: input,
12
+ maxTokens: 200,
13
+ temperature: 0,
14
+ topP: 250,
15
+ stop_sequences: [],
16
+ countPenalty: { scale: 0 },
17
+ presencePenalty: { scale: 0 },
18
+ frequencyPenalty: { scale: 0 }
19
+ }.to_json
20
+ end
21
+
22
+ describe '#build' do
23
+ it 'returns a hash with the expected structure' do
24
+ payload_builder = described_class.new(input, options)
25
+ payload = payload_builder.build
26
+
27
+ expect(payload[:model_id]).to eq('ai21labs.j2-mid-v1')
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,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/ai_21_labs/j2_ultra_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Ai21Labs::J2UltraV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ prompt: input,
12
+ maxTokens: 200,
13
+ temperature: 0,
14
+ topP: 250,
15
+ stop_sequences: [],
16
+ countPenalty: { scale: 0 },
17
+ presencePenalty: { scale: 0 },
18
+ frequencyPenalty: { scale: 0 }
19
+ }.to_json
20
+ end
21
+
22
+ describe '#build' do
23
+ it 'returns a hash with the expected structure' do
24
+ payload_builder = described_class.new(input, options)
25
+ payload = payload_builder.build
26
+
27
+ expect(payload[:model_id]).to eq('ai21labs.j2-ultra-v1')
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,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/amazon/base'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Amazon::Base do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ inputText: input,
12
+ textGenerationConfig: {
13
+ maxTokenCount: 4096,
14
+ stopSequences: [],
15
+ temperature: 0,
16
+ topP: 1
17
+ }
18
+ }.to_json
19
+ end
20
+
21
+ describe '#build' do
22
+ it 'returns a hash with the expected structure' do
23
+ payload_builder = described_class.new(input, options)
24
+ payload = payload_builder.build
25
+
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,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/amazon/titan_image_generator_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Amazon::TitanImageGeneratorV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ taskType: "TEXT_IMAGE",
12
+ textToImageParams: {
13
+ text: input,
14
+ negativeTtext: nil
15
+ },
16
+ imageGenerationConfig: {
17
+ numberOfImages: 1,
18
+ quality: 'standard',
19
+ height: 1024,
20
+ width: 1024,
21
+ cfgScale: 8.0,
22
+ seed: 0
23
+ }
24
+ }.to_json
25
+ end
26
+
27
+ describe '#build' do
28
+ it 'returns a hash with the expected structure' do
29
+ payload_builder = described_class.new(input, options)
30
+ payload = payload_builder.build
31
+
32
+ expect(payload[:model_id]).to eq('amazon.titan-image-generator-v1')
33
+ expect(payload[:content_type]).to eq('application/json')
34
+ expect(payload[:accept]).to eq('application/json')
35
+ expect(payload[:body]).to eq(body)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/amazon/titan_text_express_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Amazon::TitanTextExpressV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ inputText: input,
12
+ textGenerationConfig: {
13
+ maxTokenCount: 4096,
14
+ stopSequences: [],
15
+ temperature: 0,
16
+ topP: 1
17
+ }
18
+ }.to_json
19
+ end
20
+
21
+ describe '#build' do
22
+ it 'returns a hash with the expected structure' do
23
+ payload_builder = described_class.new(input, options)
24
+ payload = payload_builder.build
25
+
26
+ expect(payload[:model_id]).to eq('amazon.titan-text-express-v1')
27
+ expect(payload[:content_type]).to eq('application/json')
28
+ expect(payload[:accept]).to eq('*/*')
29
+ expect(payload[:body]).to eq(body)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'bedrock_runtime/payload_builders/amazon/titan_text_lite_v1'
5
+
6
+ RSpec.describe RubyAmazonBedrock::PayloadBuilders::Amazon::TitanTextLiteV1 do
7
+ let(:input) { 'example_input' }
8
+ let(:options) { { key: 'value' } }
9
+ let(:body) do
10
+ {
11
+ inputText: input,
12
+ textGenerationConfig: {
13
+ maxTokenCount: 4096,
14
+ stopSequences: [],
15
+ temperature: 0,
16
+ topP: 1
17
+ }
18
+ }.to_json
19
+ end
20
+
21
+ describe '#build' do
22
+ it 'returns a hash with the expected structure' do
23
+ payload_builder = described_class.new(input, options)
24
+ payload = payload_builder.build
25
+
26
+ expect(payload[:model_id]).to eq('amazon.titan-text-lite-v1')
27
+ expect(payload[:content_type]).to eq('application/json')
28
+ expect(payload[:accept]).to eq('*/*')
29
+ expect(payload[:body]).to eq(body)
30
+ end
31
+ end
32
+ end