文档

集成

单个工具

测试版功能

插件支持目前处于内部测试阶段。在此加入测试

要设置工具提供程序,首先在插件的 src 目录中创建一个名为 toolsProvider.ts 的文件

import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { existsSync } from "fs";
import { writeFile } from "fs/promises";
import { join } from "path";

export async function toolsProvider(ctl: ToolsProviderController) {
  const tools: Tool[] = [];

  const createFileTool = tool({
    // Name of the tool, this will be passed to the model. Aim for concise, descriptive names
    name: `create_file`,
    // Your description here, more details will help the model to understand when to use the tool
    description: "Create a file with the given name and content.",
    parameters: { file_name: z.string(), content: z.string() },
    implementation: async ({ file_name, content }) => {
      const filePath = join(ctl.getWorkingDirectory(), file_name);
      if (existsSync(filePath)) {
        return "Error: File already exists.";
      }
      await writeFile(filePath, content, "utf-8");
      return "File created.";
    },
  });
  tools.push(createFileTool);

  return tools;
}

上述工具提供程序定义了一个名为 create_file 的单个工具,它允许模型在工作目录中创建具有指定名称和内容的文件。您可以在工具定义中了解更多关于定义工具的信息。

然后在插件的 index.ts 中注册工具提供程序

// ... other imports ...
import { toolsProvider } from "./toolsProvider";

export async function main(context: PluginContext) {
  // ... other plugin setup code ...

  // Register the tools provider.
  context.withToolsProvider(toolsProvider); // <-- Register the tools provider

  // ... other plugin setup code ...
}

现在,您可以尝试让 LLM 创建一个文件,它应该能够使用您刚刚创建的工具来完成此操作。

提示

  • 使用描述性名称和描述:定义工具时,请使用描述性名称和详细描述。这有助于模型理解何时以及如何有效使用每个工具。
  • 以字符串形式返回错误:有时,模型在调用工具时可能会出错。在这种情况下,您可以将错误消息作为字符串返回。在大多数情况下,模型会尝试自行纠正并使用正确的参数再次调用该工具。

本页源文件可在 GitHub 上找到