Skip to main content

No project description provided

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.

Tests Coverage Status

Installation

You can install openai-functions from PyPI using pip:

pip install openai-functions

Usage

  1. Import the necessary modules:
import enum
import openai
from openai_functions import Runner
  1. Create a Runner instance:
runner = Runner()
  1. Define your functions using the @runner.add_function decorator:
class Unit(enum.Enum):
    FAHRENHEIT = "fahrenheit"
    CELSIUS = "celsius"

@runner.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

    Returns:
        dict: A dictionary containing the current weather
    """
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit.value,
        "forecast": ["sunny", "windy"],
    }
    return weather_info
  1. Add user messages using the runner.add_message method:
runner.add_message(
    {
        "role": "user",
        "content": "What's the weather in San Francisco?",
    }
)
  1. Call the runner.run_until_response() method to trigger the conversation and retrieve the response:
print(runner.run_until_response())

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


Download files

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

Source Distribution

openai_functions-0.2.4.tar.gz (8.6 kB view hashes)

Uploaded Source

Built Distribution

openai_functions-0.2.4-py3-none-any.whl (13.8 kB view hashes)

Uploaded Python 3

Supported by

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