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
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "bedrock"
6
+ require "dotenv/load"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ require "irb"
12
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby
4
+ module Amazon
5
+ module Bedrock
6
+ VERSION = "0.1.1"
7
+ end
8
+ end
9
+ end
data/lib/bedrock.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "bedrock/version"
4
+ require_relative "bedrock_runtime/client"
5
+
6
+ module RubyAmazonBedrock
7
+ # Raised when an unknown model identifier is provided to PayloadFactory.
8
+ class UnknownModelError < StandardError; end
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-bedrockruntime'
4
+
5
+ require 'bedrock_runtime/payload_factory'
6
+
7
+ module RubyAmazonBedrock
8
+ # Client for interfacing with the Amazon Bedrock Runtime.
9
+ #
10
+ # This class provides methods to initialize a client for AWS BedrockRuntime
11
+ # and to invoke a model using the client.
12
+ class Client
13
+ # Initializes the AWS BedrockRuntime client.
14
+ #
15
+ # @note The AWS credentials and region are fetched from the environment variables.
16
+ def initialize(region:, access_key_id:, secret_access_key:)
17
+ @client = Aws::BedrockRuntime::Client.new(
18
+ region: region,
19
+ access_key_id: access_key_id,
20
+ secret_access_key: secret_access_key
21
+ )
22
+ end
23
+
24
+ # Invokes a model using the Bedrock Runtime client.
25
+ #
26
+ # @param id [String] The ID of the model to be invoked.
27
+ # @param input [String] The input string for what needs to be generated.
28
+ # @param options [Hash] Additional options for the model invocation.
29
+ # @return [Aws::BedrockRuntime::Types::InvokeModelOutput] The output from invoking the model.
30
+ # @example Invoke a model with specific ID and input
31
+ # client = RubyAmazonBedrock::Client.new
32
+ # client.invoke_model(
33
+ # id: 'model_id', input: 'This is what you want to generate', options: { option_key: 'option_value' }
34
+ # )
35
+ def invoke_model(id:, input:, options: {})
36
+ payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, input, options).create
37
+ response = @client.invoke_model(payload_builder_class.build)
38
+ JSON.parse(response.body.read, symbolize_names: true)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Ai21Labs
8
+ # Builds and returns a payload hash suitable for the AI21Labs model processing.
9
+ # This method constructs a payload with specific parameters like `model_id`,
10
+ # `content_type`, `accept`, and a `body` that includes various AI-related settings.
11
+ #
12
+ # @return [Hash] The constructed payload containing AI model parameters and settings.
13
+ class Base < RubyAmazonBedrock::PayloadBuilders::Base
14
+ # Builds and returns a hash representing the payload.
15
+ # The method prepares the data necessary for processing by an unspecified model.
16
+ # This includes setting various parameters such as content type, model id, and
17
+ # specific attributes related to the prompt and its processing characteristics.
18
+ #
19
+ # @return [Hash] A hash containing various keys and values required to process the prompt:
20
+ # - :model_id [String] The ID of the model to which the payload is intended.
21
+ # - :content_type [String] The content type of the payload, typically 'application/json'.
22
+ # - :accept [String] Specifies the MIME type that the method response should conform to.
23
+ # - :body [String] A JSON string containing:
24
+ # - :prompt [String] The input prompt to be processed.
25
+ # - :maxTokens [Integer] The maximum number of tokens to generate.
26
+ # - :temperature [Integer] Controls randomness in the response generation.
27
+ # - :topP [Integer] Controls the diversity of the generated text.
28
+ # - :stop_sequences [Array] An array of strings which, when encountered, will end generation.
29
+ # - :countPenalty, :presencePenalty, :frequencyPenalty [Hash] Hashes containing a scale key
30
+ # used for penalty configuration.
31
+ def build
32
+ {
33
+ model_id: model_id,
34
+ content_type: 'application/json',
35
+ accept: '*/*',
36
+ body: {
37
+ prompt: @input,
38
+ maxTokens: 200,
39
+ temperature: 0,
40
+ topP: 250,
41
+ stop_sequences: [],
42
+ countPenalty: { scale: 0 },
43
+ presencePenalty: { scale: 0 },
44
+ frequencyPenalty: { scale: 0 }
45
+ }.to_json
46
+ }
47
+ end
48
+
49
+ def model_id
50
+ # noop
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Ai21Labs
8
+ # J2MidV1 is a subclass of Base. It provides functionalities specific to the AI21 Labs Jurassic-2 Mid model.
9
+ #
10
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=ai21.j2-mid-v1
11
+ # for more information about the AI21 Labs model.
12
+ class J2MidV1 < Base
13
+ # Returns the model ID for the AI21 Labs Jurassic-2 Mid model.
14
+ #
15
+ # @return [String] 'ai21labs.j2-mid-v1'
16
+ def model_id
17
+ 'ai21labs.j2-mid-v1'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require_relative 'base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Ai21Labs
8
+ # J2UltraV1 is a subclass of Base. It provides functionalities specific to the AI21 Labs Jurassic-2 Ultra model.
9
+ #
10
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=ai21.j2-ultra-v1
11
+ # for more information about the AI21 Labs model.
12
+ class J2UltraV1 < Base
13
+ # Returns the model ID for the AI21 Labs Jurassic-2 Ultra model.
14
+ #
15
+ # @return [String] 'ai21labs.j2-ultra-v1'
16
+ def model_id
17
+ 'ai21labs.j2-ultra-v1'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Amazon
6
+ # Builds and returns a payload hash suitable for the Amazon 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
+ # Builds and returns a hash representing the payload for a text generation request.
13
+ # This method prepares the necessary data structure for processing an input text through
14
+ # a specified model. It includes various configuration parameters for text generation.
15
+ #
16
+ # @return [Hash] The method returns a hash with the following key-value pairs:
17
+ # - :model_id [String] The identifier for the model that will process the request.
18
+ # - :content_type [String] Specifies the content type of the payload, set to 'application/json'.
19
+ # - :accept [String] Indicates the MIME type that the response should conform to.
20
+ # - :body [String] A JSON string that includes the following details:
21
+ # - :inputText [String] The input text to be processed by the model.
22
+ # - :textGenerationConfig [Hash] A hash containing configuration parameters for text generation:
23
+ # - :maxTokenCount [Integer] The maximum number of tokens to generate.
24
+ # - :stopSequences [Array<String>] An array of strings that, when encountered, will signal the end
25
+ # of generation.
26
+ # - :temperature [Float] A parameter controlling the randomness in the response generation.
27
+ # - :topP [Float] A parameter controlling the diversity of the generated text.
28
+ def build
29
+ {
30
+ model_id: model_id,
31
+ content_type: 'application/json',
32
+ accept: '*/*',
33
+ body: {
34
+ inputText: @input,
35
+ textGenerationConfig: {
36
+ maxTokenCount: 4096,
37
+ stopSequences: [],
38
+ temperature: 0,
39
+ topP: 1
40
+ }
41
+ }.to_json
42
+ }
43
+ end
44
+
45
+ def model_id
46
+ # noop
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Amazon
6
+ # TitanImageGeneratorV1 is a subclass of Base. It provides functionalities specific to the Amazon
7
+ # Titan Image Generator G1 - v1 model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-image-generator-v1
10
+ # for more information about the Amazon model.
11
+ # NOTE: This model is currently in preview release. Changes may be made to this model.
12
+ class TitanImageGeneratorV1 < RubyAmazonBedrock::PayloadBuilders::Base
13
+ def build
14
+ {
15
+ model_id: model_id,
16
+ content_type: 'application/json',
17
+ accept: 'application/json',
18
+ body: {
19
+ taskType: "TEXT_IMAGE",
20
+ textToImageParams: {
21
+ text: @input,
22
+ negativeTtext: @options[:negative_text]
23
+ },
24
+ imageGenerationConfig: {
25
+ numberOfImages: @options[:number_of_images] || 1,
26
+ quality: @options[:quality] || 'standard',
27
+ height: @options[:height] || 1024,
28
+ width: @options[:width] || 1024,
29
+ cfgScale: @options[:cfg_scale] || 8.0,
30
+ seed: @options[:seed] || 0
31
+ }
32
+ }.to_json
33
+ }
34
+ end
35
+
36
+ # Returns the model ID for the AI21 Labs Titan Image Generator G1 - v1 model.
37
+ #
38
+ # @return [String] 'amazon.titan-image-generator-v1'
39
+ def model_id
40
+ 'amazon.titan-image-generator-v1'
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 Amazon
8
+ # TitanTextExpressV1 is a subclass of Base. It provides functionalities specific to the Amazon
9
+ # Titan Text G1 - Express model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-express-v1
12
+ # for more information about the Amazon model.
13
+ class TitanTextExpressV1 < Base
14
+ # Returns the model ID for the Amazon Titan Text G1 - Express model.
15
+ #
16
+ # @return [String] 'amazon.titan-text-express-v1'
17
+ def model_id
18
+ 'amazon.titan-text-express-v1'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Amazon
6
+ # TitanTextLiteV1 is a subclass of Base. It provides functionalities specific to the Amazon
7
+ # Titan Text G1 - Lite model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-lite-v1
10
+ # for more information about the Amazon model.
11
+ class TitanTextLiteV1 < Base
12
+ def build
13
+ {
14
+ model_id: 'amazon.titan-text-lite-v1',
15
+ content_type: 'application/json',
16
+ accept: '*/*',
17
+ body: {
18
+ inputText: @input,
19
+ textGenerationConfig: {
20
+ maxTokenCount: 4096,
21
+ stopSequences: [],
22
+ temperature: 0,
23
+ topP: 1
24
+ }
25
+ }.to_json
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Anthropic
6
+ # Builds and returns a payload hash suitable for the Anthropic 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 hash for text generation requests.
13
+ # This method is designed to prepare a structured payload comprising various
14
+ # parameters that dictate how the input text should be processed by the models.
15
+ #
16
+ # @return [Hash] A structured payload hash containing:
17
+ # - :model_id [String] Identifies the model ('anthropic.claude-2') to process the request.
18
+ # - :content_type [String] Defines the content type of the payload ('application/json').
19
+ # - :accept [String] Specifies the MIME type expected in the response.
20
+ # - :body [String] A JSON string including the following details:
21
+ # - :prompt [String] The input text for the model to process.
22
+ # - :max_tokens_to_sample [Integer] The maximum number of tokens the model should generate.
23
+ # - :temperature [Float] A parameter influencing the randomness in response generation.
24
+ # - :top_k [Integer] A parameter specifying the number of highest probability vocabulary tokens to keep for
25
+ # top-k sampling.
26
+ # - :top_p [Float] Controls the diversity of the generated text (nucleus sampling).
27
+ # - :stop_sequences [Array<String>] An array containing specific sequences that signal the model to stop
28
+ # text generation.
29
+ # - :anthropic_version [String] Specifies the version of the underlying model or API.
30
+ def build
31
+ {
32
+ model_id: model_id,
33
+ content_type: 'application/json',
34
+ accept: '*/*',
35
+ body: {
36
+ prompt: "\n\nHuman: #{@input}\n\nAssistant:",
37
+ max_tokens_to_sample: 300,
38
+ temperature: 0.5,
39
+ top_k: 250,
40
+ top_p: 1,
41
+ stop_sequences: [
42
+ '\n\nHuman'
43
+ ],
44
+ anthropic_version: 'bedrock-2023-05-31'
45
+ }.to_json
46
+ }
47
+ end
48
+
49
+ def model_id
50
+ # noop
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Anthropic
6
+ # ClaudeInstantV1 is a subclass of Base. It provides functionalities specific to the Anthropic
7
+ # Claude Instant 1.2 model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-instant-v1
10
+ # for more information about the Anthropic model.
11
+ class ClaudeInstantV1 < Base
12
+ # Returns the model ID for the Anthropic Claude Instant 1.2 model.
13
+ #
14
+ # @return [String] 'amazon.titan-text-express-v1'
15
+ def model_id
16
+ 'anthropic.claude-instant-v1'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Anthropic
8
+ # ClaudeV1 is a subclass of Base. It provides functionalities specific to the Anthropic
9
+ # Claude 1.3 model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v1
12
+ # for more information about the Anthropic model.
13
+ class ClaudeV1 < Base
14
+ # Returns the model ID for the Anthropic Claude 1.3 model.
15
+ #
16
+ # @return [String] 'anthropic.claude-v1'
17
+ def model_id
18
+ 'anthropic.claude-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 Anthropic
6
+ # ClaudeV2 is a subclass of Base. It provides functionalities specific to the Anthropic
7
+ # Claude 2 model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v2
10
+ # for more information about the Anthropic model.
11
+ class ClaudeV2 < Base
12
+ # Returns the model ID for the Anthropic Claude 2 model.
13
+ #
14
+ # @return [String] 'anthropic.claude-v2'
15
+ def model_id
16
+ 'anthropic.claude-v2'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ # Base class serves as an abstract class for payload builders.
6
+ # It provides the basic structure and enforces the implementation
7
+ # of certain methods in derived classes.
8
+ class Base
9
+ # Initializes a new instance of the Base class.
10
+ # @param input [String] The input string for what needs to be generated.
11
+ # @param options [Hash] optional parameters to customize payload building.
12
+ # @option options [Any] :key Custom option key-value pairs.
13
+ def initialize(input, options = {})
14
+ @input = input
15
+ @options = options
16
+ end
17
+
18
+ # Abstract method to build the payload.
19
+ # @raise [NotImplementedError] if the subclass does not implement this method.
20
+ # @return [Hash] the constructed payload.
21
+ def build
22
+ raise NotImplementedError
23
+ end
24
+
25
+ # Abstract method to retrieve the model ID.
26
+ # @raise [NotImplementedError] if the subclass does not implement this method.
27
+ # @return [String] the Amazon Bedrock model ID.
28
+ def model_id
29
+ raise NotImplementedError
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Cohere
6
+ # Builds and returns a payload hash suitable for the Cohere command 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 CommandBase < RubyAmazonBedrock::PayloadBuilders::Base
12
+ # Builds and returns a hash representing the payload for command generation requests.
13
+ # This method prepares the data necessary for processing a command input through
14
+ # a specified AI model. It configures several parameters to control the behavior
15
+ # and output of the model in response to the command.
16
+ #
17
+ # @return [Hash] A hash containing the necessary information for command processing:
18
+ # - :model_id [String] The identifier for the model that will process the command.
19
+ # - :content_type [String] Specifies the content type of the payload, set to 'application/json'.
20
+ # - :accept [String] Indicates the MIME type that the response should conform to.
21
+ # - :body [String] A JSON string that includes the following details:
22
+ # - :prompt [String] The command input to be processed by the model.
23
+ # - :maxTokens [Integer] The maximum number of tokens the model should generate in response.
24
+ # - :temperature [Float] A parameter controlling the variability in the model's responses.
25
+ # - :return_likelihood [String] Indicates whether the response should include likelihood information.
26
+ def build
27
+ {
28
+ model_id: model_id,
29
+ content_type: 'application/json',
30
+ accept: '*/*',
31
+ body: {
32
+ prompt: "#{@input}:",
33
+ max_tokens: 100,
34
+ temperature: 0.8
35
+ # return_likelihood: 'GENERATION' NOTE: This was since it was giving an error
36
+ }.to_json
37
+ }
38
+ end
39
+
40
+ def model_id
41
+ # noop
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'command_base'
4
+
5
+ module RubyAmazonBedrock
6
+ module PayloadBuilders
7
+ module Cohere
8
+ # CommandLightTextV14 is a subclass of Base. It provides functionalities specific to the Cohere
9
+ # Command Light model.
10
+ #
11
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-light-text-v14
12
+ # for more information about the Cohere model.
13
+ class CommandLightTextV14 < CommandBase
14
+ # Returns the model ID for the Cohere Command Light model.
15
+ #
16
+ # @return [String] 'cohere.command-light-text-v14'
17
+ def model_id
18
+ 'cohere.command-light-text-v14'
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
+ # CommandTextV14 is a subclass of Base. It provides functionalities specific to the Cohere
7
+ # Command model.
8
+ #
9
+ # @see https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-text-v14
10
+ # for more information about the Cohere model.
11
+ class CommandTextV14 < CommandBase
12
+ # Returns the model ID for the Cohere Command model.
13
+ #
14
+ # @return [String] 'cohere.command-text-v14'
15
+ def model_id
16
+ 'cohere.command-text-v14'
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyAmazonBedrock
4
+ module PayloadBuilders
5
+ module Cohere
6
+ # Builds and returns a payload hash suitable for the Cohere command 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 EmbedBase < RubyAmazonBedrock::PayloadBuilders::Base
12
+ # Builds and returns a payload suitable for text embedding or search tasks.
13
+ # The method configures the necessary payload structure for processing text input,
14
+ # specifically for embedding or indexing in an AI-driven search system.
15
+ #
16
+ # @return [Hash] A hash representing the structured payload for text embedding or search:
17
+ # - :model_id [String] Identifier of the AI model that will process the embedding or search task.
18
+ # - :content_type [String] Specifies the content type of the payload, typically 'application/json'.
19
+ # - :accept [String] Indicates the MIME type the response should conform to.
20
+ # - :body [String] A JSON string that includes the following:
21
+ # - :texts [Array<String>] The text(s) to be processed, either a single string or an array of strings.
22
+ # - :input_type [String] Specifies the nature of the input, set to 'search_document' for search tasks.
23
+ def build
24
+ {
25
+ model_id: model_id,
26
+ content_type: 'application/json',
27
+ accept: '*/*',
28
+ body: {
29
+ texts: [@input],
30
+ input_type: 'search_document'
31
+ }.to_json
32
+ }
33
+ end
34
+
35
+ def model_id
36
+ # noop
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end