自动插件选择
在TaskWeaver中,我们提供了自动插件选择机制,可以动态地为每个用户请求选择最佳插件。 该机制旨在解决以下两个问题:
- 过多的插件可能会使LLM产生混淆,导致生成正确代码的准确性下降。
- 大量插件可能导致令牌使用量增加(可能超过LLM的令牌限制)并延长响应时间。
自动插件选择概述
以下是自动插件选择机制的工作流程概览。

自动插件选择机制仅在代码解释器的代码生成过程中激活,不会影响规划器的计划制定过程。
在TaskWeaver初始化开始时,自动插件选择器会被激活,为所有插件生成嵌入向量,包括它们的名称和描述。
这些嵌入向量是通过在taskweaver_config.json文件中配置的指定嵌入模型创建的。
更多信息,请参阅文档中的embedding部分。
当Planner向代码解释器发送请求时,将触发自动插件选择机制。
系统会首先使用相同的嵌入模型为请求生成嵌入向量。
然后计算请求嵌入向量与所有插件嵌入向量之间的余弦相似度。
选择余弦相似度得分最高的前k个插件,并将它们加载到code_generator提示中。
在完成代码生成后,code_generator会使用一个或多个插件来生成所需代码。
我们建立了一个插件池来存储参与代码生成过程的插件,同时过滤掉未使用的插件。
在后续的自动插件选择阶段,新选中的插件会被追加到现有插件中。
自动插件选择配置
code_generator.enable_auto_plugin_selection: 是否启用自动插件选择功能。默认值为false。code_generator.auto_plugin_selection_topk: 每轮自动选择的插件数量。默认值为3。
自动插件选择准备
在使用自动插件选择机制之前,我们需要运行以下命令来生成带有嵌入向量的插件元数据文件。
cd scripts
python -m plugin_mgt --refresh
之后,您可以在plugins文件夹中找到生成的.meta目录。
然后您可以启动一个新的TaskWeaver会话,并启用自动插件选择机制。
代码生成器将自动加载带有嵌入的插件元文件。
🎈插件元文件在以下情况下将被视为无效:
- 插件嵌入向量尚未生成。
- 插件已修改。
- 插件嵌入模型已更改。
在这种情况下,您无法启动TaskWeaver,需要重新运行上述命令来刷新插件元数据文件。
## Auto Plugin Selection Example
We show the auto plugin selection mechanism in the following example.
First, we start TaskWeaver with the auto plugin selection mechanism enabled.
```bash
=========================================================
_____ _ _ __
|_ _|_ _ ___| | _ | | / /__ ____ __ _____ _____
| |/ _` / __| |/ /| | /| / / _ \/ __ `/ | / / _ \/ ___/
| | (_| \__ \ < | |/ |/ / __/ /_/ /| |/ / __/ /
|_|\__,_|___/_|\_\|__/|__/\___/\__,_/ |___/\___/_/
=========================================================
TaskWeaver: I am TaskWeaver, an AI assistant. To get started, could you please enter your request?
Human:
然后我们可以检查logs文件夹中的日志文件task_weaver.log,查看自动插件选择器是否初始化成功,因为打印了Plugin embeddings generated消息。
2023-12-18 14:23:44,197 - INFO - Planner initialized successfully
2023-12-18 14:24:10,488 - INFO - Plugin embeddings generated
2023-12-18 14:24:10,490 - INFO - CodeInterpreter initialized successfully.
2023-12-18 14:24:10,490 - INFO - Session 20231218-062343-c18494b1 is initialized
我们让TaskWeaver"帮我搜索Xbox的价格"。 规划器(Planner)指示代码解释器(Code Interpreter)搜索Xbox价格。
TaskWeaver: I am TaskWeaver, an AI assistant. To get started, could you please enter your request?
Human: search xbox price for me
>>> [INIT_PLAN]
1. search xbox price
2. report the result to the user <interactively depends on 1>
>>> [PLAN]
1. instruct CodeInterpreter to search xbox price
2. report the result to the user
>>> [CURRENT_PLAN_STEP]
1. instruct CodeInterpreter to search xbox price
>>> [SEND_TO]
CodeInterpreter
>>> [MESSAGE]
Please search xbox price
>>> [PLANNER->CODEINTERPRETER]
Please search xbox price
回到代码解释器,自动插件选择机制被触发。
我们可以再次检查日志文件task_weaver.log,查看自动插件选择器选出的余弦相似度得分最高的前3个插件。
023-12-18 14:24:34,513 - INFO - Planner talk to CodeInterpreter: Please search xbox price using klarna_search plugin
2023-12-18 14:24:34,669 - INFO - Selected plugins: ['klarna_search', 'sql_pull_data', 'paper_summary']
2023-12-18 14:24:34,669 - INFO - Selected plugin pool: ['klarna_search', 'sql_pull_data', 'paper_summary']
然后代码解释器将使用选定的插件生成代码。
>>> [THOUGHT]
ProgramApe will call the klarna_search plugin function to search for Xbox prices.
>>> [PYTHON]
search_results, description = klarna_search(query="xbox")
search_results, description
>>> [VERIFICATION]
NONE
>>> [STATUS]
SUCCESS
>>> [RESULT]
The execution of the generated python code above has succeeded
The result of above Python code after execution is:
( name price url attributes
0 Microsoft Xbox Series X - Black Edition $399.00 https://www.klarna.com/us/shopping/pl/cl52/495... [Release Year:2020, Included Accessories:1 gam...
1 Microsoft Xbox Series S 1TB - Black $349.00 https://www.klarna.com/us/shopping/pl/cl52/320... [Included Accessories:1 gamepad, Media Type:DV...
.. ... ... ... ...
3 Xbox Series S – Starter Bundle $239.00 https://www.klarna.com/us/shopping/pl/cl52/320... [Platform:Xbox One]
4 Microsoft Xbox Series X 1TB Console - Diablo I... $385.58 https://www.klarna.com/us/shopping/pl/cl52/320... [Release Year:2023, Included Accessories:1 gam...
[5 rows x 4 columns],
'The response is a dataframe with the following columns: name, price, url, attributes. The attributes column is a list of tags. The price is in the format of $xx.xx.')
>>> [CODEINTERPRETER->PLANNER]
The following python code has been executed:
```python
search_results, description = klarna_search(query="xbox")
search_results, description
```
The execution of the generated python code above has succeeded
The result of above Python code after execution is:
( name price url attributes
0 Microsoft Xbox Series X - Black Edition $399.00 https://www.klarna.com/us/shopping/pl/cl52/495... [Release Year:2020, Included Accessories:1 gam...
1 Microsoft Xbox Series S 1TB - Black $349.00 https://www.klarna.com/us/shopping/pl/cl52/320... [Included Accessories:1 gamepad, Media Type:DV...
.. ... ... ... ...
3 Xbox Series S – Starter Bundle $239.00 https://www.klarna.com/us/shopping/pl/cl52/320... [Platform:Xbox One]
4 Microsoft Xbox Series X 1TB Console - Diablo I... $385.58 https://www.klarna.com/us/shopping/pl/cl52/320... [Release Year:2023, Included Accessories:1 gam...
[5 rows x 4 columns],
'The response is a dataframe with the following columns: name, price, url, attributes. The attributes column is a list of tags. The price is in the format of $xx.xx.')
最后,规划器(Planner)将向用户报告结果。
>>> [INIT_PLAN]
1. search xbox price using klarna_search plugin
2. report the result to the user <interactively depends on 1>
>>> [PLAN]
1. instruct CodeInterpreter to search xbox price using klarna_search plugin
2. report the result to the user
>>> [CURRENT_PLAN_STEP]
2. report the result to the user
>>> [SEND_TO]
User
>>> [MESSAGE]
Here are some Xbox prices I found:
1. Microsoft Xbox Series X - Black Edition: $399.00
2. Microsoft Xbox Series S 1TB - Black: $349.00
3. Xbox Series S – Starter Bundle: $239.00
4. Microsoft Xbox Series X 1TB Console - Diablo Immortal Edition: $385.58
Please note that these prices are subject to change and availability.
>>> [PLANNER->USER]
Here are some Xbox prices I found:
1. Microsoft Xbox Series X - Black Edition: $399.00
2. Microsoft Xbox Series S 1TB - Black: $349.00
3. Xbox Series S – Starter Bundle: $239.00
4. Microsoft Xbox Series X 1TB Console - Diablo Immortal Edition: $385.58
Please note that these prices are subject to change and availability.
TaskWeaver: Here are some Xbox prices I found:
1. Microsoft Xbox Series X - Black Edition: $399.00
2. Microsoft Xbox Series S 1TB - Black: $349.00
3. Xbox Series S – Starter Bundle: $239.00
4. Microsoft Xbox Series X 1TB Console - Diablo Immortal Edition: $385.58
Please note that these prices are subject to change and availability.