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.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +112 -0
  3. data/LICENSE +21 -0
  4. data/README.md +442 -0
  5. data/exe/ruby-on-dspy +7 -0
  6. data/lib/dspy_rb/cli.rb +83 -0
  7. data/lib/dspy_rb/clients/dummy_lm.rb +81 -0
  8. data/lib/dspy_rb/clients/lm.rb +44 -0
  9. data/lib/dspy_rb/datasets/data_loader.rb +74 -0
  10. data/lib/dspy_rb/datasets/dataset.rb +37 -0
  11. data/lib/dspy_rb/datasets/python_random.rb +38 -0
  12. data/lib/dspy_rb/errors.rb +135 -0
  13. data/lib/dspy_rb/evaluate/evaluate.rb +53 -0
  14. data/lib/dspy_rb/evaluate/metric.rb +61 -0
  15. data/lib/dspy_rb/execution/around.rb +63 -0
  16. data/lib/dspy_rb/execution/collector.rb +61 -0
  17. data/lib/dspy_rb/execution/context.rb +160 -0
  18. data/lib/dspy_rb/execution/evaluation.rb +90 -0
  19. data/lib/dspy_rb/execution/optimization.rb +71 -0
  20. data/lib/dspy_rb/execution/program.rb +126 -0
  21. data/lib/dspy_rb/execution/rows.rb +41 -0
  22. data/lib/dspy_rb/execution/scorer.rb +68 -0
  23. data/lib/dspy_rb/execution/step.rb +116 -0
  24. data/lib/dspy_rb/execution/toolbox.rb +28 -0
  25. data/lib/dspy_rb/predict/chain_of_thought.rb +22 -0
  26. data/lib/dspy_rb/predict/predict.rb +101 -0
  27. data/lib/dspy_rb/predict/react.rb +80 -0
  28. data/lib/dspy_rb/primitives/module.rb +105 -0
  29. data/lib/dspy_rb/primitives/parts.rb +121 -0
  30. data/lib/dspy_rb/runtime/channel/callback.rb +105 -0
  31. data/lib/dspy_rb/runtime/channel/ending.rb +35 -0
  32. data/lib/dspy_rb/runtime/channel/exchange.rb +176 -0
  33. data/lib/dspy_rb/runtime/channel/gate.rb +74 -0
  34. data/lib/dspy_rb/runtime/channel/listener.rb +125 -0
  35. data/lib/dspy_rb/runtime/channel/meter.rb +46 -0
  36. data/lib/dspy_rb/runtime/channel/reporter.rb +68 -0
  37. data/lib/dspy_rb/runtime/channel/settler.rb +69 -0
  38. data/lib/dspy_rb/runtime/channel/traffic.rb +121 -0
  39. data/lib/dspy_rb/runtime/channel/watchdog.rb +36 -0
  40. data/lib/dspy_rb/runtime/channel/writer.rb +77 -0
  41. data/lib/dspy_rb/runtime/channel.rb +145 -0
  42. data/lib/dspy_rb/runtime/child.rb +48 -0
  43. data/lib/dspy_rb/runtime/python.rb +43 -0
  44. data/lib/dspy_rb/runtime/registry.rb +48 -0
  45. data/lib/dspy_rb/runtime/startup.rb +53 -0
  46. data/lib/dspy_rb/secrets.rb +25 -0
  47. data/lib/dspy_rb/setup/environment.rb +120 -0
  48. data/lib/dspy_rb/setup/stale.rb +102 -0
  49. data/lib/dspy_rb/setup/uv.rb +122 -0
  50. data/lib/dspy_rb/signatures/signature.rb +100 -0
  51. data/lib/dspy_rb/signatures/types.rb +44 -0
  52. data/lib/dspy_rb/source.rb +64 -0
  53. data/lib/dspy_rb/teleprompt/bootstrap_few_shot.rb +9 -0
  54. data/lib/dspy_rb/teleprompt/bootstrap_few_shot_with_optuna.rb +9 -0
  55. data/lib/dspy_rb/teleprompt/bootstrap_few_shot_with_random_search.rb +9 -0
  56. data/lib/dspy_rb/teleprompt/gepa.rb +9 -0
  57. data/lib/dspy_rb/teleprompt/labeled_few_shot.rb +14 -0
  58. data/lib/dspy_rb/teleprompt/mipro_v2.rb +16 -0
  59. data/lib/dspy_rb/teleprompt/optimizer.rb +127 -0
  60. data/lib/dspy_rb/teleprompt/simba.rb +16 -0
  61. data/lib/dspy_rb/tool.rb +108 -0
  62. data/lib/dspy_rb/values/check.rb +14 -0
  63. data/lib/dspy_rb/values/context.rb +46 -0
  64. data/lib/dspy_rb/values/dataset.rb +55 -0
  65. data/lib/dspy_rb/values/dict.rb +17 -0
  66. data/lib/dspy_rb/values/env.rb +23 -0
  67. data/lib/dspy_rb/values/evaluation_result.rb +31 -0
  68. data/lib/dspy_rb/values/example.rb +134 -0
  69. data/lib/dspy_rb/values/fault.rb +15 -0
  70. data/lib/dspy_rb/values/history.rb +13 -0
  71. data/lib/dspy_rb/values/list.rb +16 -0
  72. data/lib/dspy_rb/values/literal.rb +27 -0
  73. data/lib/dspy_rb/values/loss.rb +12 -0
  74. data/lib/dspy_rb/values/optional.rb +16 -0
  75. data/lib/dspy_rb/values/prediction.rb +58 -0
  76. data/lib/dspy_rb/values/reasoning.rb +13 -0
  77. data/lib/dspy_rb/values/spend.rb +27 -0
  78. data/lib/dspy_rb/values/types.rb +164 -0
  79. data/lib/dspy_rb/version.rb +11 -0
  80. data/lib/dspy_rb/wire/departure.rb +100 -0
  81. data/lib/dspy_rb/wire/descriptions.rb +220 -0
  82. data/lib/dspy_rb/wire/facts.rb +79 -0
  83. data/lib/dspy_rb/wire/in_flight.rb +128 -0
  84. data/lib/dspy_rb/wire/json.rb +76 -0
  85. data/lib/dspy_rb/wire/optimizers.rb +240 -0
  86. data/lib/dspy_rb/wire/records.rb +44 -0
  87. data/lib/dspy_rb/wire/replies.rb +188 -0
  88. data/lib/dspy_rb/wire/requests.rb +214 -0
  89. data/lib/dspy_rb/wire/shape.rb +25 -0
  90. data/lib/dspy_rb/wire/types.rb +23 -0
  91. data/lib/dspy_rb/wire.rb +34 -0
  92. data/lib/dspy_rb.rb +191 -0
  93. data/lib/ruby-on-dspy.rb +4 -0
  94. data/python/dspy_rb/__init__.py +1 -0
  95. data/python/dspy_rb/__main__.py +76 -0
  96. data/python/dspy_rb/server/__init__.py +1 -0
  97. data/python/dspy_rb/server/build.py +281 -0
  98. data/python/dspy_rb/server/calls.py +215 -0
  99. data/python/dspy_rb/server/conversion.py +180 -0
  100. data/python/dspy_rb/server/data.py +169 -0
  101. data/python/dspy_rb/server/evaluation.py +107 -0
  102. data/python/dspy_rb/server/facts.py +357 -0
  103. data/python/dspy_rb/server/optimization.py +98 -0
  104. data/python/dspy_rb/server/pulse.py +68 -0
  105. data/python/dspy_rb/server/ruby.py +206 -0
  106. data/python/dspy_rb/server/serve.py +369 -0
  107. data/python/dspy_rb/server/standins.py +364 -0
  108. data/python/dspy_rb/server/state.py +104 -0
  109. data/python/dspy_rb/server/traffic.py +93 -0
  110. data/python/dspy_rb/values.py +51 -0
  111. data/python/dspy_rb/wire/__init__.py +1 -0
  112. data/python/dspy_rb/wire/channel.py +107 -0
  113. data/python/dspy_rb/wire/lines.py +269 -0
  114. data/python/dspy_rb/wire/shapes.py +1220 -0
  115. data/python/dspy_rb/wire/wire.json +4592 -0
  116. data/python/pyproject.toml +86 -0
  117. data/python/uv.lock +1576 -0
  118. metadata +230 -0
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPyRb
4
+ module Wire
5
+ # The LM DSPy has in force where it calls Ruby back: Ruby's description it was built from, and DSPy's changes since.
6
+ class InForce < Shape
7
+ attribute :lm, LM | DummyLM
8
+ attribute :changes, Types::Values
9
+ end
10
+
11
+ # A Ruby tool DSPy calls while serving a request, by its name, with the arguments DSPy parsed; a teacher's says so.
12
+ class ToolCall < Shape
13
+ attribute :id, Types::Id
14
+ attribute :kind, kind_type("tool")
15
+ attribute :request, Types::Id
16
+ attribute :tool, Types::Name
17
+ attribute :args, Types::Values
18
+ attribute? :teacher, Types::Strict::Bool
19
+ attribute? :in_force, InForce
20
+ end
21
+
22
+ # A step of DSPy's trace: the predictor by the name DSPy gives it, if any, with its inputs and its outputs.
23
+ class TraceStep < Shape
24
+ attribute? :predictor, Types::Name
25
+ attribute :inputs, Types::Values
26
+ attribute :outputs, Types::Values
27
+ end
28
+
29
+ # A Ruby metric DSPy calls while serving a request, with how many arguments DSPy passed, and what they were.
30
+ class MetricCall < Shape
31
+ attribute :id, Types::Id
32
+ attribute :kind, kind_type("metric")
33
+ attribute :request, Types::Id
34
+ attribute :given, Types::Count
35
+ attribute :example, Example
36
+
37
+ # Each left out for DSPy's None: SIMBA passes no prediction for a program that raised (simba_utils.py:37-49).
38
+ attribute? :prediction, Types::Values
39
+ attribute? :typed, Types::Strict::Array.of(Typed)
40
+ attribute? :trace, Types::Strict::Array.of(TraceStep)
41
+
42
+ # GEPA's feedback call's: the predictor it asks about, and its step of the trace (gepa.py:604-619).
43
+ attribute? :pred_name, Types::Name
44
+ attribute? :pred_trace, Types::Strict::Array.of(TraceStep)
45
+ attribute? :in_force, InForce
46
+ end
47
+
48
+ # A Ruby program DSPy calls while serving a request, with one example's inputs; a compile's teacher says so.
49
+ class ProgramCall < Shape
50
+ attribute :id, Types::Id
51
+ attribute :kind, kind_type("program")
52
+ attribute :request, Types::Id
53
+ attribute :inputs, Types::Values
54
+ attribute? :teacher, Types::Strict::Bool
55
+ attribute? :in_force, InForce
56
+ end
57
+
58
+ # DSPy's DummyLM asking for the answer its next call gives, which Ruby names by its index in the list.
59
+ class NextCall < Shape
60
+ attribute :id, Types::Id
61
+ attribute :kind, kind_type("next")
62
+ attribute :request, Types::Id
63
+ end
64
+
65
+ # A model call a gated request is about to send, for Ruby's gates to let go or refuse.
66
+ class GateCall < Shape
67
+ attribute :id, Types::Id
68
+ attribute :kind, kind_type("gate")
69
+ attribute :request, Types::Id
70
+ attribute :pending, PendingCall
71
+ attribute? :in_force, InForce
72
+ end
73
+
74
+ # One model call of a request, written as DSPy ends it; it may follow the request's reply.
75
+ class Progress < Shape
76
+ attribute :id, Types::Id
77
+ attribute :kind, kind_type("progress")
78
+
79
+ # Absent for a call Python could not record once its request had replied: Ruby counts it unrecorded.
80
+ attribute? :call, Call
81
+ end
82
+
83
+ # Python is alive, written while a request or a model call of Ruby's is open in it, whatever DSPy waits on.
84
+ class Alive < Shape
85
+ attribute :kind, kind_type("alive")
86
+ end
87
+
88
+ # A Ruby exception, for Python to raise in the thread waiting on its callback.
89
+ class RubyFailure < Shape
90
+ attribute :ruby_class, Types::Name
91
+ attribute :message, Types::Text
92
+ attribute :fatal, Types::Strict::Bool
93
+
94
+ # The nested request whose own Python exception this one carries back.
95
+ attribute? :of, Types::Id
96
+ end
97
+
98
+ # What a callback's Ruby code returned: a value, or a prediction's outputs.
99
+ class Returned < Shape
100
+ attribute :id, Types::Id
101
+ attribute :kind, kind_type("returned")
102
+ attribute? :value, Types::JsonValue
103
+ attribute? :prediction, Types::Values
104
+ attribute? :typed, Types::Strict::Array.of(Typed)
105
+ end
106
+
107
+ # A callback whose Ruby code raised.
108
+ class Raised < Shape
109
+ attribute :id, Types::Id
110
+ attribute :kind, kind_type("raised")
111
+ attribute :error, RubyFailure
112
+ end
113
+
114
+ # Ruby stopped waiting on a request, which takes no reply: nothing more starts for it.
115
+ class Cancel < Shape
116
+ attribute :kind, kind_type("cancel")
117
+ attribute :of, Types::Id
118
+ end
119
+
120
+ CALLBACKS = [
121
+ ToolCall,
122
+ MetricCall,
123
+ ProgramCall,
124
+ NextCall,
125
+ GateCall
126
+ ].freeze
127
+ end
128
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPyRb
4
+ module Wire
5
+ # What JSON can carry, each value refused naming where it departs, never quoting it.
6
+ module Json
7
+ class << self
8
+ # Each value as JSON carries it, by its name; a refusal names `place`, the call it was given to, if any.
9
+ def values(hash, place = nil)
10
+ valued = keyed(hash, place).to_h { |key, item| [key, value(item, place ? "#{place}'s #{key}" : key.to_s)] }
11
+
12
+ once(valued, hash, place)
13
+ end
14
+
15
+ # A Hash's keys as Symbols; a non-Hash, or a key not a String or Symbol, refused by its class, naming `place`.
16
+ def keyed(hash, place = nil)
17
+ unless hash.is_a?(::Hash)
18
+ refusal = place ? "#{place} takes values as" : "values come as"
19
+
20
+ raise Error, "#{refusal} a Hash, not #{Error.class_of(hash)}"
21
+ end
22
+
23
+ hash.transform_keys do |key|
24
+ next key.to_sym if key.is_a?(::String) || key.is_a?(::Symbol)
25
+
26
+ refusal = place ? "#{place} takes a name as" : "a name is"
27
+
28
+ raise Error, "#{refusal} a String or a Symbol, not #{Error.class_of(key)}"
29
+ end
30
+ end
31
+
32
+ # Names as the wire carries them, a Symbol as its text; any other item left for the wire to refuse, never to_s.
33
+ def names(given)
34
+ return given unless given.is_a?(::Array)
35
+
36
+ given.map { _1.is_a?(::Symbol) ? _1.to_s : _1 }
37
+ end
38
+
39
+ # `given` as JSON carries it, a refusal naming where it departs from `path`, the place it was given at.
40
+ def value(given, path = "value")
41
+ case given
42
+ when ::Array then given.each_with_index.map { |item, index| value(item, "#{path}[#{index}]") }
43
+ when ::Hash then once(given.to_h { |key, item| [key.to_s, value(item, "#{path}.#{key}")] }, given, path)
44
+ else scalar(given, path)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # Two keys JSON writes the same, such as :a and "a", refused rather than one dropped, as Python refuses them.
51
+ def once(written, given, path)
52
+ return written if written.size == given.size
53
+
54
+ same = given.keys.group_by(&:to_s).values.find { _1.size > 1 }.map(&:inspect).join(" and ")
55
+
56
+ raise Error, "#{path || "a Hash"} has #{same}, which JSON writes as one key: keep one"
57
+ end
58
+
59
+ def scalar(value, path)
60
+ case value
61
+ when ::String then utf8(value, path)
62
+ when ::Integer, true, false, nil then value
63
+ when ::Float then value.finite? ? value : raise(Error, "#{path} is #{value}, which JSON cannot carry")
64
+ else raise Error, "#{path} is #{Error.class_of(value)}, which JSON cannot carry"
65
+ end
66
+ end
67
+
68
+ def utf8(string, path)
69
+ return string if [Encoding::UTF_8, Encoding::US_ASCII].include?(string.encoding) && string.valid_encoding?
70
+
71
+ raise Error, "#{path} is not text in UTF-8 (#{string.encoding})"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,240 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPyRb
4
+ module Wire
5
+ # DSPy's LabeledFewShot options (dspy/teleprompt/vanilla.py:7-8), by its name; left out, the default meta records.
6
+ class LabeledFewShotOptions < Shape
7
+ attribute :name, kind_type("labeled_few_shot")
8
+
9
+ # Any Integer, as DSPy takes it: one it cannot sample raises, and a slice takes a negative one (vanilla.py:20-23).
10
+ attribute? :k, Types::Strict::Integer.meta(dspy_default: 16)
11
+ end
12
+
13
+ # What LabeledFewShot's compile takes beside the student and trainset (dspy/teleprompt/vanilla.py:10), by its name.
14
+ class LabeledFewShotCompile < Shape
15
+ attribute :name, kind_type("labeled_few_shot")
16
+ attribute? :sample, Types::Strict::Bool.meta(dspy_default: true)
17
+ end
18
+
19
+ # The settings DSPy puts in force for a teacher's runs (dspy/teleprompt/bootstrap.py:191): its LM, as the docs pass.
20
+ class TeacherSettings < Shape
21
+ attribute? :lm, LM | DummyLM
22
+ end
23
+
24
+ # DSPy's BootstrapFewShot options (dspy/teleprompt/bootstrap.py:37-46), by its name; left out, DSPy's default.
25
+ class BootstrapFewShotOptions < Shape
26
+ attribute :name, kind_type("bootstrap_few_shot")
27
+ attribute? :metric, Metric | RubyMetric
28
+
29
+ # A number the metric's value is compared with by >=, as DSPy compares it (bootstrap.py:210-211).
30
+ attribute? :metric_threshold, Types::Number
31
+ attribute? :teacher_settings, TeacherSettings
32
+ attribute? :max_bootstrapped_demos, Types::Strict::Integer.meta(dspy_default: 4)
33
+ attribute? :max_labeled_demos, Types::Strict::Integer.meta(dspy_default: 16)
34
+ attribute? :max_rounds, Types::Strict::Integer.meta(dspy_default: 1)
35
+
36
+ # Left out, DSPy's own setting (bootstrap.py:221).
37
+ attribute? :max_errors, Types::Strict::Integer
38
+ end
39
+
40
+ # What BootstrapFewShot's compile takes beside the student and trainset (dspy/teleprompt/bootstrap.py:87).
41
+ class BootstrapFewShotCompile < Shape
42
+ attribute :name, kind_type("bootstrap_few_shot")
43
+ attribute? :teacher, Predictor | RubyProgram
44
+ end
45
+
46
+ # DSPy's GEPA options (dspy/teleprompt/gepa/gepa.py:396-433), by its name; left out, DSPy's default.
47
+ class GEPAOptions < Shape
48
+ attribute :name, kind_type("gepa")
49
+
50
+ # One taking GEPA's five arguments, which DSPy checks (gepa.py:435-441), and giving a score or a feedback.
51
+ attribute :metric, Metric | RubyMetric
52
+ attribute? :auto, Types::Strict::String.enum("light", "medium", "heavy")
53
+ attribute? :max_full_evals, Types::Strict::Integer
54
+ attribute? :max_metric_calls, Types::Strict::Integer
55
+ attribute? :reflection_minibatch_size, Types::Strict::Integer.meta(dspy_default: 3)
56
+
57
+ # By gepa's names for its strategies and selectors, which gepa checks (gepa 0.1.4, api.py:307-346).
58
+ attribute? :candidate_selection_strategy, Types::Strict::String.meta(dspy_default: "pareto")
59
+ attribute? :reflection_lm, LM | DummyLM
60
+ attribute? :skip_perfect_score, Types::Strict::Bool.meta(dspy_default: true)
61
+ attribute? :add_format_failure_as_feedback, Types::Strict::Bool.meta(dspy_default: false)
62
+ attribute? :component_selector, Types::Strict::String.meta(dspy_default: "round_robin")
63
+ attribute? :use_merge, Types::Strict::Bool.meta(dspy_default: true)
64
+
65
+ # Never None, though DSPy's annotation allows it: gepa compares it with < (gepa 0.1.4, core/engine.py:548).
66
+ attribute? :max_merge_invocations, Types::Strict::Integer.meta(dspy_default: 5)
67
+ attribute? :num_threads, Types::Strict::Integer
68
+ attribute? :failure_score, Types::Number.meta(dspy_default: 0.0)
69
+ attribute? :perfect_score, Types::Number.meta(dspy_default: 1.0)
70
+
71
+ # The folder gepa writes its state to, a pickle it resumes from (gepa 0.1.4, api.py:176).
72
+ attribute? :log_dir, Types::Path
73
+ attribute? :track_stats, Types::Strict::Bool.meta(dspy_default: false)
74
+ attribute? :track_best_outputs, Types::Strict::Bool.meta(dspy_default: false)
75
+ attribute? :warn_on_score_mismatch, Types::Strict::Bool.meta(dspy_default: true)
76
+ attribute? :seed, Types::Strict::Integer.meta(dspy_default: 0)
77
+ attribute? :gepa_kwargs, Types::Values
78
+
79
+ # nil gives None, an unseeded run, where leaving it out gives DSPy's 0 (gepa.py:430, 602).
80
+ attribute? :none, Types::Strict::Array.of(Types::Strict::String.enum("seed"))
81
+ end
82
+
83
+ # What GEPA's compile takes beside the student and trainset (dspy/teleprompt/gepa/gepa.py:542-549).
84
+ class GEPACompile < Shape
85
+ attribute :name, kind_type("gepa")
86
+
87
+ # Taken as DSPy's signature takes it, and refused by DSPy itself (gepa.py:562).
88
+ attribute? :teacher, Predictor | RubyProgram
89
+ attribute? :valset, Types::Strict::Array.of(Example)
90
+ end
91
+
92
+ # DSPy's MIPROv2 options (dspy/teleprompt/mipro_optimizer_v2.py:61-79), by its name; left out, DSPy's default.
93
+ class MIPROv2Options < Shape
94
+ attribute :name, kind_type("mipro_v2")
95
+ attribute :metric, Metric | RubyMetric
96
+ attribute? :prompt_model, LM | DummyLM
97
+ attribute? :task_model, LM | DummyLM
98
+ attribute? :teacher_settings, TeacherSettings
99
+ attribute? :max_bootstrapped_demos, Types::Strict::Integer.meta(dspy_default: 4)
100
+ attribute? :max_labeled_demos, Types::Strict::Integer.meta(dspy_default: 4)
101
+ attribute? :auto, Types::Strict::String.enum("light", "medium", "heavy").meta(dspy_default: "light")
102
+ attribute? :num_candidates, Types::Strict::Integer
103
+ attribute? :num_threads, Types::Strict::Integer
104
+ attribute? :max_errors, Types::Strict::Integer
105
+ attribute? :seed, Types::Strict::Integer.meta(dspy_default: 9)
106
+ attribute? :init_temperature, Types::Number.meta(dspy_default: 1.0)
107
+ attribute? :verbose, Types::Strict::Bool.meta(dspy_default: false)
108
+ attribute? :track_stats, Types::Strict::Bool.meta(dspy_default: true)
109
+
110
+ # The folder DSPy saves each program it scores to, as JSON (dspy/teleprompt/utils.py:210-229).
111
+ attribute? :log_dir, Types::Path
112
+ attribute? :metric_threshold, Types::Number
113
+
114
+ # nil gives None: no auto, for budgets set by hand, and an unseeded run (mipro_optimizer_v2.py:154-171).
115
+ attribute? :none, Types::Strict::Array.of(Types::Strict::String.enum("auto", "seed"))
116
+ end
117
+
118
+ # What MIPROv2's compile takes beside the student and trainset (dspy/teleprompt/mipro_optimizer_v2.py:109-130).
119
+ class MIPROv2Compile < Shape
120
+ attribute :name, kind_type("mipro_v2")
121
+ attribute? :teacher, Predictor | RubyProgram
122
+ attribute? :valset, Types::Strict::Array.of(Example)
123
+ attribute? :num_trials, Types::Strict::Integer
124
+ attribute? :max_bootstrapped_demos, Types::Strict::Integer
125
+ attribute? :max_labeled_demos, Types::Strict::Integer
126
+ attribute? :seed, Types::Strict::Integer
127
+ attribute? :minibatch, Types::Strict::Bool.meta(dspy_default: true)
128
+ attribute? :minibatch_size, Types::Strict::Integer.meta(dspy_default: 35)
129
+ attribute? :minibatch_full_eval_steps, Types::Strict::Integer.meta(dspy_default: 5)
130
+ attribute? :program_aware_proposer, Types::Strict::Bool.meta(dspy_default: true)
131
+ attribute? :data_aware_proposer, Types::Strict::Bool.meta(dspy_default: true)
132
+ attribute? :view_data_batch_size, Types::Strict::Integer.meta(dspy_default: 10)
133
+ attribute? :tip_aware_proposer, Types::Strict::Bool.meta(dspy_default: true)
134
+ attribute? :fewshot_aware_proposer, Types::Strict::Bool.meta(dspy_default: true)
135
+
136
+ # true is refused by DSPy itself, before any call (mipro_optimizer_v2.py:131-136).
137
+ attribute? :requires_permission_to_run, Types::Strict::Bool
138
+ attribute? :provide_traceback, Types::Strict::Bool
139
+ end
140
+
141
+ # DSPy's BootstrapFewShotWithOptuna options (dspy/teleprompt/teleprompt_optuna.py:21-30), by its name.
142
+ class BootstrapFewShotWithOptunaOptions < Shape
143
+ attribute :name, kind_type("bootstrap_few_shot_with_optuna")
144
+ attribute :metric, Metric | RubyMetric
145
+ attribute? :teacher_settings, TeacherSettings
146
+ attribute? :max_bootstrapped_demos, Types::Strict::Integer.meta(dspy_default: 4)
147
+ attribute? :max_labeled_demos, Types::Strict::Integer.meta(dspy_default: 16)
148
+ attribute? :max_rounds, Types::Strict::Integer.meta(dspy_default: 1)
149
+ attribute? :num_candidate_programs, Types::Strict::Integer.meta(dspy_default: 16)
150
+ attribute? :num_threads, Types::Strict::Integer
151
+ end
152
+
153
+ # What its compile takes beside the student and trainset (dspy/teleprompt/teleprompt_optuna.py:68).
154
+ class BootstrapFewShotWithOptunaCompile < Shape
155
+ attribute :name, kind_type("bootstrap_few_shot_with_optuna")
156
+ attribute? :teacher, Predictor | RubyProgram
157
+ attribute :max_demos, Types::Strict::Integer
158
+ attribute? :valset, Types::Strict::Array.of(Example)
159
+ end
160
+
161
+ # DSPy's BootstrapFewShotWithRandomSearch options (dspy/teleprompt/random_search.py:28-40), by its name.
162
+ class BootstrapFewShotWithRandomSearchOptions < Shape
163
+ attribute :name, kind_type("bootstrap_few_shot_with_random_search")
164
+ attribute :metric, Metric | RubyMetric
165
+ attribute? :teacher_settings, TeacherSettings
166
+ attribute? :max_bootstrapped_demos, Types::Strict::Integer.meta(dspy_default: 4)
167
+ attribute? :max_labeled_demos, Types::Strict::Integer.meta(dspy_default: 16)
168
+ attribute? :max_rounds, Types::Strict::Integer.meta(dspy_default: 1)
169
+ attribute? :num_candidate_programs, Types::Strict::Integer.meta(dspy_default: 16)
170
+ attribute? :num_threads, Types::Strict::Integer
171
+ attribute? :max_errors, Types::Strict::Integer
172
+
173
+ # Numbers a score is compared with by >= (random_search.py:146; bootstrap.py:210-211).
174
+ attribute? :stop_at_score, Types::Number
175
+ attribute? :metric_threshold, Types::Number
176
+ end
177
+
178
+ # What its compile takes beside the student and trainset (dspy/teleprompt/random_search.py:57).
179
+ class BootstrapFewShotWithRandomSearchCompile < Shape
180
+ attribute :name, kind_type("bootstrap_few_shot_with_random_search")
181
+ attribute? :teacher, Predictor | RubyProgram
182
+ attribute? :valset, Types::Strict::Array.of(Example)
183
+
184
+ # The candidates' seeds to run, from -3, in any Enumerable, as DSPy takes any iterable (random_search.py:61-76).
185
+ attribute?(
186
+ :restrict,
187
+ Types::Strict::Array.of(Types::Strict::Integer).constructor { _1.is_a?(::Enumerable) ? _1.to_a : _1 }
188
+ )
189
+ attribute? :labeled_sample, Types::Strict::Bool.meta(dspy_default: true)
190
+ end
191
+
192
+ # DSPy's SIMBA options (dspy/teleprompt/simba.py:29-43), by its name; left out, DSPy's default.
193
+ class SIMBAOptions < Shape
194
+ attribute :name, kind_type("simba")
195
+
196
+ # It may get no prediction, where the program raised (dspy/teleprompt/simba_utils.py:37-49).
197
+ attribute :metric, Metric | RubyMetric
198
+ attribute? :bsize, Types::Strict::Integer.meta(dspy_default: 32)
199
+ attribute? :num_candidates, Types::Strict::Integer.meta(dspy_default: 6)
200
+ attribute? :max_steps, Types::Strict::Integer.meta(dspy_default: 8)
201
+ attribute? :max_demos, Types::Strict::Integer.meta(dspy_default: 4)
202
+ attribute? :prompt_model, LM | DummyLM
203
+ attribute? :teacher_settings, TeacherSettings
204
+ attribute? :demo_input_field_maxlen, Types::Strict::Integer.meta(dspy_default: 100_000)
205
+ attribute? :num_threads, Types::Strict::Integer
206
+ attribute? :temperature_for_sampling, Types::Number.meta(dspy_default: 0.2)
207
+ attribute? :temperature_for_candidates, Types::Number.meta(dspy_default: 0.2)
208
+ end
209
+
210
+ # What SIMBA's compile takes beside the student and trainset (dspy/teleprompt/simba.py:86-92).
211
+ class SIMBACompile < Shape
212
+ attribute :name, kind_type("simba")
213
+ attribute? :seed, Types::Strict::Integer.meta(dspy_default: 0)
214
+
215
+ # nil gives None, an unseeded run, where leaving it out gives DSPy's 0 (simba.py:108-109).
216
+ attribute? :none, Types::Strict::Array.of(Types::Strict::String.enum("seed"))
217
+ end
218
+
219
+ # Every optimizer's options and its compile's, which a compile request takes.
220
+ OPTIMIZER_OPTIONS = [
221
+ LabeledFewShotOptions,
222
+ BootstrapFewShotOptions,
223
+ GEPAOptions,
224
+ MIPROv2Options,
225
+ BootstrapFewShotWithOptunaOptions,
226
+ BootstrapFewShotWithRandomSearchOptions,
227
+ SIMBAOptions
228
+ ].freeze
229
+
230
+ COMPILE_OPTIONS = [
231
+ LabeledFewShotCompile,
232
+ BootstrapFewShotCompile,
233
+ GEPACompile,
234
+ MIPROv2Compile,
235
+ BootstrapFewShotWithOptunaCompile,
236
+ BootstrapFewShotWithRandomSearchCompile,
237
+ SIMBACompile
238
+ ].freeze
239
+ end
240
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPyRb
4
+ module Wire
5
+ # Where values hold one of DSPy's own types, which go back to it as such: the keys and indexes to it, and its type.
6
+ class Typed < Shape
7
+ attribute :path, Types::Strict::Array.of(Types::Strict::String | Types::Strict::Integer)
8
+ attribute :type, Types::Strict::String.enum("Reasoning", "History")
9
+ end
10
+
11
+ # DSPyRb::Example as it crosses: every field in order, dspy_ ones too, and its input keys once they are set.
12
+ class Example < Shape
13
+ attribute :record, Types::Values
14
+ attribute? :input_keys, Types::Strict::Array.of(Types::Name)
15
+ attribute? :typed, Types::Strict::Array.of(Typed)
16
+ end
17
+
18
+ # What a metric gave: a value, or a prediction's outputs, which DSPy sums by their score.
19
+ class Score < Shape
20
+ attribute? :value, Types::JsonValue
21
+ attribute? :prediction, Types::Values
22
+ end
23
+
24
+ # A predictor's state by its name: its raw instructions when they changed, and its demos.
25
+ class PredictorState < Shape
26
+ attribute :name, Types::Name
27
+ attribute? :instructions, Types::Text
28
+ attribute :demos, Types::Strict::Array.of(Example)
29
+ end
30
+
31
+ # What a compile or a load gave: each predictor DSPy's optimizers see, and whether DSPy marked it compiled.
32
+ class ProgramState < Shape
33
+ attribute :predictors, Types::Strict::Array.of(PredictorState)
34
+ attribute :compiled, Types::Strict::Bool
35
+ end
36
+
37
+ # One example's outputs, empty when its row failed, and its score, as DSPy's EvaluationResult.results holds them.
38
+ class Result < Shape
39
+ attribute :prediction, Types::Values
40
+ attribute? :typed, Types::Strict::Array.of(Typed)
41
+ attribute :score, Score
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DSPyRb
4
+ # The replies Python writes.
5
+ module Wire
6
+ # The model calls Python made but could not record the facts of: they never arrive.
7
+ UNRECORDED = Types::Count.default(0)
8
+
9
+ # The model calls still running as the reply was written: each one's progress follows it.
10
+ RUNNING = Types::Count.default(0)
11
+
12
+ # Python's answer to hello.
13
+ class Welcome < Shape
14
+ attribute :id, Types::Id
15
+ attribute :kind, kind_type("welcome")
16
+ attribute :protocol, Types::Strict::Integer
17
+ attribute :package, Types::Name
18
+ attribute :dspy, Types::Name
19
+ attribute :python, Types::Name
20
+ end
21
+
22
+ # A described predictor's raw instructions.
23
+ class Described < Shape
24
+ attribute :id, Types::Id
25
+ attribute :kind, kind_type("described")
26
+ attribute :instruction, Types::Text
27
+ end
28
+
29
+ # DSPy's own view of a Ruby tool: its arguments' JSON schema (Tool.args) and its text (str(tool)).
30
+ class ToolDescribed < Shape
31
+ attribute :id, Types::Id
32
+ attribute :kind, kind_type("tool_described")
33
+ attribute :args, Types::Values
34
+ attribute :text, Types::Text
35
+ end
36
+
37
+ # DSPy's own lm.kwargs of a described LM.
38
+ class LMDescribed < Shape
39
+ attribute :id, Types::Id
40
+ attribute :kind, kind_type("lm_described")
41
+ attribute :kwargs, Types::Values
42
+ end
43
+
44
+ # A call that gave outputs.
45
+ class Completed < Shape
46
+ attribute :id, Types::Id
47
+ attribute :kind, kind_type("completed")
48
+ attribute :outputs, Types::Values
49
+ attribute? :typed, Types::Strict::Array.of(Typed)
50
+ attribute :unrecorded, UNRECORDED
51
+ attribute :running, RUNNING
52
+ end
53
+
54
+ # An evaluate's score, DSPy's percentage, and its results in devset order.
55
+ class Evaluated < Shape
56
+ attribute :id, Types::Id
57
+ attribute :kind, kind_type("evaluated")
58
+ attribute :score, Types::Number
59
+ attribute :results, Types::Strict::Array.of(Result)
60
+ attribute :unrecorded, UNRECORDED
61
+ attribute :running, RUNNING
62
+ end
63
+
64
+ # A metric's score, from one call.
65
+ class Measured < Shape
66
+ attribute :id, Types::Id
67
+ attribute :kind, kind_type("measured")
68
+ attribute :score, Score
69
+ attribute :unrecorded, UNRECORDED
70
+ attribute :running, RUNNING
71
+ end
72
+
73
+ # What a direct call gave: DSPy's list of outputs, a candidate's each, as text or as an object where DSPy gives one.
74
+ class Generated < Shape
75
+ attribute :id, Types::Id
76
+ attribute :kind, kind_type("generated")
77
+ attribute :outputs, Types::Strict::Array.of(Types::JsonValue)
78
+ attribute :unrecorded, UNRECORDED
79
+ attribute :running, RUNNING
80
+ end
81
+
82
+ # What a compile gave, read back for Ruby to apply to a copy of the student.
83
+ class Compiled < Shape
84
+ attribute :id, Types::Id
85
+ attribute :kind, kind_type("compiled")
86
+ attribute :state, ProgramState
87
+
88
+ # GEPA's detailed results where track_stats asks, DSPy's to_dict as JSON values (gepa/gepa.py:142-168).
89
+ attribute? :results, Types::Values
90
+ attribute :unrecorded, UNRECORDED
91
+ attribute :running, RUNNING
92
+ end
93
+
94
+ # A program's state written.
95
+ class StateSaved < Shape
96
+ attribute :id, Types::Id
97
+ attribute :kind, kind_type("state_saved")
98
+ end
99
+
100
+ # A program's state loaded, read back for Ruby to apply.
101
+ class StateLoaded < Shape
102
+ attribute :id, Types::Id
103
+ attribute :kind, kind_type("state_loaded")
104
+ attribute :state, ProgramState
105
+ end
106
+
107
+ # One split of a loaded dataset: its Examples, or the error DSPy raised as it was read.
108
+ class Split < Shape
109
+ attribute :name, Types::Name
110
+ attribute? :examples, Types::Strict::Array.of(Example)
111
+ attribute? :error, Failure
112
+ end
113
+
114
+ # What a load gave: a dataset's splits, or the one list of Examples DSPy gave.
115
+ class Loaded < Shape
116
+ attribute :id, Types::Id
117
+ attribute :kind, kind_type("loaded")
118
+ attribute? :splits, Types::Strict::Array.of(Split)
119
+ attribute? :examples, Types::Strict::Array.of(Example)
120
+ attribute :unrecorded, UNRECORDED
121
+ attribute :running, RUNNING
122
+ end
123
+
124
+ # The rows' indices in Python's order: one list, or a split's train and test.
125
+ class Permuted < Shape
126
+ attribute :id, Types::Id
127
+ attribute :kind, kind_type("permuted")
128
+ attribute :parts, Types::Strict::Array.of(Types::Strict::Array.of(Types::Count))
129
+ end
130
+
131
+ # A request that raised.
132
+ class Failed < Shape
133
+ attribute :id, Types::Id
134
+ attribute :kind, kind_type("failed")
135
+ attribute :error, Failure
136
+ attribute :unrecorded, UNRECORDED
137
+ attribute :running, RUNNING
138
+ end
139
+
140
+ # A request cancelled, by Ruby or because the request whose callback it serves was: nothing more starts for it.
141
+ class Cancelled < Shape
142
+ attribute :id, Types::Id
143
+ attribute :kind, kind_type("cancelled")
144
+ attribute :unrecorded, UNRECORDED
145
+ attribute :running, RUNNING
146
+ end
147
+
148
+ # A compile or a load that gave state a Ruby program cannot hold: what did not fit.
149
+ class Unfit < Shape
150
+ attribute :id, Types::Id
151
+ attribute :kind, kind_type("unfit")
152
+ attribute :reason, Types::Text
153
+ attribute :unrecorded, UNRECORDED
154
+ attribute :running, RUNNING
155
+ end
156
+
157
+ # A request Python would not run.
158
+ class Refused < Shape
159
+ # Absent when the line could not be read as a request.
160
+ attribute? :id, Types::Id
161
+ attribute :kind, kind_type("refused")
162
+ attribute :reason, Types::Text
163
+ end
164
+
165
+ REPLIES = [
166
+ Welcome,
167
+ Described,
168
+ ToolDescribed,
169
+ LMDescribed,
170
+ Completed,
171
+ Evaluated,
172
+ Measured,
173
+ Generated,
174
+ Compiled,
175
+ StateSaved,
176
+ StateLoaded,
177
+ Loaded,
178
+ Permuted,
179
+ Failed,
180
+ Cancelled,
181
+ Unfit,
182
+ Refused
183
+ ].freeze
184
+
185
+ # The replies that carry their request's counts.
186
+ COUNTED = REPLIES.select { _1.attribute_names.include?(:running) }.freeze
187
+ end
188
+ end