Plugin Example Code
執行devserver.py 可以將程式碼轉換成一行.
檔案下載:
相關使用教學請參考 :
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 Pyodide — Version 0.26.4
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.
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 plugin.json template. 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 devserver.py that combines those files for you
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.