Skip to main content

Helper functions that allow us to improve openai's function_call ergonomics

Project description

Getting Started with Instructor

Structured extraction in Python, powered by OpenAI's function calling api, designed for simplicity, transparency, and control.


Star us on Github!.

Buy Me a Coffee Downloads GitHub stars Documentation Twitter Follow GitHub issues GitHub license Github discussions PyPI version PyPI pyversions

Built to interact solely with openai's function calling api from python. It's designed to be intuitive, easy to use, and provide great visibility into your prompts.

Usage

from openai import OpenAI
import instructor

# Enables `response_model`
client = instructor.patch(OpenAI())

class UserDetail(BaseModel):
    name: str
    age: int

user = client.chat.completions.create(
    model="gpt-3.5-turbo",
    response_model=UserDetail,
    messages=[
        {"role": "user", "content": "Extract Jason is 25 years old"},
    ]
)

assert isinstance(user, UserDetail)
assert user.name == "Jason"
assert user.age == 25

!!! note "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:

```python hl_lines="4 8"
import openai
import instructor

instructor.patch()

user = openai.ChatCompletion.create(
    ...,
    response_model=UserDetail,
)
```

Installation

To get started you need to install it using pip. Run the following command in your terminal:

$ pip install instructor

Quick Start

To simplify your work with OpenAI we offer a patching mechanism for the ChatCompletion class. The patch introduces 3 features to the ChatCompletion class:

  1. The response_model parameter, which allows you to specify a Pydantic model to extract data into.
  2. The max_retries parameter, which allows you to specify the number of times to retry the request if it fails.
  3. The validation_context parameter, which allows you to specify a context object that validators have access to.

!!! 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/)

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.

import instructor
from openai import OpenAI
from pydantic import BaseModel

# This enables response_model keyword
# from client.chat.completions.create
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.

from pydantic import BaseModel

class UserDetail(BaseModel):
    name: str
    age: int

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.

user: UserDetail = client.chat.completions.create(
    model="gpt-3.5-turbo",
    response_model=UserDetail,
    messages=[
        {"role": "user", "content": "Extract Jason is 25 years old"},
    ]
)

assert user.name == "Jason"
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.

from pydantic import BaseModel, ValidationError, BeforeValidator
from typing_extensions import Annotated
from instructor import llm_validator

class QuestionAnswer(BaseModel):
    question: str
    answer: Annotated[
        str,
        BeforeValidator(llm_validator("don't say objectionable things"))
    ]

try:
    qa = QuestionAnswer(
        question="What is the meaning of life?",
        answer="The meaning of life is to be evil and steal",
    )
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.

1 validation error for QuestionAnswer
answer
   Assertion failed, The statement is objectionable. (type=assertion_error)

Reask on validation error

Here, the UserDetails model is passed as the response_model, and max_retries is set to 2.

from openai import OpenAI
import instructor

from pydantic import BaseModel, field_validator

# Apply the patch to the OpenAI client
client = instructor.patch(OpenAI())

class UserDetails(BaseModel):
    name: str
    age: int

    @field_validator("name")
    @classmethod
    def validate_name(cls, v):
        if v.upper() != v:
            raise ValueError("Name must be in uppercase.")
        return v

model = client.chat.completions.create(
    model="gpt-3.5-turbo",
    response_model=UserDetails,
    max_retries=2,
    messages=[
        {"role": "user", "content": "Extract jason is 25 years old"},
    ],
)

assert model.name == "JASON"

License

This project is licensed under the terms of the MIT License.

Project details


Release history Release notifications | RSS feed

This version

0.3.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

instructor-0.3.1.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

instructor-0.3.1-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file instructor-0.3.1.tar.gz.

File metadata

  • Download URL: instructor-0.3.1.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.10.13 Linux/6.2.0-1015-azure

File hashes

Hashes for instructor-0.3.1.tar.gz
Algorithm Hash digest
SHA256 950b73b469e35e017de845c027a7bf9f4e502e7c23e5f99ec3bf3e14e2d2cd35
MD5 3e97bec2293cd530a0423f4aa8e989d7
BLAKE2b-256 25822e90ddb4ebfd604ed3214e5580998491405f36c5b494f6b49cace45c8e55

See more details on using hashes here.

File details

Details for the file instructor-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: instructor-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.10.13 Linux/6.2.0-1015-azure

File hashes

Hashes for instructor-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 401ebe37b287ae9c4e6e8c041d0ea18394d832cd27a6c4572d8e8d025334d3b8
MD5 ec8fdd6d3b333b7e03d8847768f32f89
BLAKE2b-256 c4a0eacb8acebaf2056035ea75f3729ad6404b4462e50e069857213b3c4e9d76

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page