OpenAI functions
The openai-functions Python project simplifies the usage of OpenAI's ChatGPT function calling feature. It abstracts away the complexity of parsing function signatures and docstrings by providing developers with a clean and intuitive interface.
Installation
You can install openai-functions from PyPI using pip:
pip install openai-functions
Usage
- Import the necessary modules and provide your API key:
import enum
import openai
from openai_functions import Conversation
openai.api_key = "<YOUR_API_KEY>"
- Create a
Conversationinstance:
conversation = Conversation()
- Define your functions using the
@conversation.add_functiondecorator:
class Unit(enum.Enum):
FAHRENHEIT = "fahrenheit"
CELSIUS = "celsius"
@conversation.add_function()
def get_current_weather(location: str, unit: Unit = Unit.FAHRENHEIT) -> dict:
"""Get the current weather in a given location.
Args:
location (str): The city and state, e.g., San Francisco, CA
unit (Unit): The unit to use, e.g., fahrenheit or celsius
"""
return {
"location": location,
"temperature": "72",
"unit": unit.value,
"forecast": ["sunny", "windy"],
}
- Ask the AI a question:
response = conversation.ask("What's the weather in San Francisco?")
# Should return something like:
# The current weather in San Francisco is 72 degrees Fahrenheit and it is sunny and windy.
You can read more about how to use Conversation here.
More barebones use - just schema generation and result parsing:
from openai_functions import FunctionWrapper
wrapper = FunctionWrapper(get_current_weather)
schema = wrapper.schema
result = wrapper({"location": "San Francisco, CA"})
Or you could use skills.
Another use case: data extraction
- Import the necessary modules and provide your API key:
from dataclasses import dataclass
import openai
from openai_functions import nlp
openai.api_key = "<YOUR_API_KEY>"
- Define your data container using the
@nlpdecorator:
@nlp
@dataclass
class Person:
"""Extract personal info"""
name: str
age: int
- Ask the AI for the extracted data:
person = Person.from_natural_language("I'm Jack and I'm 20 years old.")
You can read more about @nlp here.
Note: mypy does not parse class decorators (#3135), so you might have trouble getting type checking when using it like this. Consider using something like nlp(Person).from_natural_language to get proper type support.
How it Works
openai-functions takes care of the following tasks:
- Parsing the function signatures (with type annotations) and docstrings.
- Sending the conversation and function descriptions to the OpenAI model.
- Deciding whether to call a function based on the model's response.
- Calling the appropriate function with the provided arguments.
- Updating the conversation with the function response.
- Repeating the process until the model generates a user-facing message.
This abstraction allows developers to focus on defining their functions and adding user messages without worrying about the details of function calling.
Note
Please note that openai-functions is an unofficial project not maintained by OpenAI. Use it at your discretion.
Metadata
Release files for openai-functions 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openai_functions-1.0.2.tar.gz | 19.6 kB | Without provenance | Details for openai_functions-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openai_functions-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for openai_functions-1.0.2-py3-none-any.whl |
Total release size: 49.2 kB
Release files / openai_functions-1.0.2.tar.gz
| Download URL | openai_functions-1.0.2.tar.gz |
|---|---|
| Size | 19.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc1ee6d578cd2f30d2ea5f9ec9c083cd4804cdae0aa3649b0358a9b6a6a553e8
|
|
BLAKE2b-256 checksum How to use checksums |
f168155642bd0ed69746bacf1dc548a2b4492df7f0b3c4f190da898d4e614bde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.5 Linux/6.4.12-zen1-1-zen
|
Release files / openai_functions-1.0.2-py3-none-any.whl
| Download URL | openai_functions-1.0.2-py3-none-any.whl |
|---|---|
| Size | 29.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
693c68a5dbb3199334c520a8c18e55634e1980ecf26b2f49912e1cb996ece138
|
|
BLAKE2b-256 checksum How to use checksums |
18766a4ca5760420e69707b2055d1387fb80948bb750f772b3881cb516bcb4e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.5 Linux/6.4.12-zen1-1-zen
|
Security for openai-functions 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.