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
data/lib/squishling/schema.rb
CHANGED
|
@@ -4,8 +4,15 @@ module Squishling
|
|
|
4
4
|
# Normalizes the supported output-schema forms into a JSON Schema, validates data against it,
|
|
5
5
|
# and builds typed results.
|
|
6
6
|
class Schema
|
|
7
|
-
|
|
7
|
+
# Held weakly by the raw schema (a Schematist class, a Hash), so classes replaced by code reloading can be
|
|
8
|
+
# collected along with their schemas. A Hash compares by content, which yields the same schema.
|
|
9
|
+
CACHE = ObjectSpace::WeakKeyMap.new
|
|
8
10
|
CACHE_LOCK = Mutex.new
|
|
11
|
+
# What building a validator raises for a $ref (or $schema) it can't resolve. json_schemer's default resolver
|
|
12
|
+
# refuses remote URIs, so none is fetched.
|
|
13
|
+
REF_ERRORS = [
|
|
14
|
+
JSONSchemer::UnknownRef, JSONSchemer::InvalidRefResolution, JSONSchemer::InvalidRefPointer, URI::InvalidURIError
|
|
15
|
+
].freeze
|
|
9
16
|
|
|
10
17
|
# Conditional keywords (Schematist's `given` and `dependent`) that providers' strict modes don't support.
|
|
11
18
|
# They're validated locally and kept out of the schema sent to the LLM.
|
|
@@ -49,15 +56,15 @@ module Squishling
|
|
|
49
56
|
"schema" => provider_schema(@json_schema),
|
|
50
57
|
"strict" => true
|
|
51
58
|
}.compact
|
|
52
|
-
@validator =
|
|
59
|
+
@validator = validator_for(@json_schema)
|
|
53
60
|
# Built now so a property name the result can't take fails here, not on the first response.
|
|
54
61
|
@result_class = Result.build(@json_schema)
|
|
55
62
|
end
|
|
56
63
|
|
|
57
64
|
def validate(data)
|
|
58
65
|
errors = @validator.validate(data).map { |error| error["error"] }
|
|
59
|
-
# A raw Hash schema can
|
|
60
|
-
# invalid output rather than a NoMethodError
|
|
66
|
+
# A raw Hash schema's type can be a union that includes "object" (a bare `properties` is refused at build);
|
|
67
|
+
# the typed result is built from a Hash, so anything else is invalid output rather than a NoMethodError.
|
|
61
68
|
errors.empty? && result_class && !data.is_a?(Hash) ? ["root must be an object"] : errors
|
|
62
69
|
end
|
|
63
70
|
|
|
@@ -67,6 +74,39 @@ module Squishling
|
|
|
67
74
|
|
|
68
75
|
private
|
|
69
76
|
|
|
77
|
+
# The json_schemer validator, with every $ref resolved now, so one that points nowhere (a remote URI, a missing
|
|
78
|
+
# $defs entry) fails at build and in Squishling.verify, not on the first response.
|
|
79
|
+
def validator_for(json_schema)
|
|
80
|
+
validator = JSONSchemer.schema(json_schema)
|
|
81
|
+
resolve_refs!(validator, {}.compare_by_identity)
|
|
82
|
+
validator
|
|
83
|
+
rescue *REF_ERRORS => e
|
|
84
|
+
raise ConfigurationError, "output_schema has a $ref that can't be resolved (#{e.message}); " \
|
|
85
|
+
"only references within the schema are supported"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Walks the parsed schema (json_schemer's Schema#parsed and Keyword#ref_schema) and into each target a $ref
|
|
89
|
+
# resolves to, so a reference inside a subschema kept under any keyword is checked too. seen stops cycles.
|
|
90
|
+
def resolve_refs!(schema, seen)
|
|
91
|
+
return if seen.key?(schema)
|
|
92
|
+
|
|
93
|
+
seen[schema] = true
|
|
94
|
+
schema.parsed.each_value do |keyword|
|
|
95
|
+
resolve_refs!(keyword.ref_schema, seen) if keyword.respond_to?(:ref_schema)
|
|
96
|
+
subschemas(keyword.parsed).each { |subschema| resolve_refs!(subschema, seen) }
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The Schema objects directly under a parsed keyword: itself, in a list, or in a map of names.
|
|
101
|
+
def subschemas(parsed)
|
|
102
|
+
case parsed
|
|
103
|
+
when JSONSchemer::Schema then [parsed]
|
|
104
|
+
when Array then parsed.grep(JSONSchemer::Schema)
|
|
105
|
+
when Hash then parsed.values.grep(JSONSchemer::Schema)
|
|
106
|
+
else []
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
70
110
|
def to_hash(raw)
|
|
71
111
|
case raw
|
|
72
112
|
when Hash then raw
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Squishling
|
|
4
|
+
# Holds a credential (an API key, or the headers that may carry one) so a serializer that walks an object's instance
|
|
5
|
+
# variables can't write it out: to_s, inspect, to_yaml, as_json (so ActiveSupport's instance_values walk too) and
|
|
6
|
+
# to_json all show "[REDACTED]", and Marshal refuses. The value sits in a closure rather than an instance variable;
|
|
7
|
+
# #reveal is the only way to read it, and it is for the code that sends the request. Code running in the process can
|
|
8
|
+
# still call #reveal; this guards against accidental serialization (a logged config, an error tracker, a cache
|
|
9
|
+
# dump), not against a hostile caller.
|
|
10
|
+
class Secret
|
|
11
|
+
REDACTED = "[REDACTED]"
|
|
12
|
+
|
|
13
|
+
def initialize(value)
|
|
14
|
+
@reveal = -> { value }
|
|
15
|
+
freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def reveal
|
|
19
|
+
@reveal.call
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def inspect
|
|
23
|
+
REDACTED
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
alias_method :to_s, :inspect
|
|
27
|
+
|
|
28
|
+
def encode_with(coder)
|
|
29
|
+
coder.scalar = REDACTED
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def as_json(*)
|
|
33
|
+
REDACTED
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def to_json(*)
|
|
37
|
+
REDACTED.to_json
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def marshal_dump
|
|
41
|
+
raise TypeError, "Squishling::Secret can't be dumped: it holds a credential"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
private_constant :Secret
|
|
45
|
+
end
|
data/lib/squishling/squawk.rb
CHANGED
|
@@ -4,14 +4,16 @@ 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
6
|
# tracing tool). It's the one sanctioned way for model output to leave the process; nothing runs unless one
|
|
7
|
-
# is configured.
|
|
7
|
+
# is configured. A call that a token budget or a disagreement stops gets one more report, with the error and no
|
|
8
|
+
# output (metadata's stopped:), since neither has an attempt of its own.
|
|
8
9
|
module Squawk
|
|
9
10
|
KEYWORD_TYPES = %i[key keyreq].freeze
|
|
10
11
|
POSITIONAL_TYPES = %i[req opt].freeze
|
|
11
12
|
KEYWORD_CAPTURE_TYPES = %i[key keyreq keyrest].freeze
|
|
12
13
|
KEYWORDS = %i[output metadata error].freeze
|
|
13
14
|
# The squawk_on: conditions, each answering "does the hook run for this attempt?" from the attempt's 1-based
|
|
14
|
-
# number (the one its metadata carries), its error (nil when accepted), and whether it is the last one.
|
|
15
|
+
# number (the one its metadata carries), its error (nil when accepted), and whether it is the last one. A stopped
|
|
16
|
+
# call's report has an error and is final, so it passes every condition whatever its attempt number (0 or more).
|
|
15
17
|
CONDITIONS = {
|
|
16
18
|
all: ->(_attempt, _error, _final) { true },
|
|
17
19
|
rejections: ->(_attempt, error, _final) { !error.nil? },
|
|
@@ -25,7 +27,8 @@ module Squishling
|
|
|
25
27
|
# Which attempts reach the hook (squawk_on:): one condition or a list of them, or nil to inherit. :all is
|
|
26
28
|
# every attempt, :rejections only the ones that errored, :retries those plus any accepted attempt after the
|
|
27
29
|
# first, and :final the last attempt the escalation allows (metadata's final:), however it ended; an earlier
|
|
28
|
-
# accepted attempt is not final.
|
|
30
|
+
# accepted attempt is not final. The report of a stopped call is final and has an error, so every
|
|
31
|
+
# condition lets it through. A list runs the hook when any of them holds.
|
|
29
32
|
def validate_on(value, where)
|
|
30
33
|
return value if value.nil?
|
|
31
34
|
|
|
@@ -48,7 +51,7 @@ module Squishling
|
|
|
48
51
|
return hook if hook.nil? || hook == false
|
|
49
52
|
|
|
50
53
|
unless hook.respond_to?(:call)
|
|
51
|
-
raise ConfigurationError, "#{where} squawk must respond to call (or be false), got #{hook
|
|
54
|
+
raise ConfigurationError, "#{where} squawk must respond to call (or be false), got #{Appendices.describe(hook)}"
|
|
52
55
|
end
|
|
53
56
|
if positional_only?(parameters(hook))
|
|
54
57
|
raise ConfigurationError,
|
|
@@ -82,7 +82,8 @@ module Squishling
|
|
|
82
82
|
payload = Schema.jsonify(input)
|
|
83
83
|
@requests << Request.new(label:, type: :judgment, role: :judge, sample: nil, attempt: call[:attempt],
|
|
84
84
|
attempts: call[:attempts], model: step.model, provider: step.provider, params: step.params,
|
|
85
|
-
system_prompt: nil, output_schema: nil, messages: [], conversation: nil, payload:,
|
|
85
|
+
cache: step.cache, system_prompt: nil, output_schema: nil, messages: [], conversation: nil, payload:,
|
|
86
|
+
reply: outcome)
|
|
86
87
|
outcome
|
|
87
88
|
end
|
|
88
89
|
|
|
@@ -150,8 +151,9 @@ module Squishling
|
|
|
150
151
|
def record(request, call, outcome, conversation)
|
|
151
152
|
@requests << Request.new(label: request.label, type: :chat, role: call[:role], sample: call[:sample]&.to_s,
|
|
152
153
|
attempt: call[:attempt], attempts: call[:attempts], model: request.model, provider: request.provider,
|
|
153
|
-
params: request.params,
|
|
154
|
-
|
|
154
|
+
params: request.params, cache: request.cache, system_prompt: request.instructions,
|
|
155
|
+
output_schema: request.schema["schema"], messages: request.messages, conversation:,
|
|
156
|
+
payload: Request.payload_of(request.messages), reply: outcome)
|
|
155
157
|
end
|
|
156
158
|
end
|
|
157
159
|
|
|
@@ -12,9 +12,14 @@ 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. reply is the Adapter::Response, or the error raised instead.
|
|
15
|
+
# escalation a new one. reply is the Adapter::Response, or the error raised instead. cache is the prompt caching
|
|
16
|
+
# asked for: false, or a Hash of options.
|
|
16
17
|
Request = Data.define(:label, :type, :role, :sample, :attempt, :attempts, :model, :provider, :params,
|
|
17
|
-
:system_prompt, :output_schema, :messages, :conversation, :payload, :reply) do
|
|
18
|
+
:system_prompt, :output_schema, :messages, :conversation, :payload, :reply, :cache) do
|
|
19
|
+
def initialize(reply:, cache: false, **attributes)
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
|
|
18
23
|
def arguments
|
|
19
24
|
input["arguments"]
|
|
20
25
|
end
|
|
@@ -52,7 +52,8 @@ module Squishling
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
# What a test asked squawk to record: every attempt's raw output, error, and metadata (role, sample, attempt,
|
|
55
|
-
# final, usage, ...)
|
|
55
|
+
# final, usage, ...), and the report of a call stopped by a token budget or a disagreement (stopped).
|
|
56
|
+
# Use Testing.with_squawk, which puts the previous hook back.
|
|
56
57
|
class SquawkCapture
|
|
57
58
|
Call = Data.define(:output, :error, :metadata)
|
|
58
59
|
|
data/lib/squishling/version.rb
CHANGED
data/lib/squishling/wrapper.rb
CHANGED
|
@@ -4,6 +4,24 @@ module Squishling
|
|
|
4
4
|
# Module prepended to a squishling class. It intercepts squished methods regardless of
|
|
5
5
|
# whether they're defined before or after the `squish` declaration (or at all).
|
|
6
6
|
class Wrapper < Module
|
|
7
|
+
# Backtrace locations report real paths, so resolve this one too (a symlinked install keeps __FILE__ as given).
|
|
8
|
+
FILE = File.realpath(__FILE__)
|
|
9
|
+
private_constant :FILE
|
|
10
|
+
|
|
11
|
+
# Raised by the wrapper when a squished method has no implementation at all.
|
|
12
|
+
class NoImplementation < NotImplementedError; end
|
|
13
|
+
private_constant :NoImplementation
|
|
14
|
+
|
|
15
|
+
# Whether a NotImplementedError is the "no implementation" signal rather than an error from called code: the
|
|
16
|
+
# wrapper's own, or one raised directly in the method's own frame (`def x = raise NotImplementedError`). The
|
|
17
|
+
# implementation lambda below sits one frame above the method, so anything raised deeper (a helper, a block,
|
|
18
|
+
# another object, a `super` into an unsquished parent) has more frames in between. A missing backtrace fails
|
|
19
|
+
# closed; an exception re-raised or reused keeps the backtrace of its first raise, so a stub that escapes
|
|
20
|
+
# another squished call (after its `squish!`) still reads as a stub to the caller.
|
|
21
|
+
def self.stub?(error)
|
|
22
|
+
error.is_a?(NoImplementation) || error.backtrace_locations&.at(1)&.absolute_path == FILE
|
|
23
|
+
end
|
|
24
|
+
|
|
7
25
|
# The method beneath any Squishling wrappers (instance_method resolves to the prepended wrapper first),
|
|
8
26
|
# or nil when the method has no implementation.
|
|
9
27
|
def self.implementation(method)
|
|
@@ -20,7 +38,7 @@ module Squishling
|
|
|
20
38
|
if defined?(super)
|
|
21
39
|
-> { super(*args, **kwargs, &block) }
|
|
22
40
|
else
|
|
23
|
-
-> { raise
|
|
41
|
+
-> { raise NoImplementation, "#{self.class}##{name} has no implementation" }
|
|
24
42
|
end
|
|
25
43
|
|
|
26
44
|
Router.dispatch(self, name, args, kwargs, impl, wrapper:)
|
data/lib/squishling.rb
CHANGED
|
@@ -8,11 +8,14 @@ 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"
|
|
13
|
+
require_relative "squishling/secret"
|
|
12
14
|
require_relative "squishling/adapter/error_body"
|
|
13
15
|
require_relative "squishling/adapter"
|
|
14
16
|
require_relative "squishling/adapters/ruby_llm"
|
|
15
17
|
require_relative "squishling/adapters/openai_options"
|
|
18
|
+
require_relative "squishling/adapters/openai_transport"
|
|
16
19
|
require_relative "squishling/adapters/openai_compatible"
|
|
17
20
|
require_relative "squishling/squawk"
|
|
18
21
|
require_relative "squishling/observer"
|
|
@@ -36,6 +39,7 @@ require_relative "squishling/llm_client"
|
|
|
36
39
|
require_relative "squishling/output_check"
|
|
37
40
|
require_relative "squishling/run/messages"
|
|
38
41
|
require_relative "squishling/run/rejections"
|
|
42
|
+
require_relative "squishling/run/reports"
|
|
39
43
|
require_relative "squishling/run"
|
|
40
44
|
require_relative "squishling/invoker"
|
|
41
45
|
require_relative "squishling/judge"
|
|
@@ -160,11 +164,12 @@ module Squishling
|
|
|
160
164
|
# it); return it from the method. The overrides apply to this call only: append_to_purpose adds to (or,
|
|
161
165
|
# with false, replaces) the declared sections, context is sent alongside the declared squish_context,
|
|
162
166
|
# model/escalation (one or the other)/provider/purpose/harness replace the declared ones, and params merge
|
|
163
|
-
# key by key over them.
|
|
167
|
+
# key by key over them. cache: replaces the declared prompt caching setting (false turns it off, see Cache).
|
|
168
|
+
# max_total_output_tokens/max_total_input_tokens cap what the call may spend across all its
|
|
164
169
|
# requests (false lifts a declared cap).
|
|
165
170
|
def squish!(append_to_purpose: nil, context: nil, purpose: nil, model: nil, escalation: nil, provider: nil,
|
|
166
|
-
params: nil, harness: nil, max_total_output_tokens: nil, max_total_input_tokens: nil)
|
|
171
|
+
params: nil, cache: nil, harness: nil, max_total_output_tokens: nil, max_total_input_tokens: nil)
|
|
167
172
|
Router.hand_off(self, { append_to_purpose:, context:, purpose:, model:, escalation:, provider:, params:,
|
|
168
|
-
harness:, max_total_output_tokens:, max_total_input_tokens: })
|
|
173
|
+
cache:, harness:, max_total_output_tokens:, max_total_input_tokens: })
|
|
169
174
|
end
|
|
170
175
|
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.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Michael Carroll
|
|
@@ -74,9 +74,11 @@ files:
|
|
|
74
74
|
- lib/squishling/adapter/error_body.rb
|
|
75
75
|
- lib/squishling/adapters/openai_compatible.rb
|
|
76
76
|
- lib/squishling/adapters/openai_options.rb
|
|
77
|
+
- lib/squishling/adapters/openai_transport.rb
|
|
77
78
|
- lib/squishling/adapters/ruby_llm.rb
|
|
78
79
|
- lib/squishling/appendices.rb
|
|
79
80
|
- lib/squishling/budget.rb
|
|
81
|
+
- lib/squishling/cache.rb
|
|
80
82
|
- lib/squishling/class_methods.rb
|
|
81
83
|
- lib/squishling/class_registry.rb
|
|
82
84
|
- lib/squishling/collisions.rb
|
|
@@ -107,7 +109,9 @@ files:
|
|
|
107
109
|
- lib/squishling/run.rb
|
|
108
110
|
- lib/squishling/run/messages.rb
|
|
109
111
|
- lib/squishling/run/rejections.rb
|
|
112
|
+
- lib/squishling/run/reports.rb
|
|
110
113
|
- lib/squishling/schema.rb
|
|
114
|
+
- lib/squishling/secret.rb
|
|
111
115
|
- lib/squishling/source.rb
|
|
112
116
|
- lib/squishling/squawk.rb
|
|
113
117
|
- lib/squishling/tasks/squishling.rake
|