function demo_placeholder_parsing
Demonstrates the parsing of graphics placeholders embedded in text by extracting and displaying placeholder metadata including type, description, ID, and parameters.
/tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
176 - 205
simple
Purpose
This function serves as a demonstration/testing utility to showcase how the HybridResponseHandler parses specially formatted graphics placeholders from text. It processes a sample text containing three different types of graphics placeholders (chart, diagram, illustration) and displays detailed information about each parsed placeholder. This is useful for understanding the placeholder format, testing the parsing functionality, and demonstrating the hybrid response system's capabilities.
Source Code
def demo_placeholder_parsing():
"""Demonstrate placeholder parsing"""
print("\nš Placeholder Parsing Demo")
print("=" * 50)
sample_text = """
Here are some examples of graphics placeholders:
1. Simple bar chart:
[GRAPHIC:chart:Sales Data:{"type":"bar","data":[10,20,30],"labels":["A","B","C"]}]
2. Process flowchart:
[GRAPHIC:diagram:Workflow:{"steps":["Start","Process","End"],"style":"flowchart"}]
3. Mathematical concept:
[GRAPHIC:illustration:Derivatives:{"concept":"derivatives","style":"educational"}]
"""
from hybrid_response_handler import HybridResponseHandler
handler = HybridResponseHandler(api_key="demo")
hybrid_response = handler._parse_hybrid_response(sample_text, {})
print(f"š Original text length: {len(sample_text):,} characters")
print(f"šØ Graphics placeholders found: {len(hybrid_response.placeholders)}")
for i, placeholder in enumerate(hybrid_response.placeholders, 1):
print(f"\n {i}. {placeholder.graphic_type.upper()}: {placeholder.description}")
print(f" ⢠ID: {placeholder.id}")
print(f" ⢠Parameters: {json.dumps(placeholder.parameters, indent=8)}")
Return Value
This function does not return any value (implicitly returns None). It produces console output showing: the original text length, the number of graphics placeholders found, and detailed information about each placeholder including its type, description, ID, and JSON-formatted parameters.
Dependencies
jsonhybrid_response_handlergraphics_generator
Required Imports
import json
from hybrid_response_handler import HybridResponseHandler
Conditional/Optional Imports
These imports are only needed under specific conditions:
from graphics_generator import GraphicsGenerator
Condition: imported in source file but not directly used in this function
Optionalfrom graphics_generator import GraphicSpec
Condition: imported in source file but not directly used in this function
Optionalfrom graphics_generator import GraphicType
Condition: imported in source file but not directly used in this function
OptionalUsage Example
# Simple usage - just call the function
demo_placeholder_parsing()
# Expected output:
# š Placeholder Parsing Demo
# ==================================================
# š Original text length: XXX characters
# šØ Graphics placeholders found: 3
# 1. CHART: Sales Data
# ⢠ID: <generated-id>
# ⢠Parameters: {"type":"bar","data":[10,20,30],"labels":["A","B","C"]}
# 2. DIAGRAM: Workflow
# ⢠ID: <generated-id>
# ⢠Parameters: {"steps":["Start","Process","End"],"style":"flowchart"}
# 3. ILLUSTRATION: Derivatives
# ⢠ID: <generated-id>
# ⢠Parameters: {"concept":"derivatives","style":"educational"}
Best Practices
- This is a demonstration function intended for testing and showcasing functionality, not for production use
- The function uses a dummy API key ('demo') which may not work with actual API calls
- The placeholder format follows the pattern: [GRAPHIC:type:description:{json_parameters}]
- Ensure the HybridResponseHandler and related modules are properly installed before running
- This function is useful for understanding the expected format of graphics placeholders before implementing them in actual content
- The function demonstrates three common graphic types: chart, diagram, and illustration
- Use this as a reference for creating properly formatted graphics placeholders in your own text
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_placeholder_parsing 84.5% similar
-
function demo_hybrid_response 71.3% similar
-
class GraphicPlaceholder 68.5% similar
-
class HybridPromptEnhancer 63.0% similar
-
class HybridResponse 61.4% similar