šŸ” Code Extractor

function demo_placeholder_parsing

Maturity: 44

Demonstrates the parsing of graphics placeholders embedded in text by extracting and displaying placeholder metadata including type, description, ID, and parameters.

File:
/tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
Lines:
176 - 205
Complexity:
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

  • json
  • hybrid_response_handler
  • graphics_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

Optional
from graphics_generator import GraphicSpec

Condition: imported in source file but not directly used in this function

Optional
from graphics_generator import GraphicType

Condition: imported in source file but not directly used in this function

Optional

Usage 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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_placeholder_parsing 84.5% similar

    A unit test function that validates the placeholder parsing functionality of the HybridResponseHandler class by testing its ability to extract and parse graphic placeholders from formatted text.

    From: /tf/active/vicechatdev/e-ink-llm/test_hybrid_mode.py
  • function demo_hybrid_response 71.3% similar

    Demonstrates end-to-end hybrid response processing by converting an LLM response containing text and graphics placeholders into a formatted PDF document.

    From: /tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
  • class GraphicPlaceholder 68.5% similar

    A dataclass that represents a placeholder for graphics (charts, diagrams, etc.) embedded within text responses, storing metadata about the graphic's type, description, parameters, and position.

    From: /tf/active/vicechatdev/e-ink-llm/hybrid_response_handler.py
  • class HybridPromptEnhancer 63.0% similar

    A utility class that enhances LLM prompts by adding instructions and formatting guidelines to encourage hybrid text+graphics responses with embedded graphic placeholders.

    From: /tf/active/vicechatdev/e-ink-llm/hybrid_response_handler.py
  • class HybridResponse 61.4% similar

    A dataclass that encapsulates a complete hybrid response containing both text content and graphical elements with their placeholders and metadata.

    From: /tf/active/vicechatdev/e-ink-llm/hybrid_response_handler.py
← Back to Browse