ChatBot.system_prompt()
Set a custom system prompt, overriding any preset system prompt.
Usage
ChatBot.system_prompt(prompt)This method allows for fine-grained prompt engineering. The custom system prompt will take precedence over any preset system prompt, though it will still be combined with persona, constraints, and other configuration elements.
Three Approaches To System Prompt Creation
Direct String Approach: pass a complete system prompt as a single string. This is straightforward for simple prompts or when adapting existing prompts.
ChatBot.prompt_builder() Method (Recommended): use the attention-optimized prompt_builder() method from a ChatBot instance. This provides better structure, maintainability, and leverages modern prompt engineering research on attention patterns.
PromptBuilder Class: use the PromptBuilder class directly for maximum flexibility and reusable prompt templates that can be used across multiple ChatBot instances.
The structured prompt approaches (methods 2 and 3) are especially valuable for:
- complex multi-section prompts
- professional domain specializations
- prompts requiring consistent structure across variations
- team environments where prompt templates need to be maintainable
- reusable prompt templates across different chatbot configurations
Parameters
prompt: Union[str, PromptBuilder]- The custom system prompt text. Can include prompt engineering techniques, specific instructions, formatting requirements, etc.
Returns
ChatBot- Returns self for method chaining
Examples
Comparing the three approaches
Direct String Approach: simple and direct:
import talk_box as tb
# Quick setup with string prompt
bot = tb.ChatBot().model("gpt-4-turbo").system_prompt(
"You are a helpful Python tutor. Always provide working code examples "
"and explain the reasoning behind each solution."
)ChatBot.prompt_builder() Method: structured and maintainable:
import talk_box as tb
# Same functionality with better structure
bot = tb.ChatBot().model("gpt-4-turbo")
# Build an attention-optimized prompt
prompt = (
bot.prompt_builder()
.persona("helpful Python tutor", "educational programming assistance")
.core_analysis(["Provide working code examples", "Explain reasoning behind solutions"])
.output_format(["Clear step-by-step explanations", "Commented code examples"])
._build()
)
# Apply the structured prompt
bot.system_prompt(prompt)PromptBuilder Class Directly: maximum flexibility and reusability:
import talk_box as tb
# Create reusable prompt template
python_tutor_template = (
tb.PromptBuilder()
.persona("helpful Python tutor", "educational programming assistance")
.core_analysis(["Provide working code examples", "Explain reasoning behind solutions"])
.output_format(["Clear step-by-step explanations", "Commented code examples"])
._build()
)
# Use the same template across multiple bots
beginner_bot = tb.ChatBot().model("gpt-3.5-turbo").system_prompt(python_tutor_template)
advanced_bot = tb.ChatBot().model("gpt-4-turbo").system_prompt(python_tutor_template)Why structured approaches are recommended for complex prompts
For complex professional domains, structured prompt building provides superior results:
import talk_box as tb
# Create a security code reviewer with attention-optimized structure
bot = tb.ChatBot().model("gpt-4-turbo")
security_prompt = (
bot.prompt_builder()
.persona("senior security engineer", "application security code review")
.task_context("Comprehensive security review following OWASP methodology")
.critical_constraint("Prioritize security vulnerabilities over style issues")
.core_analysis([
"Input validation and sanitization",
"Authentication and authorization controls",
"Secure data handling (encryption, PII protection)",
"Error handling (no sensitive info in errors)",
"Dependencies and third-party library security"
])
.output_format([
"🚨 CRITICAL ISSUES: Security vulnerabilities with CVSS scores",
"⚠️ HIGH PRIORITY: Logic errors and architectural problems",
"📈 IMPROVEMENTS: Performance and maintainability suggestions"
])
.final_emphasis("Professional, constructive tone with educational explanations")
._build()
)
bot.system_prompt(security_prompt)Benefits of this structured approach:
- clear attention hierarchy (critical info first)
- maintainable and modifiable sections
- consistent output format across reviews
- research-backed attention optimization
Using PromptBuilder class for reusable templates
The direct PromptBuilder class approach excels for template reuse across teams:
import talk_box as tb
# Create a reusable code review template
code_review_template = (
tb.PromptBuilder()
.persona("experienced software engineer", "comprehensive code review")
.task_context("Thorough code review focusing on quality and best practices")
.core_analysis([
"Code correctness and logic",
"Performance and efficiency",
"Security considerations",
"Maintainability and readability",
"Test coverage and edge cases"
])
.output_format([
"## Summary: Overall assessment and key points",
"## Issues: Specific problems with line references",
"## Suggestions: Concrete improvement recommendations",
"## Strengths: What the code does well"
])
.final_emphasis("Constructive feedback that helps developers improve")
._build()
)
# Use the same template across different team contexts
senior_reviewer = tb.ChatBot().model("gpt-4-turbo").system_prompt(code_review_template)
junior_reviewer = tb.ChatBot().model("gpt-3.5-turbo").system_prompt(code_review_template)
# Customize for specific languages while keeping base structure
python_template = (
tb.PromptBuilder()
.from_template(code_review_template) # Inherit base structure
.add_constraint("Focus on Pythonic idioms and PEP 8 compliance")
._build()
)
python_reviewer = tb.ChatBot().model("gpt-4-turbo").system_prompt(python_template)When to use each approach:
- Direct String: simple, one-off prompts
- ChatBot.prompt_builder(): complex prompts for single bot instance
- PromptBuilder Class: reusable templates, team standards, prompt libraries
Best Practices
Use direct strings for simple prompts: quick, straightforward prompts work well with direct string assignment when you need something fast and simple.
Use ChatBot.prompt_builder() for complex single-use prompts: when creating sophisticated system prompts for a specific ChatBot instance with multiple sections, constraints, and formatting requirements.
Use PromptBuilder class directly for reusable templates: when you need prompt templates that can be shared across multiple ChatBot instances, team standards, or organizational prompt libraries.
Combine with other methods: all three approaches work seamlessly with .persona(), .avoid(), and other configuration methods for additional customization.
Template Hierarchy: consider creating base templates with PromptBuilder class and extending them for specific use cases to maintain consistency while allowing customization.
Notes
Attention Optimization: both PromptBuilder approaches create prompts that follow modern prompt engineering research on attention patterns and cognitive load optimization.
Maintenance: structured prompts created with PromptBuilder are easier to modify, debug, and version control in team environments.
Performance: all three approaches result in equivalent runtime performance; the choice is primarily about development experience, maintainability, and reusability needs.
See Also
- prompt_builder(): Create attention-optimized structured prompts from ChatBot instances
- PromptBuilder: The PromptBuilder class for reusable prompt templates and team standards
- persona(): Add personality context that complements system prompts
- preset(): Use pre-configured system prompts for common use cases
- avoid(): Add constraints that work with any system prompt approach