文档
集成
单个工具
插件支持目前处于内部测试阶段。在此加入测试。
要设置工具提供程序,首先在插件的 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 上找到