Class: Ask::Providers::OpenAICompatible
- Inherits:
-
OpenAI
- Object
- Ask::Provider
- OpenAI
- Ask::Providers::OpenAICompatible
show all
- Defined in:
- lib/ask/provider/openai_compatible.rb
Overview
Single class for all OpenAI-compatible providers.
Reads its behavior from LLM::OPENAI_COMPATIBLE via the class-level
compat_config that gets set at registration time. Each registered
provider is an anonymous subclass with its own compat_config.
This eliminates one subclass file per provider — adding a new
OpenAI-compatible API is a one-line config entry.
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from OpenAI
#build_request, #chat, #embed, #format_message, #format_tools, #list_models, #parse_error, #parse_response, #parse_stream
#build_request, #format_message, #format_tools, #parse_response, #parse_stream
#each_sse_event, #init_sse_buffer
Constructor Details
Returns a new instance of OpenAICompatible.
45
46
47
48
49
|
# File 'lib/ask/provider/openai_compatible.rb', line 45
def initialize(config = {})
@compat_cfg = self.class.compat_config || {}
config = normalize_compat_config(config)
super(config)
end
|
Class Attribute Details
.compat_config ⇒ Object
Returns the value of attribute compat_config.
15
16
17
|
# File 'lib/ask/provider/openai_compatible.rb', line 15
def compat_config
@compat_config
end
|
Class Method Details
.assume_models_exist? ⇒ Boolean
40
41
42
|
# File 'lib/ask/provider/openai_compatible.rb', line 40
def assume_models_exist?
false
end
|
.capabilities ⇒ Object
21
22
23
|
# File 'lib/ask/provider/openai_compatible.rb', line 21
def capabilities
compat_config[:capabilities] || { chat: true, streaming: true, tool_calls: true }
end
|
.configuration_options ⇒ Object
25
26
27
|
# File 'lib/ask/provider/openai_compatible.rb', line 25
def configuration_options
%i[api_key base_url]
end
|
.configuration_requirements ⇒ Object
29
30
31
|
# File 'lib/ask/provider/openai_compatible.rb', line 29
def configuration_requirements
%i[api_key]
end
|
33
34
35
36
37
38
|
# File 'lib/ask/provider/openai_compatible.rb', line 33
def configured?(config)
key = config.respond_to?(:api_key) ? config.api_key : nil
key ||= ENV[compat_config[:api_key_env].to_s]
key ||= ENV[compat_config[:alternate_env].to_s] if compat_config[:alternate_env]
key.to_s.length > 0
end
|
.slug ⇒ Object
17
18
19
|
# File 'lib/ask/provider/openai_compatible.rb', line 17
def slug
compat_config[:slug].to_s
end
|
Instance Method Details
#api_base ⇒ Object
51
52
53
|
# File 'lib/ask/provider/openai_compatible.rb', line 51
def api_base
@config.base_url || @compat_cfg[:api_base] || super
end
|
65
66
67
68
69
70
71
|
# File 'lib/ask/provider/openai_compatible.rb', line 65
def format_messages(messages)
result = super
if @compat_cfg[:reasoning_content]
result.each { |fm| fm[:reasoning_content] ||= "" if fm[:role] == "assistant" && fm[:tool_calls] }
end
result
end
|
55
56
57
58
59
60
61
62
63
|
# File 'lib/ask/provider/openai_compatible.rb', line 55
def
h = { "Content-Type" => "application/json" }
key = @config.api_key
h["Authorization"] = "Bearer #{key}" if key
if ( = @compat_cfg[:extra_headers])
.each { |k, v| h[k] = v }
end
h
end
|