squishling 0.5.0 → 0.6.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 +115 -0
- data/README.md +4 -4
- data/docs/adapters.md +94 -22
- data/docs/configuration.md +53 -6
- data/docs/failures.md +59 -13
- data/docs/harnesses.md +15 -1
- data/docs/measuring-tokens.md +6 -1
- data/docs/observers.md +5 -4
- data/docs/routing.md +11 -4
- data/docs/schemas.md +13 -0
- data/docs/testing.md +8 -4
- data/docs/verifying.md +4 -2
- data/lib/squishling/adapter/error_body.rb +9 -2
- data/lib/squishling/adapter.rb +73 -13
- data/lib/squishling/adapters/openai_compatible.rb +71 -53
- data/lib/squishling/adapters/openai_options.rb +45 -3
- data/lib/squishling/adapters/openai_transport.rb +123 -0
- data/lib/squishling/adapters/ruby_llm.rb +70 -16
- data/lib/squishling/budget.rb +11 -3
- data/lib/squishling/cache.rb +33 -0
- data/lib/squishling/class_methods.rb +23 -8
- data/lib/squishling/configuration.rb +27 -1
- data/lib/squishling/definition.rb +22 -7
- data/lib/squishling/errors.rb +7 -5
- data/lib/squishling/invoker.rb +24 -6
- data/lib/squishling/judge.rb +8 -2
- data/lib/squishling/llm_client.rb +3 -2
- data/lib/squishling/model_path.rb +12 -9
- data/lib/squishling/observer.rb +11 -4
- data/lib/squishling/params.rb +2 -2
- data/lib/squishling/result.rb +13 -0
- data/lib/squishling/router.rb +3 -2
- data/lib/squishling/run/reports.rb +63 -0
- data/lib/squishling/run.rb +6 -23
- data/lib/squishling/schema.rb +44 -4
- data/lib/squishling/secret.rb +45 -0
- data/lib/squishling/squawk.rb +7 -4
- data/lib/squishling/testing/adapter.rb +5 -3
- data/lib/squishling/testing/request.rb +7 -2
- data/lib/squishling/testing/state.rb +2 -1
- data/lib/squishling/version.rb +1 -1
- data/lib/squishling/wrapper.rb +19 -1
- data/lib/squishling.rb +8 -3
- metadata +5 -1
|
@@ -15,6 +15,9 @@ module Squishling
|
|
|
15
15
|
# parent's provider.
|
|
16
16
|
# params: are generation params merged over the configured defaults (see Configuration#default_params):
|
|
17
17
|
# squishling params: { temperature: 0.1, top_p: 0.9 }
|
|
18
|
+
# cache: asks the provider to cache the stable prompt (true by default, see Cache); false opts the class out, and
|
|
19
|
+
# a Hash such as { ttl: "1h" } passes options through:
|
|
20
|
+
# squishling cache: false
|
|
18
21
|
# append_to_purpose: adds sections after the purpose (see #append_to_purpose):
|
|
19
22
|
# squishling append_to_purpose: ["The Ruby that handles well-formed input:", self]
|
|
20
23
|
# harness: chooses how the escalation is used (see Harness):
|
|
@@ -22,14 +25,14 @@ module Squishling
|
|
|
22
25
|
# max_total_output_tokens:/max_total_input_tokens: cap the tokens one call may use across all its requests
|
|
23
26
|
# (see Budget); false lifts a cap inherited from the parent class or the configuration:
|
|
24
27
|
# squishling max_total_output_tokens: 25_000
|
|
25
|
-
# squawk: is called after each LLM attempt (narrowed by squawk_on:)
|
|
26
|
-
# Configuration#squawk); false silences
|
|
28
|
+
# squawk: is called after each LLM attempt (narrowed by squawk_on:), and once more when a token budget or a
|
|
29
|
+
# disagreement stops the call, with the raw output (see Configuration#squawk); false silences
|
|
27
30
|
# an inherited one:
|
|
28
31
|
# squishling squawk: ->(output:, metadata:, error:) { Tracer.record(output, metadata, error) }
|
|
29
32
|
# squawk_on: limits which attempts reach the hook: :all (the default), :rejections, :retries, :final, or a
|
|
30
33
|
# list of them (any one holding runs the hook):
|
|
31
34
|
# squishling squawk: hook, squawk_on: %i[rejections final]
|
|
32
|
-
def squishling(model: nil, escalation: nil, provider: nil, params: nil, harness: nil, purpose: nil,
|
|
35
|
+
def squishling(model: nil, escalation: nil, provider: nil, params: nil, cache: nil, harness: nil, purpose: nil,
|
|
33
36
|
append_to_purpose: nil, output_schema: nil, squawk: nil, squawk_on: nil, max_total_output_tokens: nil,
|
|
34
37
|
max_total_input_tokens: nil)
|
|
35
38
|
path = ModelPath.declare(model, escalation, to_s)
|
|
@@ -44,8 +47,7 @@ module Squishling
|
|
|
44
47
|
# provider meant for its parent's.
|
|
45
48
|
@squishling_model_path = path if path
|
|
46
49
|
@squishling_provider = provider if provider || path
|
|
47
|
-
|
|
48
|
-
@squishling_harness = Harness.normalize(harness, to_s) unless harness.nil?
|
|
50
|
+
declare_generation(params, cache, harness)
|
|
49
51
|
self.purpose(purpose) if purpose
|
|
50
52
|
self.append_to_purpose(append_to_purpose) unless append_to_purpose.nil?
|
|
51
53
|
self.output_schema(output_schema) if output_schema
|
|
@@ -76,6 +78,11 @@ module Squishling
|
|
|
76
78
|
squishling_lookup(:@squishling_provider)
|
|
77
79
|
end
|
|
78
80
|
|
|
81
|
+
# The class's (or nearest ancestor's) cache setting (see Cache), nil when none declared it.
|
|
82
|
+
def squishling_cache
|
|
83
|
+
squishling_lookup(:@squishling_cache)
|
|
84
|
+
end
|
|
85
|
+
|
|
79
86
|
def squishling_harness
|
|
80
87
|
squishling_lookup(:@squishling_harness)
|
|
81
88
|
end
|
|
@@ -171,13 +178,14 @@ module Squishling
|
|
|
171
178
|
# string :priority
|
|
172
179
|
# end
|
|
173
180
|
def squish(*names, purpose: nil, append_to_purpose: nil, output_schema: nil, model: nil, escalation: nil,
|
|
174
|
-
provider: nil, params: nil,
|
|
175
|
-
squawk_on: nil, max_total_output_tokens: nil, max_total_input_tokens: nil, &schema_block)
|
|
181
|
+
provider: nil, params: nil, cache: nil, harness: nil, when: nil, fallback: nil, validate: nil,
|
|
182
|
+
squawk: nil, squawk_on: nil, max_total_output_tokens: nil, max_total_input_tokens: nil, &schema_block)
|
|
176
183
|
schema = schema_block ? Schematist::Schema.create(&schema_block) : output_schema
|
|
177
184
|
model = ModelPath.declare(model, escalation, "#{self} squish")
|
|
178
185
|
raise ConfigurationError, "#{self} squish: provider: needs a model: or escalation:" if provider && !model
|
|
179
186
|
|
|
180
187
|
params &&= Params.normalize(params, "#{self} squish params")
|
|
188
|
+
cache = Cache.normalize(cache, "#{self} squish cache")
|
|
181
189
|
harness = Harness.normalize(harness, "#{self} squish") unless harness.nil?
|
|
182
190
|
unless append_to_purpose.nil?
|
|
183
191
|
append_to_purpose = Appendices.normalize(append_to_purpose, "#{self} squish append_to_purpose")
|
|
@@ -185,7 +193,7 @@ module Squishling
|
|
|
185
193
|
squawk = Squawk.validate(squawk, "#{self} squish")
|
|
186
194
|
squawk_on = Squawk.validate_on(squawk_on, "#{self} squish")
|
|
187
195
|
budget = Budget.build("#{self} squish", output: max_total_output_tokens, input: max_total_input_tokens)
|
|
188
|
-
options = { purpose:, append_to_purpose:, output_schema: schema, model:, provider:, params:, harness:,
|
|
196
|
+
options = { purpose:, append_to_purpose:, output_schema: schema, model:, provider:, params:, cache:, harness:,
|
|
189
197
|
predicate: binding.local_variable_get(:when), fallback:, validator: validate, squawk:,
|
|
190
198
|
squawk_on:, budget: }.compact
|
|
191
199
|
|
|
@@ -216,6 +224,13 @@ module Squishling
|
|
|
216
224
|
|
|
217
225
|
private
|
|
218
226
|
|
|
227
|
+
# nil leaves a level unset (so the parent's applies); false for cache: is stored, to opt the class out.
|
|
228
|
+
def declare_generation(params, cache, harness)
|
|
229
|
+
@squishling_params = Params.normalize(params, "#{self} params") if params
|
|
230
|
+
@squishling_cache = Cache.normalize(cache, "#{self} cache") unless cache.nil?
|
|
231
|
+
@squishling_harness = Harness.normalize(harness, to_s) unless harness.nil?
|
|
232
|
+
end
|
|
233
|
+
|
|
219
234
|
# nil leaves a level unset (so the parent's applies); false for squawk: is stored, to silence it.
|
|
220
235
|
def declare_squawk(squawk, squawk_on)
|
|
221
236
|
@squishling_squawk = Squawk.validate(squawk, to_s) unless squawk.nil?
|
|
@@ -24,6 +24,14 @@ module Squishling
|
|
|
24
24
|
# adapter for its provider; any other key (top_p, seed, ...) is passed to the provider as-is.
|
|
25
25
|
attr_reader :default_params
|
|
26
26
|
|
|
27
|
+
# Whether requests ask the provider to cache the stable prompt: true (the default), false, or a Hash of options
|
|
28
|
+
# such as { ttl: "1h" } (see Cache). Overridable per class, per method, per escalation step, and per squish! call.
|
|
29
|
+
attr_reader :default_cache
|
|
30
|
+
|
|
31
|
+
# Whether the tokens a request reads from the provider's prompt cache count toward max_total_input_tokens: false
|
|
32
|
+
# (the default) leaves them out, true counts them. Tokens written to the cache always count (see Budget).
|
|
33
|
+
attr_reader :budget_counts_cache_reads
|
|
34
|
+
|
|
27
35
|
# How every squishling class that doesn't declare its own harness uses its escalation (see Harness):
|
|
28
36
|
# :escalation (the default), :squishsum, :judged_squishsum, a harness you registered, or a Hash with type: and
|
|
29
37
|
# its options.
|
|
@@ -37,7 +45,8 @@ module Squishling
|
|
|
37
45
|
# Optional Logger for routing and escalation decisions.
|
|
38
46
|
attr_accessor :logger
|
|
39
47
|
|
|
40
|
-
# Optional callable run after each LLM attempt (narrowed by squawk_on)
|
|
48
|
+
# Optional callable run after each LLM attempt (narrowed by squawk_on), and once more when a token budget or a
|
|
49
|
+
# disagreement stops the call, with the raw output (see Squawk), e.g.
|
|
41
50
|
# ->(output:, metadata:, error:) { Tracer.record(output, metadata, error) }
|
|
42
51
|
attr_reader :squawk
|
|
43
52
|
|
|
@@ -66,6 +75,8 @@ module Squishling
|
|
|
66
75
|
@default_model_path = nil
|
|
67
76
|
@default_provider = nil
|
|
68
77
|
@default_params = {}
|
|
78
|
+
@default_cache = true
|
|
79
|
+
@budget_counts_cache_reads = false
|
|
69
80
|
@default_harness = nil
|
|
70
81
|
@logger = nil
|
|
71
82
|
@squawk = nil
|
|
@@ -130,6 +141,21 @@ module Squishling
|
|
|
130
141
|
@default_params = Params.normalize(params, "default_params")
|
|
131
142
|
end
|
|
132
143
|
|
|
144
|
+
# nil restores the default (true).
|
|
145
|
+
def default_cache=(cache)
|
|
146
|
+
@default_cache = cache.nil? || Cache.normalize(cache, "default_cache")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# true or false; unlike default_cache=, nil is not accepted.
|
|
150
|
+
def budget_counts_cache_reads=(counts)
|
|
151
|
+
unless [true, false].include?(counts)
|
|
152
|
+
raise ConfigurationError,
|
|
153
|
+
"Squishling.configure: budget_counts_cache_reads must be true or false, got #{counts.inspect}"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
@budget_counts_cache_reads = counts
|
|
157
|
+
end
|
|
158
|
+
|
|
133
159
|
def default_harness=(harness)
|
|
134
160
|
@default_harness = harness.nil? ? nil : Harness.normalize(harness, "default_harness")
|
|
135
161
|
end
|
|
@@ -13,8 +13,8 @@ module Squishling
|
|
|
13
13
|
attr_reader :klass, :name, :call_context, :handed_off
|
|
14
14
|
|
|
15
15
|
def initialize(klass:, name:, purpose: nil, append_to_purpose: nil, output_schema: nil, model: nil,
|
|
16
|
-
provider: nil, params: nil,
|
|
17
|
-
squawk_on: nil, budget: nil, call_context: {}, handed_off: false)
|
|
16
|
+
provider: nil, params: nil, cache: nil, harness: nil, predicate: nil, fallback: nil, validator: nil,
|
|
17
|
+
squawk: nil, squawk_on: nil, budget: nil, call_context: {}, handed_off: false)
|
|
18
18
|
@klass = klass
|
|
19
19
|
@name = name
|
|
20
20
|
@purpose = purpose
|
|
@@ -23,6 +23,7 @@ module Squishling
|
|
|
23
23
|
@model = model
|
|
24
24
|
@provider = provider
|
|
25
25
|
@params = params
|
|
26
|
+
@cache = cache
|
|
26
27
|
@harness = harness
|
|
27
28
|
@predicate = predicate
|
|
28
29
|
@fallback = fallback
|
|
@@ -37,15 +38,16 @@ module Squishling
|
|
|
37
38
|
# This definition with one call's overrides on top. The output schema, predicate, fallback, and validator
|
|
38
39
|
# can't be overridden: the call must still return the method's result type.
|
|
39
40
|
def for_call(purpose: nil, append_to_purpose: nil, context: nil, model: nil, escalation: nil,
|
|
40
|
-
provider: nil, params: nil,
|
|
41
|
+
provider: nil, params: nil, cache: nil, harness: nil, max_total_output_tokens: nil,
|
|
42
|
+
max_total_input_tokens: nil)
|
|
41
43
|
call_path = ModelPath.declare(model, escalation, "#{label} squish!")
|
|
42
44
|
raise ConfigurationError, "#{label}: squish! provider: needs a model: or escalation:" if provider && !call_path
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
end
|
|
45
|
+
|
|
46
|
+
check_context!(context)
|
|
46
47
|
|
|
47
48
|
appended = Appendices.normalize(append_to_purpose, "#{label} squish!") unless append_to_purpose.nil?
|
|
48
49
|
call_params = params && Params.normalize(params, "#{label} squish! params")
|
|
50
|
+
call_cache = Cache.normalize(cache, "#{label} squish! cache")
|
|
49
51
|
call_harness = Harness.normalize(harness, "#{label} squish!") unless harness.nil?
|
|
50
52
|
call_budget = Budget.build("#{label} squish!", output: max_total_output_tokens, input: max_total_input_tokens)
|
|
51
53
|
self.class.new(
|
|
@@ -56,6 +58,7 @@ module Squishling
|
|
|
56
58
|
model: call_path || @model, provider: call_path ? provider : @provider,
|
|
57
59
|
# merge, not Params.resolve: a nil at the method level must still unset the class's key.
|
|
58
60
|
params: call_params ? (@params || {}).merge(call_params) : @params,
|
|
61
|
+
cache: [call_cache, @cache].find { |level| !level.nil? },
|
|
59
62
|
harness: call_harness || @harness, budget: @budget.merge(call_budget),
|
|
60
63
|
call_context: @call_context.merge((context || {}).transform_keys(&:to_sym)),
|
|
61
64
|
handed_off: true # only squish! builds a definition this way; it is not part of a declared one
|
|
@@ -101,7 +104,7 @@ module Squishling
|
|
|
101
104
|
else
|
|
102
105
|
0
|
|
103
106
|
end
|
|
104
|
-
ModelPath.steps(eligible, provider:, params:, turn:)
|
|
107
|
+
ModelPath.steps(eligible, provider:, params:, cache:, turn:)
|
|
105
108
|
end
|
|
106
109
|
|
|
107
110
|
# The steps and provider of the first level (method, class, or config) that declares a model or escalation, or
|
|
@@ -139,6 +142,12 @@ module Squishling
|
|
|
139
142
|
Params.resolve(Squishling.config.default_params, klass.squishling_params, @params)
|
|
140
143
|
end
|
|
141
144
|
|
|
145
|
+
# Prompt caching: false, or the options to ask the provider for (see Cache). The config default, overridden by the
|
|
146
|
+
# class, then the method (and a squish! call).
|
|
147
|
+
def cache
|
|
148
|
+
Cache.resolve(Squishling.config.default_cache, klass.squishling_cache, @cache)
|
|
149
|
+
end
|
|
150
|
+
|
|
142
151
|
# The token limits of one call (see Budget): config defaults, overridden limit by limit by the class, then by the
|
|
143
152
|
# method (and a squish! call).
|
|
144
153
|
def budget
|
|
@@ -247,6 +256,12 @@ module Squishling
|
|
|
247
256
|
|
|
248
257
|
private
|
|
249
258
|
|
|
259
|
+
def check_context!(context)
|
|
260
|
+
return if context.nil? || (context.is_a?(Hash) && context.each_key.all?(NAME_KEY))
|
|
261
|
+
|
|
262
|
+
raise ConfigurationError, "#{label}: squish! context: must be a Hash with String or Symbol keys"
|
|
263
|
+
end
|
|
264
|
+
|
|
250
265
|
# Values JSON can't represent (NaN, Infinity, cycles) are invalid output, not a raw JSON error.
|
|
251
266
|
def jsonify_return(attrs)
|
|
252
267
|
[Schema.jsonify(attrs), []]
|
data/lib/squishling/errors.rb
CHANGED
|
@@ -7,10 +7,11 @@ module Squishling
|
|
|
7
7
|
# missing API key). Never retried and never passed to squish_fallback.
|
|
8
8
|
class ConfigurationError < Error; end
|
|
9
9
|
|
|
10
|
-
# The provider answered a request with HTTP 400 (or another 4xx that isn't a credentials, model,
|
|
11
|
-
#
|
|
12
|
-
# never
|
|
13
|
-
#
|
|
10
|
+
# The provider answered a request with HTTP 400 (or another 4xx that isn't a credentials, billing, model, redirect,
|
|
11
|
+
# or too-long-input problem; the adapter decides which, see Adapter::Base#http_failure). Still a ConfigurationError:
|
|
12
|
+
# never retried, escalated, or passed to a fallback. The provider's message is never repeated; the machine-readable
|
|
13
|
+
# status and code its error body carries are, when they are plain tokens (e.g. "INVALID_ARGUMENT",
|
|
14
|
+
# "invalid_request_error"). Both are nil when the body has none.
|
|
14
15
|
class RequestRejectedError < ConfigurationError
|
|
15
16
|
attr_reader :provider_status, :provider_code
|
|
16
17
|
|
|
@@ -130,6 +131,7 @@ module Squishling
|
|
|
130
131
|
end
|
|
131
132
|
|
|
132
133
|
# The LLM call itself failed (rate limit, server error, timeout, connection) after the
|
|
133
|
-
# adapter's own HTTP retries.
|
|
134
|
+
# adapter's own HTTP retries. #cause is the adapter's: a sanitized stand-in (Adapter::ProviderFailure) for a provider
|
|
135
|
+
# exception that carries a response body, or the transport error itself when it carries none.
|
|
134
136
|
class LLMError < Error; end
|
|
135
137
|
end
|
data/lib/squishling/invoker.rb
CHANGED
|
@@ -25,13 +25,14 @@ module Squishling
|
|
|
25
25
|
Built = Data.define(:prompt, :payload, :purpose)
|
|
26
26
|
|
|
27
27
|
# Runs the harness (Definition#harness) over a Run of the call: the harness decides how the escalation is
|
|
28
|
-
# used, and what it returns is checked against the output schema's type.
|
|
28
|
+
# used, and what it returns is checked against the output schema's type. A call that ends on the token budget
|
|
29
|
+
# or a disagreement is reported to squawk once here, as the stop no attempt of its own reported.
|
|
29
30
|
def call
|
|
30
31
|
built = build
|
|
31
32
|
harness = @definition.harness
|
|
32
33
|
run = Run.new(definition: @definition, receiver: @receiver, inputs: @inputs, prompt: built.prompt,
|
|
33
34
|
payload: built.payload, purpose: built.purpose, harness:)
|
|
34
|
-
accepted(harness
|
|
35
|
+
accepted(call_harness(harness, run), run, harness)
|
|
35
36
|
end
|
|
36
37
|
|
|
37
38
|
# The first request the call would make (system prompt, output schema, user message), without making it.
|
|
@@ -41,6 +42,13 @@ module Squishling
|
|
|
41
42
|
|
|
42
43
|
private
|
|
43
44
|
|
|
45
|
+
def call_harness(harness, run)
|
|
46
|
+
harness.klass.new.call(run, **harness.options)
|
|
47
|
+
rescue BudgetExhaustedError, DisagreementError => e
|
|
48
|
+
run.report_stop(e)
|
|
49
|
+
raise
|
|
50
|
+
end
|
|
51
|
+
|
|
44
52
|
def build
|
|
45
53
|
purpose = @definition.purpose(@receiver)
|
|
46
54
|
schema = @definition.schema
|
|
@@ -70,16 +78,26 @@ module Squishling
|
|
|
70
78
|
end
|
|
71
79
|
|
|
72
80
|
# Exceptions are sent as their class and message. Plain JSON would send only the message, and with
|
|
73
|
-
# json/add/exception loaded it would also send the backtrace, which exposes file paths.
|
|
74
|
-
|
|
81
|
+
# json/add/exception loaded it would also send the backtrace, which exposes file paths. path is the Hashes and
|
|
82
|
+
# Arrays being walked: one that contains itself is a ConfigurationError rather than a SystemStackError.
|
|
83
|
+
def describe(value, path = [])
|
|
75
84
|
case value
|
|
76
85
|
when Exception then { class: exception_class_name(value.class), message: value.message }
|
|
77
|
-
when Hash then value.transform_values { |item| describe(item) }
|
|
78
|
-
when Array then value.map { |item| describe(item) }
|
|
86
|
+
when Hash then within(value, path) { |inner| value.transform_values { |item| describe(item, inner) } }
|
|
87
|
+
when Array then within(value, path) { |inner| value.map { |item| describe(item, inner) } }
|
|
79
88
|
else value
|
|
80
89
|
end
|
|
81
90
|
end
|
|
82
91
|
|
|
92
|
+
def within(container, path)
|
|
93
|
+
if path.any? { |ancestor| ancestor.equal?(container) }
|
|
94
|
+
raise ConfigurationError, "#{@definition.label}: an argument or context value contains itself, " \
|
|
95
|
+
"so it can't be sent as JSON"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
yield path + [container]
|
|
99
|
+
end
|
|
100
|
+
|
|
83
101
|
# An anonymous error class (Class.new(StandardError)) is named after its closest named ancestor.
|
|
84
102
|
def exception_class_name(klass)
|
|
85
103
|
klass = klass.superclass until klass.name
|
data/lib/squishling/judge.rb
CHANGED
|
@@ -111,6 +111,11 @@ module Squishling
|
|
|
111
111
|
value.is_a?(String) && !value.strip.empty?
|
|
112
112
|
end
|
|
113
113
|
|
|
114
|
+
# A judgment is not a chat, so it has nothing to cache whatever its step says.
|
|
115
|
+
def judgment_steps(judge)
|
|
116
|
+
ModelPath.steps([judge], provider: nil, params: {}).map { |step| step.with(cache: false) }
|
|
117
|
+
end
|
|
118
|
+
|
|
114
119
|
def judgment?
|
|
115
120
|
@judge&.fetch(:type) == :judgment
|
|
116
121
|
end
|
|
@@ -119,8 +124,9 @@ module Squishling
|
|
|
119
124
|
# under its own, like any escalation step; a judgment judge sends only its own params, as provider options.
|
|
120
125
|
def resolve_steps(path)
|
|
121
126
|
if (judge = @judge)
|
|
122
|
-
|
|
123
|
-
|
|
127
|
+
return judgment_steps(judge) if judge[:type] == :judgment
|
|
128
|
+
|
|
129
|
+
return ModelPath.steps([judge], provider: nil, params: @definition.params, cache: @definition.cache)
|
|
124
130
|
end
|
|
125
131
|
|
|
126
132
|
rest = path.drop_while { |step| step == path.first }
|
|
@@ -16,7 +16,7 @@ module Squishling
|
|
|
16
16
|
@harness = harness
|
|
17
17
|
@handed_off = handed_off
|
|
18
18
|
@adapter = Squishling.config.resolved_adapter
|
|
19
|
-
@tally = Budget::Tally.new(budget)
|
|
19
|
+
@tally = Budget::Tally.new(budget, cache_reads: Squishling.config.budget_counts_cache_reads)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# The budget limit this client's requests have reached ({ limit:, max:, used: }), or nil. Checked before each
|
|
@@ -89,7 +89,8 @@ module Squishling
|
|
|
89
89
|
def complete(chat, message, step, reask)
|
|
90
90
|
messages = chat.messages + [{ role: :user, content: message }]
|
|
91
91
|
request = Adapter::Request.new(label: @label, model: step.model, provider: step.provider,
|
|
92
|
-
instructions: chat.instructions, messages:, schema: chat.schema, params: step.request_params(reask)
|
|
92
|
+
instructions: chat.instructions, messages:, schema: chat.schema, params: step.request_params(reask),
|
|
93
|
+
cache: step.cache)
|
|
93
94
|
response = @adapter.complete(request)
|
|
94
95
|
unless response.is_a?(Adapter::Response)
|
|
95
96
|
raise ConfigurationError, "#{@label}: the #{@adapter.class} adapter must return a " \
|
|
@@ -17,9 +17,10 @@ module Squishling
|
|
|
17
17
|
# retry_temperature: (a Numeric) is the temperature for the step's same-chat retries after the first attempt.
|
|
18
18
|
module ModelPath
|
|
19
19
|
# One attempt: the model, its provider, the fully resolved generation params, whether the previous
|
|
20
|
-
# step's rejected output is shown to it,
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
# step's rejected output is shown to it, the temperature its retries in the same chat use, if any, and the
|
|
21
|
+
# prompt caching options to request (false for none, see Cache).
|
|
22
|
+
Step = Data.define(:model, :provider, :params, :forward_rejected, :retry_temperature, :cache) do
|
|
23
|
+
def initialize(model:, provider:, params:, forward_rejected:, retry_temperature: nil, cache: false)
|
|
23
24
|
super
|
|
24
25
|
end
|
|
25
26
|
|
|
@@ -35,7 +36,7 @@ module Squishling
|
|
|
35
36
|
end
|
|
36
37
|
end
|
|
37
38
|
|
|
38
|
-
STEP_KEYS = %i[model provider params attempts priority order rotate forward_rejected include_when
|
|
39
|
+
STEP_KEYS = %i[model provider params cache attempts priority order rotate forward_rejected include_when
|
|
39
40
|
retry_temperature].freeze
|
|
40
41
|
|
|
41
42
|
module_function
|
|
@@ -72,13 +73,14 @@ module Squishling
|
|
|
72
73
|
end
|
|
73
74
|
|
|
74
75
|
# One Step per attempt. The level's provider applies to steps that don't name their own, and each
|
|
75
|
-
# step's params are merged over the level-resolved params
|
|
76
|
+
# step's params are merged over the level-resolved params, and its cache: replaces the level-resolved one (a Hash
|
|
77
|
+
# or false, see Cache). turn is this call's place in the method's rotation
|
|
76
78
|
# (see Rotation): each tier of tied priorities starts turn steps further along.
|
|
77
|
-
def steps(path, provider:, params:, turn: 0)
|
|
79
|
+
def steps(path, provider:, params:, cache: false, turn: 0)
|
|
78
80
|
rotated(path, turn).flat_map do |step|
|
|
79
81
|
attempt = Step.new(model: step[:model], provider: step[:provider] || provider,
|
|
80
82
|
params: Params.resolve(params, step[:params]), forward_rejected: step[:forward_rejected],
|
|
81
|
-
retry_temperature: step[:retry_temperature])
|
|
83
|
+
retry_temperature: step[:retry_temperature], cache: Cache.resolve(cache, step[:cache]))
|
|
82
84
|
[attempt] * step[:attempts]
|
|
83
85
|
end
|
|
84
86
|
end
|
|
@@ -131,7 +133,7 @@ module Squishling
|
|
|
131
133
|
def normalize_step(step, label, extras: [])
|
|
132
134
|
case step
|
|
133
135
|
when String, Symbol
|
|
134
|
-
{ model: model_name(step, label), provider: nil, params: nil, attempts: 1, forward_rejected: true }
|
|
136
|
+
{ model: model_name(step, label), provider: nil, params: nil, cache: nil, attempts: 1, forward_rejected: true }
|
|
135
137
|
when Hash then normalize_hash(step, label, extras)
|
|
136
138
|
else
|
|
137
139
|
raise ConfigurationError, "#{label}: each step must be a model name or a Hash with model:, got #{step.inspect}"
|
|
@@ -169,7 +171,8 @@ module Squishling
|
|
|
169
171
|
end
|
|
170
172
|
|
|
171
173
|
params = step[:params] && Params.normalize(step[:params], "#{label} (#{model}) params")
|
|
172
|
-
|
|
174
|
+
cache = Cache.normalize(step[:cache], "#{label} (#{model}) cache")
|
|
175
|
+
normalized = { model:, provider: step[:provider], params:, cache:, attempts:, forward_rejected: }
|
|
173
176
|
normalized.merge(step.slice(:priority, :rotate, :include_when, :retry_temperature, *extras))
|
|
174
177
|
end
|
|
175
178
|
|
data/lib/squishling/observer.rb
CHANGED
|
@@ -17,8 +17,13 @@ module Squishling
|
|
|
17
17
|
:request)
|
|
18
18
|
|
|
19
19
|
# What an observer's block returns: the request's token usage and the model that answered, never the content.
|
|
20
|
-
# A count the provider didn't report is nil.
|
|
21
|
-
|
|
20
|
+
# A count the provider didn't report is nil. cache_read_tokens and cache_write_tokens are the input tokens served
|
|
21
|
+
# from, and written to, the provider's prompt cache (see Cache), so a hit is visible.
|
|
22
|
+
Usage = Data.define(:input_tokens, :output_tokens, :model, :cache_read_tokens, :cache_write_tokens) do
|
|
23
|
+
def initialize(input_tokens:, output_tokens:, model:, cache_read_tokens: nil, cache_write_tokens: nil)
|
|
24
|
+
super
|
|
25
|
+
end
|
|
26
|
+
end
|
|
22
27
|
|
|
23
28
|
BUILT_IN = %i[coolhand].freeze
|
|
24
29
|
|
|
@@ -33,12 +38,12 @@ module Squishling
|
|
|
33
38
|
return observer if BUILT_IN.include?(observer)
|
|
34
39
|
|
|
35
40
|
raise ConfigurationError, "#{where} observer must be one of #{BUILT_IN.map(&:inspect).join(', ')}, " \
|
|
36
|
-
"an object that responds to around, or false, got #{observer
|
|
41
|
+
"an object that responds to around, or false, got #{Appendices.describe(observer)}"
|
|
37
42
|
end
|
|
38
43
|
return observer if observer.respond_to?(:around)
|
|
39
44
|
|
|
40
45
|
raise ConfigurationError, "#{where} observer must respond to around(call) { ... } (or be false), " \
|
|
41
|
-
"got #{observer
|
|
46
|
+
"got #{Appendices.describe(observer)}"
|
|
42
47
|
end
|
|
43
48
|
|
|
44
49
|
# The observer that wraps a request: the configured one, the Coolhand observer when nothing is configured and
|
|
@@ -96,6 +101,8 @@ module Squishling
|
|
|
96
101
|
model = response.respond_to?(:model) ? response.model : nil
|
|
97
102
|
Usage.new(input_tokens: token_count(tokens, :input_tokens, :input),
|
|
98
103
|
output_tokens: token_count(tokens, :output_tokens, :output),
|
|
104
|
+
cache_read_tokens: token_count(tokens, :cache_read_tokens, :cache_read),
|
|
105
|
+
cache_write_tokens: token_count(tokens, :cache_write_tokens, :cache_write),
|
|
99
106
|
model: model.respond_to?(:id) ? model.id : model)
|
|
100
107
|
end
|
|
101
108
|
|
data/lib/squishling/params.rb
CHANGED
|
@@ -9,8 +9,8 @@ module Squishling
|
|
|
9
9
|
# adapter builds, so these would silently replace the model, the conversation, or the strict output format.
|
|
10
10
|
RESERVED_KEYS = %i[
|
|
11
11
|
model messages input inputs instructions contents system system_instruction systemInstruction cachedContent
|
|
12
|
-
stream stream_options store include response_format text output_config outputConfig tools
|
|
13
|
-
toolConfig tool_config schema functions function_call
|
|
12
|
+
cached_content stream stream_options store include response_format text output_config outputConfig tools
|
|
13
|
+
tool_choice toolConfig tool_config schema functions function_call
|
|
14
14
|
].freeze
|
|
15
15
|
|
|
16
16
|
# Some protocols carry the structured-output format (or tool selection) inside a container that is
|
data/lib/squishling/result.rb
CHANGED
|
@@ -74,6 +74,7 @@ module Squishling
|
|
|
74
74
|
|
|
75
75
|
return @classes[schema] if @classes.key?(schema)
|
|
76
76
|
|
|
77
|
+
require_object_type!(schema)
|
|
77
78
|
klass = @classes[schema] = define_class(schema)
|
|
78
79
|
# Memoized first so recursive schemas stop; typing the fields builds nested classes now, so a bad
|
|
79
80
|
# property name in any object the result types (nested, array items, resolved $refs) raises at schema build.
|
|
@@ -83,6 +84,18 @@ module Squishling
|
|
|
83
84
|
|
|
84
85
|
private
|
|
85
86
|
|
|
87
|
+
# `properties` alone constrains objects only, so a schema without `type: "object"` accepts a String or Array
|
|
88
|
+
# that would then sit in the typed result where a nested result is expected. A raw Hash schema must say so.
|
|
89
|
+
def require_object_type!(schema)
|
|
90
|
+
type = schema["type"]
|
|
91
|
+
return if type == "object" || (type.is_a?(Array) && type.include?("object"))
|
|
92
|
+
|
|
93
|
+
raise ConfigurationError,
|
|
94
|
+
"output_schema has a schema with `properties` whose `type` isn't \"object\" (or missing), so a non-object " \
|
|
95
|
+
"value would pass validation and end up in the typed result. Set `type: \"object\"` on it " \
|
|
96
|
+
"(a union such as [\"object\", \"null\"] is also fine)"
|
|
97
|
+
end
|
|
98
|
+
|
|
86
99
|
def fields_for(schema)
|
|
87
100
|
schema["properties"].to_h { |key, property| [key.to_sym, converter_for(property)] }
|
|
88
101
|
end
|
data/lib/squishling/router.rb
CHANGED
|
@@ -25,9 +25,10 @@ module Squishling
|
|
|
25
25
|
begin
|
|
26
26
|
frame.phase = :ruby
|
|
27
27
|
value = impl.call
|
|
28
|
-
rescue NotImplementedError
|
|
28
|
+
rescue NotImplementedError => e
|
|
29
29
|
# After squish!, the error came from the LLM path (a fallback, a proc), not a missing implementation.
|
|
30
|
-
|
|
30
|
+
# Otherwise it propagates unless it is the method's own stub (see Wrapper.stub?).
|
|
31
|
+
raise if frame.handed_off || !Wrapper.stub?(e)
|
|
31
32
|
|
|
32
33
|
return route_to_llm(frame, definition, "not implemented")
|
|
33
34
|
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Squishling
|
|
4
|
+
class Run
|
|
5
|
+
# What a Run tells the squawk hook (see Squawk): the report of each attempt, and the one final report of a call that
|
|
6
|
+
# a token budget or a disagreement stopped.
|
|
7
|
+
module Reports
|
|
8
|
+
# Set on a stop error once it has been reported (see #report_stop).
|
|
9
|
+
REPORTED = :@squawk_reported
|
|
10
|
+
private_constant :REPORTED
|
|
11
|
+
|
|
12
|
+
# Reports to squawk the BudgetExhaustedError or DisagreementError that stopped the call: one final report with
|
|
13
|
+
# the error, for an ending that no attempt of its own reported (the attempt before a budget stop was already
|
|
14
|
+
# reported, as not final, and a disagreement's samples were both accepted). Nothing was sent, so there is no
|
|
15
|
+
# output, and the provider and params are unknown (the stop comes from the harness, not from a step).
|
|
16
|
+
# Internal: Invoker calls it once, whatever the harness, so a custom harness has nothing to do.
|
|
17
|
+
def report_stop(error)
|
|
18
|
+
hook = @definition.squawk
|
|
19
|
+
attempts = error.attempts.to_i
|
|
20
|
+
return if error.instance_variable_get(REPORTED) || !hook
|
|
21
|
+
return unless Squawk.report?(@definition.squawk_on, attempts, error, true)
|
|
22
|
+
|
|
23
|
+
# Marked first, so an error that comes back through an outer squished call (or a second report_stop) is not
|
|
24
|
+
# reported again, and a hook that raises doesn't get the same error again either.
|
|
25
|
+
error.instance_variable_set(REPORTED, true)
|
|
26
|
+
metadata = squawk_metadata(
|
|
27
|
+
attempt: attempts, attempts:, final: true, model: error.models&.last, input: @prompt.input,
|
|
28
|
+
stopped: error.is_a?(BudgetExhaustedError) ? :budget : :disagreement
|
|
29
|
+
)
|
|
30
|
+
Squawk.call(hook, output: nil, metadata:, error:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
# Runs the observability hook, if any, with this attempt's raw output (nil when the call itself failed),
|
|
36
|
+
# the error that ended it (nil when it was accepted), and what was asked. role is nil for escalation
|
|
37
|
+
# attempts, :sample (with the sample's name) or :judge. reask is whether the attempt retried in the same chat,
|
|
38
|
+
# repeated whether it sent back the reply the attempt before it was rejected for, and final whether no attempt
|
|
39
|
+
# follows it.
|
|
40
|
+
def squawk(prompt, path, attempt, response, error, sample: nil, reask: false, repeated: false,
|
|
41
|
+
final: attempt == path.size)
|
|
42
|
+
hook = @definition.squawk
|
|
43
|
+
return unless hook && Squawk.report?(@definition.squawk_on, attempt, error, final)
|
|
44
|
+
|
|
45
|
+
step = path[attempt - 1]
|
|
46
|
+
metadata = squawk_metadata(
|
|
47
|
+
attempt:, attempts: path.size, final:, repeated:, model: response&.model || step.model,
|
|
48
|
+
provider: step.provider, params: step.request_params(reask), input: prompt.input, response:,
|
|
49
|
+
role: sample ? :sample : prompt.role, sample: sample&.name
|
|
50
|
+
)
|
|
51
|
+
Squawk.call(hook, output: response&.content, metadata:, error:)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def squawk_metadata(attempt:, attempts:, final:, model:, input:, repeated: false, provider: nil, params: nil,
|
|
55
|
+
response: nil, role: nil, sample: nil, stopped: nil)
|
|
56
|
+
{
|
|
57
|
+
label:, attempt:, attempts:, final:, repeated:, model:, provider:, params:, input:,
|
|
58
|
+
usage: response&.usage, finish_reason: response&.finish_reason, role:, sample:, stopped:
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/squishling/run.rb
CHANGED
|
@@ -14,8 +14,9 @@ module Squishling
|
|
|
14
14
|
class Run
|
|
15
15
|
include Messages
|
|
16
16
|
include Rejections
|
|
17
|
+
include Reports
|
|
17
18
|
|
|
18
|
-
private_constant :Messages, :Rejections
|
|
19
|
+
private_constant :Messages, :Rejections, :Reports
|
|
19
20
|
|
|
20
21
|
# One of #samples' outputs, between rounds.
|
|
21
22
|
Sample = Struct.new(:name, :stage, :chat, :rejected, :result, :reask)
|
|
@@ -41,7 +42,9 @@ module Squishling
|
|
|
41
42
|
@produced = {}.compare_by_identity
|
|
42
43
|
end
|
|
43
44
|
|
|
44
|
-
# Whether value is a result one of this run's primitives returned (not one built by hand).
|
|
45
|
+
# Whether value is a result one of this run's primitives returned (not one built by hand). By identity, so it
|
|
46
|
+
# tells results that are objects (the typed Data of an object schema) apart; a schema that isn't an object
|
|
47
|
+
# returns the bare JSON value, and an immediate such as 1 or true is the same object in every run.
|
|
45
48
|
def produced?(value)
|
|
46
49
|
@produced.key?(value)
|
|
47
50
|
end
|
|
@@ -70,7 +73,7 @@ module Squishling
|
|
|
70
73
|
end
|
|
71
74
|
|
|
72
75
|
step = Harness::OptionTypes.check(:step, declaration, label, :"harness step")
|
|
73
|
-
ModelPath.steps([step], provider: nil, params: @definition.params).freeze
|
|
76
|
+
ModelPath.steps([step], provider: nil, params: @definition.params, cache: @definition.cache).freeze
|
|
74
77
|
end
|
|
75
78
|
|
|
76
79
|
# Makes each attempt of the stages in order (the escalation's steps when none are given) until an output
|
|
@@ -287,26 +290,6 @@ module Squishling
|
|
|
287
290
|
@client.chat(step, instructions: prompt.instructions, schema: prompt.schema.llm_schema)
|
|
288
291
|
end
|
|
289
292
|
|
|
290
|
-
# Runs the observability hook, if any, with this attempt's raw output (nil when the call itself failed),
|
|
291
|
-
# the error that ended it (nil when it was accepted), and what was asked. role is nil for escalation
|
|
292
|
-
# attempts, :sample (with the sample's name) or :judge. reask is whether the attempt retried in the same chat,
|
|
293
|
-
# repeated whether it sent back the reply the attempt before it was rejected for, and final whether no attempt
|
|
294
|
-
# follows it.
|
|
295
|
-
def squawk(prompt, path, attempt, response, error, sample: nil, reask: false, repeated: false,
|
|
296
|
-
final: attempt == path.size)
|
|
297
|
-
hook = @definition.squawk
|
|
298
|
-
return unless hook && Squawk.report?(@definition.squawk_on, attempt, error, final)
|
|
299
|
-
|
|
300
|
-
step = path[attempt - 1]
|
|
301
|
-
metadata = {
|
|
302
|
-
label:, attempt:, attempts: path.size, final:, repeated:,
|
|
303
|
-
model: response&.model || step.model, provider: step.provider, params: step.request_params(reask),
|
|
304
|
-
input: prompt.input, usage: response&.usage, finish_reason: response&.finish_reason,
|
|
305
|
-
role: sample ? :sample : prompt.role, sample: sample&.name
|
|
306
|
-
}
|
|
307
|
-
Squawk.call(hook, output: response&.content, metadata:, error:)
|
|
308
|
-
end
|
|
309
|
-
|
|
310
293
|
# following is the attempt that comes next, which is not the next one when a repeated reply skips ahead.
|
|
311
294
|
def log_failure(role, attempt, path, reason, following: attempt + 1, repeated: false)
|
|
312
295
|
step = path[attempt - 1]
|