mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
23 lines
638 B
Python
23 lines
638 B
Python
import openai
|
|
from pydantic import BaseModel
|
|
from instructor import patch
|
|
|
|
# By default, the patch function will patch the ChatCompletion.create and ChatCompletion.acreate methods. to support response_model parameter
|
|
patch()
|
|
|
|
|
|
# Now, we can use the response_model parameter using only a base model
|
|
# rather than having to use the OpenAISchema class
|
|
class UserExtract(BaseModel):
|
|
name: str
|
|
age: int
|
|
|
|
user: UserExtract = openai.ChatCompletion.create(
|
|
model="gpt-3.5-turbo",
|
|
response_model=UserExtract,
|
|
messages=[
|
|
{"role": "user", "content": "Extract jason is 25 years old"},
|
|
],
|
|
) # type: ignore
|
|
|
|
print(user) |