Sha256: 1da34bf873df6da613bc4912dc8242a0fe28d211ff3d7be162f55fd0f3b2e09b

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module OmniAI
  class Chat
    # An abstract class that provides a consistent interface for processing chat requests.
    #
    # Usage:
    #
    #    class OmniAI::OpenAI::ChatGPT::Completion < OmniAI::Chat::Completion
    #      module Model
    #        CHAT = "davinci"
    #      end
    #      def completion(messages, model:, temperature: 0.0, format: :text)
    #      end
    #    end
    #
    # Once defined, the subclass can be used to interface with the vendor's chat API as follows:
    #
    #    client.chat.completion(messages, model: "...", temperature: 0.0, format: :text)
    class Request
      # @param client [OmniAI::Client] the client
      # @param messages [String] required
      # @param model [String] required
      # @param temperature [Float, nil] optional
      # @param stream [Proc, nil] optional
      # @param format [Symbol, nil] optional - :text or :json
      def initialize(client:, messages:, model:, temperature: nil, stream: nil, format: nil)
        @client = client
        @messages = messages
        @model = model
        @temperature = temperature
        @stream = stream
        @format = format
      end

      # @raise [ExecutionError]
      def process!
        response = request!
        raise HTTPError, response unless response.status.ok?

        parse!(response:)
      end

      protected

      # @param response [HTTP::Response]
      # @return [OmniAI::Chat::Completion::Response]
      def parse!(response:)
        raise NotImplementedError, "#{self.class.name}#parse! undefined"
      end

      # @return [Hash]
      def payload
        raise NotImplementedError, "#{self.class.name}#payload undefined"
      end

      # @return [String]
      def path
        raise NotImplementedError, "#{self.class.name}#path undefined"
      end

      # @return [HTTP::Response]
      def request!
        @client
          .connection
          .accept(:json)
          .post(path, json: payload)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniai-0.0.8 lib/omniai/chat/request.rb