squishling 0.8.0 → 0.8.1
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 +105 -0
- data/README.md +2 -2
- data/docs/adapters.md +95 -9
- data/docs/apprentice.md +6 -0
- data/docs/configuration.md +10 -8
- data/docs/failures.md +44 -7
- data/docs/harnesses.md +9 -5
- data/docs/images.md +2 -2
- data/docs/inputs.md +23 -10
- data/docs/measuring-tokens.md +3 -3
- data/docs/outer-loop-agents.md +56 -9
- data/docs/schemas.md +3 -1
- data/docs/testing.md +6 -3
- data/docs/verifying.md +11 -6
- data/lib/squishling/adapter/candidates.rb +144 -0
- data/lib/squishling/adapter/override.rb +109 -0
- data/lib/squishling/adapter/retry_after.rb +34 -0
- data/lib/squishling/adapter.rb +31 -15
- data/lib/squishling/adapters/openai_compatible.rb +10 -9
- data/lib/squishling/adapters/openai_options.rb +2 -1
- data/lib/squishling/adapters/ruby_llm.rb +14 -5
- data/lib/squishling/apprentice/shadow.rb +45 -4
- data/lib/squishling/attachment.rb +60 -28
- data/lib/squishling/configuration.rb +42 -12
- data/lib/squishling/definition.rb +8 -3
- data/lib/squishling/errors.rb +41 -8
- data/lib/squishling/image.rb +6 -3
- data/lib/squishling/judge.rb +19 -8
- data/lib/squishling/llm_client.rb +3 -2
- data/lib/squishling/outer_loop_agent.rb +33 -6
- data/lib/squishling/params.rb +3 -1
- data/lib/squishling/payload/normalizer.rb +24 -9
- data/lib/squishling/payload.rb +32 -8
- data/lib/squishling/result.rb +87 -34
- data/lib/squishling/run/rejections.rb +64 -6
- data/lib/squishling/run/reports.rb +23 -8
- data/lib/squishling/run.rb +36 -51
- data/lib/squishling/sanitizer.rb +5 -3
- data/lib/squishling/schema.rb +55 -4
- data/lib/squishling/squawk.rb +4 -3
- data/lib/squishling/testing/adapter.rb +1 -1
- data/lib/squishling/testing/outer_loop_agent.rb +11 -0
- data/lib/squishling/testing/reply.rb +23 -10
- data/lib/squishling/testing/script.rb +7 -7
- data/lib/squishling/testing/stubs.rb +8 -2
- data/lib/squishling/verifier.rb +41 -15
- data/lib/squishling/version.rb +1 -1
- data/lib/squishling.rb +3 -0
- metadata +4 -1
data/lib/squishling/run.rb
CHANGED
|
@@ -18,11 +18,15 @@ module Squishling
|
|
|
18
18
|
|
|
19
19
|
private_constant :Messages, :Rejections, :Reports
|
|
20
20
|
|
|
21
|
-
# One of #samples' outputs, between rounds.
|
|
22
|
-
Sample = Struct.new(:name, :stage, :chat, :rejected, :
|
|
21
|
+
# One of #samples' outputs, between rounds. accepted is [result, origin], rejected [raw content, errors].
|
|
22
|
+
Sample = Struct.new(:name, :stage, :chat, :rejected, :accepted, :reask, :retry_reason)
|
|
23
23
|
|
|
24
24
|
SAMPLE_NAMES = ("a".."z").to_a.freeze
|
|
25
|
-
|
|
25
|
+
# What a round found out about one sample, before anything is reported: the reply (nil when the request failed),
|
|
26
|
+
# the error (a failed request, or the rejected reply's InvalidOutputError; nil when accepted), whether the reply
|
|
27
|
+
# repeated the rejected one, and whether no attempt follows it.
|
|
28
|
+
Check = Struct.new(:sample, :response, :error, :repeated, :terminal)
|
|
29
|
+
private_constant :Sample, :SAMPLE_NAMES, :Check
|
|
26
30
|
|
|
27
31
|
attr_reader :receiver, :inputs, :steps, :call_id
|
|
28
32
|
|
|
@@ -98,7 +102,9 @@ module Squishling
|
|
|
98
102
|
|
|
99
103
|
# One sample per stage (the attempts of one model), each asked on its own chat, each retried per its stage's
|
|
100
104
|
# attempts like #escalate, with the requests of every round sent concurrently. Returns the outputs in order.
|
|
101
|
-
# Samples are named "a", "b", ... in squawk reports.
|
|
105
|
+
# Samples are named "a", "b", ... in squawk reports. When a sample fails for good, the rest of its round is still
|
|
106
|
+
# recorded (squawk, outer-loop agent) before the first terminal error is raised, and a result counts as produced
|
|
107
|
+
# only once this returns.
|
|
102
108
|
def samples(stages)
|
|
103
109
|
owner!
|
|
104
110
|
raise ConfigurationError, "#{label}: samples takes an Array of stages" unless stages.is_a?(Array)
|
|
@@ -115,15 +121,15 @@ module Squishling
|
|
|
115
121
|
end
|
|
116
122
|
|
|
117
123
|
samples = stages.zip(SAMPLE_NAMES).map { |sample_stage, name| Sample.new(name, sample_stage) }
|
|
124
|
+
models = []
|
|
118
125
|
(1..stages.map(&:size).max).each do |attempt|
|
|
119
|
-
pending = samples.reject(&:
|
|
126
|
+
pending = samples.reject(&:accepted)
|
|
120
127
|
break if pending.empty?
|
|
121
128
|
|
|
122
|
-
check_budget!(attempts: attempt - 1, models:
|
|
123
|
-
|
|
124
|
-
round(pending, attempt)
|
|
129
|
+
check_budget!(attempts: attempt - 1, models:, rejected: pending.filter_map(&:rejected).last)
|
|
130
|
+
models.concat(round(pending, attempt))
|
|
125
131
|
end
|
|
126
|
-
samples
|
|
132
|
+
results(samples)
|
|
127
133
|
end
|
|
128
134
|
|
|
129
135
|
# A judge that picks between two outputs (see Judge#verdict, which returns a Judge::Verdict), or rejects
|
|
@@ -138,7 +144,8 @@ module Squishling
|
|
|
138
144
|
Judge.new(definition: @definition, receiver: @receiver, client: @client, judge:, instructions:, path: steps,
|
|
139
145
|
purpose: @purpose, payload: @payload, files: @prompt.files, escalate: method(:escalate_with),
|
|
140
146
|
log_failure: method(:log_failure),
|
|
141
|
-
guard: method(:owner!), budget_check: method(:check_budget!), harness_type: @harness.type
|
|
147
|
+
guard: method(:owner!), budget_check: method(:check_budget!), harness_type: @harness.type,
|
|
148
|
+
next_attempt: method(:next_attempt))
|
|
142
149
|
end
|
|
143
150
|
|
|
144
151
|
# Whether two outputs agree: compare (a Proc, evaluated against the receiver with the two results and the
|
|
@@ -192,7 +199,6 @@ module Squishling
|
|
|
192
199
|
check_budget!(attempts: models.size, models:, rejected:)
|
|
193
200
|
step = path[attempt - 1]
|
|
194
201
|
models << step.model
|
|
195
|
-
last = attempt == path.size
|
|
196
202
|
reask = !chat.nil? && step == path[attempt - 2]
|
|
197
203
|
if reask
|
|
198
204
|
message = retry_message(rejected.last)
|
|
@@ -204,20 +210,22 @@ module Squishling
|
|
|
204
210
|
begin
|
|
205
211
|
response = @client.ask(chat, message, step, attempt:, attempts: path.size, role: prompt.role, reask:)
|
|
206
212
|
rescue LLMError => e
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
# A rate limit is no use retrying on the same model, so it moves on to the next step (see #next_attempt).
|
|
214
|
+
following = next_attempt(path, attempt, e.is_a?(RateLimitError))
|
|
215
|
+
squawk(prompt, path, attempt, nil, e, reask:, final: following.nil?)
|
|
216
|
+
raise unless following
|
|
209
217
|
|
|
210
218
|
# A failed request starts the next attempt in a fresh chat, as a new step does.
|
|
211
219
|
chat = nil
|
|
212
|
-
log_failure(prompt.role, attempt, path, e.message)
|
|
213
|
-
attempt
|
|
220
|
+
log_failure(prompt.role, attempt, path, e.message, following:)
|
|
221
|
+
attempt = following
|
|
214
222
|
next
|
|
215
223
|
end
|
|
216
224
|
|
|
217
225
|
result, errors = @output_check.call(response.content, prompt)
|
|
218
226
|
if errors.empty?
|
|
219
227
|
squawk(prompt, path, attempt, response, nil, reask:)
|
|
220
|
-
return produce(result, prompt, response)
|
|
228
|
+
return produce(result, prompt, @client.origin_of(response))
|
|
221
229
|
end
|
|
222
230
|
|
|
223
231
|
repeated = reask && response.content == rejected.first
|
|
@@ -248,7 +256,11 @@ module Squishling
|
|
|
248
256
|
fatal = outcomes.map(&:last).find { |error| error && !error.is_a?(LLMError) }
|
|
249
257
|
raise fatal if fatal
|
|
250
258
|
|
|
251
|
-
pending.zip(outcomes).
|
|
259
|
+
checked = pending.zip(outcomes).map { |sample, outcome| check(sample, outcome, attempt) }
|
|
260
|
+
ending = checked.any?(&:terminal)
|
|
261
|
+
checked.each { |one| report(one, attempt, ending) }
|
|
262
|
+
settle(pending, checked.select(&:terminal).map(&:error), attempt)
|
|
263
|
+
steps.map(&:model).uniq
|
|
252
264
|
end
|
|
253
265
|
|
|
254
266
|
# The next message for a sample: a retry in its chat after invalid output, otherwise a fresh chat.
|
|
@@ -260,43 +272,16 @@ module Squishling
|
|
|
260
272
|
fresh_message(step, @prompt.input, sample.rejected)
|
|
261
273
|
end
|
|
262
274
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
role = "sample #{sample.name}"
|
|
267
|
-
if error
|
|
268
|
-
squawk(@prompt, stage, attempt, nil, error, sample:, reask: sample.reask)
|
|
269
|
-
raise error if last
|
|
270
|
-
|
|
271
|
-
sample.chat = nil
|
|
272
|
-
return log_failure(role, attempt, stage, error.message)
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
result, errors = @output_check.call(response.content, @prompt)
|
|
276
|
-
if errors.empty?
|
|
277
|
-
squawk(@prompt, stage, attempt, response, nil, sample:, reask: sample.reask)
|
|
278
|
-
sample.done = true # not `result`: a valid result can be false or nil (a boolean or nullable schema)
|
|
279
|
-
return sample.result = produce(result, @prompt, response)
|
|
280
|
-
end
|
|
281
|
-
|
|
282
|
-
# A sample is one step (#samples refuses a stage that mixes them), so a repeat leaves nothing to move on to.
|
|
283
|
-
repeated = sample.reask && response.content == sample.rejected.first
|
|
284
|
-
invalid = invalid_output(response, errors, attempt, stage.first(attempt).map(&:model))
|
|
285
|
-
squawk(@prompt, stage, attempt, response, invalid, sample:, reask: sample.reask, repeated:,
|
|
286
|
-
final: last || repeated)
|
|
287
|
-
report_rejection(response, invalid, stage, attempt, role: :sample, sample: sample.name)
|
|
288
|
-
raise invalid if last || repeated
|
|
289
|
-
|
|
290
|
-
sample.rejected = [response.content, invalid.errors]
|
|
291
|
-
log_failure(role, attempt, stage, rejection_reason(invalid.errors, response))
|
|
292
|
-
end
|
|
275
|
+
# The samples' results, produced only once every sample has passed: a result #samples never returned can't name
|
|
276
|
+
# the request behind a later one. (The same value can come from both: true is one object.)
|
|
277
|
+
def results(samples) = samples.map { |sample| produce(sample.accepted.first, @prompt, sample.accepted.last) }
|
|
293
278
|
|
|
294
279
|
# Remembers a result of the method's own output (a judge's verdict isn't one), so a harness can only return
|
|
295
280
|
# what a primitive produced.
|
|
296
|
-
def produce(result, prompt,
|
|
297
|
-
# The first
|
|
298
|
-
#
|
|
299
|
-
@produced[result] ||=
|
|
281
|
+
def produce(result, prompt, origin)
|
|
282
|
+
# The first result produced keeps the value: false, nil and true are one object, so two agreeing samples of them
|
|
283
|
+
# would otherwise name the later one's request, though the harness returns the first.
|
|
284
|
+
@produced[result] ||= origin unless prompt.role == :judge
|
|
300
285
|
result
|
|
301
286
|
end
|
|
302
287
|
|
data/lib/squishling/sanitizer.rb
CHANGED
|
@@ -8,7 +8,9 @@ module Squishling
|
|
|
8
8
|
# convert a file, or return the node as it was; a returned File, Struct, and so on is read like any input. reject.call
|
|
9
9
|
# records a problem and goes on, so every one is reported together in an InputRejectedError, raised after the pass,
|
|
10
10
|
# before the next pass or any request. Exceptions it raises are the developer's own and propagate as-is, so nothing
|
|
11
|
-
# is sent. Hash keys are not offered
|
|
11
|
+
# is sent. Hash keys are not offered, on purpose: they are schema (names the developer chose), not data, so a
|
|
12
|
+
# sanitizer can't redact one, and a key holding an identifier would reach the model as it is. An error's path quotes
|
|
13
|
+
# only a Symbol key and masks every String key (see Payload.join). Internal: not part of the public API.
|
|
12
14
|
class Sanitizer
|
|
13
15
|
KEYWORDS = %i[value path reject].freeze
|
|
14
16
|
DEFAULT_REJECTION = "rejected by the sanitizer"
|
|
@@ -48,8 +50,8 @@ module Squishling
|
|
|
48
50
|
result
|
|
49
51
|
end
|
|
50
52
|
|
|
51
|
-
# path is where the node is, as the sanitizer is told it; safe is the same for an error message, with
|
|
52
|
-
# that isn't a
|
|
53
|
+
# path is where the node is, as the sanitizer is told it; safe is the same for an error message, with every Hash key
|
|
54
|
+
# that isn't a Symbol masked (see Payload.join).
|
|
53
55
|
def visit(hook, node, path, safe, rejections)
|
|
54
56
|
deeper(safe) do
|
|
55
57
|
replaced = replace(hook, node, path, safe, rejections)
|
data/lib/squishling/schema.rb
CHANGED
|
@@ -23,6 +23,13 @@ module Squishling
|
|
|
23
23
|
SCHEMA_MAPS = %w[properties patternProperties $defs definitions].freeze
|
|
24
24
|
# Keywords whose value is literal data, copied untouched.
|
|
25
25
|
DATA_KEYWORDS = %w[enum const default examples].freeze
|
|
26
|
+
# Keywords whose schemas json_schemer applies to the very value their own schema is applied to. Not those it always
|
|
27
|
+
# applies on a condition (`then`, `else`, `dependentSchemas`), nor $dynamicRef and $recursiveRef, whose target
|
|
28
|
+
# depends on how the schema was reached. See also #in_place.
|
|
29
|
+
IN_PLACE_KEYWORDS = %w[$ref allOf anyOf oneOf not if].freeze
|
|
30
|
+
UNION_KEYWORDS = %w[anyOf oneOf].freeze
|
|
31
|
+
# Keywords that only hold schemas for a $ref to point at.
|
|
32
|
+
DEFINITIONS_KEYWORDS = %w[$defs definitions].freeze
|
|
26
33
|
|
|
27
34
|
class << self
|
|
28
35
|
def for(raw)
|
|
@@ -73,7 +80,7 @@ module Squishling
|
|
|
73
80
|
@validator = validator_for(@json_schema)
|
|
74
81
|
@validators_source = validators_source_of(raw.is_a?(Class) ? raw : raw.class)
|
|
75
82
|
# Built now so a property name the result can't take fails here, not on the first response.
|
|
76
|
-
@result_class = Result.build(@
|
|
83
|
+
@result_class = Result.build(@validator)
|
|
77
84
|
end
|
|
78
85
|
|
|
79
86
|
# field name (Symbol) => rule, from the `validate:` options of the output_schema's fields (see FieldValidation).
|
|
@@ -126,6 +133,7 @@ module Squishling
|
|
|
126
133
|
def validator_for(json_schema)
|
|
127
134
|
validator = JSONSchemer.schema(json_schema)
|
|
128
135
|
resolve_refs!(validator, {}.compare_by_identity)
|
|
136
|
+
refuse_endless!(validator)
|
|
129
137
|
validator
|
|
130
138
|
rescue *REF_ERRORS => e
|
|
131
139
|
raise ConfigurationError, "output_schema has a $ref that can't be resolved (#{e.message}); " \
|
|
@@ -138,9 +146,52 @@ module Squishling
|
|
|
138
146
|
return if seen.key?(schema)
|
|
139
147
|
|
|
140
148
|
seen[schema] = true
|
|
141
|
-
schema.parsed.
|
|
142
|
-
|
|
143
|
-
|
|
149
|
+
targets(schema.parsed).each { |target| resolve_refs!(target, seen) }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Refuses a schema that leads back to itself while still checking the same value (a $ref to itself, or through
|
|
153
|
+
# nothing but unions): json_schemer would recurse until the stack overflows, after the request was paid for. A
|
|
154
|
+
# property or an item is another value, so recursion there is fine. A definition that nothing refers to is never
|
|
155
|
+
# applied, so it isn't checked.
|
|
156
|
+
def refuse_endless!(validator)
|
|
157
|
+
checked = {}.compare_by_identity
|
|
158
|
+
pending = [validator]
|
|
159
|
+
follow_in_place(pending.pop, checked, {}.compare_by_identity, pending) until pending.empty?
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# chain holds the schemas being applied to one value. Those of its properties and items wait in pending, rather
|
|
163
|
+
# than being followed from here, so the depth of the recursion is that of one chain.
|
|
164
|
+
def follow_in_place(schema, checked, chain, pending)
|
|
165
|
+
endless!(schema) if chain.key?(schema)
|
|
166
|
+
return if checked.key?(schema)
|
|
167
|
+
|
|
168
|
+
chain[schema] = true
|
|
169
|
+
in_place = in_place(schema.parsed)
|
|
170
|
+
targets(in_place).each { |target| follow_in_place(target, checked, chain, pending) }
|
|
171
|
+
chain.delete(schema)
|
|
172
|
+
checked[schema] = true
|
|
173
|
+
pending.concat(targets(schema.parsed.except(*DEFINITIONS_KEYWORDS, *in_place.keys)))
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# The keywords that apply their schemas to the same value whatever it is. In an OpenAPI dialect, a discriminator
|
|
177
|
+
# picks the union's branch by the value (elsewhere json_schemer doesn't know the keyword, and it changes nothing).
|
|
178
|
+
def in_place(keywords)
|
|
179
|
+
discriminator = keywords["discriminator"]
|
|
180
|
+
unconditional = discriminator.nil? || discriminator.is_a?(JSONSchemer::Schema::UNKNOWN_KEYWORD_CLASS)
|
|
181
|
+
keywords.slice(*(unconditional ? IN_PLACE_KEYWORDS : IN_PLACE_KEYWORDS - UNION_KEYWORDS))
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def endless!(schema)
|
|
185
|
+
raise ConfigurationError,
|
|
186
|
+
"output_schema leads back to itself at ##{schema.schema_pointer} without reaching a property or an item, so " \
|
|
187
|
+
"checking an output against it would never end"
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# The Schema objects that parsed keywords lead to: the targets of their references, and those directly under them.
|
|
191
|
+
def targets(keywords)
|
|
192
|
+
keywords.each_value.flat_map do |keyword|
|
|
193
|
+
referenced = keyword.respond_to?(:ref_schema) ? [keyword.ref_schema] : []
|
|
194
|
+
referenced + subschemas(keyword.parsed)
|
|
144
195
|
end
|
|
145
196
|
end
|
|
146
197
|
|
data/lib/squishling/squawk.rb
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
module Squishling
|
|
4
4
|
# The opt-in observability hook: a callable run after every LLM attempt (or only the rejected and retried
|
|
5
5
|
# ones, see squawk_on:) with the attempt's raw output, so it can be sent somewhere (an error tracker, a
|
|
6
|
-
# tracing tool). It's
|
|
7
|
-
# is
|
|
8
|
-
#
|
|
6
|
+
# tracing tool). It's a sanctioned way for model output to leave the process (an outer-loop agent that defines
|
|
7
|
+
# report is sent the same reports, see OuterLoopAgent.reporter); nothing runs unless one is configured. A call
|
|
8
|
+
# that a token budget or a disagreement stops gets one more report, with the error and no output (metadata's
|
|
9
|
+
# stopped:), since neither has an attempt of its own.
|
|
9
10
|
module Squawk
|
|
10
11
|
KEYWORD_TYPES = KeywordHook::KEYWORD_TYPES
|
|
11
12
|
POSITIONAL_TYPES = KeywordHook::POSITIONAL_TYPES
|
|
@@ -110,7 +110,7 @@ module Squishling
|
|
|
110
110
|
|
|
111
111
|
def resolve(item, label)
|
|
112
112
|
case item
|
|
113
|
-
when Failure then Failures.build(item.intent, label)
|
|
113
|
+
when Failure then Failures.build(item.intent, label, retry_after: item.retry_after)
|
|
114
114
|
when Exception, Judgment then item
|
|
115
115
|
when Class then item <= Exception ? item.new : response(item)
|
|
116
116
|
when Reply then Squishling::Adapter::Response.new(content: item.content, model: item.model, tokens: item.usage)
|
|
@@ -33,6 +33,17 @@ module Squishling
|
|
|
33
33
|
@previous.apprentice(review) if @previous.respond_to?(:apprentice)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
# So does a report, when the app's outer-loop agent defines one (see Squishling::OuterLoopAgent.reporter). It
|
|
37
|
+
# takes the keywords that agent's report declares, so it claims report only when the app's agent has one:
|
|
38
|
+
# this wrapper must never make an agent that doesn't take output look like one that does.
|
|
39
|
+
def report(**payload)
|
|
40
|
+
KeywordHook.call(@previous.method(:report), **payload)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def respond_to?(name, *)
|
|
44
|
+
name == :report ? @previous.respond_to?(:report) : super
|
|
45
|
+
end
|
|
46
|
+
|
|
36
47
|
# So does a rejected reply (see Squishling::OuterLoopAgent.rejected).
|
|
37
48
|
def invalid_output(rejection)
|
|
38
49
|
@previous.invalid_output(rejection) if @previous.respond_to?(:invalid_output)
|
|
@@ -14,8 +14,11 @@ module Squishling
|
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
# A scripted provider failure, named by what went wrong (see Failures).
|
|
18
|
-
|
|
17
|
+
# A scripted provider failure, named by what went wrong (see Failures). retry_after is for :rate_limit: the seconds
|
|
18
|
+
# its RateLimitError carries.
|
|
19
|
+
Failure = Data.define(:intent, :retry_after) do
|
|
20
|
+
def initialize(intent:, retry_after: nil) = super
|
|
21
|
+
end
|
|
19
22
|
|
|
20
23
|
# The answer to the "winner" question of a System One judgment.
|
|
21
24
|
Answer = Struct.new(:choice, :probabilities)
|
|
@@ -28,12 +31,13 @@ module Squishling
|
|
|
28
31
|
end
|
|
29
32
|
|
|
30
33
|
# The provider failures a script can name by intent rather than by the error class of any one SDK. Each becomes
|
|
31
|
-
# the Squishling error an adapter raises for it: LLMError for what is worth
|
|
32
|
-
# for what is not. Messages carry the failure and its status, like the real
|
|
34
|
+
# the Squishling error an adapter raises for it: LLMError (RateLimitError for a rate limit) for what is worth
|
|
35
|
+
# another attempt, ConfigurationError for what is not. Messages carry the failure and its status, like the real
|
|
36
|
+
# adapters' do.
|
|
33
37
|
module Failures
|
|
34
38
|
# intent => [error class, what happened, HTTP status or nil]
|
|
35
39
|
INTENTS = {
|
|
36
|
-
rate_limit: [
|
|
40
|
+
rate_limit: [RateLimitError, "rate limited", 429],
|
|
37
41
|
server_error: [LLMError, "server error", 500],
|
|
38
42
|
overloaded: [LLMError, "provider overloaded", 529],
|
|
39
43
|
timeout: [LLMError, "request timed out", nil],
|
|
@@ -45,15 +49,24 @@ module Squishling
|
|
|
45
49
|
|
|
46
50
|
module_function
|
|
47
51
|
|
|
48
|
-
def check!(intent)
|
|
49
|
-
|
|
52
|
+
def check!(intent, retry_after: nil)
|
|
53
|
+
unless INTENTS.key?(intent)
|
|
54
|
+
raise ArgumentError, "unknown failure #{intent.inspect} (one of #{INTENTS.keys.map(&:inspect).join(', ')})"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
unless retry_after.nil?
|
|
58
|
+
raise ArgumentError, "retry_after: only goes with the :rate_limit failure" unless intent == :rate_limit
|
|
59
|
+
|
|
60
|
+
RateLimitError.new(retry_after:) # refuses what isn't a non-negative number, here and not at the request
|
|
61
|
+
end
|
|
50
62
|
|
|
51
|
-
|
|
63
|
+
intent
|
|
52
64
|
end
|
|
53
65
|
|
|
54
|
-
def build(intent, label)
|
|
66
|
+
def build(intent, label, retry_after: nil)
|
|
55
67
|
klass, text, status = INTENTS.fetch(intent)
|
|
56
|
-
|
|
68
|
+
message = "#{label}: #{text}#{" (HTTP #{status})" if status}"
|
|
69
|
+
klass == RateLimitError ? klass.new(message, retry_after:) : klass.new(message)
|
|
57
70
|
end
|
|
58
71
|
end
|
|
59
72
|
end
|
|
@@ -29,9 +29,9 @@ module Squishling
|
|
|
29
29
|
self
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
# A provider failure named by intent (see Failures::INTENTS).
|
|
33
|
-
def fail(intent)
|
|
34
|
-
@replies << Failure.new(Failures.check!(intent))
|
|
32
|
+
# A provider failure named by intent (see Failures::INTENTS). retry_after: (seconds) goes with :rate_limit.
|
|
33
|
+
def fail(intent, retry_after: nil)
|
|
34
|
+
@replies << Failure.new(Failures.check!(intent, retry_after:), retry_after)
|
|
35
35
|
self
|
|
36
36
|
end
|
|
37
37
|
|
|
@@ -113,13 +113,13 @@ module Squishling
|
|
|
113
113
|
self
|
|
114
114
|
end
|
|
115
115
|
|
|
116
|
-
def fail(intent)
|
|
117
|
-
@chat.fail(intent)
|
|
116
|
+
def fail(intent, **)
|
|
117
|
+
@chat.fail(intent, **)
|
|
118
118
|
self
|
|
119
119
|
end
|
|
120
120
|
|
|
121
|
-
def fail_judgment(intent)
|
|
122
|
-
@judgment.fail(intent)
|
|
121
|
+
def fail_judgment(intent, **)
|
|
122
|
+
@judgment.fail(intent, **)
|
|
123
123
|
self
|
|
124
124
|
end
|
|
125
125
|
|
|
@@ -16,7 +16,8 @@ module Squishling
|
|
|
16
16
|
@hooks = {}
|
|
17
17
|
|
|
18
18
|
ERRORS = {
|
|
19
|
-
invalid_output: :invalid_output_error, llm_error: :llm_error,
|
|
19
|
+
invalid_output: :invalid_output_error, llm_error: :llm_error, rate_limit: :rate_limit_error,
|
|
20
|
+
configuration: :configuration_error,
|
|
20
21
|
disagreement: :disagreement_error
|
|
21
22
|
}.freeze
|
|
22
23
|
|
|
@@ -29,7 +30,8 @@ module Squishling
|
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
# The method raises this kind of error: :invalid_output (errors:, raw:, attempts:, models:), :llm_error
|
|
32
|
-
# (message:), :configuration (message:), or :disagreement (candidates:,
|
|
33
|
+
# (message:), :rate_limit (message:, retry_after:), :configuration (message:), or :disagreement (candidates:,
|
|
34
|
+
# verdict:, reason:, models:).
|
|
33
35
|
def error(klass, kind, method: :call, **options, &)
|
|
34
36
|
builder = ERRORS.fetch(kind) do
|
|
35
37
|
raise ArgumentError, "unknown error #{kind.inspect} (one of #{ERRORS.keys.map(&:inspect).join(', ')})"
|
|
@@ -98,6 +100,10 @@ module Squishling
|
|
|
98
100
|
LLMError.new("#{definition.label}: #{message}")
|
|
99
101
|
end
|
|
100
102
|
|
|
103
|
+
def rate_limit_error(definition, message: "stubbed rate limit", retry_after: nil)
|
|
104
|
+
RateLimitError.new("#{definition.label}: #{message}", retry_after:)
|
|
105
|
+
end
|
|
106
|
+
|
|
101
107
|
def configuration_error(definition, message: "stubbed configuration error")
|
|
102
108
|
ConfigurationError.new("#{definition.label}: #{message}")
|
|
103
109
|
end
|
data/lib/squishling/verifier.rb
CHANGED
|
@@ -7,9 +7,14 @@ module Squishling
|
|
|
7
7
|
class Verifier
|
|
8
8
|
# Checks one squished method's Definition. Each check returns messages, or raises what the call would.
|
|
9
9
|
class MethodCheck
|
|
10
|
-
|
|
11
|
-
#
|
|
12
|
-
|
|
10
|
+
# What a squish_rescue can override (escalation steps and their include_when: procs, params, adapters, harness):
|
|
11
|
+
# checked on the method, and again on each rescue's derived definition.
|
|
12
|
+
OVERRIDABLE = %i[declarations adapters harness step_callables].freeze
|
|
13
|
+
# What the method owns and Definition#for_call carries over unchanged (the schema, squish_when, apprentice, and
|
|
14
|
+
# call_id): checked once. A new check goes in whichever list matches who owns what it checks, and a check meant
|
|
15
|
+
# for rescues too must be in OVERRIDABLE, since a rescue only re-runs that list.
|
|
16
|
+
OWNED = %i[schema method_callables apprentice call_id].freeze
|
|
17
|
+
CHECKS = (OVERRIDABLE + OWNED).freeze
|
|
13
18
|
|
|
14
19
|
def initialize(definition, checks: CHECKS, label: definition.label)
|
|
15
20
|
@definition = definition
|
|
@@ -83,7 +88,7 @@ module Squishling
|
|
|
83
88
|
names = @definition.declared_path.first.map { |step| step[:adapter] }
|
|
84
89
|
judge = @definition.harness.options[:judge]
|
|
85
90
|
names << judge[:adapter] if judge.is_a?(Hash)
|
|
86
|
-
names = names.compact
|
|
91
|
+
names = names.compact if Verifier.default_adapter_checked_once?
|
|
87
92
|
names.uniq.filter_map { |name| adapter_problem(name) }
|
|
88
93
|
end
|
|
89
94
|
|
|
@@ -112,14 +117,27 @@ module Squishling
|
|
|
112
117
|
false # no usable adapter: reported once, as the adapter's own problem
|
|
113
118
|
end
|
|
114
119
|
|
|
115
|
-
#
|
|
116
|
-
def
|
|
117
|
-
|
|
118
|
-
gates = @definition.declared_path.first.filter_map do |step|
|
|
119
|
-
[step[:include_when], "escalation step #{step[:model].inspect} include_when:"] if step[:include_when]
|
|
120
|
-
end
|
|
120
|
+
# The method's own callables that get its inputs as keywords: squish_when and the apprentice's when:.
|
|
121
|
+
def method_callables
|
|
122
|
+
gates = []
|
|
121
123
|
gates << [@definition.predicate, "squish_when"] if @definition.predicate
|
|
122
124
|
gates << [shadowed_apprentice[:when], "apprentice_mode when:"] if shadowed_apprentice&.dig(:when)
|
|
125
|
+
callable_messages(gates)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# The include_when: of each escalation step, which a squish_rescue may replace along with the escalation.
|
|
129
|
+
def step_callables
|
|
130
|
+
callable_messages(step_gates)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def step_gates
|
|
134
|
+
@definition.declared_path.first.filter_map do |step|
|
|
135
|
+
[step[:include_when], "escalation step #{step[:model].inspect} include_when:"] if step[:include_when]
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def callable_messages(gates)
|
|
140
|
+
names, open = @definition.input_names
|
|
123
141
|
gates.flat_map { |callable, what| KeywordCheck.messages(callable, names, open).map { "#{what} #{_1}" } }
|
|
124
142
|
end
|
|
125
143
|
|
|
@@ -259,6 +277,12 @@ module Squishling
|
|
|
259
277
|
end
|
|
260
278
|
end
|
|
261
279
|
|
|
280
|
+
# Whether the default adapter (what a step or judge that names none uses) is checked once for the whole run, not
|
|
281
|
+
# per method: unless named adapters are configured, and always when SQUISHLING_ADAPTER replaces it for every step.
|
|
282
|
+
def self.default_adapter_checked_once?
|
|
283
|
+
Adapter::Override.requested ? true : !Squishling.config.adapters.any?
|
|
284
|
+
end
|
|
285
|
+
|
|
262
286
|
def initialize(classes)
|
|
263
287
|
classes.each { |klass| check_squished!(klass) }
|
|
264
288
|
@classes = classes
|
|
@@ -276,11 +300,13 @@ module Squishling
|
|
|
276
300
|
raise ConfigurationError, "#{klass.inspect} doesn't include Squishling"
|
|
277
301
|
end
|
|
278
302
|
|
|
279
|
-
# The
|
|
280
|
-
#
|
|
281
|
-
#
|
|
303
|
+
# The adapter that resolves, with SQUISHLING_ADAPTER, the candidates of config.adapter (at least one registered,
|
|
304
|
+
# with its gem, and #available?), or RubyLLM's, must be usable, unless named adapters are configured and no
|
|
305
|
+
# variable is set: then the default one is only needed by the steps that use it, which each method's check
|
|
306
|
+
# reports. The named adapters are built when assigned. Resolving builds adapters and asks #available?; it never
|
|
307
|
+
# makes a request.
|
|
282
308
|
def adapter_problems
|
|
283
|
-
Squishling.config.resolved_adapter
|
|
309
|
+
Squishling.config.resolved_adapter if self.class.default_adapter_checked_once?
|
|
284
310
|
[]
|
|
285
311
|
rescue ConfigurationError => e
|
|
286
312
|
[Verification::Problem.new(label: "adapter", message: e.message)]
|
|
@@ -300,7 +326,7 @@ module Squishling
|
|
|
300
326
|
klass.squishling_rescues.flat_map do |declared|
|
|
301
327
|
label = "#{definition.label} squish_rescue"
|
|
302
328
|
derived = definition.for_call(**declared.overrides)
|
|
303
|
-
MethodCheck.new(derived, checks: MethodCheck::
|
|
329
|
+
MethodCheck.new(derived, checks: MethodCheck::OVERRIDABLE, label:).problems
|
|
304
330
|
rescue ConfigurationError, ArgumentError => e
|
|
305
331
|
[Verification::Problem.new(label:, message: e.message)]
|
|
306
332
|
end
|
data/lib/squishling/version.rb
CHANGED
data/lib/squishling.rb
CHANGED
|
@@ -17,7 +17,10 @@ require_relative "squishling/image"
|
|
|
17
17
|
require_relative "squishling/document"
|
|
18
18
|
require_relative "squishling/attachment"
|
|
19
19
|
require_relative "squishling/adapter/error_body"
|
|
20
|
+
require_relative "squishling/adapter/retry_after"
|
|
20
21
|
require_relative "squishling/adapter"
|
|
22
|
+
require_relative "squishling/adapter/candidates"
|
|
23
|
+
require_relative "squishling/adapter/override"
|
|
21
24
|
require_relative "squishling/adapters/ruby_llm_attachments"
|
|
22
25
|
require_relative "squishling/adapters/ruby_llm"
|
|
23
26
|
require_relative "squishling/adapters/openai_options"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: squishling
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Michael Carroll
|
|
@@ -74,7 +74,10 @@ files:
|
|
|
74
74
|
- docs/verifying.md
|
|
75
75
|
- lib/squishling.rb
|
|
76
76
|
- lib/squishling/adapter.rb
|
|
77
|
+
- lib/squishling/adapter/candidates.rb
|
|
77
78
|
- lib/squishling/adapter/error_body.rb
|
|
79
|
+
- lib/squishling/adapter/override.rb
|
|
80
|
+
- lib/squishling/adapter/retry_after.rb
|
|
78
81
|
- lib/squishling/adapters/openai_compatible.rb
|
|
79
82
|
- lib/squishling/adapters/openai_content.rb
|
|
80
83
|
- lib/squishling/adapters/openai_options.rb
|