import talk_box as tb
= (
prompt
tb.PromptBuilder()"data scientist", "machine learning")
.persona("analyze customer churn patterns")
.task_context("identifying the top 3 risk factors")
.focus_on(
)
PromptBuilderclass
Builds structured prompts using attention mechanisms and cognitive principles.
USAGE
PromptBuilder()
The PromptBuilder
leverages insights from modern prompt engineering research to create prompts that maximize model attention on critical information while maintaining natural conversation flow.
Returns
PromptBuilder
-
A new instance ready for fluent method chaining
Notes
The PromptBuilder
applies proven principles that enhance model performance and response quality through strategic information placement and cognitive load management.
Attention Mechanisms Applied:
- Positional encoding: critical information placed strategically
- Multi-head attention: different types of context handled separately
- Hierarchical structure: information organized by importance and relevance
- Context windowing: optimal information density for model processing
Cognitive Psychology Integration:
- Primacy effect: important instructions placed early
- Recency effect: final emphasis reinforces key objectives
- Chunking: information grouped into digestible, logical units
- Salience: critical constraints highlighted for maximum attention
Prompt Building Methods
The PromptBuilder
provides a comprehensive set of methods for creating structured, attention-optimized prompts. All methods support fluent chaining for natural prompt construction.
The core foundation methods:
persona(role, expertise=None)
: set the AI’s identity and behavioral frameworktask_context(context, priority=CRITICAL)
: define the primary objective and scopecritical_constraint(constraint)
: add front-loaded, non-negotiable requirementsconstraint(constraint)
: add important but secondary requirements
The structure and analysis methods:
structured_section(title, content, priority=MEDIUM, required=False)
: create organized content sectionscore_analysis(analysis_points)
: define required analytical focus areasoutput_format(format_specs)
: specify response structure and formatting requirementsexample(input_example, output_example)
: provide concrete input/output demonstrations
The focus and guidance methods:
focus_on(primary_goal)
: emphasize the most important objectiveavoid_topics(topics)
: explicitly exclude irrelevant or problematic areasfinal_emphasis(emphasis)
: add closing reinforcement using recency bias
Output methods:
build()
: generate the final structured prompt stringpreview_structure()
: preview the prompt organization and metadata
Each method is designed to work together in the attention-optimized prompt structure, with positioning and formatting automatically handled to maximize model performance.
Examples
Basic prompt construction
Create a simple prompt with persona and task:
We can easily print the prompt that was generated for this task:
print(prompt)
You are a data scientist with expertise in machine learning.
CRITICAL REQUIREMENTS:
- Primary objective: identifying the top 3 risk factors
TASK: analyze customer churn patterns
Focus your entire response on: identifying the top 3 risk factors
Structured analysis prompt
It is possible to build a much more comprehensive analysis prompt with multiple sections:
= (
prompt
tb.PromptBuilder()"senior software architect")
.persona("focus only on production-ready solutions")
.critical_constraint("review the codebase architecture")
.task_context(
.core_analysis(["identify design patterns used",
"assess scalability bottlenecks",
"review security implications"
])
.structured_section("Performance Metrics", [
"response time requirements",
"throughput expectations",
"memory usage constraints"
],=tb.Priority.HIGH
priority
)
.output_format(["executive summary (2-3 sentences)",
"detailed findings with code examples",
"prioritized recommendations"
])"provide actionable next steps")
.final_emphasis(
)
The generated prompt can be printed as follows:
print(prompt)
You are a senior software architect.
CRITICAL REQUIREMENTS:
- focus only on production-ready solutions
TASK: review the codebase architecture
CORE ANALYSIS (Required):
- identify design patterns used
- assess scalability bottlenecks
- review security implications
PERFORMANCE METRICS:
- response time requirements
- throughput expectations
- memory usage constraints
OUTPUT FORMAT:
- executive summary (2-3 sentences)
- detailed findings with code examples
- prioritized recommendations
provide actionable next steps
Code review prompt
Create a specialized prompt for code reviews:
= (
prompt
tb.PromptBuilder()"senior developer", "code quality and best practices")
.persona("review the pull request for potential issues")
.task_context("flag any security vulnerabilities immediately")
.critical_constraint(
.structured_section("Review Areas", [
"logic and correctness",
"security considerations",
"performance implications",
"code readability and documentation"
]
)
.output_format(["critical issues (must fix)",
"suggestions (should consider)",
"positive feedback"
])"personal coding style preferences"])
.avoid_topics(["providing constructive, actionable feedback")
.focus_on(
)
We can look at the generated prompt:
print(prompt)
You are a senior developer with expertise in code quality and best practices.
CRITICAL REQUIREMENTS:
- Primary objective: providing constructive, actionable feedback
TASK: review the pull request for potential issues
REVIEW AREAS:
- logic and correctness
- security considerations
- performance implications
- code readability and documentation
ADDITIONAL CONSTRAINTS:
- flag any security vulnerabilities immediately
- IMPORTANT CONSTRAINT: You MUST NOT provide any information, advice, or discussion about personal
coding style preferences. If asked about personal coding style preferences, politely decline and
redirect by saying something to the effect of 'I'm not able to help with personal coding style
preferences. Is there something else I can assist you with instead?' (adapt the language and
phrasing to match the conversation's language and tone).
OUTPUT FORMAT:
- critical issues (must fix)
- suggestions (should consider)
- positive feedback
Focus your entire response on: providing constructive, actionable feedback