mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
Update README.md (#291)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Welcome to Instructor - Your Gateway to Structured Outputs with OpenAI
|
||||
|
||||
_Structured extraction in Python, powered by OpenAI's function calling api, designed for simplicity, transparency, and control._
|
||||
_Structured extraction in Python, powered by OpenAI's function calling API, designed for simplicity, transparency, and control._
|
||||
|
||||
---
|
||||
|
||||
@@ -18,7 +18,7 @@ Dive into the world of Python-based structured extraction, empowered by OpenAI's
|
||||
|
||||
## Get Started in Moments
|
||||
|
||||
Installing Instructor is a breeze. Just run `pip install instructor` in your terminal and you're on your way to a smoother data handling experience.
|
||||
Installing Instructor is a breeze. Simply run `pip install instructor` in your terminal and you're on your way to a smoother data handling experience!
|
||||
|
||||
## How Instructor Enhances Your Workflow
|
||||
|
||||
@@ -28,13 +28,11 @@ Our `instructor.patch` for the `OpenAI` class introduces three key enhancements:
|
||||
- **Max Retries:** Set your desired number of retry attempts for requests.
|
||||
- **Validation Context:** Provide a context object for enhanced validator access. A Glimpse into Instructor's Capabilities.
|
||||
|
||||
!!! note "Using Validators"
|
||||
|
||||
Learn more about validators checkout our blog post [Good llm validation is just good validation](https://jxnl.github.io/instructor/blog/2023/10/23/good-llm-validation-is-just-good-validation/)
|
||||
|
||||
With Instructor, your code becomes more efficient and readable. Here’s a quick peek:
|
||||
### Using Validators
|
||||
To learn more about validators, checkout our blog post [Good LLM validation is just good validation](https://jxnl.github.io/instructor/blog/2023/10/23/good-llm-validation-is-just-good-validation/)
|
||||
|
||||
## Usage
|
||||
With Instructor, your code becomes more efficient and readable. Here’s a quick peek:
|
||||
|
||||
```py hl_lines="5 13"
|
||||
import instructor
|
||||
@@ -61,7 +59,7 @@ assert user.name == "Jason"
|
||||
assert user.age == 25
|
||||
```
|
||||
|
||||
### "Using `openai<1.0.0`"
|
||||
### Using `openai<1.0.0`
|
||||
|
||||
If you're using `openai<1.0.0` then make sure you `pip install instructor<0.3.0`
|
||||
where you can patch a global client like so:
|
||||
@@ -78,9 +76,9 @@ user = openai.ChatCompletion.create(
|
||||
)
|
||||
```
|
||||
|
||||
### "Using async clients"
|
||||
### Using async clients
|
||||
|
||||
For async clients you must use apatch vs patch like so:
|
||||
For async clients you must use `apatch` vs. `patch`, as shown:
|
||||
|
||||
```py
|
||||
import instructor
|
||||
@@ -106,7 +104,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 exposes new functionality with the `response_model` parameter.
|
||||
|
||||
```python
|
||||
import instructor
|
||||
@@ -132,8 +130,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 send a prompt and extract the data into the Pydantic object. The `response_model` parameter specifies the Pydantic model to use for extraction. It is helpful to annotate the variable with the type of the response model which will help your IDE provide autocomplete and spell check.
|
||||
|
||||
```python
|
||||
user: UserDetail = client.chat.completions.create(
|
||||
@@ -150,7 +147,9 @@ assert user.age == 25
|
||||
|
||||
## 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.
|
||||
Validation can also be plugged into the same Pydantic model.
|
||||
|
||||
In this example, if the answer attribute contains content that violates the rule "Do not say objectionable things", Pydantic will raise a validation error.
|
||||
|
||||
```python hl_lines="9 15"
|
||||
from pydantic import BaseModel, ValidationError, BeforeValidator
|
||||
@@ -173,7 +172,7 @@ except ValidationError as e:
|
||||
print(e)
|
||||
```
|
||||
|
||||
Its important to note here that the error message is generated by the LLM, not the code, so it'll be helpful for re-asking the model.
|
||||
It is important to note here that the **error message is generated by the LLM**, not the code. Thus, it is helpful for re-asking the model.
|
||||
|
||||
```plaintext
|
||||
1 validation error for QuestionAnswer
|
||||
@@ -181,7 +180,7 @@ answer
|
||||
Assertion failed, The statement is objectionable. (type=assertion_error)
|
||||
```
|
||||
|
||||
## Reask on validation error
|
||||
## Re-ask on validation error
|
||||
|
||||
Here, the `UserDetails` model is passed as the `response_model`, and `max_retries` is set to 2.
|
||||
|
||||
@@ -219,15 +218,15 @@ assert model.name == "JASON"
|
||||
|
||||
## [Evals](https://github.com/jxnl/instructor/tree/main/tests/openai/evals)
|
||||
|
||||
We invite you to contribute evals in pytest as a way to monitor the quality of the openai models and the instructor library. To get started check out the [jxnl/instructor/tests/evals](https://github.com/jxnl/instructor/tree/main/tests/openai/evals) and contribute your own evals in the form of pytest tests. These evals will be run once a week and the results will be posted.
|
||||
We invite you to contribute to evals in `pytest` as a way to monitor the quality of the OpenAI models and the `instructor` library. To get started check out the [jxnl/instructor/tests/evals](https://github.com/jxnl/instructor/tree/main/tests/openai/evals) and contribute your own evals in the form of pytest tests. These evals will be run once a week and the results will be posted.
|
||||
|
||||
## Contributing
|
||||
|
||||
If you want to help out checkout some of the issues marked as `good-first-issue` or `help-wanted`. Found [here](https://github.com/jxnl/instructor/labels/good%20first%20issue). They could be anything from code improvements, a guest blog post, or a new cook book.
|
||||
If you want to help, checkout some of the issues marked as `good-first-issue` or `help-wanted` found [here](https://github.com/jxnl/instructor/labels/good%20first%20issue). They could be anything from code improvements, a guest blog post, or a new cookbook.
|
||||
|
||||
## CLI
|
||||
|
||||
We also provide some added CLI functionality for easy convinience
|
||||
We also provide some added CLI functionality for easy convinience:
|
||||
|
||||
- `instructor jobs` : This helps with the creation of fine-tuning jobs with OpenAI. Simple use `instructor jobs create-from-file --help` to get started creating your first fine-tuned GPT3.5 model
|
||||
|
||||
|
||||
Reference in New Issue
Block a user