squishling 0.5.1 → 0.7.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 +93 -0
- data/README.md +3 -2
- data/docs/adapters.md +78 -6
- data/docs/configuration.md +52 -3
- data/docs/failures.md +4 -1
- data/docs/images.md +92 -0
- data/docs/measuring-tokens.md +2 -1
- data/docs/observers.md +3 -1
- data/docs/routing.md +10 -1
- data/docs/testing.md +14 -9
- data/docs/verifying.md +3 -1
- data/lib/squishling/adapter.rb +26 -7
- data/lib/squishling/adapters/openai_compatible.rb +23 -4
- data/lib/squishling/adapters/openai_content.rb +23 -0
- data/lib/squishling/adapters/ruby_llm.rb +52 -3
- data/lib/squishling/budget.rb +11 -3
- data/lib/squishling/cache.rb +33 -0
- data/lib/squishling/class_methods.rb +21 -6
- data/lib/squishling/configuration.rb +81 -0
- data/lib/squishling/definition.rb +22 -7
- data/lib/squishling/image.rb +169 -0
- data/lib/squishling/invoker.rb +16 -33
- data/lib/squishling/judge.rb +22 -7
- data/lib/squishling/llm_client.rb +86 -32
- data/lib/squishling/model_path.rb +29 -11
- data/lib/squishling/observer.rb +15 -4
- data/lib/squishling/payload.rb +68 -0
- data/lib/squishling/run.rb +14 -7
- data/lib/squishling/testing/adapter.rb +17 -6
- data/lib/squishling/testing/checks.rb +1 -1
- data/lib/squishling/testing/direct.rb +4 -3
- data/lib/squishling/testing/request.rb +23 -4
- data/lib/squishling/testing/script.rb +12 -10
- data/lib/squishling/testing.rb +14 -7
- data/lib/squishling/verifier.rb +24 -4
- data/lib/squishling/version.rb +1 -1
- data/lib/squishling.rb +8 -3
- metadata +6 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Squishling
|
|
4
|
+
# Turns a call's arguments and context into the plain JSON the model is sent. Internal: not part of the public API.
|
|
5
|
+
module Payload
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# label names the call in a ConfigurationError. images collects the Images found, in order: each goes into the
|
|
9
|
+
# JSON as {attached_image: N} (its 1-based number), and the same Image passed twice is attached once.
|
|
10
|
+
def jsonify(value, label, images = [])
|
|
11
|
+
Schema.jsonify(describe(value, label, images))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Exceptions are sent as their class and message. Plain JSON would send only the message, and with
|
|
15
|
+
# json/add/exception loaded it would also send the backtrace, which exposes file paths. A Struct, Data, or
|
|
16
|
+
# OpenStruct is sent as its members (to_h) and a Set as its elements, not as Ruby's inspect text. path is the
|
|
17
|
+
# containers being walked: one that contains itself is a ConfigurationError rather than a SystemStackError.
|
|
18
|
+
def describe(value, label, images, path = [])
|
|
19
|
+
case value
|
|
20
|
+
when Image then { attached_image: attach(value, images) }
|
|
21
|
+
when Exception then { class: exception_class_name(value.class), message: value.message }
|
|
22
|
+
when Array, Set then elements(value, label, images, path)
|
|
23
|
+
when Hash, Struct, Data then members(value, label, images, path)
|
|
24
|
+
else open_struct?(value) ? members(value, label, images, path) : value
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def elements(value, label, images, path)
|
|
29
|
+
within(value, label, path) { |inner| value.map { |item| describe(item, label, images, inner) } }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def members(value, label, images, path)
|
|
33
|
+
pairs = value.is_a?(Hash) ? value : value.to_h
|
|
34
|
+
unless pairs.is_a?(Hash)
|
|
35
|
+
raise ConfigurationError,
|
|
36
|
+
"#{label}: #{value.class}#to_h must return a Hash to be sent as JSON, got #{pairs.class}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
within(value, label, path) { |inner| pairs.transform_values { |item| describe(item, label, images, inner) } }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# OpenStruct is a default gem that Ruby 4.0 stops loading by default, so it can't be named unless it is loaded.
|
|
43
|
+
def open_struct?(value) = defined?(::OpenStruct) && value.is_a?(::OpenStruct) # rubocop:disable Style/OpenStructUse
|
|
44
|
+
|
|
45
|
+
def within(container, label, path)
|
|
46
|
+
if path.any? { |ancestor| ancestor.equal?(container) }
|
|
47
|
+
raise ConfigurationError, "#{label}: an argument or context value contains itself, so it can't be sent as JSON"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
yield path + [container]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The image's number in the message (1-based), added to images the first time it is seen.
|
|
54
|
+
def attach(image, images)
|
|
55
|
+
index = images.index { |attached| attached.equal?(image) }
|
|
56
|
+
return index + 1 if index
|
|
57
|
+
|
|
58
|
+
images << image
|
|
59
|
+
images.size
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# An anonymous error class (Class.new(StandardError)) is named after its closest named ancestor.
|
|
63
|
+
def exception_class_name(klass)
|
|
64
|
+
klass = klass.superclass until klass.name
|
|
65
|
+
klass.name
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
data/lib/squishling/run.rb
CHANGED
|
@@ -37,6 +37,7 @@ module Squishling
|
|
|
37
37
|
@steps = definition.escalation_path(receiver, inputs).freeze
|
|
38
38
|
@client = LLMClient.new(definition.label, purpose:, harness: harness.type, handed_off: definition.handed_off,
|
|
39
39
|
budget: definition.budget)
|
|
40
|
+
@client.check_adapters!(@steps, prompt.images)
|
|
40
41
|
@output_check = OutputCheck.new(definition, receiver, inputs)
|
|
41
42
|
@owner = Thread.current
|
|
42
43
|
@produced = {}.compare_by_identity
|
|
@@ -62,8 +63,13 @@ module Squishling
|
|
|
62
63
|
# A stage from a model name or a step Hash (model:, provider:, params:, attempts:, forward_rejected:,
|
|
63
64
|
# retry_temperature:), the way a judge step is resolved: it keeps its own provider, and its params merge over
|
|
64
65
|
# the method's.
|
|
65
|
-
# A stage (an Array of steps) is returned as is, and a single step is a stage of one attempt.
|
|
66
|
+
# A stage (an Array of steps) is returned as is, and a single step is a stage of one attempt. A step's adapter
|
|
67
|
+
# must be configured, which is checked here, before the stage makes a request.
|
|
66
68
|
def stage(declaration)
|
|
69
|
+
declared_stage(declaration).tap { |steps| @client.check_adapters!(steps, @prompt.images) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def declared_stage(declaration)
|
|
67
73
|
return checked_stage([declaration]) if declaration.is_a?(ModelPath::Step)
|
|
68
74
|
|
|
69
75
|
if declaration.is_a?(Array)
|
|
@@ -73,7 +79,7 @@ module Squishling
|
|
|
73
79
|
end
|
|
74
80
|
|
|
75
81
|
step = Harness::OptionTypes.check(:step, declaration, label, :"harness step")
|
|
76
|
-
ModelPath.steps([step], provider: nil, params: @definition.params).freeze
|
|
82
|
+
ModelPath.steps([step], provider: nil, params: @definition.params, cache: @definition.cache).freeze
|
|
77
83
|
end
|
|
78
84
|
|
|
79
85
|
# Makes each attempt of the stages in order (the escalation's steps when none are given) until an output
|
|
@@ -128,7 +134,8 @@ module Squishling
|
|
|
128
134
|
owner!
|
|
129
135
|
judge = judge_step(judge) unless judge.nil?
|
|
130
136
|
Judge.new(definition: @definition, receiver: @receiver, client: @client, judge:, instructions:, path: steps,
|
|
131
|
-
purpose: @purpose, payload: @payload,
|
|
137
|
+
purpose: @purpose, payload: @payload, images: @prompt.images, escalate: method(:escalate_with),
|
|
138
|
+
log_failure: method(:log_failure),
|
|
132
139
|
guard: method(:owner!), budget_check: method(:check_budget!), harness_type: @harness.type)
|
|
133
140
|
end
|
|
134
141
|
|
|
@@ -225,15 +232,15 @@ module Squishling
|
|
|
225
232
|
|
|
226
233
|
# One round of #samples: the pending samples' requests, sent concurrently, then each outcome recorded.
|
|
227
234
|
def round(pending, attempt)
|
|
228
|
-
|
|
229
|
-
|
|
235
|
+
steps = pending.map { |sample| sample.stage[attempt - 1] }
|
|
236
|
+
jobs = pending.zip(steps).map do |sample, step|
|
|
230
237
|
message = sample_message(sample, step)
|
|
231
238
|
chat = sample.chat
|
|
232
239
|
attempts = sample.stage.size
|
|
233
240
|
reask = sample.reask
|
|
234
241
|
-> { @client.ask(chat, message, step, attempt:, attempts:, role: :sample, sample: sample.name, reask:) }
|
|
235
242
|
end
|
|
236
|
-
outcomes = @client.concurrently(jobs)
|
|
243
|
+
outcomes = @client.concurrently(jobs, steps)
|
|
237
244
|
# A setup mistake (or anything that isn't a failed request) ends the call, whichever sample hit it.
|
|
238
245
|
fatal = outcomes.map(&:last).find { |error| error && !error.is_a?(LLMError) }
|
|
239
246
|
raise fatal if fatal
|
|
@@ -287,7 +294,7 @@ module Squishling
|
|
|
287
294
|
end
|
|
288
295
|
|
|
289
296
|
def start_chat(step, prompt)
|
|
290
|
-
@client.chat(step, instructions: prompt.instructions, schema: prompt.schema.llm_schema)
|
|
297
|
+
@client.chat(step, instructions: prompt.instructions, schema: prompt.schema.llm_schema, images: prompt.images)
|
|
291
298
|
end
|
|
292
299
|
|
|
293
300
|
# following is the attempt that comes next, which is not the next one when a repeated reply skips ahead.
|
|
@@ -24,6 +24,10 @@ module Squishling
|
|
|
24
24
|
true
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
def images?
|
|
28
|
+
true
|
|
29
|
+
end
|
|
30
|
+
|
|
27
31
|
# Every request so far, in the order they arrived (Testing::Request).
|
|
28
32
|
def requests
|
|
29
33
|
@lock.synchronize { @requests.dup }
|
|
@@ -77,12 +81,14 @@ module Squishling
|
|
|
77
81
|
|
|
78
82
|
def judged(input, _questions, step, label)
|
|
79
83
|
call = { role: :judge, sample: nil, attempt: Observer.current&.attempt, attempts: Observer.current&.attempts,
|
|
80
|
-
model: step.model, provider: step.provider }
|
|
84
|
+
model: step.model, provider: step.provider, adapter: step.adapter }
|
|
81
85
|
outcome = outcome_for(:judgment, call, label, step.model)
|
|
82
86
|
payload = Schema.jsonify(input)
|
|
83
87
|
@requests << Request.new(label:, type: :judgment, role: :judge, sample: nil, attempt: call[:attempt],
|
|
84
|
-
attempts: call[:attempts], model: step.model, provider: step.provider,
|
|
85
|
-
|
|
88
|
+
attempts: call[:attempts], model: step.model, provider: step.provider, adapter: step.adapter,
|
|
89
|
+
params: step.params,
|
|
90
|
+
cache: step.cache, system_prompt: nil, output_schema: nil, messages: [], conversation: nil, payload:,
|
|
91
|
+
reply: outcome)
|
|
86
92
|
outcome
|
|
87
93
|
end
|
|
88
94
|
|
|
@@ -123,7 +129,7 @@ module Squishling
|
|
|
123
129
|
# installed without one, what the request itself shows.
|
|
124
130
|
def call_of(request)
|
|
125
131
|
observed = Observer.current
|
|
126
|
-
base = { model: request.model, provider: request.provider }
|
|
132
|
+
base = { model: request.model, provider: request.provider, adapter: request.adapter }
|
|
127
133
|
if observed
|
|
128
134
|
return base.merge(role: observed.role, sample: observed.sample, attempt: observed.attempt,
|
|
129
135
|
attempts: observed.attempts)
|
|
@@ -150,8 +156,9 @@ module Squishling
|
|
|
150
156
|
def record(request, call, outcome, conversation)
|
|
151
157
|
@requests << Request.new(label: request.label, type: :chat, role: call[:role], sample: call[:sample]&.to_s,
|
|
152
158
|
attempt: call[:attempt], attempts: call[:attempts], model: request.model, provider: request.provider,
|
|
153
|
-
params: request.params,
|
|
154
|
-
|
|
159
|
+
adapter: request.adapter, params: request.params, cache: request.cache, system_prompt: request.instructions,
|
|
160
|
+
output_schema: request.schema["schema"], messages: request.messages, conversation:,
|
|
161
|
+
payload: Request.payload_of(request.messages), reply: outcome)
|
|
155
162
|
end
|
|
156
163
|
end
|
|
157
164
|
|
|
@@ -174,6 +181,10 @@ module Squishling
|
|
|
174
181
|
true
|
|
175
182
|
end
|
|
176
183
|
|
|
184
|
+
def images?
|
|
185
|
+
true
|
|
186
|
+
end
|
|
187
|
+
|
|
177
188
|
def complete(request)
|
|
178
189
|
refuse(request.label)
|
|
179
190
|
end
|
|
@@ -12,7 +12,7 @@ module Squishling
|
|
|
12
12
|
sent = requests_of(source)[request]
|
|
13
13
|
return "expected request ##{request} to have been sent, but only #{requests_of(source).size} were" unless sent
|
|
14
14
|
|
|
15
|
-
wanted =
|
|
15
|
+
wanted = Payload.jsonify(expected, "sent_arguments")
|
|
16
16
|
return nil if sent.arguments == wanted
|
|
17
17
|
|
|
18
18
|
"expected the model to be sent arguments #{wanted.inspect}, but it was sent #{sent.arguments.inspect}"
|
|
@@ -24,14 +24,15 @@ module Squishling
|
|
|
24
24
|
definition.coerce((receiver || klass.new).instance_exec(error, **inputs, &handler))
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
# The first request a call would make, without making it: { system_prompt:, user_message:,
|
|
28
|
-
# step: } (
|
|
27
|
+
# The first request a call would make, without making it: { system_prompt:, user_message:, images:,
|
|
28
|
+
# output_schema:, step: } (images are the Squishling::Images sent with the message; the step is the
|
|
29
|
+
# ModelPath::Step with the model, provider and params; rotation isn't advanced).
|
|
29
30
|
def preview(klass, method: :call, receiver: nil, args: [], **kwargs)
|
|
30
31
|
definition = klass.squishling_definition(method)
|
|
31
32
|
receiver ||= klass.new
|
|
32
33
|
inputs = definition.bind_arguments(args, kwargs)
|
|
33
34
|
prompt = Invoker.new(definition, receiver, inputs).prompt
|
|
34
|
-
{ system_prompt: prompt.instructions, user_message: prompt.input,
|
|
35
|
+
{ system_prompt: prompt.instructions, user_message: prompt.input, images: prompt.images,
|
|
35
36
|
output_schema: prompt.schema.llm_schema["schema"],
|
|
36
37
|
step: definition.escalation_path(receiver, inputs, advance: false).first }
|
|
37
38
|
end
|
|
@@ -12,9 +12,15 @@ module Squishling
|
|
|
12
12
|
# sent. For a judge they are those of the call being judged, and candidates holds the two samples. messages is
|
|
13
13
|
# the conversation as sent (empty for a System One judgment, whose payload is the input). conversation is the
|
|
14
14
|
# index of the chat in Adapter#conversations (nil for a judgment): a retry is the same conversation, an
|
|
15
|
-
# escalation a new one.
|
|
15
|
+
# escalation a new one. adapter is the name the step picked (nil for the default adapter). reply is the
|
|
16
|
+
# Adapter::Response, or the error raised instead. cache is the prompt caching
|
|
17
|
+
# asked for: false, or a Hash of options.
|
|
16
18
|
Request = Data.define(:label, :type, :role, :sample, :attempt, :attempts, :model, :provider, :params,
|
|
17
|
-
:system_prompt, :output_schema, :messages, :conversation, :payload, :reply) do
|
|
19
|
+
:system_prompt, :output_schema, :messages, :conversation, :payload, :reply, :cache, :adapter) do
|
|
20
|
+
def initialize(reply:, cache: false, adapter: nil, **attributes)
|
|
21
|
+
super
|
|
22
|
+
end
|
|
23
|
+
|
|
18
24
|
def arguments
|
|
19
25
|
input["arguments"]
|
|
20
26
|
end
|
|
@@ -46,14 +52,27 @@ module Squishling
|
|
|
46
52
|
role == :judge
|
|
47
53
|
end
|
|
48
54
|
|
|
55
|
+
# The Squishling::Images sent with the conversation's first message (empty when none were).
|
|
56
|
+
def images
|
|
57
|
+
content = messages.first&.fetch(:content, nil)
|
|
58
|
+
content.is_a?(Array) ? content.filter_map { |part| part[:image] if part[:type] == :image } : []
|
|
59
|
+
end
|
|
60
|
+
|
|
49
61
|
# The JSON payload of the first user message, parsed (an escalation message wraps it in its own text).
|
|
50
62
|
def self.payload_of(messages)
|
|
51
|
-
text = messages.first.fetch(:content)
|
|
63
|
+
text = text_of(messages.first.fetch(:content))
|
|
52
64
|
JSON.parse(MESSAGES.parse_escalation(text)&.input || text)
|
|
53
65
|
rescue JSON::ParserError
|
|
54
66
|
{}
|
|
55
67
|
end
|
|
56
68
|
|
|
69
|
+
# A message's text: the content itself, or its text part when it carries images.
|
|
70
|
+
def self.text_of(content)
|
|
71
|
+
return content unless content.is_a?(Array)
|
|
72
|
+
|
|
73
|
+
content.filter_map { |part| part[:text] if part[:type] == :text }.join
|
|
74
|
+
end
|
|
75
|
+
|
|
57
76
|
private
|
|
58
77
|
|
|
59
78
|
# The method's arguments and context: the payload itself, or inside a judge's "input".
|
|
@@ -68,7 +87,7 @@ module Squishling
|
|
|
68
87
|
|
|
69
88
|
def escalation
|
|
70
89
|
text = messages.first&.fetch(:content, nil)
|
|
71
|
-
text && MESSAGES.parse_escalation(text)
|
|
90
|
+
text && MESSAGES.parse_escalation(self.class.text_of(text))
|
|
72
91
|
end
|
|
73
92
|
end
|
|
74
93
|
end
|
|
@@ -7,7 +7,7 @@ module Squishling
|
|
|
7
7
|
class Rule
|
|
8
8
|
# What a rule answers. nil means any. kind is :chat (Adapter#complete) or :judgment (Adapter#judge); role is
|
|
9
9
|
# nil (an escalation attempt, or any role when the rule names none), :sample, or :judge.
|
|
10
|
-
Selector = Data.define(:kind, :role, :sample, :model, :provider, :attempt)
|
|
10
|
+
Selector = Data.define(:kind, :role, :sample, :model, :provider, :adapter, :attempt)
|
|
11
11
|
|
|
12
12
|
# Rule#take when nothing is left to answer with.
|
|
13
13
|
EXHAUSTED = Object.new.freeze
|
|
@@ -43,15 +43,15 @@ module Squishling
|
|
|
43
43
|
def matches?(kind, call)
|
|
44
44
|
selector.kind == kind && fits?(selector.role, call[:role]) && fits?(selector.sample, call[:sample]) &&
|
|
45
45
|
fits?(selector.model, call[:model]) && fits?(selector.provider, call[:provider]) &&
|
|
46
|
-
fits?(selector.attempt, call[:attempt])
|
|
46
|
+
fits?(selector.adapter, call[:adapter]) && fits?(selector.attempt, call[:attempt])
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
# How narrowly the rule aims, lowest first: rules for a sample or the judge, then those naming the most of a
|
|
50
|
-
# model, a provider, and an attempt, then the rest.
|
|
50
|
+
# model, a provider, an adapter, and an attempt, then the rest.
|
|
51
51
|
def specificity
|
|
52
52
|
return 0 if selector.sample || selector.role
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
5 - [selector.model, selector.provider, selector.adapter, selector.attempt].count { |named| !named.nil? }
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
# The next answer, or EXHAUSTED. Called with the script's lock held.
|
|
@@ -152,10 +152,11 @@ module Squishling
|
|
|
152
152
|
@rules = []
|
|
153
153
|
end
|
|
154
154
|
|
|
155
|
-
# Escalation attempts (and any other chat request no sample or judge rule claims) on a model, a provider,
|
|
156
|
-
# :any model; attempt: narrows it to one attempt of the escalation
|
|
157
|
-
|
|
158
|
-
|
|
155
|
+
# Escalation attempts (and any other chat request no sample or judge rule claims) on a model, a provider, an
|
|
156
|
+
# adapter name (the step's adapter:), or :any model; attempt: narrows it to one attempt of the escalation
|
|
157
|
+
# (counting from 1).
|
|
158
|
+
def step(model = :any, provider: nil, adapter: nil, attempt: nil)
|
|
159
|
+
add(:chat, model: model == :any ? nil : model, provider:, adapter:, attempt:)
|
|
159
160
|
end
|
|
160
161
|
|
|
161
162
|
# A harness sample's requests ("a", "b", ...). Each sample has its own queue, so the order the sample threads
|
|
@@ -181,8 +182,9 @@ module Squishling
|
|
|
181
182
|
|
|
182
183
|
private
|
|
183
184
|
|
|
184
|
-
def add(kind, role: nil, sample: nil, model: nil, provider: nil, attempt: nil)
|
|
185
|
-
Rule
|
|
185
|
+
def add(kind, role: nil, sample: nil, model: nil, provider: nil, adapter: nil, attempt: nil)
|
|
186
|
+
selector = Rule::Selector.new(kind:, role:, sample:, model:, provider:, adapter:, attempt:)
|
|
187
|
+
Rule.new(selector).tap { |rule| @rules << rule }
|
|
186
188
|
end
|
|
187
189
|
end
|
|
188
190
|
end
|
data/lib/squishling/testing.rb
CHANGED
|
@@ -24,34 +24,35 @@ module Squishling
|
|
|
24
24
|
# Squishling::Testing::Minitest and ::RSpec mixins.
|
|
25
25
|
module Testing
|
|
26
26
|
class << self
|
|
27
|
-
# Makes the scripted Adapter the one every squished call goes through
|
|
28
|
-
# (see Script); #script adds to it
|
|
27
|
+
# Makes the scripted Adapter the one every squished call goes through (also for steps that name another
|
|
28
|
+
# adapter: the requests record which), and returns it. The block scripts it (see Script); #script adds to it
|
|
29
|
+
# later.
|
|
29
30
|
def install(adapter = Adapter.new)
|
|
30
31
|
yield adapter.script if block_given?
|
|
31
32
|
State.set(:observer, Observer.new(chained_observer(Squishling.config.observer)))
|
|
32
|
-
|
|
33
|
+
route_all_to(adapter)
|
|
33
34
|
adapter
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
# Any request that reaches the provider seam raises ProviderCallError, naming the squished method. Call it in a
|
|
37
38
|
# suite-wide before hook: a test that forgot to script its calls then fails at the call, not far from it.
|
|
38
39
|
def forbid_provider_calls!
|
|
39
|
-
|
|
40
|
+
route_all_to(ForbiddenAdapter.new)
|
|
40
41
|
end
|
|
41
42
|
|
|
42
43
|
# Runs the block with provider calls forbidden and returns the calls (the squished methods' labels) that tried
|
|
43
44
|
# to reach the provider, even if the block rescued the error. The adapter is put back afterwards.
|
|
44
45
|
def no_llm_call
|
|
45
46
|
config = Squishling.config
|
|
46
|
-
previous = config.adapter
|
|
47
|
+
previous = [config.adapter, config.adapter_override]
|
|
47
48
|
guard = ForbiddenAdapter.new
|
|
48
|
-
config.adapter = guard
|
|
49
|
+
config.adapter = config.adapter_override = guard
|
|
49
50
|
begin
|
|
50
51
|
yield
|
|
51
52
|
rescue ProviderCallError
|
|
52
53
|
nil
|
|
53
54
|
ensure
|
|
54
|
-
config.adapter = previous
|
|
55
|
+
config.adapter, config.adapter_override = previous
|
|
55
56
|
end
|
|
56
57
|
guard.attempts
|
|
57
58
|
end
|
|
@@ -113,6 +114,12 @@ module Squishling
|
|
|
113
114
|
|
|
114
115
|
private
|
|
115
116
|
|
|
117
|
+
# The adapter for every request: the default, and the one a step names with adapter:.
|
|
118
|
+
def route_all_to(adapter)
|
|
119
|
+
State.set(:adapter, adapter)
|
|
120
|
+
State.set(:adapter_override, adapter)
|
|
121
|
+
end
|
|
122
|
+
|
|
116
123
|
# An observer the app configured itself (an object, or :coolhand) is kept in the chain; the default Coolhand
|
|
117
124
|
# auto-detection, and our own observer from an earlier install, are not.
|
|
118
125
|
def chained_observer(observer)
|
data/lib/squishling/verifier.rb
CHANGED
|
@@ -7,7 +7,7 @@ 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
|
-
CHECKS = %i[schema declarations harness callables].freeze
|
|
10
|
+
CHECKS = %i[schema declarations adapters harness callables].freeze
|
|
11
11
|
|
|
12
12
|
def initialize(definition)
|
|
13
13
|
@definition = definition
|
|
@@ -50,6 +50,24 @@ module Squishling
|
|
|
50
50
|
[]
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
+
# The adapter of every escalation step and of the judge must be configured. The default one (a step or judge that
|
|
54
|
+
# names none) is checked here only when named adapters exist, since otherwise the whole run reports it once.
|
|
55
|
+
def adapters
|
|
56
|
+
names = @definition.declared_path.first.map { |step| step[:adapter] }
|
|
57
|
+
judge = @definition.harness.options[:judge]
|
|
58
|
+
names << judge[:adapter] if judge.is_a?(Hash)
|
|
59
|
+
names = names.compact unless Squishling.config.adapters.any?
|
|
60
|
+
names.uniq.filter_map { |name| adapter_problem(name) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A message when the step's adapter (nil, the default one) can't be used. Not through Testing's override.
|
|
64
|
+
def adapter_problem(name)
|
|
65
|
+
name.nil? ? Squishling.config.resolved_adapter : Squishling.config.named_adapter(name)
|
|
66
|
+
nil
|
|
67
|
+
rescue ConfigurationError => e
|
|
68
|
+
"adapter: #{e.message}"
|
|
69
|
+
end
|
|
70
|
+
|
|
53
71
|
def harness
|
|
54
72
|
harness = @definition.harness
|
|
55
73
|
messages = []
|
|
@@ -62,7 +80,7 @@ module Squishling
|
|
|
62
80
|
|
|
63
81
|
def unsupported_judgment?(harness)
|
|
64
82
|
judge = harness.options[:judge]
|
|
65
|
-
judge.is_a?(Hash) && judge[:type] == :judgment && !Squishling.config.
|
|
83
|
+
judge.is_a?(Hash) && judge[:type] == :judgment && !Squishling.config.adapter_for(judge[:adapter]).judgments?
|
|
66
84
|
rescue ConfigurationError
|
|
67
85
|
false # no usable adapter: reported once, as the adapter's own problem
|
|
68
86
|
end
|
|
@@ -152,9 +170,11 @@ module Squishling
|
|
|
152
170
|
raise ConfigurationError, "#{klass.inspect} doesn't include Squishling"
|
|
153
171
|
end
|
|
154
172
|
|
|
155
|
-
# The configured adapter, or RubyLLM's, must be usable
|
|
173
|
+
# The configured adapter, or RubyLLM's, must be usable, unless named adapters are configured: then the default
|
|
174
|
+
# one is only needed by the steps that use it, which each method's check reports. The named adapters are built
|
|
175
|
+
# when assigned.
|
|
156
176
|
def adapter_problems
|
|
157
|
-
Squishling.config.resolved_adapter
|
|
177
|
+
Squishling.config.resolved_adapter unless Squishling.config.adapters.any?
|
|
158
178
|
[]
|
|
159
179
|
rescue ConfigurationError => e
|
|
160
180
|
[Verification::Problem.new(label: "adapter", message: e.message)]
|
data/lib/squishling/version.rb
CHANGED
data/lib/squishling.rb
CHANGED
|
@@ -8,13 +8,16 @@ require_relative "squishling/version"
|
|
|
8
8
|
require_relative "squishling/errors"
|
|
9
9
|
require_relative "squishling/configuration"
|
|
10
10
|
require_relative "squishling/params"
|
|
11
|
+
require_relative "squishling/cache"
|
|
11
12
|
require_relative "squishling/named_registry"
|
|
12
13
|
require_relative "squishling/secret"
|
|
14
|
+
require_relative "squishling/image"
|
|
13
15
|
require_relative "squishling/adapter/error_body"
|
|
14
16
|
require_relative "squishling/adapter"
|
|
15
17
|
require_relative "squishling/adapters/ruby_llm"
|
|
16
18
|
require_relative "squishling/adapters/openai_options"
|
|
17
19
|
require_relative "squishling/adapters/openai_transport"
|
|
20
|
+
require_relative "squishling/adapters/openai_content"
|
|
18
21
|
require_relative "squishling/adapters/openai_compatible"
|
|
19
22
|
require_relative "squishling/squawk"
|
|
20
23
|
require_relative "squishling/observer"
|
|
@@ -30,6 +33,7 @@ require_relative "squishling/harness/judged_squishsum"
|
|
|
30
33
|
require_relative "squishling/harness"
|
|
31
34
|
require_relative "squishling/result"
|
|
32
35
|
require_relative "squishling/schema"
|
|
36
|
+
require_relative "squishling/payload"
|
|
33
37
|
require_relative "squishling/source"
|
|
34
38
|
require_relative "squishling/appendices"
|
|
35
39
|
require_relative "squishling/definition"
|
|
@@ -163,11 +167,12 @@ module Squishling
|
|
|
163
167
|
# it); return it from the method. The overrides apply to this call only: append_to_purpose adds to (or,
|
|
164
168
|
# with false, replaces) the declared sections, context is sent alongside the declared squish_context,
|
|
165
169
|
# model/escalation (one or the other)/provider/purpose/harness replace the declared ones, and params merge
|
|
166
|
-
# key by key over them.
|
|
170
|
+
# key by key over them. cache: replaces the declared prompt caching setting (false turns it off, see Cache).
|
|
171
|
+
# max_total_output_tokens/max_total_input_tokens cap what the call may spend across all its
|
|
167
172
|
# requests (false lifts a declared cap).
|
|
168
173
|
def squish!(append_to_purpose: nil, context: nil, purpose: nil, model: nil, escalation: nil, provider: nil,
|
|
169
|
-
params: nil, harness: nil, max_total_output_tokens: nil, max_total_input_tokens: nil)
|
|
174
|
+
params: nil, cache: nil, harness: nil, max_total_output_tokens: nil, max_total_input_tokens: nil)
|
|
170
175
|
Router.hand_off(self, { append_to_purpose:, context:, purpose:, model:, escalation:, provider:, params:,
|
|
171
|
-
harness:, max_total_output_tokens:, max_total_input_tokens: })
|
|
176
|
+
cache:, harness:, max_total_output_tokens:, max_total_input_tokens: })
|
|
172
177
|
end
|
|
173
178
|
end
|
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.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Michael Carroll
|
|
@@ -62,6 +62,7 @@ files:
|
|
|
62
62
|
- docs/coolhand.md
|
|
63
63
|
- docs/failures.md
|
|
64
64
|
- docs/harnesses.md
|
|
65
|
+
- docs/images.md
|
|
65
66
|
- docs/measuring-tokens.md
|
|
66
67
|
- docs/naming.md
|
|
67
68
|
- docs/observers.md
|
|
@@ -73,11 +74,13 @@ files:
|
|
|
73
74
|
- lib/squishling/adapter.rb
|
|
74
75
|
- lib/squishling/adapter/error_body.rb
|
|
75
76
|
- lib/squishling/adapters/openai_compatible.rb
|
|
77
|
+
- lib/squishling/adapters/openai_content.rb
|
|
76
78
|
- lib/squishling/adapters/openai_options.rb
|
|
77
79
|
- lib/squishling/adapters/openai_transport.rb
|
|
78
80
|
- lib/squishling/adapters/ruby_llm.rb
|
|
79
81
|
- lib/squishling/appendices.rb
|
|
80
82
|
- lib/squishling/budget.rb
|
|
83
|
+
- lib/squishling/cache.rb
|
|
81
84
|
- lib/squishling/class_methods.rb
|
|
82
85
|
- lib/squishling/class_registry.rb
|
|
83
86
|
- lib/squishling/collisions.rb
|
|
@@ -91,6 +94,7 @@ files:
|
|
|
91
94
|
- lib/squishling/harness/option_types.rb
|
|
92
95
|
- lib/squishling/harness/registry.rb
|
|
93
96
|
- lib/squishling/harness/squishsum.rb
|
|
97
|
+
- lib/squishling/image.rb
|
|
94
98
|
- lib/squishling/invoker.rb
|
|
95
99
|
- lib/squishling/judge.rb
|
|
96
100
|
- lib/squishling/llm_client.rb
|
|
@@ -100,6 +104,7 @@ files:
|
|
|
100
104
|
- lib/squishling/observers/coolhand.rb
|
|
101
105
|
- lib/squishling/output_check.rb
|
|
102
106
|
- lib/squishling/params.rb
|
|
107
|
+
- lib/squishling/payload.rb
|
|
103
108
|
- lib/squishling/railtie.rb
|
|
104
109
|
- lib/squishling/result.rb
|
|
105
110
|
- lib/squishling/rotation.rb
|