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,214 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Wire
|
|
5
|
+
# Ruby's first request.
|
|
6
|
+
class Hello < Shape
|
|
7
|
+
attribute :id, Types::Id
|
|
8
|
+
attribute :kind, kind_type("hello")
|
|
9
|
+
attribute :protocol, Types::Strict::Integer
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# A predictor to read back the raw instructions of.
|
|
13
|
+
class Describe < Shape
|
|
14
|
+
attribute :id, Types::Id
|
|
15
|
+
attribute :kind, kind_type("describe")
|
|
16
|
+
attribute :predictor, Predictor
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# A Ruby tool to read back DSPy's own schema of its arguments and its text for.
|
|
20
|
+
class DescribeTool < Shape
|
|
21
|
+
attribute :id, Types::Id
|
|
22
|
+
attribute :kind, kind_type("describe_tool")
|
|
23
|
+
attribute :tool, Tool
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# An LM or a DummyLM to read back DSPy's own lm.kwargs of.
|
|
27
|
+
class DescribeLM < Shape
|
|
28
|
+
attribute :id, Types::Id
|
|
29
|
+
attribute :kind, kind_type("describe_lm")
|
|
30
|
+
attribute :lm, LM | DummyLM
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# One predictor call.
|
|
34
|
+
class CallRequest < Shape
|
|
35
|
+
attribute :id, Types::Id
|
|
36
|
+
attribute :kind, kind_type("call")
|
|
37
|
+
attribute :predictor, Predictor
|
|
38
|
+
attribute? :component, Types::Name
|
|
39
|
+
|
|
40
|
+
# Left out in a callback with none set in it: the call runs under DSPy's settings there, as a Python program's.
|
|
41
|
+
attribute? :lm, LM | DummyLM
|
|
42
|
+
attribute? :adapter, Adapter
|
|
43
|
+
attribute :inputs, Types::Values
|
|
44
|
+
attribute? :typed, Types::Strict::Array.of(Typed)
|
|
45
|
+
attribute? :within, Types::Id
|
|
46
|
+
attribute? :gated, Types::Strict::Bool
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# DSPy's Evaluate options beside devset and metric (dspy/evaluate/evaluate.py:71-85); left out, DSPy's defaults.
|
|
50
|
+
class EvaluateOptions < Shape
|
|
51
|
+
attribute? :num_threads, Types::Strict::Integer
|
|
52
|
+
attribute? :display_progress, Types::Strict::Bool
|
|
53
|
+
attribute? :display_table, Types::Strict::Bool | Types::Strict::Integer
|
|
54
|
+
attribute? :max_errors, Types::Strict::Integer
|
|
55
|
+
attribute? :provide_traceback, Types::Strict::Bool
|
|
56
|
+
attribute? :failure_score, Types::Number
|
|
57
|
+
attribute? :save_as_csv, Types::Path
|
|
58
|
+
attribute? :save_as_json, Types::Path
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# What one call of DSPy's Evaluate overrides beside program, metric and devset (dspy/evaluate/evaluate.py:117-128).
|
|
62
|
+
class EvaluateOverrides < Shape
|
|
63
|
+
attribute? :num_threads, Types::Strict::Integer
|
|
64
|
+
attribute? :display_progress, Types::Strict::Bool
|
|
65
|
+
attribute? :display_table, Types::Strict::Bool | Types::Strict::Integer
|
|
66
|
+
attribute? :callback_metadata, Types::Values
|
|
67
|
+
attribute? :save_as_csv, Types::Path
|
|
68
|
+
attribute? :save_as_json, Types::Path
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# DSPy's Evaluate of a program over a devset, scored by a metric; DSPy applies its call's overrides.
|
|
72
|
+
class Evaluate < Shape
|
|
73
|
+
attribute :id, Types::Id
|
|
74
|
+
attribute :kind, kind_type("evaluate")
|
|
75
|
+
attribute :program, Predictor | RubyProgram
|
|
76
|
+
attribute :devset, Types::Strict::Array.of(Example)
|
|
77
|
+
attribute :metric, Metric | RubyMetric
|
|
78
|
+
attribute? :component, Types::Name
|
|
79
|
+
|
|
80
|
+
# Left out when none is configured: DSPy then runs on its own LM, if any, else each row calling a model fails.
|
|
81
|
+
attribute? :lm, LM | DummyLM
|
|
82
|
+
attribute? :adapter, Adapter
|
|
83
|
+
attribute :options, EvaluateOptions
|
|
84
|
+
attribute :overrides, EvaluateOverrides
|
|
85
|
+
attribute? :within, Types::Id
|
|
86
|
+
attribute? :gated, Types::Strict::Bool
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# One call of one of DSPy's metrics, on an example and a prediction's outputs.
|
|
90
|
+
class Measure < Shape
|
|
91
|
+
attribute :id, Types::Id
|
|
92
|
+
attribute :kind, kind_type("measure")
|
|
93
|
+
attribute :metric, Metric
|
|
94
|
+
attribute :example, Example
|
|
95
|
+
attribute :prediction, Types::Values
|
|
96
|
+
|
|
97
|
+
# Left out when none is configured: DSPy then runs on its own LM, if any, as for a metric that calls no model.
|
|
98
|
+
attribute? :lm, LM | DummyLM
|
|
99
|
+
attribute? :adapter, Adapter
|
|
100
|
+
attribute? :within, Types::Id
|
|
101
|
+
attribute? :gated, Types::Strict::Bool
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# A direct call of an LM, as DSPy's lm(prompt, **options): no predictor, its options passed whole.
|
|
105
|
+
class Generate < Shape
|
|
106
|
+
attribute :id, Types::Id
|
|
107
|
+
attribute :kind, kind_type("generate")
|
|
108
|
+
attribute :lm, LM | DummyLM
|
|
109
|
+
|
|
110
|
+
# The adapter in force, as DSPy's call runs under it: only a DummyLM reads it, to format its answer.
|
|
111
|
+
attribute? :adapter, Adapter
|
|
112
|
+
attribute :prompt, Types::Text
|
|
113
|
+
|
|
114
|
+
# As an LM's own, cache and rollout_id among them, no key ever.
|
|
115
|
+
attribute :options, LM::Options
|
|
116
|
+
attribute? :within, Types::Id
|
|
117
|
+
attribute? :gated, Types::Strict::Bool
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# One of DSPy's optimizers compiling a student, a predictor or a Ruby program, on a trainset.
|
|
121
|
+
class Compile < Shape
|
|
122
|
+
attribute :id, Types::Id
|
|
123
|
+
attribute :kind, kind_type("compile")
|
|
124
|
+
attribute :optimizer, OPTIMIZER_OPTIONS.reduce(:|)
|
|
125
|
+
attribute? :compiling, COMPILE_OPTIONS.reduce(:|)
|
|
126
|
+
attribute :student, Predictor | RubyProgram
|
|
127
|
+
attribute :trainset, Types::Strict::Array.of(Example)
|
|
128
|
+
|
|
129
|
+
# Left out when none is configured: an optimizer that calls no model needs none.
|
|
130
|
+
attribute? :lm, LM | DummyLM
|
|
131
|
+
attribute? :adapter, Adapter
|
|
132
|
+
attribute? :within, Types::Id
|
|
133
|
+
attribute? :gated, Types::Strict::Bool
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# DSPy's own save of a program's state to a JSON file, which Python writes.
|
|
137
|
+
class SaveState < Shape
|
|
138
|
+
attribute :id, Types::Id
|
|
139
|
+
attribute :kind, kind_type("save_state")
|
|
140
|
+
attribute :program, Predictor | RubyProgram
|
|
141
|
+
attribute :path, Types::Path
|
|
142
|
+
attribute? :within, Types::Id
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# DSPy's own load of a program's state from a JSON file, read back as a compile's is.
|
|
146
|
+
class LoadState < Shape
|
|
147
|
+
attribute :id, Types::Id
|
|
148
|
+
attribute :kind, kind_type("load_state")
|
|
149
|
+
attribute :program, Predictor | RubyProgram
|
|
150
|
+
attribute :path, Types::Path
|
|
151
|
+
attribute? :within, Types::Id
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Rows DSPy's own code loads: one of its datasets, a Hugging Face dataset, or a file.
|
|
155
|
+
class Load < Shape
|
|
156
|
+
attribute :id, Types::Id
|
|
157
|
+
attribute :kind, kind_type("load")
|
|
158
|
+
attribute :source, DSPyDataset | HubDataset | DataFile
|
|
159
|
+
attribute? :within, Types::Id
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Python's order of a count of rows' indices, by random.Random(seed) or DSPy's DataLoader (dataloader.py:123-200).
|
|
163
|
+
class Permute < Shape
|
|
164
|
+
attribute :id, Types::Id
|
|
165
|
+
attribute :kind, kind_type("permute")
|
|
166
|
+
attribute :draw, Types::Strict::String.enum("shuffle", "sample", "train_test_split")
|
|
167
|
+
attribute :count, Types::Count
|
|
168
|
+
attribute? :seed, Types::Strict::Integer
|
|
169
|
+
|
|
170
|
+
# Any Integer, since Python refuses a count the rows cannot give, a negative one too, with its own error.
|
|
171
|
+
attribute? :k, Types::Strict::Integer
|
|
172
|
+
attribute? :train_size, Types::Number
|
|
173
|
+
attribute? :test_size, Types::Number
|
|
174
|
+
attribute? :random_state, Types::Strict::Integer
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
REQUESTS = [
|
|
178
|
+
Hello,
|
|
179
|
+
Describe,
|
|
180
|
+
DescribeTool,
|
|
181
|
+
DescribeLM,
|
|
182
|
+
CallRequest,
|
|
183
|
+
Evaluate,
|
|
184
|
+
Measure,
|
|
185
|
+
Generate,
|
|
186
|
+
Compile,
|
|
187
|
+
SaveState,
|
|
188
|
+
LoadState,
|
|
189
|
+
Load,
|
|
190
|
+
Permute
|
|
191
|
+
].freeze
|
|
192
|
+
|
|
193
|
+
# The requests whose DSPy code may call a model; a gated one asks Ruby's gates before each call it makes.
|
|
194
|
+
CALLED = [
|
|
195
|
+
CallRequest,
|
|
196
|
+
Evaluate,
|
|
197
|
+
Measure,
|
|
198
|
+
Generate,
|
|
199
|
+
Compile
|
|
200
|
+
].freeze
|
|
201
|
+
|
|
202
|
+
# Served on Python's pool, so Ruby admits at most `threads` of them that serve no callback.
|
|
203
|
+
POOLED = [
|
|
204
|
+
CallRequest,
|
|
205
|
+
Evaluate,
|
|
206
|
+
Measure,
|
|
207
|
+
Generate,
|
|
208
|
+
Compile,
|
|
209
|
+
SaveState,
|
|
210
|
+
LoadState,
|
|
211
|
+
Load
|
|
212
|
+
].freeze
|
|
213
|
+
end
|
|
214
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Wire
|
|
5
|
+
# Every message's base: keys strict, and an omittable key left out, never null.
|
|
6
|
+
class Shape < Dry::Struct
|
|
7
|
+
schema schema.strict
|
|
8
|
+
|
|
9
|
+
def self.kind_type(name)
|
|
10
|
+
Types::Strict::String.default(name).enum(name)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.kind
|
|
14
|
+
schema.key(:kind).type.values.first
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Key-like values hidden.
|
|
18
|
+
def inspect
|
|
19
|
+
shown = self.class.attribute_names.map { " #{_1}=#{Secrets.hidden(self[_1]).inspect}" }
|
|
20
|
+
|
|
21
|
+
"#<#{self.class.name}#{shown.join}>"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DSPyRb
|
|
4
|
+
module Wire
|
|
5
|
+
# The wire's dry-types.
|
|
6
|
+
module Types
|
|
7
|
+
include Dry.Types(default: :strict)
|
|
8
|
+
|
|
9
|
+
Text = Strict::String
|
|
10
|
+
Name = Strict::String.constrained(min_size: 1)
|
|
11
|
+
Id = Strict::Integer.constrained(gteq: 1)
|
|
12
|
+
Count = Strict::Integer.constrained(gteq: 0)
|
|
13
|
+
Number = Strict::Integer | Strict::Float
|
|
14
|
+
Values = Strict::Hash.constructor { |hash| Json.values(hash) }
|
|
15
|
+
|
|
16
|
+
# Any one value JSON carries, as a Ruby callback returns it.
|
|
17
|
+
JsonValue = Any.constructor { |value| Json.value(value) }
|
|
18
|
+
|
|
19
|
+
# A file Python writes or reads, named in full, since Python's working folder may not be Ruby's.
|
|
20
|
+
Path = Strict::String.constructor { |path| path.is_a?(::String) ? File.expand_path(path) : path }
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/dspy_rb/wire.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "wire/types"
|
|
4
|
+
require_relative "wire/json"
|
|
5
|
+
require_relative "wire/shape"
|
|
6
|
+
require_relative "wire/records"
|
|
7
|
+
require_relative "wire/descriptions"
|
|
8
|
+
require_relative "wire/optimizers"
|
|
9
|
+
require_relative "wire/requests"
|
|
10
|
+
require_relative "wire/facts"
|
|
11
|
+
require_relative "wire/replies"
|
|
12
|
+
require_relative "wire/in_flight"
|
|
13
|
+
require_relative "wire/departure"
|
|
14
|
+
|
|
15
|
+
module DSPyRb
|
|
16
|
+
# The wire between Ruby and Python, defined once; `rake wire` writes Python's copy.
|
|
17
|
+
module Wire
|
|
18
|
+
PROTOCOL = 4
|
|
19
|
+
REQUESTS_FD = 3
|
|
20
|
+
REPLIES_FD = 4
|
|
21
|
+
|
|
22
|
+
# Seconds between Python's alive lines while anything of Ruby's is open in it.
|
|
23
|
+
ALIVE_INTERVAL = 1
|
|
24
|
+
|
|
25
|
+
# Each side writes only its own lines, and reads only the other's.
|
|
26
|
+
FROM_RUBY = [*REQUESTS, Returned, Raised, Cancel].freeze
|
|
27
|
+
FROM_PYTHON = [*REPLIES, Progress, Alive, *CALLBACKS].freeze
|
|
28
|
+
KINDS = FROM_PYTHON.to_h { [_1.kind, _1] }.freeze
|
|
29
|
+
|
|
30
|
+
def self.write(message)
|
|
31
|
+
JSON.generate(message.to_h)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/dspy_rb.rb
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry-configurable"
|
|
4
|
+
require "dry-initializer"
|
|
5
|
+
require "dry-struct"
|
|
6
|
+
require "dry-types"
|
|
7
|
+
require "json"
|
|
8
|
+
require "prism"
|
|
9
|
+
|
|
10
|
+
require_relative "dspy_rb/version"
|
|
11
|
+
require_relative "dspy_rb/wire"
|
|
12
|
+
require_relative "dspy_rb/errors"
|
|
13
|
+
require_relative "dspy_rb/values/reasoning"
|
|
14
|
+
require_relative "dspy_rb/values/history"
|
|
15
|
+
require_relative "dspy_rb/values/types"
|
|
16
|
+
require_relative "dspy_rb/values/spend"
|
|
17
|
+
require_relative "dspy_rb/values/prediction"
|
|
18
|
+
require_relative "dspy_rb/values/example"
|
|
19
|
+
require_relative "dspy_rb/values/evaluation_result"
|
|
20
|
+
require_relative "dspy_rb/values/dataset"
|
|
21
|
+
require_relative "dspy_rb/values/context"
|
|
22
|
+
require_relative "dspy_rb/values/fault"
|
|
23
|
+
require_relative "dspy_rb/values/loss"
|
|
24
|
+
require_relative "dspy_rb/values/env"
|
|
25
|
+
require_relative "dspy_rb/values/check"
|
|
26
|
+
require_relative "dspy_rb/values/list"
|
|
27
|
+
require_relative "dspy_rb/values/dict"
|
|
28
|
+
require_relative "dspy_rb/values/optional"
|
|
29
|
+
require_relative "dspy_rb/values/literal"
|
|
30
|
+
require_relative "dspy_rb/secrets"
|
|
31
|
+
require_relative "dspy_rb/setup/uv"
|
|
32
|
+
require_relative "dspy_rb/setup/stale"
|
|
33
|
+
require_relative "dspy_rb/setup/environment"
|
|
34
|
+
require_relative "dspy_rb/runtime/channel/meter"
|
|
35
|
+
require_relative "dspy_rb/runtime/channel/watchdog"
|
|
36
|
+
require_relative "dspy_rb/runtime/channel/ending"
|
|
37
|
+
require_relative "dspy_rb/runtime/channel/reporter"
|
|
38
|
+
require_relative "dspy_rb/runtime/channel/exchange"
|
|
39
|
+
require_relative "dspy_rb/runtime/channel/traffic"
|
|
40
|
+
require_relative "dspy_rb/runtime/channel/settler"
|
|
41
|
+
require_relative "dspy_rb/runtime/channel/writer"
|
|
42
|
+
require_relative "dspy_rb/runtime/channel/listener"
|
|
43
|
+
require_relative "dspy_rb/runtime/channel/gate"
|
|
44
|
+
require_relative "dspy_rb/runtime/channel/callback"
|
|
45
|
+
require_relative "dspy_rb/runtime/channel"
|
|
46
|
+
require_relative "dspy_rb/runtime/child"
|
|
47
|
+
require_relative "dspy_rb/runtime/python"
|
|
48
|
+
require_relative "dspy_rb/runtime/startup"
|
|
49
|
+
require_relative "dspy_rb/runtime/registry"
|
|
50
|
+
require_relative "dspy_rb/signatures/types"
|
|
51
|
+
require_relative "dspy_rb/signatures/signature"
|
|
52
|
+
require_relative "dspy_rb/execution/collector"
|
|
53
|
+
require_relative "dspy_rb/execution/around"
|
|
54
|
+
require_relative "dspy_rb/execution/context"
|
|
55
|
+
require_relative "dspy_rb/execution/step"
|
|
56
|
+
require_relative "dspy_rb/execution/scorer"
|
|
57
|
+
require_relative "dspy_rb/execution/evaluation"
|
|
58
|
+
require_relative "dspy_rb/execution/toolbox"
|
|
59
|
+
require_relative "dspy_rb/execution/program"
|
|
60
|
+
require_relative "dspy_rb/execution/optimization"
|
|
61
|
+
require_relative "dspy_rb/execution/rows"
|
|
62
|
+
require_relative "dspy_rb/clients/lm"
|
|
63
|
+
require_relative "dspy_rb/clients/dummy_lm"
|
|
64
|
+
require_relative "dspy_rb/predict/predict"
|
|
65
|
+
require_relative "dspy_rb/predict/chain_of_thought"
|
|
66
|
+
require_relative "dspy_rb/tool"
|
|
67
|
+
require_relative "dspy_rb/predict/react"
|
|
68
|
+
require_relative "dspy_rb/primitives/module"
|
|
69
|
+
require_relative "dspy_rb/primitives/parts"
|
|
70
|
+
require_relative "dspy_rb/source"
|
|
71
|
+
require_relative "dspy_rb/evaluate/evaluate"
|
|
72
|
+
require_relative "dspy_rb/teleprompt/optimizer"
|
|
73
|
+
require_relative "dspy_rb/teleprompt/labeled_few_shot"
|
|
74
|
+
require_relative "dspy_rb/teleprompt/bootstrap_few_shot"
|
|
75
|
+
require_relative "dspy_rb/teleprompt/gepa"
|
|
76
|
+
require_relative "dspy_rb/teleprompt/mipro_v2"
|
|
77
|
+
require_relative "dspy_rb/teleprompt/bootstrap_few_shot_with_optuna"
|
|
78
|
+
require_relative "dspy_rb/teleprompt/bootstrap_few_shot_with_random_search"
|
|
79
|
+
require_relative "dspy_rb/teleprompt/simba"
|
|
80
|
+
require_relative "dspy_rb/evaluate/metric"
|
|
81
|
+
require_relative "dspy_rb/datasets/dataset"
|
|
82
|
+
require_relative "dspy_rb/datasets/data_loader"
|
|
83
|
+
require_relative "dspy_rb/datasets/python_random"
|
|
84
|
+
|
|
85
|
+
# Python DSPy from Ruby, in a Python process the gem manages.
|
|
86
|
+
module DSPyRb
|
|
87
|
+
extend Dry::Configurable
|
|
88
|
+
|
|
89
|
+
# Each setting's value as its type takes it, one it refuses raised naming the setting, never quoting the value.
|
|
90
|
+
taking = ->(name, type) { ->(value) { Wire::Departure.taken(type, value, "DSPyRb.config.#{name}") } }
|
|
91
|
+
|
|
92
|
+
# The models a setting takes, told by class, since a context below the API knows one by its description alone.
|
|
93
|
+
LMS = Wire::Types.Instance(LM) | Wire::Types.Instance(DummyLM)
|
|
94
|
+
private_constant :LMS
|
|
95
|
+
|
|
96
|
+
setting :lm, constructor: taking[:lm, LMS.optional]
|
|
97
|
+
setting :adapter, constructor: taking[:adapter, Wire::Adapter.optional]
|
|
98
|
+
|
|
99
|
+
# Read from RUBY_ON_DSPY_ENVIRONMENT, so `ruby-on-dspy setup` and the app agree.
|
|
100
|
+
setting :environment,
|
|
101
|
+
default: ENV.fetch("RUBY_ON_DSPY_ENVIRONMENT", "~/.cache/ruby-on-dspy"),
|
|
102
|
+
constructor: ->(path) { File.expand_path(taking[:environment, Wire::Types::Strict::String][path]) }
|
|
103
|
+
setting :threads, default: 8, constructor: taking[:threads, Wire::Types::Strict::Integer.constrained(gteq: 1)]
|
|
104
|
+
|
|
105
|
+
# Keys go here: Python reads them once, as it starts, and none travels in a request.
|
|
106
|
+
setting :env, default: Values::Env.of({}), constructor: ->(env) { Values::Env.of(env) }
|
|
107
|
+
|
|
108
|
+
# Each kind of number constrained apart, since a constraint on their sum compares whatever is given.
|
|
109
|
+
seconds = Wire::Types::Strict::Integer.constrained(gteq: 0) | Wire::Types::Strict::Float.constrained(gteq: 0)
|
|
110
|
+
|
|
111
|
+
setting :grace, default: 30, constructor: taking[:grace, seconds]
|
|
112
|
+
|
|
113
|
+
# DSPy's own logs to stderr, on by default as in DSPy; Python applies it as it starts.
|
|
114
|
+
setting :logging, default: true, constructor: taking[:logging, Wire::Types::Strict::Bool]
|
|
115
|
+
|
|
116
|
+
# The gate every model call asks where no context sets one, such as one that refuses: none by default.
|
|
117
|
+
setting :before_call, constructor: taking[:before_call, Wire::Types.Interface(:call).optional]
|
|
118
|
+
|
|
119
|
+
Adapter = Wire::Adapter
|
|
120
|
+
PendingCall = Wire::PendingCall
|
|
121
|
+
Call = Wire::Call
|
|
122
|
+
Prediction = Values::Prediction
|
|
123
|
+
Example = Values::Example
|
|
124
|
+
EvaluationResult = Values::EvaluationResult
|
|
125
|
+
Spend = Values::Spend
|
|
126
|
+
|
|
127
|
+
class << self
|
|
128
|
+
def context(lm: nil, adapter: nil, on_call: nil, before_call: nil, around: nil, &)
|
|
129
|
+
hooks = { on_call:, before_call:, around: }
|
|
130
|
+
|
|
131
|
+
refuse_context(lm, hooks)
|
|
132
|
+
|
|
133
|
+
outer = Execution::Context.current
|
|
134
|
+
|
|
135
|
+
# Each hook after those of the contexts around it, which come first.
|
|
136
|
+
stacked = hooks.to_h { |name, hook| [name, hook ? [*outer[name], hook].freeze : outer[name]] }
|
|
137
|
+
fields = { **outer.attributes, lm: lm || outer.lm, adapter: adapter || outer.adapter, **stacked }
|
|
138
|
+
inner = Wire::Departure.taken(Values::Context, fields, "DSPyRb.context")
|
|
139
|
+
|
|
140
|
+
Execution::Context.within(inner, &)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# The LM calls made here go to, as dspy.settings.lm: the context's, else config.lm, else DSPy's in a callback.
|
|
144
|
+
def lm
|
|
145
|
+
Execution::Step.new.own_lm || in_force(Execution::Context.current)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# A reused pool thread would otherwise keep the first caller's context.
|
|
149
|
+
def capture(&)
|
|
150
|
+
Execution::Context.capture(&)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def start
|
|
154
|
+
Runtime::Registry.current
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def shutdown
|
|
158
|
+
Runtime::Registry.shutdown
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
|
|
163
|
+
# DSPy's LM in force where it called back, its changes since as a copy; a DummyLM the one holding its answers.
|
|
164
|
+
def in_force(context)
|
|
165
|
+
given = context.in_force
|
|
166
|
+
|
|
167
|
+
# A change no copy can make leaves it unknown rather than wrong.
|
|
168
|
+
return if given.nil? || given.changes.keys.intersect?(Wire::LM::KEPT)
|
|
169
|
+
|
|
170
|
+
lm = built(given.lm, context)
|
|
171
|
+
|
|
172
|
+
given.changes.empty? || lm.nil? ? lm : lm.copy(**given.changes)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# A new LM from its description; a DummyLM the one an asker holds, which holds its answers.
|
|
176
|
+
def built(described, context)
|
|
177
|
+
return LM.new(described) if described.is_a?(Wire::LM)
|
|
178
|
+
|
|
179
|
+
[*context.held, config.lm].grep(DummyLM).find { _1.description == described }
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Refused before the context is built, so a refusal names what was given, an LM by its class as config.lm does.
|
|
183
|
+
def refuse_context(lm, hooks)
|
|
184
|
+
hooks.each do |name, hook|
|
|
185
|
+
raise Error, "DSPyRb.context cannot take #{name} as given" if hook && !hook.respond_to?(:call)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
raise Error, "DSPyRb.context cannot take lm as given" unless lm.nil? || LMS.valid?(lm)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
data/lib/ruby-on-dspy.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""ruby-on-dspy's Python side: the server a Ruby process spawns, `python -m dspy_rb`."""
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""The spawned entry: `python -I -m dspy_rb --threads N`, on the two pipes Ruby passes."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
from typing import Final
|
|
8
|
+
|
|
9
|
+
import dspy
|
|
10
|
+
import tqdm
|
|
11
|
+
from dspy.clients import base_lm
|
|
12
|
+
|
|
13
|
+
from dspy_rb.server.serve import Server
|
|
14
|
+
from dspy_rb.values import Spawning
|
|
15
|
+
from dspy_rb.wire.channel import Channel
|
|
16
|
+
|
|
17
|
+
# Unset, LiteLLM fetches its cost map and beta headers from GitHub as it loads, changing requests.
|
|
18
|
+
LITELLM_OFFLINE: Final = {
|
|
19
|
+
"LITELLM_OFFLINE_MODE": "true",
|
|
20
|
+
"LITELLM_LOCAL_MODEL_COST_MAP": "True",
|
|
21
|
+
"LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS": "True",
|
|
22
|
+
}
|
|
23
|
+
DONE: Final = 0
|
|
24
|
+
FAILED: Final = 1
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def main() -> None:
|
|
28
|
+
"""Serve the spawning Ruby process until its requests end, then exit at once."""
|
|
29
|
+
for name, value in LITELLM_OFFLINE.items():
|
|
30
|
+
os.environ.setdefault(name, value)
|
|
31
|
+
|
|
32
|
+
# os._exit skips multiprocessing's cleanup, so tqdm's own lock (tqdm/std.py:115-119) would be reported leaked.
|
|
33
|
+
tqdm.tqdm.set_lock(threading.RLock())
|
|
34
|
+
settings = _settings()
|
|
35
|
+
|
|
36
|
+
# Ruby's DSPyRb.config.logging, by DSPy's own switch (dspy/utils/logging_utils.py:40), before DSPy logs a line.
|
|
37
|
+
if not settings.logging:
|
|
38
|
+
dspy.disable_logging()
|
|
39
|
+
|
|
40
|
+
# A spawned Python lives as long as its owner, and its DSPy is its own to bound.
|
|
41
|
+
keep_history(settings.threads)
|
|
42
|
+
server = Server(Channel.spawned(), threads=settings.threads)
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
server.run()
|
|
46
|
+
except BaseException as fatal: # noqa: BLE001 - reported in one line, then the process ends.
|
|
47
|
+
sys.stderr.write(f"ruby-on-dspy: {type(fatal).__name__}: {fatal}\n")
|
|
48
|
+
code = FAILED
|
|
49
|
+
else:
|
|
50
|
+
code = DONE
|
|
51
|
+
|
|
52
|
+
# At once: a call in flight must not outlive an owner that is gone.
|
|
53
|
+
os._exit(code)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def keep_history(calls: int) -> None:
|
|
57
|
+
"""Bound DSPy's global history to `calls` entries, the fewest `calls` threads at once allow."""
|
|
58
|
+
base_lm.MAX_HISTORY_SIZE = calls
|
|
59
|
+
|
|
60
|
+
del base_lm.GLOBAL_HISTORY[: max(0, len(base_lm.GLOBAL_HISTORY) - calls)]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _settings() -> Spawning:
|
|
64
|
+
parser = argparse.ArgumentParser(prog="python -m dspy_rb")
|
|
65
|
+
|
|
66
|
+
parser.add_argument("--threads", type=int, required=True)
|
|
67
|
+
parser.add_argument("--logging", action=argparse.BooleanOptionalAction, default=True)
|
|
68
|
+
arguments = parser.parse_args()
|
|
69
|
+
threads: int = arguments.threads
|
|
70
|
+
logging: bool = arguments.logging
|
|
71
|
+
|
|
72
|
+
return Spawning(threads=threads, logging=logging)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
if __name__ == "__main__":
|
|
76
|
+
main()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""The server: each request served, each predictor built from its description, and what DSPy tells Ruby."""
|