mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
clean up readme
This commit is contained in:
@@ -54,16 +54,18 @@ from pydantic import BaseModel
|
||||
# Enables `response_model`
|
||||
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)
|
||||
@@ -71,23 +73,6 @@ assert user.name == "Jason"
|
||||
assert user.age == 25
|
||||
```
|
||||
|
||||
### Using `openai<1.0.0`
|
||||
|
||||
If you're using `openai<1.0.0` then make sure you `pip install instructor<0.3.0`
|
||||
where you can patch a global client like so:
|
||||
|
||||
```python hl_lines="4 8"
|
||||
import openai
|
||||
import instructor
|
||||
|
||||
instructor.patch()
|
||||
|
||||
user = openai.ChatCompletion.create(
|
||||
...,
|
||||
response_model=UserDetail,
|
||||
)
|
||||
```
|
||||
|
||||
### Using async clients
|
||||
|
||||
For async clients you must use `apatch` vs. `patch`, as shown:
|
||||
@@ -99,10 +84,12 @@ from pydantic import BaseModel
|
||||
|
||||
aclient = instructor.apatch(AsyncOpenAI())
|
||||
|
||||
|
||||
class UserExtract(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
|
||||
model = await aclient.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserExtract,
|
||||
@@ -121,7 +108,6 @@ First, import the required libraries and apply the `patch` function to the OpenA
|
||||
```python
|
||||
import instructor
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
# This enables response_model keyword
|
||||
# from client.chat.completions.create
|
||||
@@ -135,6 +121,7 @@ Create a Pydantic model to define the structure of the data you want to extract.
|
||||
```python
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class UserDetail(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
@@ -145,12 +132,13 @@ class UserDetail(BaseModel):
|
||||
Use the `client.chat.completions.create` method to send a prompt and extract the data into the Pydantic object. The `response_model` parameter specifies the Pydantic model to use for extraction. It is helpful to annotate the variable with the type of the response model which will help your IDE provide autocomplete and spell check.
|
||||
|
||||
```python
|
||||
|
||||
user: UserDetail = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserDetail,
|
||||
messages=[
|
||||
{"role": "user", "content": "Extract Jason is 25 years old"},
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
assert user.name == "Jason"
|
||||
@@ -168,13 +156,14 @@ from pydantic import BaseModel, ValidationError, BeforeValidator
|
||||
from typing_extensions import Annotated
|
||||
from instructor import llm_validator
|
||||
|
||||
|
||||
class QuestionAnswer(BaseModel):
|
||||
question: str
|
||||
answer: Annotated[
|
||||
str,
|
||||
BeforeValidator(llm_validator("don't say objectionable things"))
|
||||
str, BeforeValidator(llm_validator("don't say objectionable things"))
|
||||
]
|
||||
|
||||
|
||||
try:
|
||||
qa = QuestionAnswer(
|
||||
question="What is the meaning of life?",
|
||||
@@ -182,6 +171,12 @@ try:
|
||||
)
|
||||
except ValidationError as e:
|
||||
print(e)
|
||||
"""
|
||||
1 validation error for QuestionAnswer
|
||||
answer
|
||||
Assertion failed, The statement promotes objectionable behavior. [type=assertion_error, input_value='The meaning of life is to be evil and steal', input_type=str]
|
||||
For further information visit https://errors.pydantic.dev/2.6/v/assertion_error
|
||||
"""
|
||||
```
|
||||
|
||||
It is important to note here that the **error message is generated by the LLM**, not the code. Thus, it is helpful for re-asking the model.
|
||||
@@ -205,6 +200,7 @@ from pydantic import BaseModel, field_validator
|
||||
# Apply the patch to the OpenAI client
|
||||
client = instructor.patch(OpenAI())
|
||||
|
||||
|
||||
class UserDetails(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
@@ -216,6 +212,7 @@ class UserDetails(BaseModel):
|
||||
raise ValueError("Name must be in uppercase.")
|
||||
return v
|
||||
|
||||
|
||||
model = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserDetails,
|
||||
|
||||
Reference in New Issue
Block a user