mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
docs highlights
This commit is contained in:
+6
-7
@@ -87,10 +87,9 @@ The patch introduces 3 features to the `ChatCompletion` class:
|
||||
|
||||
First, import the required libraries and apply the patch function to the OpenAI module. This exposes new functionality with the response_model parameter.
|
||||
|
||||
```python
|
||||
```python hl_lines="6"
|
||||
import instructor
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
# This enables response_model keyword
|
||||
# from client.chat.completions.create
|
||||
@@ -114,7 +113,7 @@ 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. Its helpful to annotate the variable with the type of the response model.
|
||||
which will help your IDE provide autocomplete and spell check.
|
||||
|
||||
```python
|
||||
```python hl_lines="3"
|
||||
user: UserDetail = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserDetail,
|
||||
@@ -127,7 +126,7 @@ assert user.name == "Jason"
|
||||
assert user.age == 25
|
||||
```
|
||||
|
||||
## Pydantic Validation
|
||||
## Advanced: Pydantic Validation
|
||||
|
||||
Validation can also be plugged into the same Pydantic model. Here, if the answer attribute contains content that violates the rule "don't say objectionable things," Pydantic will raise a validation error.
|
||||
|
||||
@@ -154,7 +153,7 @@ except ValidationError as e:
|
||||
|
||||
Its important to not here that the error message is generated by the LLM, not the code, so it'll be helpful for re asking the model.
|
||||
|
||||
```plaintext
|
||||
```plaintext hl_lines="3"
|
||||
1 validation error for QuestionAnswer
|
||||
answer
|
||||
Assertion failed, The statement is objectionable. (type=assertion_error)
|
||||
@@ -164,10 +163,10 @@ answer
|
||||
|
||||
Here, the `UserDetails` model is passed as the `response_model`, and `max_retries` is set to 2.
|
||||
|
||||
```python
|
||||
from openai import OpenAI
|
||||
```python hl_lines="15-18 22 23 29"
|
||||
import instructor
|
||||
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
# Apply the patch to the OpenAI client
|
||||
|
||||
Reference in New Issue
Block a user