Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Created January 11, 2024 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/07bf0b278f1a5d4dec77846592fed595 to your computer and use it in GitHub Desktop.
Save heathermiller/07bf0b278f1a5d4dec77846592fed595 to your computer and use it in GitHub Desktop.
Instructor
import instructor
from openai import OpenAI
from pydantic import BaseModel
# This enables response_model keyword
# from client.chat.completions.create
client = instructor.patch(OpenAI())
class UserDetail(BaseModel):
name: str
age: int
user = client.chat.completions.create(model="gpt-3.5-turbo", response_model=
UserDetail,messages=[{"role": "user", "content": "Extract Jason is 25 years old"}]
)
assert isinstance(user, UserDetail)
assert user.name == "Jason"
assert user.age == 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment