mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
Creative acts in documentation (#188)
This commit is contained in:
+9
-12
@@ -4,10 +4,8 @@ _Structured extraction in Python, powered by OpenAI's function calling api, desi
|
||||
|
||||
---
|
||||
|
||||
[Star us on Github!](www.github.com/jxnl/instructor).
|
||||
|
||||
[](https://pypi.python.org/pypi/instructor)
|
||||
[](https://github.com/jxnl/instructor/stargazers)
|
||||

|
||||
[](https://jxnl.github.io/instructor)
|
||||
[](https://github.com/jxnl/instructor/issues)
|
||||
[](https://twitter.com/jxnlco)
|
||||
@@ -36,10 +34,12 @@ With Instructor, your code becomes more efficient and readable. Here’s a quick
|
||||
## Usage
|
||||
|
||||
```py hl_lines="5 13"
|
||||
from openai import OpenAI
|
||||
import instructor
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
# Enables `response_model`
|
||||
# This enables response_model keyword
|
||||
# from client.chat.completions.create
|
||||
client = instructor.patch(OpenAI())
|
||||
|
||||
class UserDetail(BaseModel):
|
||||
@@ -103,7 +103,7 @@ assert isinstance(model, UserExtract)
|
||||
|
||||
### Step 1: Patch the client
|
||||
|
||||
First, import the required libraries and apply the patch function to the OpenAI module. This exposes new functionality with the response_model parameter.
|
||||
First, import the required libraries and apply the patch function to the OpenAI module. This allows us to parse the raw JSON from our OpenAI completions into Pydantic output.
|
||||
|
||||
```python
|
||||
import instructor
|
||||
@@ -117,11 +117,9 @@ client = instructor.patch(OpenAI())
|
||||
|
||||
### Step 2: Define the Pydantic Model
|
||||
|
||||
Create a Pydantic model to define the structure of the data you want to extract. This model will map directly to the information in the prompt.
|
||||
Create a Pydantic model to define the structure of the data extracted from the OpenAI response. This model will map directly to the information in the prompt.
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel
|
||||
|
||||
class UserDetail(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
@@ -129,8 +127,7 @@ class UserDetail(BaseModel):
|
||||
|
||||
### Step 3: Extract
|
||||
|
||||
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.
|
||||
Use the `client.chat.completions.create` method to generate a completion and extract response data into the Pydantic object. The response_model parameter enables autocomplete and spell check in your IDE.
|
||||
|
||||
```python
|
||||
user: UserDetail = client.chat.completions.create(
|
||||
@@ -170,7 +167,7 @@ except ValidationError as e:
|
||||
print(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.
|
||||
Note, the error message is generated by the LLM, not the code, so it'll be helpful for re asking the model.
|
||||
|
||||
```plaintext
|
||||
1 validation error for QuestionAnswer
|
||||
|
||||
Reference in New Issue
Block a user