Class: OmniAI::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/chat.rb,
lib/omniai/chat/chunk.rb,
lib/omniai/chat/delta.rb,
lib/omniai/chat/usage.rb,
lib/omniai/chat/choice.rb,
lib/omniai/chat/stream.rb,
lib/omniai/chat/content.rb,
lib/omniai/chat/message.rb,
lib/omniai/chat/completion.rb

Overview

An abstract class that provides a consistent interface for processing chat requests.

Usage:

class OmniAI::OpenAI::Chat < OmniAI::Chat
  module Model
    GPT_4O = "gpt-4o"
  end

  protected

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

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

client.chat(messages, model: "...", temperature: 0.0, format: :text)

Defined Under Namespace

Modules: Format, Role Classes: Choice, Chunk, Completion, Content, Delta, Message, Stream, Usage

Constant Summary collapse

JSON_PROMPT =
'Respond with valid JSON. Do not include any non-JSON in the response.'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages, client:, model:, temperature: nil, stream: nil, format: nil) ⇒ Chat

Returns a new instance of Chat.

Parameters:

  • messages (String)

    required

  • client (OmniAI::Client)

    the client

  • model (String)

    required

  • temperature (Float, nil) (defaults to: nil)

    optional

  • stream (Proc, nil) (defaults to: nil)

    optional

  • format (Symbol, nil) (defaults to: nil)

    optional - :json



50
51
52
53
54
55
56
57
# File 'lib/omniai/chat.rb', line 50

def initialize(messages, client:, model:, temperature: nil, stream: nil, format: nil)
  @messages = messages
  @client = client
  @model = model
  @temperature = temperature
  @stream = stream
  @format = format
end

Class Method Details

.process!Object



40
41
42
# File 'lib/omniai/chat.rb', line 40

def self.process!(...)
  new(...).process!
end

Instance Method Details

#process!Object

Raises:

  • (ExecutionError)


60
61
62
63
64
65
# File 'lib/omniai/chat.rb', line 60

def process!
  response = request!
  raise HTTPError, response.flush unless response.status.ok?

  parse!(response:)
end