跳至橫幅的結尾
前往橫幅的開頭

Plugin Example Code

跳至中繼資料的結尾
前往中繼資料的開頭

您正在檢視此頁面的舊版本。請檢視目前版本

比較目前 檢視頁面歷程記錄

版本 1 目前 »

執行deserver.py 可以將程式碼轉換成一行.

檔案下載:
相關使用教學請參考 :

DaVinci Developer Guide.mp4

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/

What can I do with python plugin?

  1. You can define your own python functions for Davinci to invoke.

  2. You can access the PLUGIN_USER_COOKIE global variable to identify who your user is.

  3. You can access the SELECTED_FILES global variable to read user selected files.

  4. You can access the SELECTED_MODEL_TOKEN_LIMIT global variable and count_token function to keep response tokens under model limit.

  5. You can access the CURRENT_CONVERSATION global variable and infer_params function to get required inputs.

  6. You can use the chat function to access the GPT model. See the multiple_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:

#use this function to get user's username
async def get_username() -> str

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.

  • 無標籤