Convert Python function to GPT function call API

This is not a question, but rather more of an announcement that could be useful for the community.

Recently, OpenAI has released the GPT function API that allows models like GPT-4 to “invoke” functions on the user’s behalf. The problem is that the format, in which these functions is specified, is JSON Schema, which is relatively niche and hard to work with. I created a GitHub repo that addresses this issue. It converts regular Python function declarations into JSON consumable by the OpenAI API.

For example, this Python code:

def get_current_weather(location: str, format: Literal["fahrenheit", "celsius"]):
    """
    Get the current weather
    :param location: The city and state, e.g. San Francisco, CA
    :param format: The temperature unit to use. Infer this from the users location.
    """

Produces the following JSON that matches one of OpenAI’s own examples:

{
    'name': 'get_current_weather',
    'description': 'Get the current weather',
    'parameters': {
        'type': 'object',
        'properties': {
            'location': {
                'description': 'The city and state, e.g. San Francisco, CA',
                'type': 'string'
            },
            'format': {
                'description': 'The temperature unit to use. Infer this from the
users location.',
                'type': 'string',
                'enum': ('fahrenheit', 'celsius')
            }
        }
    },
    'required': ['location', 'format']
}

The project is in its very early stages (was hacked in a few hours), but already works for most applications.

You can check it out on my GitHub - py2gpt, user @janekb04. I cannot post a link here.

3 Likes

Thank you this was exactly what I was looking for! I will check out your lib

  • Creating links (New user - Trust Level 0)
    New users do not have the ability to post links on this Discourse forum. Yes most Discourse forums allow new users to post links but this site was configured to disallow that.
    It takes about 12 minutes to get to Trust Level 1 which does allow a user to post links on this Discourse forum.
  • To get from Trust Level 0 (New user) to Trust Level 1 (Basic user)
    • Entering at least 5 topics
    • Reading at least 30 posts
    • Spend a total of 10 minutes reading posts

Excerpt from here.