ドキュメント

LLMによる予測

エージェントフロー

テキスト埋め込み

トークン化

モデルの管理

モデル情報

APIリファレンス

ツール定義

tool()関数でツールを定義し、act()呼び出しでモデルに渡すことができます。

ツールの構造

関数をツールとして定義するには、この標準形式に従ってください。

import { tool } from "@lmstudio/sdk";
import { z } from "zod";

const exampleTool = tool({
  // The name of the tool
  name: "add",

  // A description of the tool
  description: "Given two numbers a and b. Returns the sum of them.",

  // zod schema of the parameters
  parameters: { a: z.number(), b: z.number() },

  // The implementation of the tool. Just a regular function.
  implementation: ({ a, b }) => a + b,
});

重要: ツール名、説明、およびパラメータ定義はすべてモデルに渡されます!

これは、あなたの記述が生成の品質に影響することを意味します。モデルがツールの使用方法を理解できるように、常に明確な説明を提供するようにしてください。

外部効果を伴うツール (コンピューターの使用やAPI呼び出しなど)

ツールは、ファイルの作成やプログラム、さらにはAPIの呼び出しなど、外部効果を持つこともできます。外部効果を持つツールを実装することで、LLMをローカルマシン上でタスクを実行できる自律エージェントに変えることができます。

例: createFileTool

ツール定義

import { tool } from "@lmstudio/sdk";
import { existsSync } from "fs";
import { writeFile } from "fs/promises";
import { z } from "zod";

const createFileTool = tool({
  name: "createFile",
  description: "Create a file with the given name and content.",
  parameters: { name: z.string(), content: z.string() },
  implementation: async ({ name, content }) => {
    if (existsSync(name)) {
      return "Error: File already exists.";
    }
    await writeFile(name, content, "utf-8");
    return "File created.";
  },
});

createFileツールを使用したコード例:

import { LMStudioClient } from "@lmstudio/sdk";
import { createFileTool } from "./createFileTool";

const client = new LMStudioClient();

const model = await client.llm.model("qwen2.5-7b-instruct");
await model.act(
  "Please create a file named output.txt with your understanding of the meaning of life.",
  [createFileTool],
);