Class: DSPy::Signature

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/signature.rb

Defined Under Namespace

Classes: FieldDescriptor, StructBuilder

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.descObject (readonly)

Returns the value of attribute desc.



90
91
92
# File 'lib/dspy/signature.rb', line 90

def desc
  @desc
end

.input_field_descriptorsObject (readonly)

Returns the value of attribute input_field_descriptors.



99
100
101
# File 'lib/dspy/signature.rb', line 99

def input_field_descriptors
  @input_field_descriptors
end

.input_struct_classObject (readonly)

Returns the value of attribute input_struct_class.



93
94
95
# File 'lib/dspy/signature.rb', line 93

def input_struct_class
  @input_struct_class
end

.output_field_descriptorsObject (readonly)

Returns the value of attribute output_field_descriptors.



102
103
104
# File 'lib/dspy/signature.rb', line 102

def output_field_descriptors
  @output_field_descriptors
end

.output_struct_classObject (readonly)

Returns the value of attribute output_struct_class.



96
97
98
# File 'lib/dspy/signature.rb', line 96

def output_struct_class
  @output_struct_class
end

Class Method Details

.constructor_properties(args, kwargs) ⇒ Object

Raises:

  • (ArgumentError)


105
106
107
108
109
110
# File 'lib/dspy/signature.rb', line 105

def constructor_properties(args, kwargs)
  return kwargs if args.empty?
  return args.first if args.length == 1 && args.first.is_a?(Hash) && kwargs.empty?

  raise ArgumentError, "Expected properties as keywords or one positional hash, not both"
end

.description(desc = nil) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/dspy/signature.rb', line 125

def description(desc = nil)
  if desc.nil?
    @desc
  else
    @desc = desc
  end
end

.input(&block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dspy/signature.rb', line 134

def input(&block)
  builder = StructBuilder.new

  if block.arity > 0
    block.call(builder)
  else
    # Preferred format
    builder.instance_eval(&block)
  end

  @input_field_descriptors = builder.field_descriptors
  @input_struct_class = builder.build_struct_class
end

.input_json_schemaObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dspy/signature.rb', line 164

def input_json_schema
  return {} unless @input_struct_class

  properties = {}
  required = []

  @input_field_descriptors&.each do |name, descriptor|
    schema = DSPy::TypeSystem::SorbetJsonSchema.type_to_json_schema(descriptor.type)
    schema[:description] = descriptor.description if descriptor.description
    properties[name] = schema
    required << name.to_s unless descriptor.has_default
  end

  {
    "$schema": "http://json-schema.org/draft-06/schema#",
    type: "object",
    properties: properties,
    required: required
  }
end

.input_schemaObject



186
187
188
# File 'lib/dspy/signature.rb', line 186

def input_schema
  @input_struct_class
end

.output(&block) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dspy/signature.rb', line 149

def output(&block)
  builder = StructBuilder.new

  if block.arity > 0
    block.call(builder)
  else
    # Preferred format
    builder.instance_eval(&block)
  end

  @output_field_descriptors = builder.field_descriptors
  @output_struct_class = builder.build_struct_class
end

.output_json_schemaObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dspy/signature.rb', line 191

def output_json_schema
  return {} unless @output_struct_class

  properties = {}
  required = []

  @output_field_descriptors&.each do |name, descriptor|
    schema = DSPy::TypeSystem::SorbetJsonSchema.type_to_json_schema(descriptor.type)
    schema[:description] = descriptor.description if descriptor.description
    properties[name] = schema
    required << name.to_s unless descriptor.has_default
  end

  {
    "$schema": "http://json-schema.org/draft-06/schema#",
    type: "object",
    properties: properties,
    required: required
  }
end

.output_json_schema_with_defsObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dspy/signature.rb', line 215

def output_json_schema_with_defs
  properties = {}
  required = []
  all_definitions = {}

  @output_field_descriptors&.each do |name, descriptor|
    result = DSPy::TypeSystem::SorbetJsonSchema.type_to_json_schema_with_defs(descriptor.type, nil, all_definitions)
    schema = result.schema
    schema[:description] = descriptor.description if descriptor.description
    properties[name] = schema
    required << name.to_s unless descriptor.has_default
  end

  final_schema = {
    "$schema": "http://json-schema.org/draft-06/schema#",
    type: "object",
    properties: properties,
    required: required
  }

  DSPy::TypeSystem::SorbetJsonSchema::SchemaResult.new(
    schema: final_schema,
    definitions: all_definitions
  )
end

.output_schemaObject



242
243
244
# File 'lib/dspy/signature.rb', line 242

def output_schema
  @output_struct_class
end

.validate_required_fields!(properties, descriptors) ⇒ Object

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
120
121
122
# File 'lib/dspy/signature.rb', line 113

def validate_required_fields!(properties, descriptors)
  required_fields = descriptors.reject { |_name, descriptor| descriptor.has_default }.keys
  missing_fields = required_fields.reject do |name|
    properties.key?(name) || properties.key?(name.to_s)
  end
  return if missing_fields.empty?

  names = missing_fields.map(&:inspect).join(', ')
  raise ArgumentError, "Missing required properties: #{names}"
end