diff --git a/README.md b/README.md index 0fb398415..aade234ac 100644 --- a/README.md +++ b/README.md @@ -126,13 +126,14 @@ In this updated schema, we use the `Field` class from `pydantic` to add descript ```python hl_lines="2 3" class UserDetails(OpenAISchema): "Correctly extracted user information" + name: str = Field(..., description="User's full name") age: int UserDetails.openai_schema ``` -```json hl_lines="3 8" +```json { "name": "UserDetails", "description": "Correctly extracted user information", @@ -204,7 +205,7 @@ Including **autocompletion**: ![autocomplete](docs/img/ide_support.png) And even **inline errors** -d + ![errors](docs/img/error2.png) ## OpenAI Schema and Pydantic diff --git a/docs/index.md b/docs/index.md index 66248bb4f..927001cae 100644 --- a/docs/index.md +++ b/docs/index.md @@ -146,60 +146,57 @@ Note that if the `Field` contains a description for a parameter as well as the d !!! note "Code, schema, and prompt" We can run `openai_schema` to see exactly what the API will see, notice how the docstrings, attributes, types, and parameter descriptions are now part of the schema. This describes on this library's core philosophies. - ```python hl_lines="3 5 7" - class UserDetails(OpenAISchema): - """ - Correctly extracted user information - :param name: the user's full name - :param age: age of the user - """ - name: str = Field(..., description="User's full name") - age: int - - UserDetails.openai_schema - ``` +```python +class UserDetails(OpenAISchema): + """ + Correctly extracted user information - ```json hl_lines="3 9 13" - { - "name": "UserDetails", - "description": "Correctly extracted user information", - "parameters": { - "type": "object", - "properties": { - "name": { - "type": "string", - "description": "User's full name" - }, - "age": { - "type": "integer" - "description": "age of the user" - } - }, - "required": [ - "age", - "name" - ] - } + :param name: the user's full name + :param age: age of the user + """ + name: str = Field(..., description="User's full name") + age: int + +UserDetails.openai_schema +``` + +```json hl_lines="3 9 13" +{ +"name": "UserDetails", +"description": "Correctly extracted user information", +"parameters": { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "User's full name" + }, + "age": { + "type": "integer" + "description": "age of the user" } - ``` + }, + "required": [ + "age", + "name" + ] +} +} +``` ### Section 3: Calling the ChatCompletion With the schema defined, let's proceed with calling the `ChatCompletion` API using the defined schema and messages. -```python hl_lines="11 12 15" +```python hl_lines="11 12" from instructor import OpenAISchema from pydantic import Field class UserDetails(OpenAISchema): - """ - Correctly extracted user information - :param name: the user's full name - :param age: age of the user - """ name: str = Field(..., description="User's full name") age: int + completion = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", functions=[UserDetails.openai_schema], @@ -219,8 +216,9 @@ Note that we have omitted the additional parameters that can be included in the To deserialize the response from the `ChatCompletion` API back into an instance of the `UserDetails` class, we can use the `from_response` method. -```python hl_lines="1" +```python user = UserDetails.from_response(completion) + print(user.name) # Output: John Doe print(user.age) # Output: 30 ```