Skip to content

Commit

Permalink
docs highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Nov 8, 2023
1 parent 87e9a9b commit 509ec5d
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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.

Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 509ec5d

Please sign in to comment.