class HybridPromptEnhancer
A utility class that enhances LLM prompts by adding instructions and formatting guidelines to encourage hybrid text+graphics responses with embedded graphic placeholders.
/tf/active/vicechatdev/e-ink-llm/hybrid_response_handler.py
204 - 300
simple
Purpose
This class provides functionality to augment base prompts with comprehensive instructions for generating hybrid responses that include both text and graphics. It analyzes content type and elements to provide context-specific guidance on when and how to include graphics (charts, diagrams, illustrations, sketches) using a placeholder format. The enhanced prompts guide LLMs to strategically incorporate visual elements that complement textual explanations, particularly useful for e-ink displays and educational content.
Source Code
class HybridPromptEnhancer:
"""Enhances LLM prompts to encourage hybrid text+graphics responses"""
@staticmethod
def enhance_prompt_for_hybrid_output(base_prompt: str, content_analysis: Dict[str, Any]) -> str:
"""
Enhance prompt to encourage hybrid text+graphics responses
Args:
base_prompt: Original prompt
content_analysis: Content analysis from LLM handler
Returns:
Enhanced prompt encouraging graphics generation
"""
# Graphics capability instruction
graphics_instruction = """
**HYBRID RESPONSE CAPABILITY:**
You can now include graphics in your responses! Use the following placeholder format to request graphics:
[GRAPHIC:type:description:parameters]
**Available Graphic Types:**
- chart: Data visualizations (bar, line, pie, scatter charts)
- diagram: Process flows, organizational charts, concept maps
- illustration: Educational diagrams, mathematical concepts, technical drawings
- sketch: Simple drawings, annotations, visual explanations
**Placeholder Format Examples:**
- [GRAPHIC:chart:Sales Comparison:{"type":"bar","data":[25,40,30,45],"labels":["Q1","Q2","Q3","Q4"],"title":"Quarterly Sales"}]
- [GRAPHIC:diagram:Process Flow:{"steps":["Input","Process","Output"],"style":"flowchart","direction":"horizontal"}]
- [GRAPHIC:illustration:Mathematical Concept:{"concept":"quadratic_function","style":"educational","annotations":true}]
**When to Include Graphics:**
- Data that would benefit from visualization
- Complex processes that need step-by-step diagrams
- Mathematical or scientific concepts
- Comparisons that work better visually
- Any content where a graphic would enhance understanding
**Graphics Integration Guidelines:**
1. Place graphic placeholders exactly where you want them in your text
2. Ensure graphics complement and enhance your written explanation
3. Provide clear, descriptive parameters for graphic generation
4. Use graphics strategically - not every response needs them
5. Consider the e-ink display limitations (high contrast, simple designs work best)
**Response Structure with Graphics:**
- Start with your text explanation
- Insert graphic placeholders at relevant points
- Continue your explanation referencing the graphics
- Ensure the response flows naturally even without the graphics
"""
# Content-specific graphics suggestions
content_type = content_analysis.get("content_type", "mixed")
elements = content_analysis.get("elements", [])
if "math" in elements:
graphics_instruction += """
**For Mathematical Content:**
- Use illustration graphics for mathematical concepts
- Include diagrams for geometric problems
- Create charts for data analysis or statistics
- Show step-by-step visual solutions where helpful
"""
if content_type == "diagram" or "diagrams" in elements:
graphics_instruction += """
**For Diagram Analysis:**
- Create enhanced versions of hand-drawn diagrams
- Add professional diagram representations
- Include process flow improvements
- Provide alternative visual perspectives
"""
if "question" in content_type.lower():
graphics_instruction += """
**For Questions Requiring Visual Answers:**
- Include relevant charts or graphs for data questions
- Create diagrams for process or concept questions
- Use illustrations for educational explanations
- Provide visual examples where they enhance understanding
"""
# Combine base prompt with graphics enhancement
enhanced_prompt = base_prompt + graphics_instruction + """
**Important:** Only include graphics when they genuinely enhance your response. A good text-only response is better than a response with unnecessary graphics. Focus on clarity and helpfulness above all else.
"""
return enhanced_prompt
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
bases: This class has no constructor parameters. It is designed as a utility class with static methods only, requiring no instantiation or initialization.
Return Value
The class itself returns a HybridPromptEnhancer type when instantiated (though instantiation is unnecessary). The enhance_prompt_for_hybrid_output method returns a string containing the original prompt augmented with detailed graphics capability instructions, placeholder format examples, content-specific suggestions, and integration guidelines.
Class Interface
Methods
enhance_prompt_for_hybrid_output(base_prompt: str, content_analysis: Dict[str, Any]) -> str
static
Purpose: Enhances a base prompt by appending comprehensive instructions for generating hybrid text+graphics responses, with content-specific suggestions based on the analysis
Parameters:
base_prompt: The original prompt string that will be augmented with graphics capability instructionscontent_analysis: Dictionary containing content analysis metadata with keys 'content_type' (str) and 'elements' (list) to customize graphics suggestions
Returns: Enhanced prompt string containing the original prompt plus detailed graphics instructions, placeholder format examples, content-specific suggestions, and integration guidelines
Dependencies
typing
Required Imports
from typing import Dict, Any
Usage Example
from typing import Dict, Any
# No instantiation needed - use static method directly
base_prompt = "Explain the water cycle to a student."
content_analysis = {
"content_type": "question",
"elements": ["science", "educational"]
}
# Call static method directly on class
enhanced_prompt = HybridPromptEnhancer.enhance_prompt_for_hybrid_output(
base_prompt=base_prompt,
content_analysis=content_analysis
)
# Use enhanced prompt with LLM
print(enhanced_prompt)
# Example with mathematical content
math_analysis = {
"content_type": "question",
"elements": ["math", "geometry"]
}
math_prompt = HybridPromptEnhancer.enhance_prompt_for_hybrid_output(
base_prompt="Solve this quadratic equation: x^2 + 5x + 6 = 0",
content_analysis=math_analysis
)
Best Practices
- Use the static method directly without instantiating the class (HybridPromptEnhancer.enhance_prompt_for_hybrid_output())
- Provide accurate content_analysis dictionaries with 'content_type' and 'elements' keys to get context-specific graphics suggestions
- The content_analysis should include 'content_type' (string) and 'elements' (list) to trigger appropriate graphics instructions
- Use 'math' in elements list to get mathematical graphics suggestions
- Use 'diagram' as content_type or 'diagrams' in elements to get diagram-specific instructions
- Include 'question' in content_type to get question-answering graphics guidance
- The enhanced prompt is designed for e-ink displays, so it emphasizes high contrast and simple designs
- The method is stateless and thread-safe, making it suitable for concurrent use
- The returned prompt includes placeholder format [GRAPHIC:type:description:parameters] that downstream systems should parse
- Content analysis can be minimal (empty dict) and the method will still provide base graphics instructions
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class HybridResponseHandler 71.8% similar
-
function demo_hybrid_response 68.8% similar
-
class HybridResponse 66.1% similar
-
function demo_placeholder_parsing 63.0% similar
-
function main_v59 62.9% similar