Simplifies the usage of OpenAI's function calling.
Project description
OpenAI functions
The openai-functions
Python project simplifies the usage of OpenAI's 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:
import enum
import openai
from openai_functions import Conversation
- Create a
Conversation
instance:
conversation = Conversation()
- Define your functions using the
@conversation.add_function
decorator:
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
"""
weather_info = {
"location": location,
"temperature": "72",
"unit": unit.value,
"forecast": ["sunny", "windy"],
}
return weather_info
- Ask the AI a question:
conversation.ask("What's the weather in San Francisco?")
Another use case: data extraction
- Import the necessary modules:
from dataclasses import dataclass
import openai
from openai_functions import nlp
- Define your data container using the
@nlp
decorator:
@nlp
@dataclass
class Person:
"""Extract personal info"""
name: str
age: int
- Ask the AI for a summary of the data:
person = Person.from_natural_language("I'm Jack and I'm 20 years old.")
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 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for openai_functions-0.6.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfbaf3521c641c4933a7a2903a6989b03cbd00be93f463e65f9eea765206fb80 |
|
MD5 | 4c4e68c7dc2ac77d8e3d1eb3fc0bb288 |
|
BLAKE2b-256 | d1890df21a525817a508a79f79a5becf101df4684144d90c2d648c415a50cc11 |