Create ChatGPT plugins from Python code
Project description
AutoPlugin
AutoPlugin is a Python package that makes it easy to convert Python functions into ChatGPT plugins. With just a couple lines of code, you can:
- Automatically create an OpenAPI spec with custom endpoints for your registered Python functions, telling ChatGPT how to use it. Pull endpoint descriptions from the function docstring or generate them automatically with the OpenAI API.
- Generate the
ai-plugin.json
file to register your plugin with ChatGPT. - Launch a local server that can be used by ChatGPT for development.
Installation
To install AutoPlugin, simply run the following command:
pip install autoplugin
To install with the ability to generate endpoint descriptions for the OpenAPI specification automatically from source code, install with
pip install autoplugin[gen]
Basic Usage
To get started with AutoPlugin, follow these steps:
- Import the necessary functions from AutoPlugin and FastAPI:
from autoplugin.autoplugin import register, generate, launch
from fastapi import FastAPI
- Create a FastAPI app instance:
app = FastAPI()
- Use the register decorator to register your functions as API endpoints.
AutoPlugin generates function descriptions in the OpenAPI spec so that ChatGPT knows how to use your endpoints.
By default, the description is fetched from the docstring. If there's no docstring, or if you specify
generate_description=True
, AutoPlugin will generate one automatically from OpenAI's API (requires setting theOPENAI_API_KEY
environment variable). Finally, you can specify a description (e.g. if the docstring contains extra information not needed in the OpenAPI description) by passing thedescription
keyword argument:
@register(app, methods=["GET", "POST"], generate_description=True)
async def hello(name: str, age: int = 5) -> str:
return f"Hello, {name}! Age {age}."
- Generate the necessary files (
openapi.yaml
andai-plugin.json
) for your ChatGPT plugin. Optionally, specifyout_dir
to change where they're saved to:
generate(app) # generated files saved to `.well-known/` directory
- Launch the server. Optionally, specify
host
andport
:
launch(app) # API hosted at localhost:8000
Example
Here's a complete example that demonstrates how to use AutoPlugin to create API endpoints for two functions, hello
and add
.
It also generates the openapi.yaml
and ai-plugin.json
files, by default in the .well-known
directory. :
from autoplugin.autoplugin import register, generate, launch
from fastapi import FastAPI
app = FastAPI()
@register(app, methods=["GET", "POST"])
async def hello(name: str, age: int = 5) -> str:
return f"Hello, {name}! Age {age}."
@register(app, methods=["GET"])
async def add(a: int, b: int) -> int:
""" Adds two numbers """
return a + b
# Generate the necessary files
generate(app)
# Launch the server
launch(app)
This example creates a FastAPI server with two endpoints, /hello
and /add
, that can be accessed using GET or POST requests.
AutoPlugin will use the docstring for the OpenAPI description of /add
and generate an automatic description for /hello
by passing the source code of the function to OpenAI's API.
Testing
AutoPlugin also provides a testing_server
utility (courtesy of florimondmanca) for testing your endpoints. Here's an example of how you can use it to test the /hello
and /add
endpoints from the example above:
from autoplugin.testing import testing_server
from os.path import join
import requests
def test_api():
host = "127.0.0.1"
port = 8000
server, base_url = testing_server(host=host, port=port, app_file="path/to/example.py", app_var="app")
with server.run_in_thread():
# Server is started. Do your tests here.
response = requests.post(join(base_url, "hello"), json={"name": "John Doe", "age": 31})
assert response.json() == {"result": "Hello, John Doe! Age 31."}
response = requests.get(join(base_url, "hello"), params={"name": "Jane Smith"})
assert response.json() == {"result": "Hello, Jane Smith! Age 5."}
response = requests.get(join(base_url, "add"), params={"a": 6, "b": 8})
assert response.json() == {"result": 14}
# Server will be stopped.
test_hello()
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
Built Distribution
Hashes for autoplugin-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40dbc011888d1ba00cfd80dc5bbca504cc500bd5b3f77eb18d35144c0ed536c9 |
|
MD5 | 710ea490f7d9583890d5d6a5778cda24 |
|
BLAKE2b-256 | e7c2992c7e6d86cba916bb8e7deb9f360bad28ebb3e17bc090d6b7214380cfef |