Sha256: 5286b9cd62c0ed2bda51dbf6c64b3a44969e3f24d64d86308cd263a9970a7f63

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module RubyAmazonBedrock
  module ResponseBuilders
    # The CohereCommand class is responsible for parsing and building a structured response from a raw text response.
    # It converts the HTTP response for an Cohere Command model into a structured format to make it easier to access
    # the response data.
    class CohereCommand
      # Initializes a new instance of the Text class.
      #
      # @param response [Object] The raw response object with is an HTTP response.
      def initialize(response, _options = {})
        @response = response
      end

      # Builds and returns a structured representation of the raw text response.
      # This method parses the raw response body as JSON and symbolizes the names
      # for easier access in Ruby.
      #
      # @return [Hash] A hash representing the structured data from the response body.
      # The keys of the hash are symbolized for convenient access.
      def build
        response = JSON.parse(@response.body.read, symbolize_names: true)

        {
          text: response[:generations]&.first&.[](:text),
          full_response: response
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-amazon-bedrock-0.2.4 lib/bedrock_runtime/response_builders/cohere_command.rb
ruby-amazon-bedrock-0.2.3 lib/bedrock_runtime/response_builders/cohere_command.rb