Sha256: cfb6e9a7b9379794498c3e1b2084311812bbc3390c0d7a6c928cea45d5b7a588

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'base64'
require 'aws-sdk-bedrockruntime'
require 'bedrock_runtime/payload_factory'
require 'bedrock_runtime/response_factory'

module RubyAmazonBedrock
  # 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.
  class Client
    # Initializes the AWS BedrockRuntime client.
    #
    # @note The AWS credentials and region are fetched from the environment variables.
    def initialize(region: nil, access_key_id: nil, secret_access_key: nil)
      config = RubyAmazonBedrock.configuration || RubyAmazonBedrock::Configuration.new

      @client = 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

    # Invokes a model using the Bedrock Runtime client.
    #
    # @param id [String] The ID of the model to be invoked.
    # @param prompt [String] The prompt string for what needs to be generated.
    # @param options [Hash] Additional options for the model invocation.
    # @return [Aws::BedrockRuntime::Types::InvokeModelOutput] The output from invoking the model.
    # @example 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' }
    # 	)
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-amazon-bedrock-0.2.3 lib/bedrock_runtime/client.rb