Giter Site home page Giter Site logo

janekb04 / py2gpt Goto Github PK

View Code? Open in Web Editor NEW
24.0 4.0 6.0 734 KB

Convert Python code into JSON consumable by OpenAI's function API.

Home Page: https://janekb04.github.io/py2gpt/

License: MIT License

HTML 30.72% Python 69.28%
ai api chatgpt gpt gpt-4 json openai openai-api python converter function nlp schema transcoding

py2gpt's Introduction

py2gpt

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. This repository 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']
}

Roadmap

  • Support classes (treat them as objects with certain `"properties"
  • Support methods (treat them as functions with an additional self parameter`)
  • Support multiple functions
  • Support Enums
  • Support named tuples
  • Make the code safe for local execution
  • Refactor code and make locally executable

py2gpt's People

Contributors

janekb04 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

Watchers

 avatar  avatar  avatar  avatar

py2gpt's Issues

using it as a library

Hey there! Great little project you wrote! I find your implementation to be way better for handling functions and more focused and strict than langchain's one for example, it's great not having to use pydantic or some middle step

In fact I've used in my library, check it out: https://github.com/rogeriochaves/litechain/blob/main/litechain/contrib/utils/open_ai_functions.py

I've just made a small change:

     if (
-        len(func.body) != 1
+        len(func.body) == 0
         or not isinstance(func.body[0], ast.Expr)
         or not isinstance(func.body[0].value, ast.Str)
     ):
-        raise ValueError("The function body should only contain a docstring")
+        raise ValueError("The function body should contain a docstring")

This is because I want my functions to have actual execution bodies too, so LiteChain actually calls them directly. I've also removed the pyscript bits, as I'm just using it as a module.

It is working well, however I didn't want to maintain this inside LiteChain, do you have plans to publish it as a library? I can help providing updates as well

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.