Prompt Templates¶
This guide covers how to use prompt templates with Instructor to create reusable, parameterized prompts for structured data extraction.
Why Prompt Templates Matter¶
Good prompts are essential for effective structured data extraction. Prompt templates help you:
- Create consistent and reusable prompts
- Parameterize prompts with dynamic values
- Separate prompt engineering from application logic
- Standardize prompt patterns for different use cases
Basic Prompt Templates¶
The simplest form of a prompt template is a string with placeholders for variables:
from pydantic import BaseModel, Field
import instructor
from openai import OpenAI
client = instructor.from_openai(OpenAI())
class Person(BaseModel):
name: str
age: int
occupation: str
# Define a template with parameters
prompt_template = """
Extract information about the person mentioned in the following {document_type}:
{content}
Please provide their name, age, and occupation.
"""
# Use the template with specific values
document_type = "email"
content = "Hi team, I'm introducing our new project manager, Sarah Johnson. She's 34 and has been in project management for 8 years."
prompt = prompt_template.format(
document_type=document_type,
content=content
)
# Extract structured data using the formatted prompt
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
],
response_model=Person
)
Using f-strings for Simple Templates¶
For simple cases, you can use f-strings to create prompt templates:
def extract_person(content, document_type="text"):
prompt = f"""
Extract information about the person mentioned in the following {document_type}:
{content}
Please provide their name, age, and occupation.
"""
return client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
],
response_model=Person
)
# Use the function
person = extract_person(
"According to his resume, John Smith (42) works as a software developer.",
document_type="resume"
)
Template Functions¶
For more complex templates, create dedicated template functions:
from typing import List, Optional
from pydantic import BaseModel
import instructor
from openai import OpenAI
client = instructor.from_openai(OpenAI())
class ProductReview(BaseModel):
product_name: str
rating: int
pros: List[str]
cons: List[str]
summary: str
def create_review_extraction_prompt(
review_text: str,
product_category: str,
include_sentiment: bool = False
) -> str:
sentiment_instruction = """
Also include a brief sentiment analysis of the review.
""" if include_sentiment else ""
return f"""
Extract product review information from the following {product_category} review:
{review_text}
Please identify:
- The name of the product being reviewed
- The numerical rating (1-5)
- A list of pros/positive points
- A list of cons/negative points
- A brief summary of the review
{sentiment_instruction}
"""
# Use the template function
review_text = """
I recently purchased the UltraSound X300 headphones, and I'm mostly satisfied.
The sound quality is amazing and the battery lasts for days. They're also very
comfortable to wear for long periods. However, they're a bit pricey at $299, and
the Bluetooth occasionally disconnects. Overall, I'd give them 4 out of 5 stars.
"""
prompt = create_review_extraction_prompt(
review_text=review_text,
product_category="headphone",
include_sentiment=True
)
review = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
],
response_model=ProductReview
)
Best Practices for Prompt Templates¶
- Be explicit about the output format: Clearly specify what fields you need and in what format
- Use consistent language: Maintain consistent terminology throughout the template
- Keep it concise: Avoid unnecessary verbosity that could confuse the model
- Parameterize only what varies: Only make template parameters for parts that need to change
- Include examples for complex tasks: Provide few-shot examples for more complex extractions
- Test with different inputs: Ensure your template works well with a variety of inputs
Related Resources¶
- Simple Object Extraction - Extracting basic objects
- List Extraction - Working with lists of objects
- Optional Fields - Handling optional data
- Prompting - General prompting concepts
- Templating - Advanced template techniques
Next Steps¶
- Explore Field Validation for ensuring data quality
- Try List Extraction for extracting multiple items
- Learn about Nested Structure for complex data