llm_gateway 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +149 -64
- data/Rakefile +5 -0
- data/docs/migration_guide_0.10.0.md +53 -0
- data/docs/structured-output.md +28 -0
- data/lib/llm_gateway/adapter_definition.rb +64 -0
- data/lib/llm_gateway/adapters/adapter.rb +105 -21
- data/lib/llm_gateway/adapters/anthropic/messages_adapter.rb +8 -0
- data/lib/llm_gateway/adapters/anthropic_option_mapper.rb +29 -19
- data/lib/llm_gateway/adapters/groq/chat_completions_adapter.rb +8 -0
- data/lib/llm_gateway/adapters/groq/option_mapper.rb +7 -9
- data/lib/llm_gateway/adapters/normalized_stream_accumulator.rb +7 -3
- data/lib/llm_gateway/adapters/openai/chat_completions/option_mapper.rb +7 -9
- data/lib/llm_gateway/adapters/openai/chat_completions/stream_mapper.rb +1 -1
- data/lib/llm_gateway/adapters/openai/chat_completions_adapter.rb +6 -0
- data/lib/llm_gateway/adapters/openai/responses/option_mapper.rb +11 -9
- data/lib/llm_gateway/adapters/openai/responses/stream_mapper.rb +1 -1
- data/lib/llm_gateway/adapters/openai/responses_adapter.rb +6 -0
- data/lib/llm_gateway/adapters/openai_codex/responses_adapter.rb +8 -0
- data/lib/llm_gateway/adapters/stream_mapper.rb +4 -3
- data/lib/llm_gateway/agents/harness.rb +63 -29
- data/lib/llm_gateway/agents/in_memory_session_manager.rb +35 -4
- data/lib/llm_gateway/agents/tools/tool_utils.rb +2 -2
- data/lib/llm_gateway/errors.rb +3 -0
- data/lib/llm_gateway/models/catalog.rb +68 -0
- data/lib/llm_gateway/models/cost_calculator.rb +47 -0
- data/lib/llm_gateway/models/definition.rb +325 -0
- data/lib/llm_gateway/models/explicit.rb +46 -0
- data/lib/llm_gateway/models/generated.json +3055 -0
- data/lib/llm_gateway/models.rb +78 -0
- data/lib/llm_gateway/prompt.rb +13 -13
- data/lib/llm_gateway/proxy/adapter.rb +22 -12
- data/lib/llm_gateway/proxy/client.rb +4 -4
- data/lib/llm_gateway/proxy/protocol.rb +50 -0
- data/lib/llm_gateway/proxy/server.rb +12 -9
- data/lib/llm_gateway/utils.rb +12 -6
- data/lib/llm_gateway/version.rb +1 -1
- data/lib/llm_gateway.rb +4 -67
- data/scripts/generate_model_catalog.rb +195 -0
- metadata +13 -4
- data/lib/llm_gateway/client.rb +0 -35
- data/lib/llm_gateway/provider_registry.rb +0 -37
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
require_relative "models/definition"
|
|
6
|
+
require_relative "models/catalog"
|
|
7
|
+
require_relative "models/cost_calculator"
|
|
8
|
+
require_relative "models/explicit"
|
|
9
|
+
|
|
10
|
+
module LlmGateway
|
|
11
|
+
module Models
|
|
12
|
+
class << self
|
|
13
|
+
def catalog
|
|
14
|
+
@catalog ||= begin
|
|
15
|
+
result = Catalog.new(definitions)
|
|
16
|
+
registered_definitions.each_value { |definition| result.register(definition, replace: true) }
|
|
17
|
+
result
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def find(reference = nil, provider: nil, id: nil)
|
|
22
|
+
catalog.find(reference, provider:, id:)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def fetch(reference = nil, provider: nil, id: nil)
|
|
26
|
+
catalog.fetch(reference, provider:, id:)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def all(provider: nil)
|
|
30
|
+
catalog.all(provider:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def register(definition = nil, replace: false, **attributes)
|
|
34
|
+
if definition && attributes.any?
|
|
35
|
+
raise ArgumentError, "Pass either a model definition or definition attributes, not both"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
definition ||= Definition.new(**{ source: :user }.merge(attributes))
|
|
39
|
+
unless definition.is_a?(Definition)
|
|
40
|
+
raise ArgumentError, "Expected a model definition, got #{definition.inspect}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
key = [ definition.provider, definition.id ]
|
|
44
|
+
catalog.register(definition, replace:)
|
|
45
|
+
registered_definitions[key] = definition
|
|
46
|
+
definition
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Rebuilds generated and explicit entries while retaining user registrations.
|
|
50
|
+
def reset_catalog!
|
|
51
|
+
@catalog = nil
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def registered_definitions
|
|
57
|
+
@registered_definitions ||= {}
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def definitions
|
|
61
|
+
generated_catalog.fetch(:definitions).map { |attributes| Definition.new(**attributes) } +
|
|
62
|
+
Explicit::DEFINITIONS.map { |attributes| Definition.new(**attributes) }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def generated_catalog
|
|
66
|
+
@generated_catalog ||= JSON.parse(File.read(generated_catalog_path), symbolize_names: true)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def generated_catalog_path
|
|
70
|
+
File.join(__dir__, "models", "generated.json")
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.models
|
|
76
|
+
Models
|
|
77
|
+
end
|
|
78
|
+
end
|
data/lib/llm_gateway/prompt.rb
CHANGED
|
@@ -5,7 +5,7 @@ require_relative "agents/event"
|
|
|
5
5
|
|
|
6
6
|
module LlmGateway
|
|
7
7
|
class Prompt
|
|
8
|
-
class_attribute :
|
|
8
|
+
class_attribute :adapter, :model, :reasoning
|
|
9
9
|
class_attribute :before_execute_callbacks, :after_execute_callbacks, instance_accessor: false, default: []
|
|
10
10
|
attr_accessor :cache_key, :cache_retention
|
|
11
11
|
|
|
@@ -19,33 +19,33 @@ module LlmGateway
|
|
|
19
19
|
self.after_execute_callbacks += [ block ] if block_given?
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
def initialize(
|
|
23
|
-
@
|
|
22
|
+
def initialize(adapter: nil, model: nil, reasoning: nil, cache_key: nil, cache_retention: nil)
|
|
23
|
+
@adapter = adapter || self.class.adapter
|
|
24
24
|
@model = model || self.class.model
|
|
25
25
|
@reasoning = reasoning || self.class.reasoning
|
|
26
26
|
@cache_key = cache_key
|
|
27
27
|
@cache_retention = cache_retention
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
def run(
|
|
30
|
+
def run(adapter: nil, model: nil, reasoning: nil, **options, &block)
|
|
31
31
|
# Resolve the prompt once so dynamic or expensive prompt builders are not
|
|
32
32
|
# evaluated multiple times during a single run.
|
|
33
33
|
input = prompt
|
|
34
34
|
|
|
35
35
|
run_callbacks(:before_execute, input)
|
|
36
36
|
|
|
37
|
-
response = run_tool_loop(input,
|
|
37
|
+
response = run_tool_loop(input, adapter: resolved_adapter(adapter), model: model, reasoning: reasoning, **options, &block)
|
|
38
38
|
|
|
39
39
|
run_callbacks(:after_execute, response)
|
|
40
40
|
|
|
41
41
|
response
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
def stream(input = prompt,
|
|
45
|
-
|
|
44
|
+
def stream(input = prompt, adapter: nil, model: nil, reasoning: nil, **options, &block)
|
|
45
|
+
stream_adapter = resolved_adapter(adapter)
|
|
46
46
|
stream_options = default_stream_options(model: model, reasoning: reasoning).merge(options)
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
stream_adapter.stream(input, **stream_options, &block)
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
def self.tools
|
|
@@ -107,12 +107,12 @@ module LlmGateway
|
|
|
107
107
|
requests.map { |request| find_and_execute_tool(request, **kwargs) }
|
|
108
108
|
end
|
|
109
109
|
|
|
110
|
-
def run_tool_loop(input,
|
|
111
|
-
response = stream(input,
|
|
110
|
+
def run_tool_loop(input, adapter: nil, model: nil, reasoning: nil, **options, &block)
|
|
111
|
+
response = stream(input, adapter: adapter, model: model, reasoning: reasoning, **options, &block)
|
|
112
112
|
|
|
113
113
|
while tool_requests(response).any?
|
|
114
114
|
input = prompt_with_tool_results(input, response, tool_requests(response))
|
|
115
|
-
response = stream(input,
|
|
115
|
+
response = stream(input, adapter: adapter, model: model, reasoning: reasoning, **options, &block)
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
response
|
|
@@ -150,8 +150,8 @@ module LlmGateway
|
|
|
150
150
|
}.compact
|
|
151
151
|
end
|
|
152
152
|
|
|
153
|
-
def
|
|
154
|
-
|
|
153
|
+
def resolved_adapter(adapter)
|
|
154
|
+
adapter || self.adapter
|
|
155
155
|
end
|
|
156
156
|
|
|
157
157
|
def resolved_model(model)
|
|
@@ -2,31 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
module LlmGateway
|
|
4
4
|
module Proxy
|
|
5
|
-
class Adapter
|
|
6
|
-
|
|
5
|
+
class Adapter < LlmGateway::Adapters::Adapter
|
|
6
|
+
provider "proxy"
|
|
7
|
+
client_class LlmGateway::Proxy::Client
|
|
7
8
|
|
|
8
|
-
def
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def stream(message, tools: nil, system: nil, **options, &block)
|
|
14
|
-
target_adapter = LlmGateway.build_provider(client.target_config.merge(provider: client.target_provider))
|
|
9
|
+
def stream(message, model:, tools: nil, system: nil, **options, &block)
|
|
10
|
+
target_adapter = client.adapter_class.build(**client.target_config)
|
|
11
|
+
model = target_adapter.resolve_model!(model)
|
|
15
12
|
mapper_class = target_adapter.stream_mapper_class
|
|
16
13
|
raise LlmGateway::Errors::MissingMapperForProvider, "No stream_mapper configured" unless mapper_class
|
|
17
14
|
|
|
18
15
|
mapper = mapper_class.new(
|
|
19
|
-
provider:
|
|
20
|
-
api: target_adapter.stream_api_name
|
|
16
|
+
provider: target_adapter.provider,
|
|
17
|
+
api: target_adapter.stream_api_name,
|
|
18
|
+
model_definition: model
|
|
21
19
|
)
|
|
22
20
|
|
|
23
|
-
client.stream(
|
|
21
|
+
client.stream(
|
|
22
|
+
normalize_messages(message),
|
|
23
|
+
tools: tools,
|
|
24
|
+
system: normalize_system(system),
|
|
25
|
+
**options.merge(model: target_adapter.class.provider_model_key(model))
|
|
26
|
+
) do |chunk|
|
|
24
27
|
mapper.map(chunk, &block)
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
mapper.result
|
|
28
31
|
end
|
|
29
32
|
|
|
33
|
+
def resolve_model!(model)
|
|
34
|
+
client.adapter_class.resolve_model!(model)
|
|
35
|
+
end
|
|
36
|
+
|
|
30
37
|
private
|
|
31
38
|
|
|
32
39
|
def normalize_system(system)
|
|
@@ -45,5 +52,8 @@ module LlmGateway
|
|
|
45
52
|
message.is_a?(String) ? [ { role: "user", content: message } ] : message
|
|
46
53
|
end
|
|
47
54
|
end
|
|
55
|
+
|
|
56
|
+
extend LlmGateway::Adapters::DefinitionFacade
|
|
57
|
+
define_adapter Adapter
|
|
48
58
|
end
|
|
49
59
|
end
|
|
@@ -7,11 +7,11 @@ require "uri"
|
|
|
7
7
|
module LlmGateway
|
|
8
8
|
module Proxy
|
|
9
9
|
class Client
|
|
10
|
-
attr_reader :url, :
|
|
10
|
+
attr_reader :url, :adapter_class, :target_config, :path
|
|
11
11
|
|
|
12
|
-
def initialize(url:,
|
|
12
|
+
def initialize(url:, adapter:, target_config: {}, api_key: nil, path: "/agent/llm_proxy", **_options)
|
|
13
13
|
@url = url.to_s.sub(%r{/+\z}, "")
|
|
14
|
-
@
|
|
14
|
+
@adapter_class = Protocol.adapter_class(adapter)
|
|
15
15
|
@target_config = (target_config || {}).transform_keys(&:to_sym)
|
|
16
16
|
@api_key = api_key
|
|
17
17
|
@path = path.to_s.start_with?("/") ? path.to_s : "/#{path}"
|
|
@@ -30,7 +30,7 @@ module LlmGateway
|
|
|
30
30
|
request["accept-encoding"] = "identity"
|
|
31
31
|
request["authorization"] = "Bearer #{@api_key}" if @api_key
|
|
32
32
|
request.body = {
|
|
33
|
-
|
|
33
|
+
adapter: Protocol.dump_adapter(adapter_class),
|
|
34
34
|
config: target_config,
|
|
35
35
|
messages: messages,
|
|
36
36
|
system: system,
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LlmGateway
|
|
4
|
+
module Proxy
|
|
5
|
+
# The only string-to-class mapping in the application. Adapter names are
|
|
6
|
+
# protocol values used to cross the proxy boundary; runtime code uses the
|
|
7
|
+
# adapter classes themselves.
|
|
8
|
+
module Protocol
|
|
9
|
+
ADAPTERS = {
|
|
10
|
+
"anthropic-messages" => LlmGateway::Adapters::Anthropic::MessagesAdapter,
|
|
11
|
+
"openai-completions" => LlmGateway::Adapters::OpenAI::ChatCompletionsAdapter,
|
|
12
|
+
"openai-responses" => LlmGateway::Adapters::OpenAI::ResponsesAdapter,
|
|
13
|
+
"openai-codex" => LlmGateway::Adapters::OpenAICodex::ResponsesAdapter,
|
|
14
|
+
"groq-completions" => LlmGateway::Adapters::Groq::ChatCompletionsAdapter
|
|
15
|
+
}.freeze
|
|
16
|
+
ADAPTER_NAMES = ADAPTERS.invert.freeze
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def load_adapter(name)
|
|
21
|
+
ADAPTERS.fetch(name.to_s) do
|
|
22
|
+
raise LlmGateway::Errors::UnsupportedProvider, "Unknown proxy adapter: #{name}"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def dump_adapter(selector)
|
|
27
|
+
adapter_class = adapter_class_for(selector)
|
|
28
|
+
ADAPTER_NAMES.fetch(adapter_class) do
|
|
29
|
+
raise LlmGateway::Errors::UnsupportedProvider,
|
|
30
|
+
"Adapter is not supported by the proxy: #{adapter_class}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def adapter_class(selector)
|
|
35
|
+
adapter_class = adapter_class_for(selector)
|
|
36
|
+
dump_adapter(adapter_class)
|
|
37
|
+
adapter_class
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def adapter_class_for(selector)
|
|
41
|
+
return selector.adapter_class if selector.respond_to?(:adapter_class)
|
|
42
|
+
return selector if selector.is_a?(Class) && selector <= LlmGateway::Adapters::Adapter
|
|
43
|
+
return selector.class if selector.is_a?(LlmGateway::Adapters::Adapter)
|
|
44
|
+
|
|
45
|
+
raise ArgumentError, "Expected an adapter class, definition, or instance, got #{selector.inspect}"
|
|
46
|
+
end
|
|
47
|
+
private_class_method :adapter_class_for
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -6,18 +6,21 @@ module LlmGateway
|
|
|
6
6
|
module Proxy
|
|
7
7
|
class Server
|
|
8
8
|
PATH = "/agent/llm_proxy"
|
|
9
|
-
|
|
10
9
|
def call(env)
|
|
11
10
|
return not_found unless env["REQUEST_METHOD"] == "POST" && env["PATH_INFO"] == PATH
|
|
12
11
|
|
|
13
12
|
request = JSON.parse(env["rack.input"].read).deep_symbolize_keys
|
|
14
13
|
options = request[:options] || {}
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
provider_model_key = request.key?(:model) ? request[:model] : options.delete(:model)
|
|
15
|
+
adapter_name = request.fetch(:adapter)
|
|
16
|
+
adapter_class = Protocol.load_adapter(adapter_name)
|
|
17
|
+
model = resolve_model!(adapter_class, adapter_name, provider_model_key)
|
|
18
|
+
adapter = adapter_class.build(**(request[:config] || {}))
|
|
17
19
|
|
|
18
20
|
body = Enumerator.new do |yielder|
|
|
19
21
|
adapter.raw_stream(
|
|
20
22
|
request[:messages],
|
|
23
|
+
model: model,
|
|
21
24
|
system: request[:system],
|
|
22
25
|
tools: request[:tools],
|
|
23
26
|
**options
|
|
@@ -27,9 +30,9 @@ module LlmGateway
|
|
|
27
30
|
end
|
|
28
31
|
|
|
29
32
|
[ 200, { "content-type" => "text/event-stream", "cache-control" => "no-cache" }, body ]
|
|
30
|
-
rescue KeyError, JSON::ParserError, ArgumentError => e
|
|
33
|
+
rescue KeyError, JSON::ParserError, ArgumentError, Errors::InvalidModelDefinition => e
|
|
31
34
|
json_error(400, e.message)
|
|
32
|
-
rescue Errors::UnsupportedProvider => e
|
|
35
|
+
rescue Errors::UnsupportedProvider, Errors::UnsupportedModelForAdapter => e
|
|
33
36
|
json_error(404, e.message)
|
|
34
37
|
rescue StandardError => e
|
|
35
38
|
json_error(500, e.message)
|
|
@@ -37,11 +40,11 @@ module LlmGateway
|
|
|
37
40
|
|
|
38
41
|
private
|
|
39
42
|
|
|
40
|
-
def
|
|
41
|
-
|
|
42
|
-
config = (request[:config] || {}).merge(provider: provider)
|
|
43
|
+
def resolve_model!(adapter_class, adapter_name, provider_model_key)
|
|
44
|
+
raise ArgumentError, "Proxy request must include a model" if provider_model_key.nil?
|
|
43
45
|
|
|
44
|
-
|
|
46
|
+
adapter_class.model_for_provider_model_key(provider_model_key) ||
|
|
47
|
+
raise(KeyError, "Unknown model for #{adapter_name}: #{provider_model_key}")
|
|
45
48
|
end
|
|
46
49
|
|
|
47
50
|
def encode_sse(chunk)
|
data/lib/llm_gateway/utils.rb
CHANGED
|
@@ -97,16 +97,22 @@ unless Class.method_defined?(:class_attribute)
|
|
|
97
97
|
end
|
|
98
98
|
|
|
99
99
|
class Object
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
unless method_defined?(:blank?)
|
|
101
|
+
def blank?
|
|
102
|
+
LlmGateway::Utils.blank?(self)
|
|
103
|
+
end
|
|
102
104
|
end
|
|
103
105
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
+
unless method_defined?(:present?)
|
|
107
|
+
def present?
|
|
108
|
+
!blank?
|
|
109
|
+
end
|
|
106
110
|
end
|
|
107
111
|
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
unless method_defined?(:presence)
|
|
113
|
+
def presence
|
|
114
|
+
self if present?
|
|
115
|
+
end
|
|
110
116
|
end
|
|
111
117
|
end
|
|
112
118
|
|
data/lib/llm_gateway/version.rb
CHANGED
data/lib/llm_gateway.rb
CHANGED
|
@@ -4,7 +4,6 @@ require_relative "llm_gateway/utils"
|
|
|
4
4
|
require_relative "llm_gateway/version"
|
|
5
5
|
require_relative "llm_gateway/errors"
|
|
6
6
|
require_relative "llm_gateway/base_client"
|
|
7
|
-
require_relative "llm_gateway/client"
|
|
8
7
|
require_relative "llm_gateway/tool"
|
|
9
8
|
require_relative "llm_gateway/prompt"
|
|
10
9
|
require_relative "llm_gateway/agents/event"
|
|
@@ -22,6 +21,7 @@ require_relative "llm_gateway/clients/openai_codex/token_manager"
|
|
|
22
21
|
require_relative "llm_gateway/clients/groq"
|
|
23
22
|
|
|
24
23
|
# Load adapters
|
|
24
|
+
require_relative "llm_gateway/adapter_definition"
|
|
25
25
|
require_relative "llm_gateway/adapters/option_mapper"
|
|
26
26
|
require_relative "llm_gateway/adapters/anthropic_option_mapper"
|
|
27
27
|
require_relative "llm_gateway/adapters/structs"
|
|
@@ -46,8 +46,9 @@ require_relative "llm_gateway/adapters/openai/responses_adapter"
|
|
|
46
46
|
require_relative "llm_gateway/adapters/openai_codex/responses_adapter"
|
|
47
47
|
require_relative "llm_gateway/adapters/groq/chat_completions_adapter"
|
|
48
48
|
|
|
49
|
-
# Load provider
|
|
50
|
-
require_relative "llm_gateway/
|
|
49
|
+
# Load the provider-level model catalog and proxy transport
|
|
50
|
+
require_relative "llm_gateway/models"
|
|
51
|
+
require_relative "llm_gateway/proxy/protocol"
|
|
51
52
|
require_relative "llm_gateway/proxy/client"
|
|
52
53
|
require_relative "llm_gateway/proxy/adapter"
|
|
53
54
|
require_relative "llm_gateway/proxy/server"
|
|
@@ -103,68 +104,4 @@ module LlmGateway
|
|
|
103
104
|
Client = LlmGateway::Clients::Groq
|
|
104
105
|
end
|
|
105
106
|
end
|
|
106
|
-
|
|
107
|
-
def self.build_provider(config)
|
|
108
|
-
config = config.transform_keys(&:to_sym)
|
|
109
|
-
provider_name = config.delete(:provider)
|
|
110
|
-
if config.key?(:model_key)
|
|
111
|
-
raise ArgumentError, "model_key is no longer a provider option; pass model: to chat/stream instead"
|
|
112
|
-
end
|
|
113
|
-
entry = ProviderRegistry.resolve(provider_name)
|
|
114
|
-
|
|
115
|
-
client = entry[:client].new(**config)
|
|
116
|
-
entry[:adapter].new(client, provider_key: provider_name)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def self.configure(configs)
|
|
120
|
-
@configured_clients ||= {}
|
|
121
|
-
|
|
122
|
-
configs.each do |entry|
|
|
123
|
-
name = entry[:name] || entry["name"]
|
|
124
|
-
config = entry[:config] || entry["config"]
|
|
125
|
-
|
|
126
|
-
raise ArgumentError, "Each config entry must have a :name" unless name
|
|
127
|
-
|
|
128
|
-
client = build_provider(config)
|
|
129
|
-
@configured_clients[name.to_sym] = client
|
|
130
|
-
|
|
131
|
-
define_singleton_method(name.to_sym) { @configured_clients[name.to_sym] }
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
def self.configured_clients
|
|
136
|
-
@configured_clients ||= {}
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def self.reset_configuration!
|
|
140
|
-
@configured_clients&.each_key do |name|
|
|
141
|
-
singleton_class.remove_method(name) if respond_to?(name)
|
|
142
|
-
end
|
|
143
|
-
@configured_clients = {}
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
# Register built-in providers (canonical keys)
|
|
147
|
-
ProviderRegistry.register("anthropic_messages",
|
|
148
|
-
client: Clients::Anthropic,
|
|
149
|
-
adapter: Adapters::Anthropic::MessagesAdapter)
|
|
150
|
-
|
|
151
|
-
ProviderRegistry.register("openai_completions",
|
|
152
|
-
client: Clients::OpenAI,
|
|
153
|
-
adapter: Adapters::OpenAI::ChatCompletionsAdapter)
|
|
154
|
-
|
|
155
|
-
ProviderRegistry.register("openai_responses",
|
|
156
|
-
client: Clients::OpenAI,
|
|
157
|
-
adapter: Adapters::OpenAI::ResponsesAdapter)
|
|
158
|
-
|
|
159
|
-
ProviderRegistry.register("groq_completions",
|
|
160
|
-
client: Clients::Groq,
|
|
161
|
-
adapter: Adapters::Groq::ChatCompletionsAdapter)
|
|
162
|
-
|
|
163
|
-
ProviderRegistry.register("openai_codex",
|
|
164
|
-
client: Clients::OpenAI,
|
|
165
|
-
adapter: Adapters::OpenAICodex::ResponsesAdapter)
|
|
166
|
-
|
|
167
|
-
ProviderRegistry.register("proxy",
|
|
168
|
-
client: Proxy::Client,
|
|
169
|
-
adapter: Proxy::Adapter)
|
|
170
107
|
end
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bigdecimal"
|
|
5
|
+
require "date"
|
|
6
|
+
require "json"
|
|
7
|
+
require "net/http"
|
|
8
|
+
|
|
9
|
+
SOURCE_URL = "https://models.dev/api.json"
|
|
10
|
+
OUTPUT_PATH = File.expand_path("../lib/llm_gateway/models/generated.json", __dir__)
|
|
11
|
+
OPENAI_LONG_CONTEXT_INPUT_THRESHOLD = 272_000
|
|
12
|
+
OPENAI_LONG_CONTEXT_PRICING_MODEL_IDS = %w[
|
|
13
|
+
gpt-5.4
|
|
14
|
+
gpt-5.4-pro
|
|
15
|
+
gpt-5.5
|
|
16
|
+
gpt-5.5-pro
|
|
17
|
+
gpt-5.6-sol
|
|
18
|
+
gpt-5.6-terra
|
|
19
|
+
gpt-5.6-luna
|
|
20
|
+
].freeze
|
|
21
|
+
|
|
22
|
+
PROVIDERS = %w[anthropic openai groq].freeze
|
|
23
|
+
NON_TEXT_GENERATION_FAMILIES = %w[gpt-image text-embedding whisper].freeze
|
|
24
|
+
|
|
25
|
+
source =
|
|
26
|
+
if ARGV.first
|
|
27
|
+
JSON.parse(File.read(ARGV.first))
|
|
28
|
+
else
|
|
29
|
+
response = Net::HTTP.get_response(URI(SOURCE_URL))
|
|
30
|
+
abort "Failed to fetch #{SOURCE_URL}: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
|
31
|
+
|
|
32
|
+
JSON.parse(response.body)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def pricing_for(cost, provider:, id:)
|
|
36
|
+
return nil if cost.empty?
|
|
37
|
+
|
|
38
|
+
pricing = {
|
|
39
|
+
input: cost.fetch("input", 0).to_s,
|
|
40
|
+
output: cost.fetch("output", 0).to_s,
|
|
41
|
+
cache_read: cost.fetch("cache_read", 0).to_s,
|
|
42
|
+
cache_write: cost.fetch("cache_write", 0).to_s
|
|
43
|
+
}
|
|
44
|
+
return pricing unless provider == "openai" && OPENAI_LONG_CONTEXT_PRICING_MODEL_IDS.include?(id)
|
|
45
|
+
|
|
46
|
+
pricing.merge(
|
|
47
|
+
tiers: [
|
|
48
|
+
{
|
|
49
|
+
input_tokens_above: OPENAI_LONG_CONTEXT_INPUT_THRESHOLD,
|
|
50
|
+
input: (BigDecimal(pricing[:input]) * 2).to_s("F"),
|
|
51
|
+
output: (BigDecimal(pricing[:output]) * 1.5).to_s("F"),
|
|
52
|
+
cache_read: (BigDecimal(pricing[:cache_read]) * 2).to_s("F"),
|
|
53
|
+
cache_write: (BigDecimal(pricing[:cache_write]) * 2).to_s("F")
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def source_capability(model, key)
|
|
60
|
+
value = model[key]
|
|
61
|
+
value if value == true || value == false
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def text_generation_capability(model)
|
|
65
|
+
return false if NON_TEXT_GENERATION_FAMILIES.include?(model["family"])
|
|
66
|
+
|
|
67
|
+
input_modalities = Array(model.dig("modalities", "input"))
|
|
68
|
+
output_modalities = Array(model.dig("modalities", "output"))
|
|
69
|
+
input_modalities.include?("text") && output_modalities.include?("text")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def invalid_metadata!(provider, id, message)
|
|
73
|
+
raise ArgumentError, "Invalid models.dev metadata for #{provider}/#{id}: #{message}"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def non_negative_integer!(value, name, provider:, id:)
|
|
77
|
+
normalized = Integer(value)
|
|
78
|
+
if normalized.negative? || (value.is_a?(Numeric) && value != normalized)
|
|
79
|
+
raise ArgumentError
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
normalized
|
|
83
|
+
rescue TypeError, ArgumentError, RangeError
|
|
84
|
+
invalid_metadata!(provider, id, "#{name} must be a non-negative integer")
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def date_for(value, name, provider:, id:)
|
|
88
|
+
return nil if value.nil?
|
|
89
|
+
invalid_metadata!(provider, id, "#{name} must be an ISO-8601 date") unless value.is_a?(String)
|
|
90
|
+
|
|
91
|
+
Date.iso8601(value).iso8601
|
|
92
|
+
rescue Date::Error
|
|
93
|
+
invalid_metadata!(provider, id, "#{name} must be an ISO-8601 date")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def normalize_source_value(value, provider:, id:)
|
|
97
|
+
case value
|
|
98
|
+
when String, Numeric, TrueClass, FalseClass, NilClass then value
|
|
99
|
+
when Array then value.map { |item| normalize_source_value(item, provider:, id:) }
|
|
100
|
+
when Hash
|
|
101
|
+
value.each_with_object({}) do |(key, item), normalized|
|
|
102
|
+
unless key.is_a?(String) || key.is_a?(Symbol)
|
|
103
|
+
invalid_metadata!(provider, id, "reasoning option keys must be strings")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
normalized[key.to_sym] = normalize_source_value(item, provider:, id:)
|
|
107
|
+
end
|
|
108
|
+
else
|
|
109
|
+
invalid_metadata!(provider, id, "reasoning option attributes must contain JSON values")
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def reasoning_controls_for(model, provider:, id:)
|
|
114
|
+
return nil unless model.key?("reasoning_options")
|
|
115
|
+
|
|
116
|
+
options = model["reasoning_options"]
|
|
117
|
+
unless options.is_a?(Array)
|
|
118
|
+
invalid_metadata!(provider, id, "reasoning_options must be an array")
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
options.map.with_index do |option, index|
|
|
122
|
+
unless option.is_a?(Hash)
|
|
123
|
+
invalid_metadata!(provider, id, "reasoning_options[#{index}] must be an object")
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
option = normalize_source_value(option, provider:, id:)
|
|
127
|
+
type = option[:type]
|
|
128
|
+
unless type.is_a?(String) && !type.empty?
|
|
129
|
+
invalid_metadata!(provider, id, "reasoning_options[#{index}].type must be a non-empty string")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
case type
|
|
133
|
+
when "effort"
|
|
134
|
+
values = option[:values]
|
|
135
|
+
unless values.is_a?(Array)
|
|
136
|
+
invalid_metadata!(provider, id, "reasoning_options[#{index}].values must be an array")
|
|
137
|
+
end
|
|
138
|
+
unless values.compact.all? { |value| value.is_a?(String) }
|
|
139
|
+
invalid_metadata!(provider, id, "reasoning_options[#{index}].values must contain strings or null")
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
option[:values] = values.compact.uniq
|
|
143
|
+
when "budget_tokens"
|
|
144
|
+
%i[min max].each do |boundary|
|
|
145
|
+
next unless option.key?(boundary) && !option[boundary].nil?
|
|
146
|
+
|
|
147
|
+
option[boundary] = non_negative_integer!(
|
|
148
|
+
option[boundary], "reasoning_options[#{index}].#{boundary}", provider:, id:
|
|
149
|
+
)
|
|
150
|
+
end
|
|
151
|
+
if option[:min] && option[:max] && option[:max] < option[:min]
|
|
152
|
+
invalid_metadata!(provider, id, "reasoning_options[#{index}].max must not be below min")
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
option
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def definitions_for(data, provider:)
|
|
161
|
+
data.fetch(provider).fetch("models").map do |id, model|
|
|
162
|
+
cost = model["cost"] || {}
|
|
163
|
+
limit = model["limit"] || {}
|
|
164
|
+
{
|
|
165
|
+
provider: provider,
|
|
166
|
+
id: id,
|
|
167
|
+
name: model["name"] || id,
|
|
168
|
+
source: :models_dev,
|
|
169
|
+
release_date: date_for(model["release_date"], "release_date", provider:, id:),
|
|
170
|
+
last_updated: date_for(model["last_updated"], "last_updated", provider:, id:),
|
|
171
|
+
context_window: limit["context"],
|
|
172
|
+
max_output_tokens: limit["output"],
|
|
173
|
+
input_modalities: Array(model.dig("modalities", "input")),
|
|
174
|
+
output_modalities: Array(model.dig("modalities", "output")),
|
|
175
|
+
capabilities: {
|
|
176
|
+
text_generation: text_generation_capability(model),
|
|
177
|
+
tool_calling: source_capability(model, "tool_call"),
|
|
178
|
+
structured_output: source_capability(model, "structured_output"),
|
|
179
|
+
reasoning: source_capability(model, "reasoning")
|
|
180
|
+
},
|
|
181
|
+
reasoning_controls: reasoning_controls_for(model, provider:, id:),
|
|
182
|
+
pricing: pricing_for(cost, provider:, id:)
|
|
183
|
+
}.compact
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
definitions = PROVIDERS.flat_map { |provider| definitions_for(source, provider:) }
|
|
188
|
+
|
|
189
|
+
payload = {
|
|
190
|
+
source: SOURCE_URL,
|
|
191
|
+
definitions: definitions.sort_by { |model| [ model[:provider], model[:id] ] }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
File.write(OUTPUT_PATH, JSON.pretty_generate(payload) + "\n")
|
|
195
|
+
puts "Wrote #{definitions.size} definitions to #{OUTPUT_PATH}"
|