From 509ec5dfddb857b2b491405dde1cb24a2d4332c8 Mon Sep 17 00:00:00 2001 From: Jason Liu Date: Wed, 8 Nov 2023 15:07:01 -0500 Subject: [PATCH] docs highlights --- docs/index.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/index.md b/docs/index.md index abf1cf1..a5796eb 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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