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/delta.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/function.rb,
lib/omniai/chat/response.rb,
lib/omniai/chat/tool_call.rb,
lib/omniai/chat/tool_call_list.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, Delta, File, Function, Media, Message, Prompt, Response, Stream, Text, ToolCall, ToolCallError, ToolCallList, 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, **options) {|prompt| ... } ⇒ OmniAi::Chat

Parameters:

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

    optional

  • client (OmniAI::Client)

    required

  • model (String)

    required

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

    optional

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

    optional

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

    optional

  • format (:json, :text, OmniAI::Schema::Object, nil) (defaults to: nil)

    optional

  • options (Hash)

    optional (used for vendor specific options)

Yields:

  • (prompt)

    optional

Yield Parameters:

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/omniai/chat.rb', line 75

def initialize(
  prompt = nil,
  client:,
  model:,
  temperature: nil,
  stream: nil,
  tools: nil,
  format: nil,
  **options,
  &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
  @options = options || {}
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!OmniAI::Chat::Response

Returns:

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/omniai/chat.rb', line 104

def process!
  begin
    response = request!

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

    completion = parse!(response:)
  rescue OpenSSL::SSL::SSLError => e
    raise SSLError, e.message, cause: e
  end

  if @tools && completion.tool_call_list?
    spawn!(
      @prompt.dup.tap do |prompt|
        prompt.messages += completion.messages
        prompt.messages += build_tool_call_messages(completion.tool_call_list)
      end
    ).process!
  else
    completion
  end
end