Class: DSPy::Prompt

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

Direct Known Subclasses

StructuredOutputsPrompt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instruction:, input_schema:, output_schema:, few_shot_examples: [], signature_class_name: nil, schema_format: nil, signature_class: nil, data_format: nil) ⇒ Prompt

Returns a new instance of Prompt.



55
56
57
58
59
60
61
62
63
64
# File 'lib/dspy/prompt.rb', line 55

def initialize(instruction:, input_schema:, output_schema:, few_shot_examples: [], signature_class_name: nil, schema_format: nil, signature_class: nil, data_format: nil)
  @instruction = instruction
  @few_shot_examples = few_shot_examples.freeze
  @input_schema = input_schema.freeze
  @output_schema = output_schema.freeze
  @signature_class_name = signature_class_name
  @schema_format = resolve_schema_format(schema_format)
  @signature_class = signature_class
  @data_format = resolve_data_format(data_format)
end

Instance Attribute Details

#few_shot_examplesObject (readonly)

Returns the value of attribute few_shot_examples.



19
20
21
# File 'lib/dspy/prompt.rb', line 19

def few_shot_examples
  @few_shot_examples
end

#input_schemaObject (readonly)

Returns the value of attribute input_schema.



22
23
24
# File 'lib/dspy/prompt.rb', line 22

def input_schema
  @input_schema
end

#instructionObject (readonly)

Returns the value of attribute instruction.



16
17
18
# File 'lib/dspy/prompt.rb', line 16

def instruction
  @instruction
end

#output_schemaObject (readonly)

Returns the value of attribute output_schema.



25
26
27
# File 'lib/dspy/prompt.rb', line 25

def output_schema
  @output_schema
end

#signature_classObject (readonly)

Returns the value of attribute signature_class.



41
42
43
# File 'lib/dspy/prompt.rb', line 41

def signature_class
  @signature_class
end

#signature_class_nameObject (readonly)

Returns the value of attribute signature_class_name.



28
29
30
# File 'lib/dspy/prompt.rb', line 28

def signature_class_name
  @signature_class_name
end

Class Method Details

.from_h(hash) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/dspy/prompt.rb', line 270

def self.from_h(hash)
  examples = (hash[:few_shot_examples] || []).map { |ex| FewShotExample.from_h(ex) }

  new(
    instruction: hash[:instruction] || "",
    input_schema: hash[:input_schema] || {},
    output_schema: hash[:output_schema] || {},
    few_shot_examples: examples,
    signature_class_name: hash[:signature_class_name],
    schema_format: hash[:schema_format] || :json,
    data_format: hash[:data_format] || :json
  )
end

.from_signature(signature_class, schema_format: nil, data_format: nil) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/dspy/prompt.rb', line 292

def self.from_signature(signature_class, schema_format: nil, data_format: nil)
  new(
    instruction: signature_class.description || "Complete this task.",
    input_schema: signature_class.input_json_schema,
    output_schema: signature_class.output_json_schema,
    few_shot_examples: [],
    signature_class_name: signature_class.name,
    schema_format: schema_format,
    signature_class: signature_class,
    data_format: data_format
  )
end

Instance Method Details

#==(other) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/dspy/prompt.rb', line 307

def ==(other)
  return false unless other.is_a?(Prompt)
  
  @instruction == other.instruction &&
    @few_shot_examples == other.few_shot_examples &&
    @input_schema == other.input_schema &&
    @output_schema == other.output_schema
end

#add_examples(new_examples) ⇒ Object



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

def add_examples(new_examples)
  combined_examples = @few_shot_examples + new_examples
  with_examples(combined_examples)
end

#data_formatObject



36
37
38
# File 'lib/dspy/prompt.rb', line 36

def data_format
  @data_format || :json
end

#diff(other) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/dspy/prompt.rb', line 317

def diff(other)
  changes = {}
  
  changes[:instruction] = {
    from: @instruction,
    to: other.instruction
  } if @instruction != other.instruction
  
  changes[:few_shot_examples] = {
    from: @few_shot_examples.length,
    to: other.few_shot_examples.length,
    added: other.few_shot_examples - @few_shot_examples,
    removed: @few_shot_examples - other.few_shot_examples
  } if @few_shot_examples != other.few_shot_examples
  
  changes
end

#render_system_promptObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dspy/prompt.rb', line 131

def render_system_prompt
  sections = []

  case schema_format
  when :baml
    sections << "Your input schema fields are:"
    sections << "```baml"
    sections << render_baml_schema(@input_schema, :input)
    sections << "```"

    sections << "Your output schema fields are:"
    sections << "```baml"
    sections << render_baml_schema(@output_schema, :output)
    sections << "```"
  when :toon
    sections << "Your input schema fields (TOON order) are:"
    sections << Sorbet::Toon::SignatureFormatter.describe_signature(@signature_class, :input)
    sections << ""
    sections << "Your output schema fields (TOON order) are:"
    sections << Sorbet::Toon::SignatureFormatter.describe_signature(@signature_class, :output)
  else
    sections << "Your input schema fields are:"
    sections << "```json"
    sections << JSON.pretty_generate(@input_schema)
    sections << "```"

    sections << "Your output schema fields are:"
    sections << "```json"
    sections << JSON.pretty_generate(@output_schema)
    sections << "```"
  end

  sections << ""
  sections << "All interactions will be structured in the following way, with the appropriate values filled in."

  if @few_shot_examples.any?
    sections << ""
    sections << "Here are some examples:"
    sections << ""
    @few_shot_examples.each_with_index do |example, index|
      sections << "### Example #{index + 1}"
      sections << example.to_prompt_section
      sections << ""
    end
  end

  if toon_data_format_enabled?
    sections << "## TOON data format instructions"
    sections << "All input and output payloads must use Token-Oriented Object Notation (TOON). Do not return JSON, YAML, or prose."
    sections << ""
    sections << "## Input values"
    sections << "Copy the TOON block below and replace the placeholder values with the correct inputs."
    sections << "```toon"
    sections << "{input_values}"
    sections << "```"

    if (example_input = example_toon_payload(:input))
      sections << ""
      sections << "### Example TOON input"
      sections << "```toon"
      sections << example_input
      sections << "```"
    end

    sections << ""
    sections << "## Output values"
    sections << "Respond exclusively with a ```toon``` block that lists the output fields in the exact order shown in the schema."
    sections << "```toon"
    sections << "{output_values}"
    sections << "```"

    if (example_output = example_toon_payload(:output))
      sections << ""
      sections << "### Example TOON output"
      sections << "```toon"
      sections << example_output
      sections << "```"
    end
  else
    sections << "## Input values"
    sections << "```json"
    sections << "{input_values}"
    sections << "```"

    sections << "## Output values"
    sections << "Respond exclusively with the output schema fields in the json block below."
    sections << "```json"
    sections << "{output_values}"
    sections << "```"
  end

  sections << ""
  sections << "In adhering to this structure, your objective is: #{@instruction}"

  sections.join("\n")
end

#render_user_prompt(input_values) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/dspy/prompt.rb', line 229

def render_user_prompt(input_values)
  sections = []

  if toon_data_format_enabled?
    toon_payload = DSPy::Schema::SorbetToonAdapter.render_input(@signature_class, input_values)

    sections << "## Input Values"
    sections << "Use the TOON block below as-is; do not convert it to JSON."
    sections << "```toon"
    sections << toon_payload
    sections << "```"
    sections << ""
    sections << "Respond with the corresponding output schema fields encoded as TOON inside a ```toon``` block starting with the heading `## Output values`. Do not include any JSON."
  else
    sections << "## Input Values"
    sections << "```json"
    sections << JSON.pretty_generate(DSPy::Utils::Serialization.deep_serialize(input_values))
    sections << "```"
    sections << ""
    sections << "Respond with the corresponding output schema fields wrapped in a ```json ``` block,"
    sections << "starting with the heading `## Output values`."
  end

  sections.join("\n")
end

#schema_formatObject



31
32
33
# File 'lib/dspy/prompt.rb', line 31

def schema_format
  @schema_format || :json
end

#statsObject



337
338
339
340
341
342
343
344
345
# File 'lib/dspy/prompt.rb', line 337

def stats
  {
    character_count: @instruction.length,
    example_count: @few_shot_examples.length,
    total_example_chars: @few_shot_examples.sum { |ex| ex.to_prompt_section.length },
    input_fields: @input_schema.dig(:properties)&.keys&.length || 0,
    output_fields: @output_schema.dig(:properties)&.keys&.length || 0
  }
end

#to_hObject



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/dspy/prompt.rb', line 257

def to_h
  {
    instruction: @instruction,
    few_shot_examples: @few_shot_examples.map(&:to_h),
    input_schema: @input_schema,
    output_schema: @output_schema,
    signature_class_name: @signature_class_name,
    schema_format: @schema_format,
    data_format: @data_format
  }
end

#with_data_format(new_data_format) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dspy/prompt.rb', line 116

def with_data_format(new_data_format)
  self.class.new(
    instruction: @instruction,
    input_schema: @input_schema,
    output_schema: @output_schema,
    few_shot_examples: @few_shot_examples,
    signature_class_name: @signature_class_name,
    schema_format: @schema_format,
    signature_class: @signature_class,
    data_format: new_data_format
  )
end

#with_examples(new_examples) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dspy/prompt.rb', line 82

def with_examples(new_examples)
  self.class.new(
    instruction: @instruction,
    input_schema: @input_schema,
    output_schema: @output_schema,
    few_shot_examples: new_examples,
    signature_class_name: @signature_class_name,
    schema_format: @schema_format,
    signature_class: @signature_class,
    data_format: @data_format
  )
end

#with_instruction(new_instruction) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dspy/prompt.rb', line 68

def with_instruction(new_instruction)
  self.class.new(
    instruction: new_instruction,
    input_schema: @input_schema,
    output_schema: @output_schema,
    few_shot_examples: @few_shot_examples,
    signature_class_name: @signature_class_name,
    schema_format: @schema_format,
    signature_class: @signature_class,
    data_format: @data_format
  )
end

#with_schema_format(new_schema_format) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dspy/prompt.rb', line 102

def with_schema_format(new_schema_format)
  self.class.new(
    instruction: @instruction,
    input_schema: @input_schema,
    output_schema: @output_schema,
    few_shot_examples: @few_shot_examples,
    signature_class_name: @signature_class_name,
    schema_format: new_schema_format,
    signature_class: @signature_class,
    data_format: @data_format
  )
end