Skip to main content

Helper functions that allow us to improve openai's function_call ergonomics fork from jxnl/instructor

Project description

Instructor (openai_function_call)

GitHub stars GitHub forks GitHub issues GitHub license Documentation Buy Me a Coffee Twitter Follow

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

This library is built to interact with openai's function call api from python code, with python structs / objects. It's designed to be intuitive, easy to use, but give great visibily in how we call openai.

Requirements

This library depends on Pydantic and OpenAI that's all.

Installation

To get started with OpenAI Function Call, you need to install it using pip. Run the following command in your terminal:

$ pip install instructor

Quick Start with Patching ChatCompletion

To simplify your work with OpenAI models and streamline the extraction of Pydantic objects from prompts, we offer a patching mechanism for the `ChatCompletion`` class. Here's a step-by-step guide:

Step 1: Import and Patch the Module

First, import the required libraries and apply the patch function to the OpenAI module. This exposes new functionality with the response_model parameter.

import openai
from pydantic import BaseModel
from instructor import patch

patch()

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.

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

Step 3: Extract Data with ChatCompletion

Use the openai.ChatCompletion.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.

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

Step 4: Validate the Extracted Data

You can then validate the extracted data by asserting the expected values. By adding the type things you also get a bunch of nice benefits with your IDE like spell check and auto complete!

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:

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.

from instructor import OpenAISchema
from pydantic import Field

class UserDetails(OpenAISchema):
    "Correctly extracted user information"
    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. The description provides information about the field, giving even more context to the language model.

!!! 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 field descriptions are now part of the schema. This describes on this library's core philosophies.

class UserDetails(OpenAISchema):
    "Correctly extracted user information"

    name: str = Field(..., description="User's full name")
    age: int

UserDetails.openai_schema
{
"name": "UserDetails",
"description": "Correctly extracted user information",
"parameters": {
    "type": "object",
    "properties": {
    "name": {
        "description": "User's full name",
        "type": "string"
    },
    "age": {
        "type": "integer"
    }
    },
    "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.

from instructor import OpenAISchema
from pydantic import Field

class UserDetails(OpenAISchema):
    "Correctly extracted user information"
    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.

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.

Including autocompletion:

autocomplete

And even inline errors

errors

OpenAI Schema and Pydantic

This quick start guide provided you with a basic understanding of how to use OpenAI Function Call for schema extraction and function calls. You can now explore more advanced use cases and creative applications of this library.

Since UserDetails is a OpenAISchems and a pydantic.BaseModel you can use inheritance and nesting to create more complex emails while avoiding code duplication

class UserDetails(OpenAISchema):
    name: str = Field(..., description="User's full name")
    age: int

class UserWithAddress(UserDetails):
    address: str 

class UserWithFriends(UserDetails):
    best_friend: UserDetails
    friends: List[UserDetails]

If you have any questions, feel free to leave an issue or reach out to the library's author on Twitter. For a more comprehensive solution with additional features, consider checking out MarvinAI.

To see more examples of how we can create interesting models check out some examples.

License

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

Project details


Download files

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

Source Distribution

synna_instructor-0.2.8.tar.gz (847.2 kB view details)

Uploaded Source

Built Distribution

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

synna_instructor-0.2.8-py3-none-any.whl (867.9 kB view details)

Uploaded Python 3

File details

Details for the file synna_instructor-0.2.8.tar.gz.

File metadata

  • Download URL: synna_instructor-0.2.8.tar.gz
  • Upload date:
  • Size: 847.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/5.15.49-linuxkit

File hashes

Hashes for synna_instructor-0.2.8.tar.gz
Algorithm Hash digest
SHA256 bf04752204b5c4c80aeca69f601e2d7b431f678af7a407109d865a5f1bb1304b
MD5 7d01dbd38ac55433d04a4ff4b3dea39c
BLAKE2b-256 89d7909fa9a801d22900f58b6a60d8db2c8d675de846d25d7b4a14066b9cd5c0

See more details on using hashes here.

File details

Details for the file synna_instructor-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: synna_instructor-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 867.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/5.15.49-linuxkit

File hashes

Hashes for synna_instructor-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e25f29120430023cb2a98591dd5605b30771ec729e601fd2173b175a81425c58
MD5 17ab5f9ba8f2bee6a0b6f520feab9dea
BLAKE2b-256 ecb70a43494653e7b13565b01785278d48beda7fae59a86814a1e842f2f9706a

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