插件#

Agent与插件能力是大模型能否自动化的核心,在本的项目中,原生支持插件模式,大模型可以自动化完成目标。 同时为了充分发挥社区的优势,本项目中所用的插件原生支持Auto-GPT插件生态,即Auto-GPT的插件可以直接在我们的项目中运行。

Local Plugins#

1.1 How to write local plugins.#

  • Local plugins use the Auto-GPT plugin template. A simple example is as follows: first write a plugin file called “sql_executor.py”.

import pymysql
import pymysql.cursors

def get_conn():
    return pymysql.connect(
        host="127.0.0.1",
        port=int("2883"),
        user="mock",
        password="mock",
        database="mock",
        charset="utf8mb4",
        ssl_ca=None,
    )

def ob_sql_executor(sql: str):
    try:
        conn = get_conn()
        with conn.cursor() as cursor:
            cursor.execute(sql)
            result = cursor.fetchall()
        field_names = tuple(i[0] for i in cursor.description)
        result = list(result)
        result.insert(0, field_names)
        return result
    except pymysql.err.ProgrammingError as e:
        return str(e)

Then set the “can_handle_post_prompt” method of the plugin template to True. In the “post_prompt” method, write the prompt information and the mapped plugin function.

"""This is a template for DB-GPT plugins."""
from typing import Any, Dict, List, Optional, Tuple, TypeVar, TypedDict

from auto_gpt_plugin_template import AutoGPTPluginTemplate

PromptGenerator = TypeVar("PromptGenerator")

class Message(TypedDict):
    role: str
    content: str

class DBGPTOceanBase(AutoGPTPluginTemplate):
    """
    This is an DB-GPT plugin to connect OceanBase.
    """

    def __init__(self):
        super().__init__()
        self._name = "DB-GPT-OB-Serverless-Plugin"
        self._version = "0.1.0"
        self._description = "This is an DB-GPT plugin to connect OceanBase."

    def can_handle_post_prompt(self) -> bool:
        return True

    def post_prompt(self, prompt: PromptGenerator) -> PromptGenerator:
        from .sql_executor import ob_sql_executor

        prompt.add_command(
            "ob_sql_executor",
            "Execute SQL in OceanBase Database.",
            {"sql": "<sql>"},
            ob_sql_executor,
        )
        return prompt
    ...

1.2 How to use local plugins#

  • Pack your plugin project into your-plugin.zip and place it in the /plugins/ directory of the DB-GPT project. After starting the webserver, you can select and use it in the Plugin Model section.

Public Plugins#

1.1 How to use public plugins#

  • By default, after launching the webserver, plugins from the public plugin library DB-GPT-Plugins will be automatically loaded. For more details, please refer to DB-GPT-Plugins

1.2 Contribute to the DB-GPT-Plugins repository#

  • Please refer to the plugin development process in the public plugin library, and put the configuration parameters in .plugin_env

  • We warmly welcome everyone to contribute plugins to the public plugin library!