dspy 0.2.0 → 0.3.1

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.
@@ -1,218 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'sorbet-runtime'
4
-
5
- module DSPy
6
- class SorbetSignature
7
- extend T::Sig
8
-
9
- # Container for field type and description
10
- class FieldDescriptor
11
- extend T::Sig
12
-
13
- sig { returns(T.untyped) }
14
- attr_reader :type
15
-
16
- sig { returns(T.nilable(String)) }
17
- attr_reader :description
18
-
19
- sig { returns(T::Boolean) }
20
- attr_reader :has_default
21
-
22
- sig { params(type: T.untyped, description: T.nilable(String), has_default: T::Boolean).void }
23
- def initialize(type, description = nil, has_default = false)
24
- @type = type
25
- @description = description
26
- @has_default = has_default
27
- end
28
- end
29
-
30
- # DSL helper for building struct classes with field descriptions
31
- class StructBuilder
32
- extend T::Sig
33
-
34
- sig { returns(T::Hash[Symbol, FieldDescriptor]) }
35
- attr_reader :field_descriptors
36
-
37
- sig { void }
38
- def initialize
39
- @field_descriptors = {}
40
- end
41
-
42
- sig { params(name: Symbol, type: T.untyped, kwargs: T.untyped).void }
43
- def const(name, type, **kwargs)
44
- description = kwargs[:description]
45
- has_default = kwargs.key?(:default)
46
- @field_descriptors[name] = FieldDescriptor.new(type, description, has_default)
47
- # Store default for future use if needed
48
- end
49
-
50
- sig { returns(T.class_of(T::Struct)) }
51
- def build_struct_class
52
- descriptors = @field_descriptors
53
- Class.new(T::Struct) do
54
- extend T::Sig
55
- descriptors.each do |name, descriptor|
56
- const name, descriptor.type
57
- end
58
- end
59
- end
60
- end
61
-
62
- class << self
63
- extend T::Sig
64
-
65
- sig { returns(T.nilable(String)) }
66
- attr_reader :desc
67
-
68
- sig { returns(T.nilable(T.class_of(T::Struct))) }
69
- attr_reader :input_struct_class
70
-
71
- sig { returns(T.nilable(T.class_of(T::Struct))) }
72
- attr_reader :output_struct_class
73
-
74
- sig { returns(T::Hash[Symbol, FieldDescriptor]) }
75
- attr_reader :input_field_descriptors
76
-
77
- sig { returns(T::Hash[Symbol, FieldDescriptor]) }
78
- attr_reader :output_field_descriptors
79
-
80
- sig { params(desc: T.nilable(String)).returns(T.nilable(String)) }
81
- def description(desc = nil)
82
- if desc.nil?
83
- @desc
84
- else
85
- @desc = desc
86
- end
87
- end
88
-
89
- sig { params(block: T.proc.void).void }
90
- def input(&block)
91
- builder = StructBuilder.new
92
-
93
- if block.arity > 0
94
- block.call(builder)
95
- else
96
- # Preferred format
97
- builder.instance_eval(&block)
98
- end
99
-
100
- @input_field_descriptors = builder.field_descriptors
101
- @input_struct_class = builder.build_struct_class
102
- end
103
-
104
- sig { params(block: T.proc.void).void }
105
- def output(&block)
106
- builder = StructBuilder.new
107
-
108
- if block.arity > 0
109
- block.call(builder)
110
- else
111
- # Preferred format
112
- builder.instance_eval(&block)
113
- end
114
-
115
- @output_field_descriptors = builder.field_descriptors
116
- @output_struct_class = builder.build_struct_class
117
- end
118
-
119
- sig { returns(T::Hash[Symbol, T.untyped]) }
120
- def input_json_schema
121
- return {} unless @input_struct_class
122
-
123
- properties = {}
124
- required = []
125
-
126
- @input_field_descriptors&.each do |name, descriptor|
127
- schema = type_to_json_schema(descriptor.type)
128
- schema[:description] = descriptor.description if descriptor.description
129
- properties[name] = schema
130
- required << name.to_s unless descriptor.has_default
131
- end
132
-
133
- {
134
- "$schema": "http://json-schema.org/draft-06/schema#",
135
- type: "object",
136
- properties: properties,
137
- required: required
138
- }
139
- end
140
-
141
- sig { returns(T::Hash[Symbol, T.untyped]) }
142
- def output_json_schema
143
- return {} unless @output_struct_class
144
-
145
- properties = {}
146
- required = []
147
-
148
- @output_field_descriptors&.each do |name, descriptor|
149
- schema = type_to_json_schema(descriptor.type)
150
- schema[:description] = descriptor.description if descriptor.description
151
- properties[name] = schema
152
- required << name.to_s unless descriptor.has_default
153
- end
154
-
155
- {
156
- "$schema": "http://json-schema.org/draft-06/schema#",
157
- type: "object",
158
- properties: properties,
159
- required: required
160
- }
161
- end
162
-
163
- private
164
-
165
- sig { params(type: T.untyped).returns(T::Hash[Symbol, T.untyped]) }
166
- def type_to_json_schema(type)
167
- # Handle raw class types first
168
- if type.is_a?(Class)
169
- if type < T::Enum
170
- # Get all enum values
171
- values = type.values.map(&:serialize)
172
- { type: "string", enum: values }
173
- elsif type == String
174
- { type: "string" }
175
- elsif type == Integer
176
- { type: "integer" }
177
- elsif type == Float
178
- { type: "number" }
179
- elsif [TrueClass, FalseClass].include?(type)
180
- { type: "boolean" }
181
- else
182
- { type: "string" } # Default fallback
183
- end
184
- elsif type.is_a?(T::Types::Simple)
185
- case type.raw_type.to_s
186
- when "String"
187
- { type: "string" }
188
- when "Integer"
189
- { type: "integer" }
190
- when "Float"
191
- { type: "number" }
192
- when "TrueClass", "FalseClass"
193
- { type: "boolean" }
194
- else
195
- # Check if it's an enum
196
- if type.raw_type < T::Enum
197
- # Get all enum values
198
- values = type.raw_type.values.map(&:serialize)
199
- { type: "string", enum: values }
200
- else
201
- { type: "string" } # Default fallback
202
- end
203
- end
204
- elsif type.is_a?(T::Types::Union)
205
- # For optional types (T.nilable), just use the non-nil type
206
- non_nil_types = type.types.reject { |t| t == T::Utils.coerce(NilClass) }
207
- if non_nil_types.size == 1
208
- type_to_json_schema(non_nil_types.first)
209
- else
210
- { type: "string" } # Fallback for complex unions
211
- end
212
- else
213
- { type: "string" } # Default fallback
214
- end
215
- end
216
- end
217
- end
218
- end
data/lib/dspy/types.rb DELETED
@@ -1,3 +0,0 @@
1
- module Types
2
- include Dry.Types()
3
- end