https://github.com/janekb04/py2gpt
Convert Python code into JSON consumable by OpenAI's function API.
https://github.com/janekb04/py2gpt
ai api chatgpt converter function gpt gpt-4 json nlp openai openai-api python schema transcoding
Last synced: 11 months ago
JSON representation
Convert Python code into JSON consumable by OpenAI's function API.
- Host: GitHub
- URL: https://github.com/janekb04/py2gpt
- Owner: janekb04
- License: mit
- Created: 2023-06-18T19:43:16.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-26T06:55:58.000Z (over 2 years ago)
- Last Synced: 2024-11-05T06:34:18.252Z (over 1 year ago)
- Topics: ai, api, chatgpt, converter, function, gpt, gpt-4, json, nlp, openai, openai-api, python, schema, transcoding
- Language: Python
- Homepage: https://janekb04.github.io/py2gpt/
- Size: 717 KB
- Stars: 25
- Watchers: 4
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```py
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:
```js
{
'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 `object`s with certain `"properties"
- [ ] Support methods (treat them as functions with an additional `self` parameter`)
- [x] Support multiple functions
- [ ] Support `Enum`s
- [ ] Support named tuples
- [ ] Make the code safe for local execution
- [ ] Refactor code and make locally executable