自定义 LLM 工具#
在本文档中,我们将指导您完成自定义 LLM 工具的过程,允许用户使用 PromptTemplate
无缝连接到具有 Prompt 调优经验的大型语言模型。
先决条件#
请确保您的 适用于 VS Code 的 Prompt flow 已更新到 1.8.0 或更高版本。
如何自定义 LLM 工具#
这里我们使用 一个现有工具包 作为示例。如果您想创建自己的工具,请参考 创建和使用工具包。
按照 此示例 中的方法开发工具代码。
为工具添加
CustomConnection
输入,用于认证并建立与大型语言模型的连接。为工具添加
PromptTemplate
输入,作为传递给大型语言模型的参数。from jinja2 import Template from promptflow.core import tool from promptflow.connections import CustomConnection from promptflow.contracts.types import PromptTemplate @tool def my_tool( connection: CustomConnection, api: str, deployment_name: str, temperature: float, prompt: PromptTemplate, **kwargs ) -> str: # Replace with your tool code, customise your own code to handle and use the prompt here. # Usually connection contains configs to connect to an API. # Not all tools need a connection. You can remove it if you don't need it. rendered_prompt = Template(prompt, trim_blocks=True, keep_trailing_newline=True).render(**kwargs) return rendered_prompt
生成自定义 LLM 工具的 YAML 文件。
在您的工具项目目录中运行以下命令以自动生成您的工具 YAML 文件,使用 -t “custom_llm” 或 –tool-type “custom_llm” 来指示这是一个自定义 LLM 工具python <promptflow github repo>\scripts\tool\generate_package_tool_meta.py -m <tool_module> -o <tool_yaml_path> -t "custom_llm"
这里我们使用 一个现有工具 作为示例。
cd D:\proj\github\promptflow\examples\tools\tool-package-quickstart python D:\proj\github\promptflow\scripts\tool\generate_package_tool_meta.py -m my_tool_package.tools.tool_with_custom_llm_type -o my_tool_package\yamls\tool_with_custom_llm_type.yaml -n "My Custom LLM Tool" -d "This is a tool to demonstrate how to customize an LLM tool with a PromptTemplate." -t "custom_llm"
此命令将生成如下所示的 YAML 文件
my_tool_package.tools.tool_with_custom_llm_type.my_tool: name: My Custom LLM Tool description: This is a tool to demonstrate how to customize an LLM tool with a PromptTemplate. # The type is custom_llm. type: custom_llm module: my_tool_package.tools.tool_with_custom_llm_type function: my_tool inputs: connection: type: - CustomConnection api: type: - string deployment_name: type: - string temperature: type: - double
在 VS Code 中使用工具#
按照步骤 构建和安装您的工具包 以及 从 VS Code 扩展中使用您的工具。
这里我们使用一个现有流程来演示体验,在 VS Code 扩展中打开 此流程。
有一个名为“my_custom_llm_tool”的节点,带有一个提示模板文件。您可以使用现有文件或创建一个新文件作为提示模板文件。
常见问题#
我能否自定义工具输入的文本框大小?#
是的,您可以为您的工具输入添加 ui_hints.text_box_size
字段。有 4 种可用大小,从特小到大,分别为 xs
、sm
、md
、lg
。下表提供了这些大小的详细信息
值 |
描述 |
UI 显示大小 |
---|---|---|
xs |
特小 |
40px |
sm |
小 |
80px |
md |
中 |
130px |
lg |
大 |
180px |
您可以根据工具输入的预期值长度选择使用不同的值。以下面的 YAML 为例
my_tool_package.tools.tool_with_custom_llm_type.my_tool:
name: My Custom LLM Tool
description: This is a tool to demonstrate how to customize an LLM tool with a PromptTemplate.
type: custom_llm
module: my_tool_package.tools.tool_with_custom_llm_type
function: my_tool
inputs:
connection:
type:
- CustomConnection
ui_hints:
text_box_size: lg
api:
type:
- string
ui_hints:
text_box_size: sm
deployment_name:
type:
- string
ui_hints:
text_box_size: md
temperature:
type:
- double
ui_hints:
text_box_size: xs
当您在 此示例流程 中使用该工具时,您可以看到输入文本框的大小按设置的值显示。