Class: DSPy::StructuredOutputsPrompt

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

Overview

Optimized prompt for structured outputs that omits redundant schema information since the schema is already enforced by API parameters (response_format, generation_config, tools)

Instance Attribute Summary

Attributes inherited from Prompt

#few_shot_examples, #input_schema, #instruction, #output_schema, #signature_class, #signature_class_name

Instance Method Summary collapse

Methods inherited from Prompt

#==, #add_examples, #data_format, #diff, from_h, from_signature, #schema_format, #stats, #to_h, #to_messages, #with_examples, #with_instruction

Constructor Details

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

Returns a new instance of StructuredOutputsPrompt.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dspy/structured_outputs_prompt.rb', line 24

def initialize(instruction:, input_schema:, output_schema:, few_shot_examples: [], signature_class_name: nil, schema_format: :json, signature_class: nil, data_format: :json)
  normalized_examples = few_shot_examples.map do |example|
    case example
    when FewShotExample
      example
    when Hash
      FewShotExample.from_h(symbolize_keys(example))
    else
      raise ArgumentError, "Unsupported few-shot example type: #{example.class}"
    end
  end

  super(
    instruction: instruction,
    input_schema: input_schema,
    output_schema: output_schema,
    few_shot_examples: normalized_examples,
    signature_class_name: signature_class_name,
    schema_format: schema_format,
    signature_class: signature_class,
    data_format: data_format
  )
end

Instance Method Details

#render_system_promptObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dspy/structured_outputs_prompt.rb', line 50

def render_system_prompt
  sections = []

  sections << "Your input schema fields are:"
  sections << "```json"
  sections << JSON.pretty_generate(@input_schema)
  sections << "```"

  # Add few-shot examples if present
  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

  sections << ""
  sections << "Your objective is: #{@instruction}"

  sections.join("\n")
end

#render_user_prompt(input_values) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/dspy/structured_outputs_prompt.rb', line 78

def render_user_prompt(input_values)
  sections = []

  sections << "## Input Values"
  sections << "```json"
  sections << JSON.pretty_generate(serialize_for_json(input_values))
  sections << "```"

  sections.join("\n")
end