ruby-on-dspy 0.1.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 +7 -0
- data/CHANGELOG.md +112 -0
- data/LICENSE +21 -0
- data/README.md +442 -0
- data/exe/ruby-on-dspy +7 -0
- data/lib/dspy_rb/cli.rb +83 -0
- data/lib/dspy_rb/clients/dummy_lm.rb +81 -0
- data/lib/dspy_rb/clients/lm.rb +44 -0
- data/lib/dspy_rb/datasets/data_loader.rb +74 -0
- data/lib/dspy_rb/datasets/dataset.rb +37 -0
- data/lib/dspy_rb/datasets/python_random.rb +38 -0
- data/lib/dspy_rb/errors.rb +135 -0
- data/lib/dspy_rb/evaluate/evaluate.rb +53 -0
- data/lib/dspy_rb/evaluate/metric.rb +61 -0
- data/lib/dspy_rb/execution/around.rb +63 -0
- data/lib/dspy_rb/execution/collector.rb +61 -0
- data/lib/dspy_rb/execution/context.rb +160 -0
- data/lib/dspy_rb/execution/evaluation.rb +90 -0
- data/lib/dspy_rb/execution/optimization.rb +71 -0
- data/lib/dspy_rb/execution/program.rb +126 -0
- data/lib/dspy_rb/execution/rows.rb +41 -0
- data/lib/dspy_rb/execution/scorer.rb +68 -0
- data/lib/dspy_rb/execution/step.rb +116 -0
- data/lib/dspy_rb/execution/toolbox.rb +28 -0
- data/lib/dspy_rb/predict/chain_of_thought.rb +22 -0
- data/lib/dspy_rb/predict/predict.rb +101 -0
- data/lib/dspy_rb/predict/react.rb +80 -0
- data/lib/dspy_rb/primitives/module.rb +105 -0
- data/lib/dspy_rb/primitives/parts.rb +121 -0
- data/lib/dspy_rb/runtime/channel/callback.rb +105 -0
- data/lib/dspy_rb/runtime/channel/ending.rb +35 -0
- data/lib/dspy_rb/runtime/channel/exchange.rb +176 -0
- data/lib/dspy_rb/runtime/channel/gate.rb +74 -0
- data/lib/dspy_rb/runtime/channel/listener.rb +125 -0
- data/lib/dspy_rb/runtime/channel/meter.rb +46 -0
- data/lib/dspy_rb/runtime/channel/reporter.rb +68 -0
- data/lib/dspy_rb/runtime/channel/settler.rb +69 -0
- data/lib/dspy_rb/runtime/channel/traffic.rb +121 -0
- data/lib/dspy_rb/runtime/channel/watchdog.rb +36 -0
- data/lib/dspy_rb/runtime/channel/writer.rb +77 -0
- data/lib/dspy_rb/runtime/channel.rb +145 -0
- data/lib/dspy_rb/runtime/child.rb +48 -0
- data/lib/dspy_rb/runtime/python.rb +43 -0
- data/lib/dspy_rb/runtime/registry.rb +48 -0
- data/lib/dspy_rb/runtime/startup.rb +53 -0
- data/lib/dspy_rb/secrets.rb +25 -0
- data/lib/dspy_rb/setup/environment.rb +120 -0
- data/lib/dspy_rb/setup/stale.rb +102 -0
- data/lib/dspy_rb/setup/uv.rb +122 -0
- data/lib/dspy_rb/signatures/signature.rb +100 -0
- data/lib/dspy_rb/signatures/types.rb +44 -0
- data/lib/dspy_rb/source.rb +64 -0
- data/lib/dspy_rb/teleprompt/bootstrap_few_shot.rb +9 -0
- data/lib/dspy_rb/teleprompt/bootstrap_few_shot_with_optuna.rb +9 -0
- data/lib/dspy_rb/teleprompt/bootstrap_few_shot_with_random_search.rb +9 -0
- data/lib/dspy_rb/teleprompt/gepa.rb +9 -0
- data/lib/dspy_rb/teleprompt/labeled_few_shot.rb +14 -0
- data/lib/dspy_rb/teleprompt/mipro_v2.rb +16 -0
- data/lib/dspy_rb/teleprompt/optimizer.rb +127 -0
- data/lib/dspy_rb/teleprompt/simba.rb +16 -0
- data/lib/dspy_rb/tool.rb +108 -0
- data/lib/dspy_rb/values/check.rb +14 -0
- data/lib/dspy_rb/values/context.rb +46 -0
- data/lib/dspy_rb/values/dataset.rb +55 -0
- data/lib/dspy_rb/values/dict.rb +17 -0
- data/lib/dspy_rb/values/env.rb +23 -0
- data/lib/dspy_rb/values/evaluation_result.rb +31 -0
- data/lib/dspy_rb/values/example.rb +134 -0
- data/lib/dspy_rb/values/fault.rb +15 -0
- data/lib/dspy_rb/values/history.rb +13 -0
- data/lib/dspy_rb/values/list.rb +16 -0
- data/lib/dspy_rb/values/literal.rb +27 -0
- data/lib/dspy_rb/values/loss.rb +12 -0
- data/lib/dspy_rb/values/optional.rb +16 -0
- data/lib/dspy_rb/values/prediction.rb +58 -0
- data/lib/dspy_rb/values/reasoning.rb +13 -0
- data/lib/dspy_rb/values/spend.rb +27 -0
- data/lib/dspy_rb/values/types.rb +164 -0
- data/lib/dspy_rb/version.rb +11 -0
- data/lib/dspy_rb/wire/departure.rb +100 -0
- data/lib/dspy_rb/wire/descriptions.rb +220 -0
- data/lib/dspy_rb/wire/facts.rb +79 -0
- data/lib/dspy_rb/wire/in_flight.rb +128 -0
- data/lib/dspy_rb/wire/json.rb +76 -0
- data/lib/dspy_rb/wire/optimizers.rb +240 -0
- data/lib/dspy_rb/wire/records.rb +44 -0
- data/lib/dspy_rb/wire/replies.rb +188 -0
- data/lib/dspy_rb/wire/requests.rb +214 -0
- data/lib/dspy_rb/wire/shape.rb +25 -0
- data/lib/dspy_rb/wire/types.rb +23 -0
- data/lib/dspy_rb/wire.rb +34 -0
- data/lib/dspy_rb.rb +191 -0
- data/lib/ruby-on-dspy.rb +4 -0
- data/python/dspy_rb/__init__.py +1 -0
- data/python/dspy_rb/__main__.py +76 -0
- data/python/dspy_rb/server/__init__.py +1 -0
- data/python/dspy_rb/server/build.py +281 -0
- data/python/dspy_rb/server/calls.py +215 -0
- data/python/dspy_rb/server/conversion.py +180 -0
- data/python/dspy_rb/server/data.py +169 -0
- data/python/dspy_rb/server/evaluation.py +107 -0
- data/python/dspy_rb/server/facts.py +357 -0
- data/python/dspy_rb/server/optimization.py +98 -0
- data/python/dspy_rb/server/pulse.py +68 -0
- data/python/dspy_rb/server/ruby.py +206 -0
- data/python/dspy_rb/server/serve.py +369 -0
- data/python/dspy_rb/server/standins.py +364 -0
- data/python/dspy_rb/server/state.py +104 -0
- data/python/dspy_rb/server/traffic.py +93 -0
- data/python/dspy_rb/values.py +51 -0
- data/python/dspy_rb/wire/__init__.py +1 -0
- data/python/dspy_rb/wire/channel.py +107 -0
- data/python/dspy_rb/wire/lines.py +269 -0
- data/python/dspy_rb/wire/shapes.py +1220 -0
- data/python/dspy_rb/wire/wire.json +4592 -0
- data/python/pyproject.toml +86 -0
- data/python/uv.lock +1576 -0
- metadata +230 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
# One of DSPy's optimizers, which DSPy runs in Python: its compile gives a copy of the program, the program unchanged.
|
|
5
|
+
class Optimizer
|
|
6
|
+
extend Dry::Initializer
|
|
7
|
+
|
|
8
|
+
# As `[]` makes them.
|
|
9
|
+
param :options, proc { |given, optimizer| taken_options(given, optimizer.class) }, reader: :private
|
|
10
|
+
|
|
11
|
+
# The options as given, whose Ruby objects DSPy calls back: the metric, and a DummyLM's answers.
|
|
12
|
+
param :given, Wire::Types::Strict::Hash, default: proc { {} }, reader: :private
|
|
13
|
+
|
|
14
|
+
# Each of DSPy's options by its name; one left out or given as nil keeps DSPy's default.
|
|
15
|
+
def self.[](**options)
|
|
16
|
+
new(Given.taken(self::OPTIONS, options, name), options)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Options given to `new` of `made`, any other refused by the place alone; a Ruby metric's callbacks need `[]`.
|
|
20
|
+
def self.taken_options(given, made)
|
|
21
|
+
Wire::Departure.taken(Wire::Types.Instance(made::OPTIONS), given, "#{made.name}.new")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private_class_method :taken_options
|
|
25
|
+
|
|
26
|
+
# A copy of a DSPyRb::Predict, its kinds or a Module, compiled on the trainset; each compile option by its name.
|
|
27
|
+
def compile(student, trainset:, **compiling)
|
|
28
|
+
place = "#{self.class.name}#compile"
|
|
29
|
+
parts = Parts.new(program(student, place))
|
|
30
|
+
taken = Given.taken(self.class::COMPILING, compiling, place)
|
|
31
|
+
examples = Values::Types::Examples[trainset]
|
|
32
|
+
|
|
33
|
+
refuse_reserved(student, examples, compiling, place)
|
|
34
|
+
called = { **called_back(compiling[:teacher], place), source: source(student, taken) }
|
|
35
|
+
|
|
36
|
+
Execution::Optimization.new(options, taken, **called).compile(parts, examples)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
# The student's source, sent only to an optimizer that reads a program's.
|
|
42
|
+
def source(_student, _compiling); end
|
|
43
|
+
|
|
44
|
+
# What DSPy calls back: the metric given, the teacher, and the DummyLMs the options name.
|
|
45
|
+
def called_back(teacher, place)
|
|
46
|
+
{ metric: given[:metric], teacher:, models: scripts(place) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The DummyLMs the options name; two lists of answers, the one in force's included, can't be told apart.
|
|
50
|
+
def scripts(place)
|
|
51
|
+
models = Given.to_enum(:crossing, given).to_a
|
|
52
|
+
listed = [Execution::Step.new.own_lm, *models].select { Execution::Step.scripted?(_1) }
|
|
53
|
+
|
|
54
|
+
return models.grep(DummyLM) if listed.uniq.size < 2
|
|
55
|
+
|
|
56
|
+
raise Error,
|
|
57
|
+
"#{place} can't tell two DummyLMs' lists of answers apart, a copy and its original included: use the " \
|
|
58
|
+
"same one in both places, or give the other its answers by text"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Refused by its class alone.
|
|
62
|
+
def program(student, place)
|
|
63
|
+
return student if student.is_a?(Predict) || student.is_a?(Module)
|
|
64
|
+
|
|
65
|
+
raise Error, "#{place} compiles a DSPyRb::Predict, its kinds or a Module, not #{Error.class_of(student)}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# DSPy calls a predictor on each row's inputs (dspy/teleprompt/bootstrap.py:202), so a name it reserves is refused.
|
|
69
|
+
def refuse_reserved(student, examples, compiling, place)
|
|
70
|
+
predictors = [student, compiling[:teacher]].grep(Predict)
|
|
71
|
+
rows = { place => examples, "#{place}'s valset" => compiling[:valset] || [] }
|
|
72
|
+
|
|
73
|
+
rows.each do |at, set|
|
|
74
|
+
predictors.product(set.each_with_index.to_a).each do |predictor, (example, index)|
|
|
75
|
+
inputs = example.input_keys ? example.inputs.to_h : {}
|
|
76
|
+
|
|
77
|
+
Values::Types.refuse_reserved(inputs, "#{at}'s example #{index}", predictor.predictor.kind)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Options as a struct takes them, each by its kind: nil leaves DSPy's default, or is None for a name `none` takes.
|
|
83
|
+
module Given
|
|
84
|
+
def self.taken(struct, given, place)
|
|
85
|
+
none = given.filter_map { |name, value| name.to_s if value.nil? } & nones(struct)
|
|
86
|
+
wired = given.compact.to_h { |name, value| [name, wired(name, value)] }
|
|
87
|
+
|
|
88
|
+
Wire::Departure.taken(struct, none.empty? ? wired : { **wired, none: }, place)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def self.nones(struct)
|
|
92
|
+
struct.attribute_names.include?(:none) ? struct.schema.key(:none).type.member.values : []
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Each value as it crosses, at any depth: a model as the block makes it, an Example as the wire's, else as given.
|
|
96
|
+
def self.crossing(value, &)
|
|
97
|
+
case value
|
|
98
|
+
when LM, DummyLM then yield value
|
|
99
|
+
when Example then value.wire
|
|
100
|
+
when ::Hash then value.transform_values { crossing(_1, &) }
|
|
101
|
+
when ::Array then value.map { crossing(_1, &) }
|
|
102
|
+
else value
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The metric and the teacher by their names, since Ruby serves their callbacks; a model by its description.
|
|
107
|
+
def self.wired(name, value)
|
|
108
|
+
case name
|
|
109
|
+
when :metric then Execution::Scorer.new(value.instance_of?(Metric) ? value.description : value).description
|
|
110
|
+
when :teacher then taught(value)
|
|
111
|
+
else crossing(value, &:description)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# A program's description saying it is the teacher, whose program and tool callbacks then call the teacher.
|
|
116
|
+
def self.taught(teacher)
|
|
117
|
+
return teacher unless teacher.is_a?(Predict) || teacher.is_a?(Module)
|
|
118
|
+
|
|
119
|
+
Execution::Program.new(Parts.new(teacher)).description.new(teacher: true)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
private_class_method :wired, :taught
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
private_constant :Given
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
# DSPy's SIMBA, which adds demos and its prompt model's rules from runs whose scores differ: DSPyRb::SIMBA[metric:].
|
|
5
|
+
class SIMBA < Optimizer
|
|
6
|
+
OPTIONS = Wire::SIMBAOptions
|
|
7
|
+
COMPILING = Wire::SIMBACompile
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# A Module's source, as its rule strategy reads a Python program's class (dspy/teleprompt/simba_utils.py:144).
|
|
12
|
+
def source(student, _compiling)
|
|
13
|
+
Source.text(student) if student.is_a?(Module)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/dspy_rb/tool.rb
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
# A Ruby tool DSPy's ReAct calls: its name, description and arguments declared, its work done in `call`.
|
|
5
|
+
class Tool
|
|
6
|
+
# What an argument declared with no default holds: the model must give it.
|
|
7
|
+
REQUIRED = Object.new.freeze
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
def inherited(subclass)
|
|
11
|
+
super
|
|
12
|
+
subclass.instance_variable_set(:@arguments, arguments.dup)
|
|
13
|
+
subclass.instance_variable_set(:@desc, desc)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The name the model calls it by: the one given, else its class's own in snake case, as DSPy takes a function's.
|
|
17
|
+
def tool_name(name = nil)
|
|
18
|
+
return @tool_name || snake_case if name.nil?
|
|
19
|
+
|
|
20
|
+
@tool_name = Wire::Departure.taken(Wire::Types::Name, name.to_s, "#{self}.tool_name")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Its description, given to DSPy exactly as written.
|
|
24
|
+
def desc(text = nil)
|
|
25
|
+
return @desc if text.nil?
|
|
26
|
+
raise Error, "#{self}.desc takes text, not #{Error.class_of(text)}" unless text.is_a?(::String)
|
|
27
|
+
|
|
28
|
+
@desc = text
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# An argument, typed as a signature's field is; a default, declared here and nowhere else, makes it optional.
|
|
32
|
+
def arg(name, type = nil, default: REQUIRED, desc: nil)
|
|
33
|
+
name = name.to_s
|
|
34
|
+
place = "#{self}.arg"
|
|
35
|
+
|
|
36
|
+
Signature.refuse_name(name, place, "argument")
|
|
37
|
+
raise Error, "#{place} takes no second argument named #{name}" if arguments.any? { _1.name == name }
|
|
38
|
+
|
|
39
|
+
declared = { name:, type: type && Types.annotation(type, place), desc:, **given(default, place) }
|
|
40
|
+
|
|
41
|
+
(@arguments ||= []) << Wire::Departure.taken(Wire::ToolArg, declared.compact, place)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def arguments
|
|
45
|
+
(@arguments || []).dup.freeze
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Python's description of it, once its `call` is checked to take what DSPy will give it.
|
|
49
|
+
def description
|
|
50
|
+
callable
|
|
51
|
+
|
|
52
|
+
Wire::Tool.new({ name: tool_name, desc:, args: arguments }.compact)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The declared default of each argument that is not required, which DSPy leaves out when the model does.
|
|
56
|
+
def defaults
|
|
57
|
+
arguments.reject(&:required).to_h { [_1.name.to_sym, Values::Types.frozen(_1.default)] }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def callable
|
|
63
|
+
return if declared_only?
|
|
64
|
+
|
|
65
|
+
keywords = arguments.map { "#{_1.name}:" }.join(", ")
|
|
66
|
+
reason = "#{tool_name}#call takes each declared argument, and only those, as a required keyword " \
|
|
67
|
+
"(#{keywords}): a default is declared in arg alone"
|
|
68
|
+
|
|
69
|
+
raise Error, reason
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Each declared argument, and only those, a required keyword of `call`: the gem fills declared defaults.
|
|
73
|
+
def declared_only?
|
|
74
|
+
parameters = instance_method(:call).parameters
|
|
75
|
+
taken = parameters.filter_map { |kind, name| name.to_s if kind == :keyreq }
|
|
76
|
+
|
|
77
|
+
taken.sort == arguments.map(&:name).sort && parameters.none? { |kind, _| %i[req opt].include?(kind) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# A default refused naming the declaration, as the wire's own check cannot.
|
|
81
|
+
def given(default, place)
|
|
82
|
+
return { required: true } if default.equal?(REQUIRED)
|
|
83
|
+
|
|
84
|
+
{ required: false, default: Wire::Json.value(default, "#{place}'s default") }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def snake_case
|
|
88
|
+
raise Error, "a tool of a class with no name needs one: give it with tool_name" unless name
|
|
89
|
+
|
|
90
|
+
name.split("::").last.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def call(**)
|
|
95
|
+
raise Error, "#{self.class} defines no call"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# DSPy's own JSON schema of its arguments, Tool.args, read back from Python.
|
|
99
|
+
def args
|
|
100
|
+
Execution::Step.new.tool(self.class.description).args
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# DSPy's own text for it, str(tool), as ReAct shows it to a model that calls tools by text.
|
|
104
|
+
def text
|
|
105
|
+
Execution::Step.new.tool(self.class.description).text
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# One of doctor's checks.
|
|
6
|
+
class Check < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute :name, Wire::Types::Name
|
|
10
|
+
attribute :passed, Wire::Types::Strict::Bool
|
|
11
|
+
attribute :detail, Wire::Types::Text
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# What the requests made in one place go by: LM, adapter, hooks, collecting program and callback served.
|
|
6
|
+
class Context < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
# A DSPyRb::LM or DummyLM, which the requests go by its description.
|
|
10
|
+
attribute :lm, Wire::Types.Interface(:description).optional
|
|
11
|
+
attribute :adapter, Wire::Adapter.optional
|
|
12
|
+
attribute :on_call, Wire::Types::Strict::Array.of(Wire::Types.Interface(:call))
|
|
13
|
+
attribute :before_call, Wire::Types::Strict::Array.of(Wire::Types.Interface(:call))
|
|
14
|
+
|
|
15
|
+
# Each takes a block, and runs it around the code the gem runs for the caller on a thread of its own.
|
|
16
|
+
attribute :around, Wire::Types::Strict::Array.of(Wire::Types.Interface(:call))
|
|
17
|
+
attribute :collector, Wire::Types.Interface(:add, :name).optional
|
|
18
|
+
attribute :callback, Wire::Types.Interface(:ask).optional
|
|
19
|
+
|
|
20
|
+
# Set while gates run, so a model call made in one is refused.
|
|
21
|
+
attribute :gating, Wire::Types::Strict::Bool
|
|
22
|
+
|
|
23
|
+
# In a callback, the LM DSPy has in force there, and the models the askers hold, which DummyLM's resolves to.
|
|
24
|
+
attribute :in_force, Wire::InForce.optional
|
|
25
|
+
attribute :held, Wire::Types::Strict::Array.of(Wire::Types.Interface(:description))
|
|
26
|
+
|
|
27
|
+
NONE = new(
|
|
28
|
+
lm: nil,
|
|
29
|
+
adapter: nil,
|
|
30
|
+
on_call: [],
|
|
31
|
+
before_call: [],
|
|
32
|
+
around: [],
|
|
33
|
+
collector: nil,
|
|
34
|
+
callback: nil,
|
|
35
|
+
gating: false,
|
|
36
|
+
in_force: nil,
|
|
37
|
+
held: []
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# Code a callback runs under: outside any program, DSPy's LM and adapter in force, as for a Python program's.
|
|
41
|
+
def serving(callback, gating, in_force)
|
|
42
|
+
new(callback:, gating:, in_force:, collector: nil, lm: nil, adapter: nil, held: [lm, *held].compact)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# DSPy's Dataset as loaded: its train, dev and test splits, each its Examples or the error DSPy raised reading it.
|
|
6
|
+
class Dataset < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
FAILURE = Wire::Types.Instance(Wire::Failure)
|
|
10
|
+
|
|
11
|
+
attribute(:splits, Wire::Types::Strict::Hash.constructor { |splits| named(splits, Types::Examples) })
|
|
12
|
+
attribute(:failures, Wire::Types::Strict::Hash.constructor { |failures| named(failures, FAILURE) })
|
|
13
|
+
|
|
14
|
+
# Each split by its name, refused by kind alone, its value checked by `type`, frozen through.
|
|
15
|
+
def self.named(splits, type)
|
|
16
|
+
raise Error, "splits come as a Hash, not #{Error.class_of(splits)}" unless splits.is_a?(::Hash)
|
|
17
|
+
|
|
18
|
+
Types.frozen(splits.to_h { |name, value| [Wire::Types::Strict::Symbol[name], type[value]] })
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private_class_method :named
|
|
22
|
+
|
|
23
|
+
# The splits a load gave, each read or failed, by its name.
|
|
24
|
+
def self.of(splits)
|
|
25
|
+
read, failed = splits.partition(&:examples)
|
|
26
|
+
|
|
27
|
+
new(
|
|
28
|
+
splits: read.to_h { [_1.name.to_sym, _1.examples.map { |each| Example.of(each) }] },
|
|
29
|
+
failures: failed.to_h { [_1.name.to_sym, _1.error] }
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def train
|
|
34
|
+
split(:train)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def dev
|
|
38
|
+
split(:dev)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test
|
|
42
|
+
split(:test)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# A split DSPy could not read raises its error as it is read, as DSPy raises it reading the attribute.
|
|
48
|
+
def split(name)
|
|
49
|
+
raise DSPyError.for(failures.fetch(name), Spend.new) if failures.key?(name)
|
|
50
|
+
|
|
51
|
+
splits.fetch(name)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# A field type: a map from one type to another.
|
|
6
|
+
class Dict < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute :key, Wire::Types::Any
|
|
10
|
+
attribute :value, Wire::Types::Any
|
|
11
|
+
|
|
12
|
+
def self.[](key, value)
|
|
13
|
+
new(key:, value:)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# The variables Python starts with, keys among them, which print as their names only.
|
|
6
|
+
class Env < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
VARIABLES = Wire::Types::Strict::Hash.map(Wire::Types::Strict::String, Wire::Types::Strict::String)
|
|
10
|
+
|
|
11
|
+
attribute :variables, VARIABLES
|
|
12
|
+
|
|
13
|
+
# As DSPyRb.config.env takes them; variables it refuses are named by the setting alone, never quoting a key.
|
|
14
|
+
def self.of(env)
|
|
15
|
+
env.is_a?(Env) ? env : new(variables: Wire::Departure.taken(VARIABLES, env, "DSPyRb.config.env"))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def inspect
|
|
19
|
+
"#<#{self.class.name} #{variables.keys.join(" ")}>"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# DSPy's EvaluationResult: its percentage, each example's prediction and score in order, and what it spent.
|
|
6
|
+
class EvaluationResult < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute :score, Wire::Types::Number
|
|
10
|
+
attribute(:results, Wire::Types::Strict::Array.constructor { |given| results(given) })
|
|
11
|
+
attribute :spend, Spend
|
|
12
|
+
|
|
13
|
+
# Each [example, prediction, score], refused by place and kind alone, then copied and frozen through.
|
|
14
|
+
def self.results(given)
|
|
15
|
+
raise Error, "results come as an Array, not #{Error.class_of(given)}" unless given.is_a?(::Array)
|
|
16
|
+
|
|
17
|
+
stray = given.index { !result?(_1) }
|
|
18
|
+
|
|
19
|
+
raise Error, "results come as [DSPyRb::Example, DSPyRb::Prediction, score]: item #{stray} is not one" if stray
|
|
20
|
+
|
|
21
|
+
Types.frozen(given)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.result?(result)
|
|
25
|
+
result.is_a?(::Array) && result.size == 3 && result[0].is_a?(Example) && result[1].is_a?(Prediction)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private_class_method :results, :result?
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# DSPy's Example: its fields in order, and which of them are inputs once with_inputs sets them.
|
|
6
|
+
class Example < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute(:record, Wire::Types::Strict::Hash.constructor { |fields| recorded(fields) })
|
|
10
|
+
attribute?(:input_keys, Wire::Types::Strict::Array.constructor { |names| named(names) })
|
|
11
|
+
|
|
12
|
+
# DSPy's Example(**fields), as Example[question: "What is 2+2?", answer: "4"].
|
|
13
|
+
def self.[](**fields)
|
|
14
|
+
new(record: fields)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.of(wire)
|
|
18
|
+
new({ record: Types.retyped(wire.record, wire.typed), input_keys: wire.input_keys&.map(&:to_sym) }.compact)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Its fields as JSON carries them, DSPy's own types kept, none hidden by its readers, copied and frozen through.
|
|
22
|
+
def self.recorded(fields)
|
|
23
|
+
values = Types.refuse_marked(Wire::Json.values(fields), "an example")
|
|
24
|
+
|
|
25
|
+
Types.frozen(Types.shown(Types.retyped(values, Types.typed(fields)), Example, "a field"))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.named(names)
|
|
29
|
+
names.map { Wire::Types::Strict::Symbol[_1] }.freeze
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private_class_method :recorded, :named
|
|
33
|
+
|
|
34
|
+
def wire
|
|
35
|
+
Wire::Example.new({ record:, input_keys: input_keys&.map(&:to_s), typed: Types.typed(record) }.compact)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# DSPy holds its input keys as a set; each is refused, by its class alone, as a field's name is.
|
|
39
|
+
def with_inputs(*names)
|
|
40
|
+
new(input_keys: Wire::Json.keyed(names.to_h { [_1, true] }).keys)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# DSPy's get: nil when absent.
|
|
44
|
+
def [](name)
|
|
45
|
+
record[name.to_sym]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# DSPy's example["k"]: raises when absent, unless given a default as Hash#fetch is.
|
|
49
|
+
def fetch(name, *default, &)
|
|
50
|
+
record.fetch(name.to_sym, *default, &)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def key?(name)
|
|
54
|
+
record.key?(name.to_sym)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def keys(include_dspy: false)
|
|
58
|
+
shown(include_dspy).keys
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def values(include_dspy: false)
|
|
62
|
+
shown(include_dspy).values
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def items(include_dspy: false)
|
|
66
|
+
shown(include_dspy).to_a
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def size
|
|
70
|
+
shown(false).size
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Its input fields, in its own order, keeping its input keys.
|
|
74
|
+
def inputs
|
|
75
|
+
raise Error, "this example's inputs are not set: set them with with_inputs" unless input_keys
|
|
76
|
+
|
|
77
|
+
new(record: record.slice(*(record.keys & input_keys)))
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Every field its inputs do not show, with no input keys.
|
|
81
|
+
def labels
|
|
82
|
+
Example.new(record: record.except(*inputs.keys))
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def copy(**fields)
|
|
86
|
+
new(record: record.merge(fields))
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def without(*names)
|
|
90
|
+
names = names.map(&:to_sym)
|
|
91
|
+
|
|
92
|
+
# DSPy's del raises for an absent field.
|
|
93
|
+
record.fetch_values(*names)
|
|
94
|
+
new(record: record.except(*names))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# DSPy's toDict.
|
|
98
|
+
def to_h
|
|
99
|
+
record
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Dry::Struct's own to_hash reads the struct's attributes through [].
|
|
103
|
+
alias to_hash to_h
|
|
104
|
+
|
|
105
|
+
# Its fields alone, never its input keys (dspy/primitives/example.py:164-168).
|
|
106
|
+
def ==(other)
|
|
107
|
+
other.is_a?(Example) && record == other.record
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
alias eql? ==
|
|
111
|
+
|
|
112
|
+
def hash
|
|
113
|
+
record.hash
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def respond_to_missing?(name, include_private = false)
|
|
117
|
+
record.key?(name) || super
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def method_missing(name, *arguments)
|
|
121
|
+
return record[name] if arguments.empty? && record.key?(name)
|
|
122
|
+
|
|
123
|
+
super
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
private
|
|
127
|
+
|
|
128
|
+
# DSPy hides its own dspy_ fields from its readers (dspy/primitives/example.py:153-154, 170-210).
|
|
129
|
+
def shown(include_dspy)
|
|
130
|
+
include_dspy ? record : record.reject { |name, _| name.start_with?("dspy_") }
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# A line of Python's that breaks the wire: why, by where and never by a value, and what it claims to answer.
|
|
6
|
+
class Fault < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute :reason, Wire::Types::Text
|
|
10
|
+
attribute? :shape, Wire::Types::Strict::Class
|
|
11
|
+
attribute? :request, Wire::Types::Id
|
|
12
|
+
attribute? :callback, Wire::Types::Id
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# A ReAct's history as a Hash, which goes back to DSPy as its History (predict/react.py:176).
|
|
6
|
+
class History < ::Hash
|
|
7
|
+
# YAML writes it plain, as JSON does, so YAML.safe_load reads it back.
|
|
8
|
+
def encode_with(coder)
|
|
9
|
+
coder.represent_object(nil, to_h)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# A field type: a list of one type.
|
|
6
|
+
class List < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute :of, Wire::Types::Any
|
|
10
|
+
|
|
11
|
+
def self.[](of)
|
|
12
|
+
new(of:)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# A field type: one of the given Strings or Integers.
|
|
6
|
+
class Literal < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
NONE = "DSPyRb::Types::Literal needs at least one value"
|
|
10
|
+
|
|
11
|
+
# Refused with none, saying so, however it is built.
|
|
12
|
+
VALUES = Wire::Types::Strict::Array.of(Wire::Types::Strict::String | Wire::Types::Strict::Integer)
|
|
13
|
+
.constructor { |values| values == [] ? raise(Error, NONE) : values }
|
|
14
|
+
|
|
15
|
+
attribute :values, VALUES
|
|
16
|
+
|
|
17
|
+
# The values as given, so a refusal names one by its index alone.
|
|
18
|
+
def self.[](*values)
|
|
19
|
+
stray = values.index { !VALUES.member.valid?(_1) }
|
|
20
|
+
|
|
21
|
+
raise Error, "DSPyRb::Types::Literal cannot take values[#{stray}] as given" if stray
|
|
22
|
+
|
|
23
|
+
new(values:)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Values
|
|
5
|
+
# A field type: a type or nothing.
|
|
6
|
+
class Optional < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
attribute :of, Wire::Types::Any
|
|
10
|
+
|
|
11
|
+
def self.[](of)
|
|
12
|
+
new(of:)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|