Class: OmniAI::Chat
- Inherits:
-
Object
- Object
- OmniAI::Chat
- 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(, 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
- #initialize(prompt = nil, client:, model:, temperature: nil, stream: nil, tools: nil, format: nil, **options) {|prompt| ... } ⇒ OmniAi::Chat constructor
- #process! ⇒ OmniAI::Chat::Response
Constructor Details
#initialize(prompt = nil, client:, model:, temperature: nil, stream: nil, tools: nil, format: nil, **options) {|prompt| ... } ⇒ OmniAi::Chat
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, **, &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 = || {} 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
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., cause: e end if @tools && completion.tool_call_list? spawn!( @prompt.dup.tap do |prompt| prompt. += completion. prompt. += (completion.tool_call_list) end ).process! else completion end end |