mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
remove openaischema from index.md (#129)
This commit is contained in:
-134
@@ -93,140 +93,6 @@ assert user.name == "Jason"
|
||||
assert user.age == 25
|
||||
```
|
||||
|
||||
## Introduction to `OpenAISchema`
|
||||
|
||||
If you want more control than just passing a single class we can use the `OpenAISchema` which extends `BaseModel`.
|
||||
|
||||
This quick start guide contains the follow sections:
|
||||
|
||||
1. Defining a schema
|
||||
2. Adding Additional Prompting
|
||||
3. Calling the ChatCompletion
|
||||
4. Deserializing back to the instance
|
||||
|
||||
OpenAI Function Call allows you to leverage OpenAI's powerful language models for function calls and schema extraction. This guide provides a quick start for using OpenAI Function Call.
|
||||
|
||||
### Section 1: Defining a Schema
|
||||
|
||||
To begin, let's define a schema using OpenAI Function Call. A schema describes the structure of the input and output data for a function. In this example, we'll define a simple schema for a `User` object:
|
||||
|
||||
```python
|
||||
from instructor import OpenAISchema
|
||||
|
||||
class UserDetails(OpenAISchema):
|
||||
name: str
|
||||
age: int
|
||||
```
|
||||
|
||||
In this schema, we define a `UserDetails` class that extends `OpenAISchema`. We declare two fields, `name` and `age`, of type `str` and `int` respectively.
|
||||
|
||||
### Section 2: Adding Additional Prompting
|
||||
|
||||
To enhance the performance of the OpenAI language model, you can add additional prompting in the form of docstrings and field descriptions. They can provide context and guide the model on how to process the data.
|
||||
|
||||
!!! note Using `patch`
|
||||
these docstrings and fields descriptions are powered by `pydantic.BaseModel` so they'll work via the patching approach as well.
|
||||
|
||||
```python hl_lines="5 6"
|
||||
from instructor import OpenAISchema
|
||||
from pydantic import Field
|
||||
|
||||
class UserDetails(OpenAISchema):
|
||||
""""
|
||||
Correctly extracted user information
|
||||
:param age: age of the user
|
||||
"""
|
||||
name: str = Field(..., description="User's full name")
|
||||
age: int
|
||||
```
|
||||
|
||||
In this updated schema, we use the `Field` class from `pydantic` to add descriptions to the `name` field. Moreover, we use the docstring to add information for the parameter `age`.
|
||||
In both cases, the description provides information about the fields, giving even more context to the language model.
|
||||
Information from the docstring is extracted using [docstring-parser](https://github.com/rr-/docstring_parser) which supports different docstring styles.
|
||||
Note that if the `Field` contains a description for a parameter as well as the docstring, the `Field`'s description is used.
|
||||
|
||||
!!! 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
|
||||
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
|
||||
```
|
||||
|
||||
```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"
|
||||
from instructor import OpenAISchema
|
||||
from pydantic import Field
|
||||
|
||||
class UserDetails(OpenAISchema):
|
||||
name: str = Field(..., description="User's full name")
|
||||
age: int
|
||||
|
||||
|
||||
completion = openai.ChatCompletion.create(
|
||||
model="gpt-3.5-turbo-0613",
|
||||
functions=[UserDetails.openai_schema],
|
||||
function_call={"name": UserDetails.openai_schema["name"]},
|
||||
messages=[
|
||||
{"role": "system", "content": "Extract user details from my requests"},
|
||||
{"role": "user", "content": "My name is John Doe and I'm 30 years old."},
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
In this example, we make a call to the `ChatCompletion` API by providing the model name (`gpt-3.5-turbo-0613`) and a list of messages. The messages consist of a system message and a user message. The system message sets the context by requesting user details, while the user message provides the input with the user's name and age.
|
||||
|
||||
Note that we have omitted the additional parameters that can be included in the API request, such as `temperature`, `max_tokens`, and `n`. These parameters can be customized according to your requirements.
|
||||
|
||||
### Section 4: Deserializing Back to the Instance
|
||||
|
||||
To deserialize the response from the `ChatCompletion` API back into an instance of the `UserDetails` class, we can use the `from_response` method.
|
||||
|
||||
```python
|
||||
user = UserDetails.from_response(completion)
|
||||
|
||||
print(user.name) # Output: John Doe
|
||||
print(user.age) # Output: 30
|
||||
```
|
||||
|
||||
By calling `UserDetails.from_response`, we create an instance of the `UserDetails` class using the response from the API call. Subsequently, we can access the extracted user details through the `name` and `age` attributes of the `user` object.
|
||||
|
||||
## IDE Support
|
||||
|
||||
Everything is designed for you to get the best developer experience possible, with the best editor support.
|
||||
|
||||
Reference in New Issue
Block a user