结构化输出入门¶
大型语言模型(LLM)是强大的文本生成工具,但从其输出中提取结构化数据可能具有挑战性。结构化输出通过让 LLM 以一致的机器可读格式返回数据来解决此问题。
非结构化输出的问题¶
让我们看看当我们要求 LLM 在没有任何结构的情况下提取信息时会发生什么
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Extract customer: John Doe, age 35, email: john@example.com",
}
],
)
print(response.choices[0].message.content)
输出可能看起来像
或者可能是
I found the following customer information:
- Name: John Doe
- Age: 35
- Email address: john@example.com
这种不一致性使得在下游应用程序中可靠地解析信息变得困难。
解决方案:使用 Instructor 实现结构化输出¶
Instructor 通过使用 Pydantic 模型来定义预期的输出结构,从而解决了这个问题
import instructor
from openai import OpenAI
from pydantic import BaseModel, Field, EmailStr
class Customer(BaseModel):
name: str = Field(description="Customer's full name")
age: int = Field(description="Customer's age in years", ge=0, le=120)
email: EmailStr = Field(description="Customer's email address")
client = instructor.from_openai(OpenAI())
customer = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Extract customer: John Doe, age 35, email: john@example.com",
}
],
response_model=Customer, # This is the key part
)
print(customer) # Customer(name='John Doe', age=35, email='john@example.com')
print(f"Name: {customer.name}, Age: {customer.age}, Email: {customer.email}")
这种方法的好处包括
- 一致性:始终以相同的格式获取数据
- 验证:年龄必须在 0 到 120 之间,电子邮件必须有效
- 类型安全:
age
始终是整数,而不是字符串 - 文档:模型字段带有描述,具有自文档功能
复杂示例:嵌套结构¶
Instructor 在处理复杂数据结构时表现出色
from typing import List, Optional
from pydantic import BaseModel, Field
import instructor
from openai import OpenAI
client = instructor.from_openai(OpenAI())
class Address(BaseModel):
street: str
city: str
state: str
zip_code: str
class Contact(BaseModel):
email: Optional[str] = None
phone: Optional[str] = None
class Person(BaseModel):
name: str
age: int
occupation: str
address: Address
contact: Contact
skills: List[str] = Field(description="List of professional skills")
person = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": """
Extract detailed information for this person:
John Smith is a 42-year-old software engineer living at 123 Main St, San Francisco, CA 94105.
His email is john.smith@example.com and phone is 555-123-4567.
John is skilled in Python, JavaScript, and cloud architecture.
""",
}
],
response_model=Person,
)
print(f"Name: {person.name}")
print(f"Location: {person.address.city}, {person.address.state}")
print(f"Skills: {', '.join(person.skills)}")
安装¶
要开始使用 Instructor,请通过 pip 安装它
您还需要为您正在使用的 LLM 提供商设置 API 密钥。
下一步¶
在下一节中,您将学习如何
- 创建您的首次提取
- 理解您可以创建的响应模型
- 设置各种 LLM 提供商的客户端