Class: DSPy::LM::Strategies::EnhancedPromptingStrategy

Inherits:
BaseStrategy
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/lm/strategies/enhanced_prompting_strategy.rb

Overview

Enhanced prompting strategy that works with any LLM Adds explicit JSON formatting instructions to improve reliability

Instance Method Summary collapse

Methods inherited from BaseStrategy

#handle_error, #initialize

Constructor Details

This class inherits a constructor from DSPy::LM::Strategies::BaseStrategy

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/dspy/lm/strategies/enhanced_prompting_strategy.rb', line 14

def available?
  # This strategy is always available as a fallback
  true
end

#extract_json(response) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dspy/lm/strategies/enhanced_prompting_strategy.rb', line 56

def extract_json(response)
  return nil if response.content.nil?

  content = response.content.strip

  # Try multiple extraction patterns
  # 1. Check for markdown code blocks
  if content.include?('```json')
    json_content = content.split('```json').last.split('```').first.strip
    return json_content if valid_json?(json_content)
  elsif content.include?('```')
    code_block = content.split('```')[1]
    if code_block
      json_content = code_block.strip
      return json_content if valid_json?(json_content)
    end
  end

  # 2. Check if the entire response is JSON
  return content if valid_json?(content)

  # 3. Look for JSON-like structures in the content
  json_match = content.match(/\{[\s\S]*\}|\[[\s\S]*\]/)
  if json_match
    json_content = json_match[0]
    return json_content if valid_json?(json_content)
  end

  nil
end

#nameObject



25
26
27
# File 'lib/dspy/lm/strategies/enhanced_prompting_strategy.rb', line 25

def name
  "enhanced_prompting"
end

#prepare_request(messages, request_params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dspy/lm/strategies/enhanced_prompting_strategy.rb', line 30

def prepare_request(messages, request_params)
  # Enhance the user message with explicit JSON instructions
  return if messages.empty?

  # Get the output schema
  output_schema = signature_class.output_json_schema
  
  # Find the last user message
  last_user_idx = messages.rindex { |msg| msg[:role] == "user" }
  return unless last_user_idx

  # Add JSON formatting instructions
  original_content = messages[last_user_idx][:content]
  enhanced_content = enhance_prompt_with_json_instructions(original_content, output_schema)
  messages[last_user_idx][:content] = enhanced_content

  # Add system instructions if no system message exists
  if messages.none? { |msg| msg[:role] == "system" }
    messages.unshift({
      role: "system",
      content: "You are a helpful assistant that always responds with valid JSON when requested."
    })
  end
end

#priorityObject



20
21
22
# File 'lib/dspy/lm/strategies/enhanced_prompting_strategy.rb', line 20

def priority
  50 # Medium priority - use when native methods aren't available
end