Class: OmniAI::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/chat.rb,
lib/omniai/chat/url.rb,
lib/omniai/chat/file.rb,
lib/omniai/chat/text.rb,
lib/omniai/chat/media.rb,
lib/omniai/chat/usage.rb,
lib/omniai/chat/choice.rb,
lib/omniai/chat/prompt.rb,
lib/omniai/chat/stream.rb,
lib/omniai/chat/content.rb,
lib/omniai/chat/message.rb,
lib/omniai/chat/payload.rb,
lib/omniai/chat/function.rb,
lib/omniai/chat/response.rb,
lib/omniai/chat/tool_call.rb,
lib/omniai/chat/message/builder.rb,
lib/omniai/chat/tool_call_result.rb,
lib/omniai/chat/tool_call_message.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, Content, File, Function, Media, Message, Payload, Prompt, Response, Stream, Text, ToolCall, ToolCallError, ToolCallMessage, ToolCallMissingError, ToolCallResult, URL, 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(prompt = nil, client:, model:, temperature: nil, stream: nil, tools: nil, format: nil) {|prompt| ... } ⇒ OmniAi::Chat

Parameters:

  • prompt (OmniAI::Chat::Prompt, String, nil) (defaults to: nil)

    optional

  • client (OmniAI::Client)

    the client

  • model (String)

    required

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

    optional

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

    optional

  • tools (Array<OmniAI::Tool>) (defaults to: nil)

    optional

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

    optional - :json

Yields:

  • (prompt)

    optional

Yield Parameters:

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/omniai/chat.rb', line 74

def initialize(prompt = nil, client:, model:, temperature: nil, stream: nil, tools: nil, format: nil, &block)
  raise ArgumentError, 'prompt or block is required' if !prompt && !block

  @prompt = prompt ? Prompt.parse(prompt) : Prompt.new
  block&.call(@prompt)

  @client = client
  @model = model
  @temperature = temperature
  @stream = stream
  @tools = tools
  @format = format
end

Class Method Details

.process!Object



58
59
60
# File 'lib/omniai/chat.rb', line 58

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

Instance Method Details

#process!Object

Raises:



89
90
91
92
93
94
95
# File 'lib/omniai/chat.rb', line 89

def process!
  response = request!

  raise HTTPError, response.flush unless response.status.ok?

  parse!(response:)
end