I am trying to structure outputs from a Large Language Model (LLM) and came across the instructor
package, which is a true gem. However, I don’t fully understand how instructor
works “under the hood.” Here are two simple examples that achieve the same result:
import instructor
from openai import OpenAI
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
client = instructor.from_openai(OpenAI())
user = client.chat.completions.create(
model="gpt-4-turbo",
response_model=User,
messages=[{"role": "user", "content": "John Doe is 30 years old."}]
)
print(user.name) # Outputs: John Doe
print(user.age) # Outputs: 30
As it is stated on the instructor website:
Here all docstrings, types, and field annotations will be used to generate the prompt. The prompt will be generated by the create method of the client and will be used to generate the response.
I want to understand how exactly both code snippets work:
import instructor
from openai import OpenAI
from pydantic import BaseModel
class UserInfo(BaseModel):
"""
This is a doc-string for the UserInfo model.
It describes the structure and purpose of the model.
"""
name: str
age: int
client = instructor.from_openai(OpenAI())
user_info = client.chat.completions.create(
model="gpt-4-turbo",
response_model=UserInfo,
messages=[{"role": "user", "content": "John Doe is 30 years old."}]
)
print(user_info.name) # Outputs: John Doe
print(user_info.age) # Outputs: 30
Can anyone explain the behaviour?
Thanks in advance!