PromptSection
Represents a structured section of an attention-optimized prompt with priority and ordering metadata.
USAGE
PromptSection(
content,=Priority.MEDIUM,
priority='general',
section_type=0,
order_hint )
The PromptSection
class is a fundamental building block used by PromptBuilder
to create sophisticated, attention-optimized prompts. Each section encapsulates content along with metadata that controls how the section is positioned and prioritized within the final prompt. This enables precise control over attention flow and information hierarchy.
Integration with PromptBuilder:
While users can create PromptSection
objects directly, they are typically created automatically by PromptBuilder
methods. The sections are then assembled according to attention principles to create optimized final prompts. This design provides both high-level convenience through PromptBuilder
and fine-grained control through direct PromptSection
manipulation.
Attention Optimization:
Each section contributes to the overall attention strategy:
- Priority: determines relative importance and influences final ordering
- Section Type: enables grouping and specialized handling of content types
- Order Hint: provides fine-grained control over section positioning
- Content: the actual prompt text optimized for the section’s role
The combination of these attributes allows the prompt building system to create prompts that leverage attention mechanisms effectively, ensuring critical information receives appropriate model focus while maintaining natural conversation flow.
Parameters
content : str
-
The text content of the prompt section. This is the actual text that will appear in the final prompt. Content should be crafted to serve the section’s specific purpose within the overall prompt strategy.
priority : Priority = Priority.MEDIUM
-
Attention priority level determining section placement order and emphasis. Higher priority sections are typically placed in more prominent positions. Defaults to
Priority.MEDIUM
. section_type : str = 'general'
-
Type classification for the section enabling specialized handling and grouping. This allows the prompt builder to apply type-specific optimization strategies. Defaults to
"general"
. order_hint : int = 0
-
Fine-grained ordering hint where lower numbers appear earlier in the prompt. This provides precise control over section positioning beyond priority levels. Sections with the same priority are ordered by this value. Defaults to
0
.
Returns
PromptSection
-
A new prompt section with the specified content and metadata.
Priority Levels
The available priority levels are:
Priority.CRITICAL
: highest importance, placed prominentlyPriority.HIGH
: important content requiring strong attentionPriority.MEDIUM
: standard priority for general contentPriority.LOW
: supporting information, de-emphasized placementPriority.MINIMAL
: background context, least prominent placement
Section Types
Common section types include:
"persona"
: role and behavioral context"constraint"
: requirements and limitations"analysis"
: core analysis tasks and objectives"format"
: output formatting requirements"example"
: input/output examples and demonstrations"emphasis"
: final reinforcement and focus directives"general"
: general-purpose content
Section Lifecycle
Prompt sections typically follow this lifecycle within the prompt building process:
- Creation: sections are created with content and metadata
- Collection: multiple sections are gathered by the PromptBuilder
- Sorting: sections are ordered by priority and order_hint values
- Grouping: sections are grouped by type for specialized handling
- Assembly: final prompt is constructed from ordered sections
- Optimization: content is refined for attention and coherence
Design Principles
Attention Optimization: sections are designed to work together to guide model attention effectively, with priority and positioning controlling information hierarchy.
Modularity: each section encapsulates a specific aspect of the prompt, enabling reusable components and systematic prompt construction.
Flexibility: the section system supports both structured workflows through standard section types and custom applications through extensible metadata.
Composability: sections can be combined, reordered, and manipulated to create sophisticated prompt strategies for different use cases.
Cognitive Alignment: section design aligns with cognitive psychology principles like primacy/recency effects and information chunking for optimal comprehension.
Integration Notes
- Automatic Ordering: when used with
PromptBuilder
, sections are automatically ordered by priority and order_hint for optimal attention flow - Type-Based Processing: section types enable specialized handling and validation within the prompt building pipeline
- Content Optimization: section content should be crafted for clarity and specificity to maximize prompt effectiveness
- Memory Efficiency: sections are lightweight dataclasses suitable for large-scale prompt construction workflows
The PromptSection
class provides the foundation for systematic, attention-optimized prompt engineering, enabling both simple prompt construction and sophisticated multi-component prompt strategies.
Examples
Creating basic prompt sections
Create sections for different types of prompt content:
import talk_box as tb
# High-priority persona section
= tb.PromptSection(
persona_section ="You are a senior software architect with expertise in distributed systems.",
content=tb.Priority.CRITICAL,
priority="persona",
section_type=1
order_hint
)
# Critical constraint section
= tb.PromptSection(
constraint_section ="Focus only on scalability issues that impact performance.",
content=tb.Priority.CRITICAL,
priority="constraint",
section_type=2
order_hint
)
# Medium-priority analysis section
= tb.PromptSection(
analysis_section ="Analyze the system architecture for bottlenecks and optimization opportunities.",
content=tb.Priority.MEDIUM,
priority="analysis",
section_type=10
order_hint
)
print(f"Persona: {persona_section.content}")
print(f"Priority: {persona_section.priority}")
print(f"Type: {persona_section.section_type}")
Working with section priorities
Use priorities to control attention hierarchy:
# Create sections with different priorities
= [
sections
tb.PromptSection(="Secondary consideration: Check for code style consistency.",
content=tb.Priority.LOW,
priority="analysis"
section_type
),
tb.PromptSection(="CRITICAL: Identify security vulnerabilities immediately.",
content=tb.Priority.CRITICAL,
priority="constraint"
section_type
),
tb.PromptSection(="Important: Focus on performance bottlenecks.",
content=tb.Priority.HIGH,
priority="analysis"
section_type
),
tb.PromptSection(="Background context: This is a financial application.",
content=tb.Priority.MINIMAL,
priority="general"
section_type
) ]
Using section types for specialized handling
Organize content by type for targeted optimization:
# Create sections representing different prompt components
= [
prompt_sections
tb.PromptSection(="You are an expert code reviewer.",
content=tb.Priority.CRITICAL,
priority="persona"
section_type
),
tb.PromptSection(="Focus on security issues and performance problems.",
content=tb.Priority.HIGH,
priority="constraint"
section_type
),
tb.PromptSection(="Analyze the code for bugs, security flaws, and inefficiencies.",
content=tb.Priority.MEDIUM,
priority="analysis"
section_type
),
tb.PromptSection(="Format: List critical issues first, then suggestions.",
content=tb.Priority.MEDIUM,
priority="format"
section_type
),
tb.PromptSection(="Example: 'CRITICAL: SQL injection vulnerability on line 42'",
content=tb.Priority.LOW,
priority="example"
section_type
) ]
Fine-grained ordering with order_hint
Use order_hint for precise section positioning:
# Create sections with same priority but different order hints
= [
setup_sections
tb.PromptSection(="You are a helpful assistant.",
content=tb.Priority.HIGH,
priority="persona",
section_type=1 # First
order_hint
),
tb.PromptSection(="You specialize in Python programming.",
content=tb.Priority.HIGH,
priority="persona",
section_type=2 # Second
order_hint
),
tb.PromptSection(="You focus on writing clean, efficient code.",
content=Priority.HIGH,
priority="persona",
section_type=3 # Third
order_hint
) ]
Building sections for different prompt strategies
Create sections optimized for specific attention patterns:
# Front-loading critical information (primacy bias)
= tb.PromptSection(
critical_first ="IMMEDIATE PRIORITY: Check for buffer overflow vulnerabilities.",
content=tb.Priority.CRITICAL,
priority="constraint",
section_type=1
order_hint
)
# Core task definition
= tb.PromptSection(
main_task ="Review this C++ code for security issues and memory management problems.",
content=tb.Priority.HIGH,
priority="analysis",
section_type=10
order_hint
)
# Final emphasis (recency bias)
= tb.PromptSection(
final_emphasis ="Remember: Security vulnerabilities are the highest priority.",
content=Priority.HIGH,
priority="emphasis",
section_type=100
order_hint )
Integration with PromptBuilder workflow
See how sections work within the larger prompt building process:
# Create a prompt builder
= tb.PromptBuilder()
builder
# Builder methods create PromptSection objects internally
"senior developer", "code review")
builder.persona("Focus on security vulnerabilities")
builder.critical_constraint("Memory management", "Input validation", "Error handling"])
builder.core_analysis([
# You can also add custom sections directly
= tb.PromptSection(
custom_section ="Pay special attention to authentication mechanisms.",
content=tb.Priority.HIGH,
priority="constraint",
section_type=5
order_hint
)
# Add custom section to builder's internal collection
builder.sections.append(custom_section)
# Build final prompt (sections are automatically ordered and assembled)
= builder
final_prompt print("Final assembled prompt:")
print(final_prompt)
Advanced section manipulation
Perform sophisticated operations on section collections:
# Create a collection of mixed sections
= [
mixed_sections "Core task", tb.Priority.HIGH, "analysis", 1),
tb.PromptSection("Important constraint", tb.Priority.HIGH, "constraint", 2),
tb.PromptSection("Background info", tb.Priority.LOW, "general", 3),
tb.PromptSection("Critical requirement", tb.Priority.CRITICAL, "constraint", 0),
tb.PromptSection("Output format", tb.Priority.MEDIUM, "format", 4)
tb.PromptSection(
]
# Filter high-priority sections
= [s for s in mixed_sections if s.priority.value >= tb.Priority.HIGH.value]
high_priority
# Find all constraint sections
= [s for s in mixed_sections if s.section_type == "constraint"]
constraints
# Get earliest section by order_hint
= min(mixed_sections, key=lambda s: s.order_hint)
earliest
# Calculate total content length
= sum(len(s.content) for s in mixed_sections)
total_length
print(f"High priority sections: {len(high_priority)}")
print(f"Constraint sections: {len(constraints)}")
print(f"Earliest section: {earliest.content}")
print(f"Total content length: {total_length} characters")
Custom section types for specialized workflows
Define custom section types for specific applications:
# Custom section types for code review workflow
= [
code_review_sections
tb.PromptSection(="You are a senior code reviewer with 10+ years experience.",
content=tb.Priority.CRITICAL,
priority="reviewer_persona"
section_type
),
tb.PromptSection(="This code will be deployed to production systems.",
content=tb.Priority.HIGH,
priority="deployment_context"
section_type
),
tb.PromptSection(="Check: Security, Performance, Maintainability, Testing",
content=tb.Priority.HIGH,
priority="review_checklist"
section_type
),
tb.PromptSection(="Format: Critical issues first, then improvements, then praise",
content=tb.Priority.MEDIUM,
priority="response_structure"
section_type
),
tb.PromptSection(="Remember: Constructive feedback builds better developers.",
content=tb.Priority.MEDIUM,
priority="review_philosophy"
section_type
)
]
# Process sections by custom type
= {
workflow_map "reviewer_persona": "Sets reviewer identity and expertise",
"deployment_context": "Provides operational context",
"review_checklist": "Defines evaluation criteria",
"response_structure": "Controls output organization",
"review_philosophy": "Guides feedback tone and approach"
}
print("Code review workflow sections:")
for section in code_review_sections:
= workflow_map.get(section.section_type, "General purpose")
purpose print(f"• {section.section_type}: {purpose}")
print(f" Content: {section.content}")
print()