ドキュメント

インテグレーション

単一ツール

ベータ版機能

プラグインサポートは現在プライベートベータ版です。こちらからベータ版に参加してください。

ツールプロバイダーをセットアップするには、まずプラグインの 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 で利用可能です。