讲解器
将 LiteLLM 与 jxnl 的 instructor 库 结合使用,以获得更健壮的结构化输出。输出会自动验证为 Pydantic 类型,并将验证错误提供回模型,以增加重试时成功响应的机会。
使用方法 (同步)
import instructor
from litellm import completion
from pydantic import BaseModel
client = instructor.from_litellm(completion)
class User(BaseModel):
name: str
age: int
def extract_user(text: str):
return client.chat.completions.create(
model="gpt-4o-mini",
response_model=User,
messages=[
{"role": "user", "content": text},
],
max_retries=3,
)
user = extract_user("Jason is 25 years old")
assert isinstance(user, User)
assert user.name == "Jason"
assert user.age == 25
print(f"{user=}")
使用方法 (异步)
import asyncio
import instructor
from litellm import acompletion
from pydantic import BaseModel
client = instructor.from_litellm(acompletion)
class User(BaseModel):
name: str
age: int
async def extract(text: str) -> User:
return await client.chat.completions.create(
model="gpt-4o-mini",
response_model=User,
messages=[
{"role": "user", "content": text},
],
max_retries=3,
)
user = asyncio.run(extract("Alice is 30 years old"))
assert isinstance(user, User)
assert user.name == "Alice"
assert user.age == 30
print(f"{user=}")