Class: RubyAmazonBedrock::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bedrock_runtime/client.rb

Overview

Client for interfacing with the Amazon Bedrock Runtime.

This class provides methods to initialize a client for AWS BedrockRuntime and to invoke a model using the client.

Instance Method Summary collapse

Constructor Details

#initialize(region: nil, access_key_id: nil, secret_access_key: nil, profile: nil) ⇒ Client

Note:

The AWS credentials and region are fetched from the environment variables.

Initializes the AWS BedrockRuntime client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bedrock_runtime/client.rb', line 17

def initialize(region: nil, access_key_id: nil, secret_access_key: nil, profile: nil)
  config = RubyAmazonBedrock.configuration || RubyAmazonBedrock::Configuration.new

  @client = if profile
              Aws::BedrockRuntime::Client.new(
                profile: profile
              )
            else
              Aws::BedrockRuntime::Client.new(
                region: region || config.region,
                access_key_id: access_key_id || config.access_key_id,
                secret_access_key: secret_access_key || config.secret_access_key
              )
            end
end

Instance Method Details

#invoke_model(id:, prompt:, options: {}) ⇒ Aws::BedrockRuntime::Types::InvokeModelOutput

Invokes a model using the Bedrock Runtime client.

)

Examples:

Invoke a model with specific ID and input

client = RubyAmazonBedrock::Client.new
client.invoke_model(
id: 'model_id', prompt: 'This is what you want to generate', options: { option_key: 'option_value' }

Parameters:

  • id (String)

    The ID of the model to be invoked.

  • prompt (String)

    The prompt string for what needs to be generated.

  • options (Hash) (defaults to: {})

    Additional options for the model invocation.

Returns:

  • (Aws::BedrockRuntime::Types::InvokeModelOutput)

    The output from invoking the model.



44
45
46
47
48
49
50
# File 'lib/bedrock_runtime/client.rb', line 44

def invoke_model(id:, prompt:, options: {})
  payload_builder_class = RubyAmazonBedrock::PayloadFactory.new(id, prompt, options).create
  response = @client.invoke_model(payload_builder_class.build)

  response_builder_class = RubyAmazonBedrock::ResponseFactory.new(id, response, options).create
  response_builder_class.build
end