llm_gateway 0.8.1 → 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 +36 -0
- data/README.md +190 -79
- data/Rakefile +5 -0
- data/docs/migration_guide_0.10.0.md +53 -0
- data/docs/migration_guide_0.9.0.md +166 -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/event.rb +31 -3
- data/lib/llm_gateway/agents/harness.rb +107 -74
- data/lib/llm_gateway/agents/in_memory_session_manager.rb +40 -9
- data/lib/llm_gateway/agents/tools/bash_tool.rb +6 -5
- data/lib/llm_gateway/agents/tools/edit_tool.rb +7 -6
- data/lib/llm_gateway/agents/tools/read_tool.rb +11 -10
- data/lib/llm_gateway/agents/tools/tool_utils.rb +2 -2
- data/lib/llm_gateway/agents/tools/write_tool.rb +4 -3
- 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 +55 -31
- 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/tool.rb +49 -40
- data/lib/llm_gateway/utils.rb +12 -6
- data/lib/llm_gateway/version.rb +1 -1
- data/lib/llm_gateway.rb +5 -68
- data/scripts/generate_model_catalog.rb +195 -0
- metadata +14 -4
- data/lib/llm_gateway/client.rb +0 -35
- data/lib/llm_gateway/provider_registry.rb +0 -37
|
@@ -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}"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: llm_gateway
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- billybonks
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: dry-struct
|
|
@@ -42,9 +42,13 @@ files:
|
|
|
42
42
|
- README.md
|
|
43
43
|
- Rakefile
|
|
44
44
|
- docs/migration-guide.md
|
|
45
|
+
- docs/migration_guide_0.10.0.md
|
|
45
46
|
- docs/migration_guide_0.6.0.md
|
|
46
47
|
- docs/migration_guide_0.7.0.md
|
|
48
|
+
- docs/migration_guide_0.9.0.md
|
|
49
|
+
- docs/structured-output.md
|
|
47
50
|
- lib/llm_gateway.rb
|
|
51
|
+
- lib/llm_gateway/adapter_definition.rb
|
|
48
52
|
- lib/llm_gateway/adapters/adapter.rb
|
|
49
53
|
- lib/llm_gateway/adapters/anthropic/acts_like_messages.rb
|
|
50
54
|
- lib/llm_gateway/adapters/anthropic/input_mapper.rb
|
|
@@ -86,7 +90,6 @@ files:
|
|
|
86
90
|
- lib/llm_gateway/agents/tools/tool_utils.rb
|
|
87
91
|
- lib/llm_gateway/agents/tools/write_tool.rb
|
|
88
92
|
- lib/llm_gateway/base_client.rb
|
|
89
|
-
- lib/llm_gateway/client.rb
|
|
90
93
|
- lib/llm_gateway/clients/anthropic.rb
|
|
91
94
|
- lib/llm_gateway/clients/claude_code/oauth_flow.rb
|
|
92
95
|
- lib/llm_gateway/clients/claude_code/token_manager.rb
|
|
@@ -95,16 +98,23 @@ files:
|
|
|
95
98
|
- lib/llm_gateway/clients/openai_codex/oauth_flow.rb
|
|
96
99
|
- lib/llm_gateway/clients/openai_codex/token_manager.rb
|
|
97
100
|
- lib/llm_gateway/errors.rb
|
|
101
|
+
- lib/llm_gateway/models.rb
|
|
102
|
+
- lib/llm_gateway/models/catalog.rb
|
|
103
|
+
- lib/llm_gateway/models/cost_calculator.rb
|
|
104
|
+
- lib/llm_gateway/models/definition.rb
|
|
105
|
+
- lib/llm_gateway/models/explicit.rb
|
|
106
|
+
- lib/llm_gateway/models/generated.json
|
|
98
107
|
- lib/llm_gateway/prompt.rb
|
|
99
|
-
- lib/llm_gateway/provider_registry.rb
|
|
100
108
|
- lib/llm_gateway/proxy/adapter.rb
|
|
101
109
|
- lib/llm_gateway/proxy/client.rb
|
|
110
|
+
- lib/llm_gateway/proxy/protocol.rb
|
|
102
111
|
- lib/llm_gateway/proxy/server.rb
|
|
103
112
|
- lib/llm_gateway/tool.rb
|
|
104
113
|
- lib/llm_gateway/utils.rb
|
|
105
114
|
- lib/llm_gateway/version.rb
|
|
106
115
|
- scripts/create_anthropic_credentials.rb
|
|
107
116
|
- scripts/create_openai_codex_credentials.rb
|
|
117
|
+
- scripts/generate_model_catalog.rb
|
|
108
118
|
- sig/llm_gateway.rbs
|
|
109
119
|
homepage: https://github.com/Hyper-Unearthing/llm_gateway
|
|
110
120
|
licenses:
|
data/lib/llm_gateway/client.rb
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
module LlmGateway
|
|
5
|
-
class Client
|
|
6
|
-
def self.provider_id_from_client(client)
|
|
7
|
-
case client
|
|
8
|
-
when LlmGateway::Clients::Anthropic
|
|
9
|
-
"anthropic"
|
|
10
|
-
when LlmGateway::Clients::OpenAI
|
|
11
|
-
"openai"
|
|
12
|
-
when LlmGateway::Clients::Groq
|
|
13
|
-
"groq"
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def self.upload_file(provider, **kwargs)
|
|
18
|
-
api_key = kwargs.delete(:api_key)
|
|
19
|
-
adapter = LlmGateway.build_provider(
|
|
20
|
-
provider: provider,
|
|
21
|
-
api_key: api_key
|
|
22
|
-
)
|
|
23
|
-
adapter.upload_file(**kwargs)
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def self.download_file(provider, **kwargs)
|
|
27
|
-
api_key = kwargs.delete(:api_key)
|
|
28
|
-
adapter = LlmGateway.build_provider(
|
|
29
|
-
provider: provider,
|
|
30
|
-
api_key: api_key
|
|
31
|
-
)
|
|
32
|
-
adapter.download_file(**kwargs)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module LlmGateway
|
|
4
|
-
class ProviderRegistry
|
|
5
|
-
class << self
|
|
6
|
-
def register(name, client:, adapter:)
|
|
7
|
-
registry[name.to_s] = { client: client, adapter: adapter }
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def resolve(name)
|
|
11
|
-
name = name.to_s
|
|
12
|
-
entry = registry[name]
|
|
13
|
-
raise Errors::UnsupportedProvider, "Unknown provider: #{name}" unless entry
|
|
14
|
-
|
|
15
|
-
entry
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def registered?(name)
|
|
19
|
-
registry.key?(name.to_s)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def providers
|
|
23
|
-
registry.keys
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def reset!
|
|
27
|
-
@registry = {}
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
private
|
|
31
|
-
|
|
32
|
-
def registry
|
|
33
|
-
@registry ||= {}
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|