Giter Club home page Giter Club logo

openai-functions's Introduction

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.

Tests Coverage Status License: MIT PyPI version Documentation Status

Installation

You can install openai-functions from PyPI using pip:

pip install openai-functions

Usage

  1. Import the necessary modules and provide your API key:
import enum
import openai
from openai_functions import Conversation

openai.api_key = "<YOUR_API_KEY>"
  1. Create a Conversation instance:
conversation = Conversation()
  1. 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
    """
    return {
        "location": location,
        "temperature": "72",
        "unit": unit.value,
        "forecast": ["sunny", "windy"],
    }
  1. 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

  1. 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>"
  1. Define your data container using the @nlp decorator:
@nlp
@dataclass
class Person:
    """Extract personal info"""

    name: str
    age: int
  1. 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.

openai-functions's People

Contributors

rizerphe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

openai-functions's Issues

BrokenSchemaError

If I get a BrokenSchemaError, is there any way I can inspect both the prompt that was sent to OpenAI and the raw output we got from the model? Thanks.

Not able to connect with Azure OpenAI

I am trying to utilize this library using Azure OpenAI. Here I am not directly connecting to OpenAI; here I have created a deployment in Azure, and with the help of that, I am connecting to OpenAI. Whenever i am connecting through Azure, I need to provide details like "openai.api_key", "openai.api_base", "openai.api_type," and engine/deployment_id, which were created in Azure Openai, to be passed to the "openai.ChatCompletion.create() method.

"Conversation" model asking for "model_id", Here I am trying to pass Azure "deployment_id" or "engine" but it's not working. I have passed "model_name" as well, but still it's showing me an error as "InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a ." I have attached the error screenshot.
image

Could you please make it work for the Azure OpenAI engine as well?

Async Support

Thanks for creating this. It's the best python openai wrapper on the market and I tried a few.

Do you plan to add async support? It would be especially useful for streaming responses. It's good to have something happening on the screen when you're working with an assistant type application.

If you are unwilling to work on this feature I can offer some help myself. Let me know.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.