文档
集成
多个工具
插件支持目前处于内部测试阶段。在此加入测试。
工具提供者可以为模型定义多个工具。只需创建额外的工具实例并将其添加到工具数组中即可。
在下面的示例中,我们添加了第二个工具来读取文件的内容。
import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { existsSync } from "fs";
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
export async function toolsProvider(ctl: ToolsProviderController) {
const tools: Tool[] = [];
const createFileTool = tool({
name: `create_file`,
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); // First tool
const readFileTool = tool({
name: `read_file`,
description: "Read the content of a file with the given name.",
parameters: { file_name: z.string() },
implementation: async ({ file_name }) => {
const filePath = join(ctl.getWorkingDirectory(), file_name);
if (!existsSync(filePath)) {
return "Error: File does not exist.";
}
const content = await readFile(filePath, "utf-8");
return content;
},
});
tools.push(readFileTool); // Second tool
return tools; // Return the tools array
}
此页面的源代码可在 GitHub 上找到