Davinci Python Plugin Quickstart
What is a Python plugin?
Python plugin is a package of your python source code and a set of prompts to let model know how to use your python source code.
Your python source code will be executed on users' browser by https://pyodide.org/en/stable/
Pyodide already has some popular python packages builtin, such as pandas, you can also use the micropip
to install non builtin packages.
What can I do with python plugin?
You can define your own python functions for Davinci to invoke.
You can access the
PLUGIN_USER_COOKIE
global variable to identify who your user is.You can access the
SELECTED_FILES
global variable to read user selected files.You can access the
SELECTED_MODEL_TOKEN_LIMIT
global variable andcount_token
function to keep response tokens under model limit.You can access the
CURRENT_CONVERSATION
global variable andinfer_params
function to get required inputs.You can use the
chat
function to access the GPT model. See themultiple_function
example for more detail.You can use the
emb
function to embed a text chunk. See theembedding
example for more detail.
How to build a Python plugin?
For example, let's say we are going to build a plugin to retrive user's username.
First, write your python source in one file.
import json
import asyncio
from pyodide.http import pyfetch
resp = await pyfetch(
'/api/userinfo',
method='POST',
headers={ 'Content-Type': 'application/json' },
body=json.dumps({ 'cookie': PLUGIN_USER_COOKIE })
)
print((await resp.json())['username'])
Save the above python script to plugin_source.py
.
Second, prepare the plugin.json
Modify the template. Fill id
, schema_version
, name_for_human
, name_for_model
, description_for_human
, description_for_human
, description_for_model
these fields first and all the fields are required.
{
"id": "< your plugin id >",
"schema_version": "v1",
"name_for_human": "< your plugin name, This will be displayed to user >",
"name_for_model": "< your plugin id, should be the same as the previous id >",
"description_for_human": "< describe what your plugin can do here. This will be displayed to user >",
"description_for_model": "< describe what your plugin can do here. This will be considered by model >",
"auth": {
"type": "none"
},
"api": {
"type": "python",
"python": {
"source": "< content of plugin_source.py >"
}
}
}
Third, upload plugin.json to Davinci with the that combines those files for you
devserver.py will make your python code into one line and fill into source
automatically.
python devserver.py
And register the url <http://localhost:5003
> to the Davinci. Davinci will fetch this url every time it want to use your plugin.
Optional, plugin code generation with usage_hint
You can also instruct the model to generate code that uses the functions defined in your source. For example, you can define a get_username
function in the source and prepare the following hint:
Save the above description to plugin_hint.txt
.
The model will use this hint to generate a code snippet and execute it after loading the source.