跳至主要内容

开发新插件的示例

在本教程中,我们将介绍如何在TaskWeaver中开发一个基础插件。该插件可以将输入文本渲染为ASCII艺术字。

实现Python代码

plugins文件夹中创建一个名为ascii_render.py的Python文件。文件名应与插件名称相同。插件名称在插件模式中定义。在本示例中,插件名称为ascii_render。 以下代码是插件实现的模板。

from taskweaver.plugin import Plugin, register_plugin

@register_plugin
class PluginTemplate(Plugin):
def __call__(self, *args, **kwargs):
"""Implementation Starts"""
result, description = YourImplementation()
"""Implementation Ends"""

# if your want to add artifact from the execution result, uncomment the following code
# self.ctx.add_artifact(
# name="artifact",
# file_name="artifact.csv",
# type="df",
# val=result,
# )
return result, description

实现该插件的典型方法是修改位于Implementation StartsImplementation Ends之间的代码。请注意返回的是两个变量resultdescriptionresult存储后续处理所需的任何输出(例如DataFrame),description则是用于描述结果的字符串。

让我们对代码做一些修改,结果如下:

from taskweaver.plugin import Plugin, register_plugin

@register_plugin
class AsciiRenderPlugin(Plugin):
def __call__(self, text: str):
import pyfiglet
ascii_art_str = pyfiglet.figlet_format(text, font='isometric1')
return ascii_art_str

请注意,此功能依赖于pyfiglet包,因此我们需要通过pip install pyfiglet来安装它。

配置模式

接下来,我们需要配置模式(schema),以便LLM能够理解该插件的功能。在模式中,有几个需要填写的字段,包括nameenabledrequireddescriptionparametersreturns。 更多详情请查看插件介绍。 创建一个名为ascii_render.yaml的yaml文件,并将以下内容复制到其中。

name: ascii_render
enabled: true
required: true
description: >-
This plugin renders the input text into ASCII art form.
The input should be a string and the output is also a string in ASCII art.
For example, result = ascii_render(text='Hello World').

parameters:
- name: text
type: str
required: true
description: >-
This is the input text to be rendered into ASCII art form.

returns:
- name: result
type: str
description: >-
The rendered text in ASCII art.

调用插件

在插件实现并配置完成后,我们可以在对话中调用该插件。 完整对话如下:

=========================================================
_____ _ _ __
|_ _|_ _ ___| | _ | | / /__ ____ __ _____ _____
| |/ _` / __| |/ /| | /| / / _ \/ __ `/ | / / _ \/ ___/
| | (_| \__ \ < | |/ |/ / __/ /_/ /| |/ / __/ /
|_|\__,_|___/_|\_\|__/|__/\___/\__,_/ |___/\___/_/
=========================================================
TaskWeaver: I am TaskWeaver, an AI assistant. To get started, could you please enter your request?
Human: render ABC in ascii art
>>> [INIT_PLAN]
1. Render the text 'ABC' into ASCII art
>>> [PLAN]
1. Instruct CodeInterpreter to render the text 'ABC' into ASCII art
>>> [CURRENT_PLAN_STEP]
1. Instruct CodeInterpreter to render the text 'ABC' into ASCII art
>>> [SEND_TO]
CodeInterpreter
>>> [MESSAGE]
Please render the text 'ABC' into ASCII art
>>> [PLANNER->CODEINTERPRETER]
Please render the text 'ABC' into ASCII art
>>> [PYTHON]
# Call the plugin function to render the text 'ABC' into ASCII art
ascii_art_result = ascii_render("ABC")
ascii_art_result
>>> [VERIFICATION]
NONE
>>> [STATUS]
SUCCESS
>>> [RESULT]
The execution of the generated python code above has succeeded

The result of above Python code after execution is:
___ ___ ___
/\ \ /\ \ /\ \
/::\ \ /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ /:/\:\ \
/::\~\:\ \ /::\~\:\__\ /:/ \:\ \
/:/\:\ \:\__\ /:/\:\ \:|__| /:/__/ \:\__\
\/__\:\/:/ / \:\~\:\/:/ / \:\ \ \/__/
\::/ / \:\ \::/ / \:\ \
/:/ / \:\/:/ / \:\ \
/:/ / \::/__/ \:\__\
\/__/ ~~ \/__/

>>> [CODEINTERPRETER->PLANNER]
The following python code has been executed:

# Call the plugin function to render the text 'ABC' into ASCII art
ascii_art_result = ascii_render("ABC")
ascii_art_result



The execution of the generated python code above has succeeded

The result of above Python code after execution is:
___ ___ ___
/\ \ /\ \ /\ \
/::\ \ /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ /:/\:\ \
/::\~\:\ \ /::\~\:\__\ /:/ \:\ \
/:/\:\ \:\__\ /:/\:\ \:|__| /:/__/ \:\__\
\/__\:\/:/ / \:\~\:\/:/ / \:\ \ \/__/
\::/ / \:\ \::/ / \:\ \
/:/ / \:\/:/ / \:\ \
/:/ / \::/__/ \:\__\
\/__/ ~~ \/__/

>>> [INIT_PLAN]
1. Render the text 'ABC' into ASCII art
>>> [PLAN]
1. Instruct CodeInterpreter to render the text 'ABC' into ASCII art
>>> [CURRENT_PLAN_STEP]
Report the ASCII art to the user
>>> [SEND_TO]
User
>>> [MESSAGE]
Here is the text 'ABC' rendered in ASCII art:
___ ___ ___
/\ \ /\ \ /\ \
/::\ \ /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ /:/\:\ \
/::\~\:\ \ /::\~\:\__\ /:/ \:\ \
/:/\:\ \:\__\ /:/\:\ \:|__| /:/__/ \:\__\
\/__\:\/:/ / \:\~\:\/:/ / \:\ \ \/__/
\::/ / \:\ \::/ / \:\ \
/:/ / \:\/:/ / \:\ \
/:/ / \::/__/ \:\__\
\/__/ ~~ \/__/
>>> [PLANNER->USER]
Here is the text 'ABC' rendered in ASCII art:
___ ___ ___
/\ \ /\ \ /\ \
/::\ \ /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ /:/\:\ \
/::\~\:\ \ /::\~\:\__\ /:/ \:\ \
/:/\:\ \:\__\ /:/\:\ \:|__| /:/__/ \:\__\
\/__\:\/:/ / \:\~\:\/:/ / \:\ \ \/__/
\::/ / \:\ \::/ / \:\ \
/:/ / \:\/:/ / \:\ \
/:/ / \::/__/ \:\__\
\/__/ ~~ \/__/
TaskWeaver: Here is the text 'ABC' rendered in ASCII art:
___ ___ ___
/\ \ /\ \ /\ \
/::\ \ /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ /:/\:\ \
/::\~\:\ \ /::\~\:\__\ /:/ \:\ \
/:/\:\ \:\__\ /:/\:\ \:|__| /:/__/ \:\__\
\/__\:\/:/ / \:\~\:\/:/ / \:\ \ \/__/
\::/ / \:\ \::/ / \:\ \
/:/ / \:\/:/ / \:\ \
/:/ / \::/__/ \:\__\
\/__/ ~~ \/__/
info

在GitHub上提交问题前,如果您在开发插件时遇到任何问题,请查阅常见问题解答