提示模板¶
本指南介绍如何将提示模板与 Instructor 结合使用,以创建可重用、参数化的提示,用于结构化数据提取。
为什么提示模板很重要¶
良好的提示对于有效的结构化数据提取至关重要。提示模板可帮助你
- 创建一致且可重用的提示
- 使用动态值参数化提示
- 将提示工程与应用程序逻辑分离
- 标准化不同用例的提示模式
基本提示模板¶
提示模板最简单的形式是包含变量占位符的字符串
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
)
使用 f-strings 创建简单模板¶
对于简单情况,你可以使用 f-strings 来创建提示模板
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"
)
模板函数¶
对于更复杂的模板,创建专门的模板函数
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
)
提示模板的最佳实践¶
- 明确输出格式:清晰指定你需要哪些字段以及采用何种格式
- 使用一致的语言:在整个模板中保持一致的术语
- 保持简洁:避免可能使模型困惑的不必要冗长
- 仅参数化变化部分:只为需要更改的部分创建模板参数
- 为复杂任务包含示例:为更复杂的提取提供少样本示例
- 使用不同输入进行测试:确保你的模板适用于各种输入