-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiple modalities: tools, functions, json_mode (#218)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7de55a9
commit 359c5f9
Showing
7 changed files
with
358 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Patching | ||
|
||
Instructor enhances client functionality with three new keywords for backwards compatibility. This allows use of the enhanced client as usual, with structured output benefits. | ||
|
||
- `response_model`: Defines the response type for `chat.completions.create`. | ||
- `max_retries`: Determines retry attempts for failed `chat.completions.create` validations. | ||
- `validation_context`: Provides extra context to the validation process. | ||
|
||
There are three methods for structured output: | ||
|
||
1. **Function Calling**: The primary method. Use this for stability and testing. | ||
2. **Tool Calling**: Useful in specific scenarios; lacks the reasking feature of OpenAI's tool calling API. | ||
3. **JSON Mode**: Offers closer adherence to JSON but with more potential validation errors. Suitable for specific non-function calling clients. | ||
|
||
## Function Calling | ||
|
||
```python | ||
from openai import OpenAI | ||
import instructor | ||
|
||
client = instructor.patch(OpenAI()) | ||
``` | ||
|
||
## Tool Calling | ||
|
||
```python | ||
import instructor | ||
from instructor import Mode | ||
|
||
client = instructor.patch(OpenAI(), mode=Mode.TOOL_CALL) | ||
``` | ||
|
||
## JSON Mode | ||
|
||
```python | ||
import instructor | ||
from instructor import Mode | ||
from openai import OpenAI | ||
|
||
client = instructor.patch(OpenAI(), mode=Mode.JSON) | ||
``` | ||
|
||
### Schema Integration | ||
|
||
In JSON Mode, the schema is part of the system message: | ||
|
||
```python | ||
import instructor | ||
from openai import OpenAI | ||
|
||
client = instructor.patch(OpenAI()) | ||
|
||
response = client.chat.completions.create( | ||
model="gpt-3.5-turbo-1106", | ||
response_format={"type": "json_object"}, | ||
messages=[ | ||
{ | ||
"role": "system", | ||
"content": f"Match your response to this json_schema: \n{UserExtract.model_json_schema()['properties']}", | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Extract jason is 25 years old", | ||
}, | ||
], | ||
) | ||
user = UserExtract.from_response(response, mode=Mode.JSON) | ||
assert user.name.lower() == "jason" | ||
assert user.age == 25 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.