Class: RubyAmazonBedrock::PayloadBuilders::Ai21Labs::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb

Overview

Builds and returns a payload hash suitable for the AI21Labs model processing. This method constructs a payload with specific parameters like ‘model_id`, `content_type`, `accept`, and a `body` that includes various AI-related settings.

Returns:

  • (Hash)

    The constructed payload containing AI model parameters and settings.

Direct Known Subclasses

J2MidV1, J2UltraV1

Instance Method Summary collapse

Methods inherited from Base

#initialize, #type

Constructor Details

This class inherits a constructor from RubyAmazonBedrock::PayloadBuilders::Base

Instance Method Details

#buildHash

Builds and returns a hash representing the payload. The method prepares the data necessary for processing by an unspecified model. This includes setting various parameters such as content type, model id, and specific attributes related to the prompt and its processing characteristics.

Returns:

  • (Hash)

    A hash containing various keys and values required to process the prompt:

    • :model_id [String] The ID of the model to which the payload is intended.

    • :content_type [String] The content type of the payload, typically ‘application/json’.

    • :accept [String] Specifies the MIME type that the method response should conform to.

    • :body [String] A JSON string containing:

      • :prompt [String] The input prompt to be processed.

      • :maxTokens [Integer] The maximum number of tokens to generate.

      • :temperature [Integer] Controls randomness in the response generation.

      • :topP [Integer] Controls the diversity of the generated text.

      • :stop_sequences [Array] An array of strings which, when encountered, will end generation.

      • :countPenalty, :presencePenalty, :frequencyPenalty [Hash] Hashes containing a scale key

    used for penalty configuration.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb', line 31

def build
  {
    model_id: model_id,
    content_type: 'application/json',
    accept: '*/*',
    body: {
      prompt: @prompt,
      maxTokens: parameters[:maxTokens],
      temperature: parameters[:temperature],
      topP: parameters[:topP],
      stopSequences: parameters[:stopSequences],
      countPenalty: { scale: parameters[:countPenalty] },
      presencePenalty: { scale: parameters[:presencePenalty] },
      frequencyPenalty: { scale: parameters[:frequencyPenalty] }
    }.to_json
  }
end

#model_idObject



49
50
51
# File 'lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb', line 49

def model_id
  # noop
end

#parametersObject

rubocop:disable Metrics/CyclomaticComplexity



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bedrock_runtime/payload_builders/ai_21_labs/base.rb', line 53

def parameters # rubocop:disable Metrics/CyclomaticComplexity
  {
    maxTokens: @options[:max_tokens] || 200,
    stopSequences: @options[:stop_sequences] || [],
    temperature: @options[:temperature] || 0,
    topP: @options[:top_p] || 1,
    countPenalty: @options[:count_penalty] || 0,
    presencePenalty: @options[:presence_penalty] || 0,
    frequencyPenalty: @options[:frequency_penalty] || 0
  }
end