Class: RubyAmazonBedrock::PayloadBuilders::Anthropic::Base

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

Overview

Builds and returns a payload hash suitable for the Anthropic 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

ClaudeInstantV1, ClaudeV1, ClaudeV2

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

Constructs and returns a payload hash for text generation requests. This method is designed to prepare a structured payload comprising various parameters that dictate how the input text should be processed by the models.

Returns:

  • (Hash)

    A structured payload hash containing:

    • :model_id [String] Identifies the model (‘anthropic.claude-2’) to process the request.

    • :content_type [String] Defines the content type of the payload (‘application/json’).

    • :accept [String] Specifies the MIME type expected in the response.

    • :body [String] A JSON string including the following details:

      • :prompt [String] The input text for the model to process.

      • :max_tokens_to_sample [Integer] The maximum number of tokens the model should generate.

      • :temperature [Float] A parameter influencing the randomness in response generation.

      • :top_k [Integer] A parameter specifying the number of highest probability vocabulary tokens to keep for

    top-k sampling.

    - :top_p [Float] Controls the diversity of the generated text (nucleus sampling).
    - :stop_sequences [Array<String>] An array containing specific sequences that signal the model to stop
    

    text generation.

    - :anthropic_version [String] Specifies the version of the underlying model or API.
    


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

def build
  {
    model_id: model_id,
    content_type: 'application/json',
    accept: '*/*',
    body: {
      prompt: "\n\nHuman: #{@prompt}\n\nAssistant:",
      max_tokens_to_sample: parameters[:max_tokens_to_sample],
      temperature: parameters[:temperature],
      top_k: parameters[:top_k],
      top_p: parameters[:top_p],
      stop_sequences: parameters[:stop_sequences],
      anthropic_version: 'bedrock-2023-05-31'
    }.to_json
  }
end

#model_idObject



47
48
49
# File 'lib/bedrock_runtime/payload_builders/anthropic/base.rb', line 47

def model_id
  # noop
end

#parametersObject



51
52
53
54
55
56
57
58
59
# File 'lib/bedrock_runtime/payload_builders/anthropic/base.rb', line 51

def parameters
  {
    max_tokens_to_sample: @options[:max_tokens] || 200,
    temperature: @options[:temperature] || 0.5,
    top_k: @options[:top_k] || 250,
    top_p: @options[:top_p] || 1,
    stop_sequences: @options[:stop_sequences] || ['\n\nHuman']
  }
end