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,53 @@
|
|
|
1
|
+
# Migration guide: v0.9.0 to v0.10.0
|
|
2
|
+
|
|
3
|
+
## Build adapters directly
|
|
4
|
+
|
|
5
|
+
`LlmGateway.build_provider`, `LlmGateway.configure`, `configured_clients`, and `ProviderRegistry` were removed. Build and retain the adapter your application needs:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
# Before
|
|
9
|
+
adapter = LlmGateway.build_provider(provider: "openai_responses", api_key: key)
|
|
10
|
+
|
|
11
|
+
# After
|
|
12
|
+
adapter = LlmGateway::Adapters::OpenAI::Responses.build(api_key: key)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Use `Anthropic::Messages`, `OpenAI::ChatCompletions`, `OpenAI::Responses`, `OpenAICodex::Responses`, or `Groq::ChatCompletions`. `Adapter.build` accepts only client/transport configuration, not `model:` or `model_key:`.
|
|
16
|
+
|
|
17
|
+
## Pass model definitions, not strings
|
|
18
|
+
|
|
19
|
+
`stream` now requires a catalog model definition. Fetch one and reuse it:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
model = LlmGateway.models.fetch("openai/gpt-5.4")
|
|
23
|
+
adapter.stream("Hello", model: model)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
For a newly released or custom model, register a `LlmGateway::Models::Definition` through `LlmGateway.models.register(...)` before using it. The adapter and model providers must match.
|
|
27
|
+
|
|
28
|
+
## Update Prompt and Harness setup
|
|
29
|
+
|
|
30
|
+
`Prompt` accepts `adapter:` instead of `provider:`. Set `self.adapter` on subclasses or pass `adapter:` to `new`, `run`, or `stream`.
|
|
31
|
+
|
|
32
|
+
A `Harness` also requires an adapter, while its model and reasoning live in the session:
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
session.change_model(model)
|
|
36
|
+
session.change_reasoning("high")
|
|
37
|
+
harness = MyHarness.new(session, adapter: adapter)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Use `harness.change_adapter(new_adapter, model: new_model)` when switching providers.
|
|
41
|
+
|
|
42
|
+
## Update proxy clients
|
|
43
|
+
|
|
44
|
+
Build proxies with `adapter:` rather than `target_provider:`. Pass an adapter definition, class, or instance:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
proxy = LlmGateway::Proxy.build(
|
|
48
|
+
url: url,
|
|
49
|
+
adapter: LlmGateway::Adapters::OpenAI::Responses
|
|
50
|
+
)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The proxy protocol now uses allowlisted hyphenated adapter names internally.
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Migration guide: v0.8.1 to v0.9.0
|
|
2
|
+
|
|
3
|
+
This guide covers user-facing changes since the latest released gem, `v0.8.1`.
|
|
4
|
+
|
|
5
|
+
Relevant PRs/changes:
|
|
6
|
+
|
|
7
|
+
- #95: pass the persisted session event into tool execution
|
|
8
|
+
- #97: fix harness queue draining semantics
|
|
9
|
+
- current branch: tools return `LlmGateway::Agents::Event::ToolCallResult` objects instead of hashes/strings, with `is_error` support
|
|
10
|
+
|
|
11
|
+
## 1. Update custom tools to accept `tool_use_id:` and return `ToolCallResult`
|
|
12
|
+
|
|
13
|
+
Custom `LlmGateway::Tool` subclasses must now return a `LlmGateway::Agents::Event::ToolCallResult` object, or a subclass of it. Use the inherited `tool_result` helper for the common case.
|
|
14
|
+
|
|
15
|
+
### Before
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
class AddTool < LlmGateway::Tool
|
|
19
|
+
name "add"
|
|
20
|
+
description "Add two numbers"
|
|
21
|
+
input_schema(type: "object")
|
|
22
|
+
|
|
23
|
+
def execute(input)
|
|
24
|
+
input[:left] + input[:right]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### After
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
class AddTool < LlmGateway::Tool
|
|
33
|
+
name "add"
|
|
34
|
+
description "Add two numbers"
|
|
35
|
+
input_schema(type: "object")
|
|
36
|
+
|
|
37
|
+
def execute(input, tool_use_id:)
|
|
38
|
+
tool_result(input[:left] + input[:right], tool_use_id: tool_use_id)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If a tool returns anything other than `ToolCallResult`, the prompt/harness catches the resulting `TypeError` and sends an error result to the model instead. The exception does not escape from the normal tool loop.
|
|
44
|
+
|
|
45
|
+
## 2. Use `is_error:` for failed tool results when needed
|
|
46
|
+
|
|
47
|
+
`ToolCallResult` now includes an `is_error` boolean and serializes it through `to_h`.
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
LlmGateway::Agents::Event::ToolCallResult.new(
|
|
51
|
+
tool_use_id: tool_use_id,
|
|
52
|
+
content: "Something went wrong",
|
|
53
|
+
is_error: true
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The default is `false`.
|
|
58
|
+
|
|
59
|
+
## 3. Update event consumers for tool result objects
|
|
60
|
+
|
|
61
|
+
Harness events now expose tool results as `ToolCallResult` objects, not raw hashes or provider-specific `ToolResult` structs.
|
|
62
|
+
|
|
63
|
+
Affected events:
|
|
64
|
+
|
|
65
|
+
- `:tool_execution_end` via `event.result`
|
|
66
|
+
- `:turn_end` via `event.tool_results`
|
|
67
|
+
|
|
68
|
+
### Before
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
case event.type
|
|
72
|
+
when :tool_execution_end
|
|
73
|
+
puts event.result[:content]
|
|
74
|
+
when :turn_end
|
|
75
|
+
event.tool_results.each { |result| persist(result) }
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### After
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
case event.type
|
|
83
|
+
when :tool_execution_end
|
|
84
|
+
puts event.result.content
|
|
85
|
+
when :turn_end
|
|
86
|
+
event.tool_results.each { |result| persist(result.to_h) }
|
|
87
|
+
end
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 4. Wrap tool execution if needed
|
|
91
|
+
|
|
92
|
+
`Prompt` and `Harness` now call the protected `execute_tool_requests` hook to execute a batch of tool requests. If you already override tool execution, migrate that customization to this hook. Override it on a `Prompt` or `Harness` subclass when you need setup before tools run, post-processing after results return, or customization of the transcript message that carries tool results. Call `yield requests` to let llm_gateway execute tools normally, then return a `LlmGateway::Agents::Event::ToolResultMessage`.
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
class MyHarness < LlmGateway::Agents::Harness
|
|
96
|
+
def execute_tool_requests(requests:, assistant_message:, session_event:)
|
|
97
|
+
context = build_context(requests, assistant_message)
|
|
98
|
+
results = yield requests
|
|
99
|
+
audit_results(context, results)
|
|
100
|
+
|
|
101
|
+
LlmGateway::Agents::Event::ToolResultMessage.new(
|
|
102
|
+
content: results,
|
|
103
|
+
details: { assistant_message_id: assistant_message.id }
|
|
104
|
+
)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The default `ToolResultMessage` serializes as:
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
{
|
|
113
|
+
role: "user",
|
|
114
|
+
content: tool_results.map(&:to_h)
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 5. Review harness queue behavior
|
|
119
|
+
|
|
120
|
+
Harness queue semantics changed to avoid stale queues and recursive runs.
|
|
121
|
+
|
|
122
|
+
- `default_queue_mode` is now `:follow_up` instead of `:next_turn`.
|
|
123
|
+
- `next_turn_message` was removed.
|
|
124
|
+
- Valid `default_queue_mode` values are now `:steer` and `:follow_up`.
|
|
125
|
+
- `prompt_message`, `steer_message`, and `follow_up_message` always enqueue first.
|
|
126
|
+
- If the agent is idle, those methods enqueue and then call `continue`.
|
|
127
|
+
- `continue` now raises `RuntimeError, "Cannot continue a busy agent"` if called while the session is busy.
|
|
128
|
+
- `continue` marks the session busy, drains `:steer`, drains `:follow_up`, runs, and marks the session idle.
|
|
129
|
+
|
|
130
|
+
### Before
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
harness.default_queue_mode = :next_turn
|
|
134
|
+
harness.next_turn_message("do this after the current run")
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### After
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
harness.default_queue_mode = :follow_up
|
|
141
|
+
harness.follow_up_message("do this after the current turn")
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
If you previously relied on `next_turn` work running after the entire agent run, move that behavior into your application-level scheduler or enqueue a `follow_up` after the current operation completes.
|
|
145
|
+
|
|
146
|
+
## 6. Tool execution can access the persisted session event
|
|
147
|
+
|
|
148
|
+
For a `Harness`, `execute_tool_requests` receives the persisted assistant session event as `session_event:`. This is useful for subclasses/custom harnesses that need access to the stored message/event that produced the tool call. `Prompt` invokes the same hook with `session_event: nil`, because it has no session manager.
|
|
149
|
+
|
|
150
|
+
Existing simple tools do not receive this event directly and do not need to use it; they only need the `execute(input, tool_use_id:)` signature and `ToolCallResult` return value described above.
|
|
151
|
+
|
|
152
|
+
## 7. Message metadata is safe to keep in transcripts
|
|
153
|
+
|
|
154
|
+
Input messages may now carry app-owned metadata, such as a `details` hash. The gateway preserves it locally but strips unsupported metadata before sending user/assistant messages to providers.
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
adapter.stream([
|
|
158
|
+
{
|
|
159
|
+
role: "user",
|
|
160
|
+
content: "Hello",
|
|
161
|
+
details: { trace_id: "msg-123" }
|
|
162
|
+
}
|
|
163
|
+
])
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
No migration is required unless you previously stripped this metadata yourself; that cleanup can now be simplified.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Structured final output
|
|
2
|
+
|
|
3
|
+
Pass a JSON Schema through `response_format` when the selected provider/model supports structured output:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
adapter.stream(messages, model: model, tools: tools, response_format: {
|
|
7
|
+
type: "json_schema",
|
|
8
|
+
json_schema: {
|
|
9
|
+
name: "repair_result",
|
|
10
|
+
strict: true,
|
|
11
|
+
schema: {
|
|
12
|
+
type: "object",
|
|
13
|
+
additionalProperties: false,
|
|
14
|
+
properties: {
|
|
15
|
+
outcome: { type: "string", enum: ["resolved", "needs_human"] },
|
|
16
|
+
message: { type: "string" }
|
|
17
|
+
},
|
|
18
|
+
required: ["outcome", "message"]
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
OpenAI Chat Completions and Groq receive this envelope directly. OpenAI Responses (including Codex) receives the definition under `text.format`; Anthropic receives the schema under `output_config.format`, preserving native output settings and reasoning effort. Symbol and JSON string keys are supported. Provider-native options remain available.
|
|
25
|
+
|
|
26
|
+
Anthropic does not support schema-less `json_object` mode: use an explicit schema. Older gateway versions incorrectly discarded the supplied schema and emitted an invalid `output_config.format` string.
|
|
27
|
+
|
|
28
|
+
This changes request translation, not response parsing or agent completion semantics. Tool-call turns remain ordinary tool calls; validate the final text against the application's result contract. A refusal, interruption, missing final message, or invalid JSON is not a successful structured result. Provider/model support for schemas alongside tools varies and is ultimately enforced by the provider.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LlmGateway
|
|
4
|
+
# Public, immutable description of an adapter. Definitions are useful when an
|
|
5
|
+
# application wants to pass an adapter choice around without using its string ID.
|
|
6
|
+
class AdapterDefinition
|
|
7
|
+
attr_reader :adapter_class, :provider, :client_class
|
|
8
|
+
|
|
9
|
+
def initialize(adapter_class)
|
|
10
|
+
@adapter_class = adapter_class
|
|
11
|
+
@provider = adapter_class.provider
|
|
12
|
+
@client_class = adapter_class.client_class
|
|
13
|
+
freeze
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def build(**config)
|
|
17
|
+
adapter_class.build(**config)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
module Adapters
|
|
22
|
+
# Adds the small public definition API to names such as
|
|
23
|
+
# Adapters::OpenAI::Responses without exposing adapter construction details.
|
|
24
|
+
module DefinitionFacade
|
|
25
|
+
def define_adapter(adapter_class)
|
|
26
|
+
raise ArgumentError, "Adapter definition already configured for #{self}" if @definition
|
|
27
|
+
|
|
28
|
+
@definition = AdapterDefinition.new(adapter_class)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def definition
|
|
32
|
+
@definition || raise("No adapter definition configured for #{self}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def build(**config)
|
|
36
|
+
definition.build(**config)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def adapter_class
|
|
40
|
+
definition.adapter_class
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def provider
|
|
44
|
+
definition.provider
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def supports_model?(model)
|
|
48
|
+
adapter_class.supports_model?(model)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def provider_model_key(model)
|
|
52
|
+
adapter_class.provider_model_key(model)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def model_for_provider_model_key(provider_model_key)
|
|
56
|
+
adapter_class.model_for_provider_model_key(provider_model_key)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def resolve_model!(model)
|
|
60
|
+
adapter_class.resolve_model!(model)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -5,44 +5,134 @@ require_relative "structs"
|
|
|
5
5
|
module LlmGateway
|
|
6
6
|
module Adapters
|
|
7
7
|
class Adapter
|
|
8
|
-
|
|
8
|
+
class << self
|
|
9
|
+
def provider(value = nil)
|
|
10
|
+
if value
|
|
11
|
+
if instance_variable_defined?(:@provider)
|
|
12
|
+
raise ArgumentError, "#{name} already declares a provider"
|
|
13
|
+
end
|
|
9
14
|
|
|
10
|
-
|
|
15
|
+
return @provider = value.to_s.freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
return @provider if instance_variable_defined?(:@provider)
|
|
19
|
+
return superclass.provider if superclass.respond_to?(:provider)
|
|
20
|
+
|
|
21
|
+
raise ArgumentError, "#{name} does not declare a provider"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def client_class(value = nil)
|
|
25
|
+
if value
|
|
26
|
+
if instance_variable_defined?(:@client_class)
|
|
27
|
+
raise ArgumentError, "#{name} already declares a client_class"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
return @client_class = value
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
return @client_class if instance_variable_defined?(:@client_class)
|
|
34
|
+
return superclass.client_class if superclass.respond_to?(:client_class)
|
|
35
|
+
|
|
36
|
+
raise ArgumentError, "#{name} does not declare a client_class"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def definition
|
|
40
|
+
@definition ||= AdapterDefinition.new(self)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def supports_model?(model)
|
|
44
|
+
model.provider == provider && !LlmGateway.models.find(provider: model.provider, id: model.id).nil?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def provider_model_key(model)
|
|
48
|
+
model.id
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def model_for_provider_model_key(provider_model_key)
|
|
52
|
+
LlmGateway.models.all(provider: provider).find do |model|
|
|
53
|
+
supports_model?(model) && self.provider_model_key(model).to_s == provider_model_key.to_s
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def resolve_model!(model)
|
|
58
|
+
if model.provider != provider
|
|
59
|
+
raise LlmGateway::Errors::ModelProviderMismatch,
|
|
60
|
+
"Model provider #{model.provider.inspect} does not match adapter provider #{provider.inspect}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
canonical = LlmGateway.models.find(provider: model.provider, id: model.id)
|
|
64
|
+
return canonical if canonical && supports_model?(canonical)
|
|
65
|
+
|
|
66
|
+
raise LlmGateway::Errors::UnsupportedModelForAdapter,
|
|
67
|
+
"Model #{model.provider}/#{model.id} is not supported by #{name}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def build(**config)
|
|
71
|
+
config = config.transform_keys(&:to_sym)
|
|
72
|
+
if config.key?(:model) || config.key?(:model_key)
|
|
73
|
+
raise ArgumentError, "Models are supplied to Adapter#stream, not Adapter.build"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
new(client_class.new(**config))
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
attr_reader :client
|
|
81
|
+
|
|
82
|
+
def initialize(client)
|
|
11
83
|
@client = client
|
|
12
|
-
@provider_key = provider_key
|
|
13
84
|
end
|
|
14
85
|
|
|
15
|
-
def
|
|
86
|
+
def provider
|
|
87
|
+
self.class.provider
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def raw_stream(message, model:, tools: nil, system: nil, **options, &block)
|
|
91
|
+
model = resolve_model!(model)
|
|
92
|
+
provider_model_key = self.class.provider_model_key(model)
|
|
16
93
|
normalized_input = map_input({
|
|
17
|
-
messages: sanitize_messages(normalize_messages(message), target_model:
|
|
94
|
+
messages: sanitize_messages(normalize_messages(message), target_model: provider_model_key),
|
|
18
95
|
tools: tools,
|
|
19
96
|
system: normalize_system(system)
|
|
20
97
|
})
|
|
21
98
|
|
|
99
|
+
mapped_options = options.merge(model: provider_model_key)
|
|
100
|
+
if mapped_options.key?(:reasoning)
|
|
101
|
+
reasoning = mapped_options.delete(:reasoning)
|
|
102
|
+
mapped_options[:reasoning_control] = model.reasoning_control_for(reasoning) unless reasoning.nil?
|
|
103
|
+
end
|
|
104
|
+
|
|
22
105
|
perform_stream(
|
|
23
106
|
normalized_input[:messages],
|
|
24
107
|
tools: normalized_input[:tools],
|
|
25
108
|
system: normalized_input[:system],
|
|
26
|
-
**map_options(
|
|
109
|
+
**map_options(mapped_options),
|
|
27
110
|
&block
|
|
28
111
|
)
|
|
29
112
|
end
|
|
30
113
|
|
|
31
|
-
def stream(message, tools: nil, system: nil, **options, &block)
|
|
114
|
+
def stream(message, model:, tools: nil, system: nil, **options, &block)
|
|
32
115
|
raise LlmGateway::Errors::MissingMapperForProvider, "No stream_mapper configured" unless stream_mapper
|
|
33
116
|
|
|
117
|
+
model = resolve_model!(model)
|
|
34
118
|
mapper = stream_mapper.new(
|
|
35
|
-
provider:
|
|
36
|
-
api: api_name
|
|
119
|
+
provider: provider,
|
|
120
|
+
api: api_name,
|
|
121
|
+
model_definition: model
|
|
37
122
|
)
|
|
38
123
|
|
|
39
|
-
raw_stream(message, tools: tools, system: system, **options) do |chunk|
|
|
124
|
+
raw_stream(message, model: model, tools: tools, system: system, **options) do |chunk|
|
|
40
125
|
mapper.map(chunk, &block)
|
|
41
126
|
end
|
|
42
127
|
|
|
43
128
|
mapper.result
|
|
44
129
|
end
|
|
45
130
|
|
|
131
|
+
# Resolves a candidate to its canonical catalog definition before streaming.
|
|
132
|
+
def resolve_model!(model)
|
|
133
|
+
self.class.resolve_model!(model)
|
|
134
|
+
end
|
|
135
|
+
|
|
46
136
|
def upload_file(filename:, content:, mime_type: "application/octet-stream", purpose: "assistants")
|
|
47
137
|
raise LlmGateway::Errors::MissingMapperForProvider, "No file_output_mapper configured" unless file_output_mapper
|
|
48
138
|
|
|
@@ -97,6 +187,7 @@ module LlmGateway
|
|
|
97
187
|
|
|
98
188
|
public
|
|
99
189
|
|
|
190
|
+
# Proxy adapters use these to normalize the target provider's raw stream.
|
|
100
191
|
def stream_api_name
|
|
101
192
|
api_name
|
|
102
193
|
end
|
|
@@ -118,15 +209,12 @@ module LlmGateway
|
|
|
118
209
|
def sanitize_messages(messages, target_model: nil)
|
|
119
210
|
return messages unless input_sanitizer
|
|
120
211
|
|
|
121
|
-
|
|
122
|
-
target_api = api_name
|
|
123
|
-
|
|
124
|
-
return messages unless target_provider.present? && target_api.present? && target_model.present?
|
|
212
|
+
return messages unless provider.present? && api_name.present? && target_model.present?
|
|
125
213
|
|
|
126
214
|
input_sanitizer.sanitize(
|
|
127
215
|
messages,
|
|
128
|
-
target_provider:
|
|
129
|
-
target_api:
|
|
216
|
+
target_provider: provider,
|
|
217
|
+
target_api: api_name,
|
|
130
218
|
target_model: target_model
|
|
131
219
|
)
|
|
132
220
|
end
|
|
@@ -144,11 +232,7 @@ module LlmGateway
|
|
|
144
232
|
end
|
|
145
233
|
|
|
146
234
|
def normalize_messages(message)
|
|
147
|
-
|
|
148
|
-
[ { role: "user", content: message } ]
|
|
149
|
-
else
|
|
150
|
-
message
|
|
151
|
-
end
|
|
235
|
+
message.is_a?(String) ? [ { role: "user", content: message } ] : message
|
|
152
236
|
end
|
|
153
237
|
end
|
|
154
238
|
end
|
|
@@ -12,8 +12,16 @@ module LlmGateway
|
|
|
12
12
|
module Adapters
|
|
13
13
|
module Anthropic
|
|
14
14
|
class MessagesAdapter < Adapter
|
|
15
|
+
provider "anthropic"
|
|
16
|
+
client_class LlmGateway::Clients::Anthropic
|
|
17
|
+
|
|
15
18
|
include ActsLikeAnthropicMessages
|
|
16
19
|
end
|
|
20
|
+
|
|
21
|
+
module Messages
|
|
22
|
+
extend DefinitionFacade
|
|
23
|
+
define_adapter MessagesAdapter
|
|
24
|
+
end
|
|
17
25
|
end
|
|
18
26
|
end
|
|
19
27
|
end
|
|
@@ -4,13 +4,6 @@ module LlmGateway
|
|
|
4
4
|
module Adapters
|
|
5
5
|
module AnthropicOptionMapper
|
|
6
6
|
DEFAULT_MAX_TOKENS = 20_480
|
|
7
|
-
REASONING_EFFORT_BUDGET_TOKENS = {
|
|
8
|
-
"low" => 1024,
|
|
9
|
-
"medium" => 5 * 1024,
|
|
10
|
-
"high" => 10 * 1024,
|
|
11
|
-
"xhigh" => 20 * 1024
|
|
12
|
-
}.freeze
|
|
13
|
-
|
|
14
7
|
# Source: https://platform.claude.com/docs/en/api/messages/create.md
|
|
15
8
|
# API: Anthropic Messages Create; accessed 2026-05-18.
|
|
16
9
|
# Body parameters listed by the API reference: max_tokens, messages, model,
|
|
@@ -39,7 +32,7 @@ module LlmGateway
|
|
|
39
32
|
].freeze
|
|
40
33
|
|
|
41
34
|
MANAGED_OPTIONS = %i[
|
|
42
|
-
|
|
35
|
+
reasoning_control
|
|
43
36
|
max_completion_tokens
|
|
44
37
|
response_format
|
|
45
38
|
cache_key
|
|
@@ -54,10 +47,11 @@ module LlmGateway
|
|
|
54
47
|
mapped_options[:max_tokens] = options[:max_completion_tokens] || DEFAULT_MAX_TOKENS
|
|
55
48
|
|
|
56
49
|
response_format = options[:response_format]
|
|
57
|
-
|
|
50
|
+
unless response_format.nil?
|
|
51
|
+
mapped_options[:output_config] = (mapped_options[:output_config] || {}).merge(normalize_output_config(response_format))
|
|
52
|
+
end
|
|
58
53
|
|
|
59
|
-
|
|
60
|
-
mapped_options[:thinking] = normalize_reasoning(reasoning) unless reasoning.nil? || reasoning.to_s == "none"
|
|
54
|
+
apply_reasoning_control!(mapped_options, options[:reasoning_control])
|
|
61
55
|
|
|
62
56
|
validate_options!(mapped_options)
|
|
63
57
|
mapped_options
|
|
@@ -76,19 +70,35 @@ module LlmGateway
|
|
|
76
70
|
format_type = response_format.is_a?(Hash) ? response_format[:type] || response_format["type"] : response_format
|
|
77
71
|
|
|
78
72
|
case format_type.to_s
|
|
79
|
-
when "
|
|
80
|
-
|
|
73
|
+
when "json_schema"
|
|
74
|
+
raise ArgumentError, "response_format json_schema requires a schema object" unless response_format.is_a?(Hash)
|
|
75
|
+
|
|
76
|
+
definition = response_format[:json_schema] || response_format["json_schema"] || response_format
|
|
77
|
+
schema = definition[:schema] || definition["schema"]
|
|
78
|
+
raise ArgumentError, "response_format json_schema requires a schema object" unless schema.is_a?(Hash)
|
|
79
|
+
|
|
80
|
+
{ format: { type: "json_schema", schema: schema } }
|
|
81
|
+
when "text"
|
|
82
|
+
{}
|
|
81
83
|
else
|
|
82
|
-
|
|
84
|
+
raise ArgumentError, "Unsupported Anthropic response_format: #{format_type}"
|
|
83
85
|
end
|
|
84
86
|
end
|
|
85
87
|
|
|
86
|
-
def
|
|
87
|
-
|
|
88
|
-
raise(ArgumentError,
|
|
89
|
-
"Invalid reasoning '#{reasoning}'. Use 'none', 'low', 'medium', 'high', or 'xhigh'.")
|
|
88
|
+
def apply_reasoning_control!(mapped_options, control)
|
|
89
|
+
return if control.nil? || control[:type] == :none
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
case control[:type]
|
|
92
|
+
when :budget_tokens
|
|
93
|
+
mapped_options[:thinking] = { type: "enabled", budget_tokens: control.fetch(:value) }
|
|
94
|
+
when :effort
|
|
95
|
+
mapped_options[:thinking] = { type: "adaptive" }
|
|
96
|
+
mapped_options[:output_config] = (mapped_options[:output_config] || {}).merge(effort: control.fetch(:value))
|
|
97
|
+
when :toggle
|
|
98
|
+
mapped_options[:thinking] = { type: "adaptive" } if control[:value]
|
|
99
|
+
else
|
|
100
|
+
raise ArgumentError, "Unsupported Anthropic reasoning control: #{control.inspect}"
|
|
101
|
+
end
|
|
92
102
|
end
|
|
93
103
|
end
|
|
94
104
|
end
|
|
@@ -11,6 +11,9 @@ module LlmGateway
|
|
|
11
11
|
module Adapters
|
|
12
12
|
module Groq
|
|
13
13
|
class ChatCompletionsAdapter < Adapter
|
|
14
|
+
provider "groq"
|
|
15
|
+
client_class LlmGateway::Clients::Groq
|
|
16
|
+
|
|
14
17
|
include ActsLikeOpenAIChatCompletions
|
|
15
18
|
|
|
16
19
|
private
|
|
@@ -43,6 +46,11 @@ module LlmGateway
|
|
|
43
46
|
mapped
|
|
44
47
|
end
|
|
45
48
|
end
|
|
49
|
+
|
|
50
|
+
module ChatCompletions
|
|
51
|
+
extend DefinitionFacade
|
|
52
|
+
define_adapter ChatCompletionsAdapter
|
|
53
|
+
end
|
|
46
54
|
end
|
|
47
55
|
end
|
|
48
56
|
end
|
|
@@ -6,7 +6,6 @@ module LlmGateway
|
|
|
6
6
|
module OptionMapper
|
|
7
7
|
DEFAULT_TEMPERATURE = 0
|
|
8
8
|
DEFAULT_MAX_COMPLETION_TOKENS = 20_480
|
|
9
|
-
VALID_REASONING_LEVELS = %w[default low medium high].freeze
|
|
10
9
|
|
|
11
10
|
# Source: https://console.groq.com/docs/text-chat.md and
|
|
12
11
|
# https://console.groq.com/docs/api-reference.md#chat-create
|
|
@@ -60,7 +59,7 @@ module LlmGateway
|
|
|
60
59
|
].freeze
|
|
61
60
|
|
|
62
61
|
MANAGED_OPTIONS = %i[
|
|
63
|
-
|
|
62
|
+
reasoning_control
|
|
64
63
|
cache_key
|
|
65
64
|
cache_retention
|
|
66
65
|
].freeze
|
|
@@ -73,9 +72,9 @@ module LlmGateway
|
|
|
73
72
|
mapped_options[:max_completion_tokens] = options[:max_completion_tokens] || DEFAULT_MAX_COMPLETION_TOKENS
|
|
74
73
|
mapped_options[:response_format] = normalize_response_format(options[:response_format] || "text")
|
|
75
74
|
|
|
76
|
-
|
|
77
|
-
unless
|
|
78
|
-
mapped_options[:reasoning_effort] =
|
|
75
|
+
reasoning_control = options[:reasoning_control]
|
|
76
|
+
unless reasoning_control.nil? || reasoning_control[:type] == :none
|
|
77
|
+
mapped_options[:reasoning_effort] = reasoning_effort(reasoning_control)
|
|
79
78
|
mapped_options[:reasoning_format] = "parsed"
|
|
80
79
|
end
|
|
81
80
|
|
|
@@ -100,11 +99,10 @@ module LlmGateway
|
|
|
100
99
|
end
|
|
101
100
|
end
|
|
102
101
|
|
|
103
|
-
def
|
|
104
|
-
|
|
105
|
-
return effort if VALID_REASONING_LEVELS.include?(effort)
|
|
102
|
+
def reasoning_effort(control)
|
|
103
|
+
return control.fetch(:value) if control[:type] == :effort
|
|
106
104
|
|
|
107
|
-
raise ArgumentError, "
|
|
105
|
+
raise ArgumentError, "Unsupported Groq reasoning control: #{control.inspect}"
|
|
108
106
|
end
|
|
109
107
|
end
|
|
110
108
|
end
|