Class: Yorishiro::Provider::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/yorishiro/provider/open_ai.rb

Constant Summary collapse

API_URL =
"https://api.openai.com/v1/chat/completions"
SUPPORTED_MODELS =

Chat-completions models compatible with this client, which always streams and sends system messages and tools — the o1/o3 reasoning series rejects parts of that flow, so it is intentionally absent.

%w[
  gpt-4o
  gpt-4o-mini
  gpt-4-turbo
  gpt-4
  gpt-3.5-turbo
].freeze

Instance Attribute Summary

Attributes inherited from Base

#api_key, #model_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#context_budget_tokens, #debug?, #debug_log, #initialize

Constructor Details

This class inherits a constructor from Yorishiro::Provider::Base

Class Method Details

.supported_modelsObject



19
20
21
# File 'lib/yorishiro/provider/open_ai.rb', line 19

def self.supported_models
  SUPPORTED_MODELS
end

Instance Method Details

#chat(conversation, tools: []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yorishiro/provider/open_ai.rb', line 23

def chat(conversation, tools: [], &)
  uri = URI(API_URL)

  body = {
    model: @model_name,
    messages: format_messages(conversation.to_api_messages),
    stream: true,
    stream_options: { include_usage: true }
  }
  body[:tools] = format_tools(tools) unless tools.empty?

  headers = {
    "Content-Type" => "application/json",
    "Authorization" => "Bearer #{@api_key}"
  }

  @last_usage = {}
  result = post_stream(uri, headers: headers, body: body, &)
  result[:usage] = @last_usage
  result
end