ドキュメント

LLMによる予測

エージェントフロー

テキスト埋め込み

トークン化

モデル管理

モデル情報

APIリファレンス

The .act()呼び出し

自動ツール呼び出し

ツールを実行し、その出力をLLMに提供し、LLMが次に何をすべきかを決定するのを待つという一連のプロセスを説明するために、「実行ラウンド」という概念を導入します。

実行ラウンド

 • run a tool →
 ↑   • provide the result to the LLM →
 │       • wait for the LLM to generate a response

 └────────────────────────────────────────┘ └➔ (return)

モデルは、最終結果を返す前にツールを複数回実行することを選択する場合があります。たとえば、LLMがコードを書いている場合、プログラムをコンパイルまたは実行し、エラーを修正し、再び実行するといった繰り返しを、望ましい結果が得られるまで行うことがあります。

これを踏まえ、.act() APIは自動的な「複数ラウンド」ツール呼び出しAPIであると言えます。

クイック例

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

const client = new LMStudioClient();

const multiplyTool = tool({
  name: "multiply",
  description: "Given two numbers a and b. Returns the product of them.",
  parameters: { a: z.number(), b: z.number() },
  implementation: ({ a, b }) => a * b,
});

const model = await client.llm.model("qwen2.5-7b-instruct");
await model.act("What is the result of 12345 multiplied by 54321?", [multiplyTool], {
  onMessage: (message) => console.info(message.toString()),
});

LLMが「ツールを使用する」とはどういう意味ですか?

LLMは基本的にテキスト入力、テキスト出力のプログラムです。そのため、「LLMはどのようにツールを使用できるのか?」と疑問に思うかもしれません。その答えは、一部のLLMは、人間がツールの呼び出しを行い、そのツール出力が特定の形式で返されることを期待するように学習されているということです。

電話で誰かにコンピューターサポートを提供しているところを想像してみてください。あなたは「このコマンドを実行してくれますか?...出力は何でしたか?...では、そこをクリックして何が表示されるか教えてください...」などと言うかもしれません。この場合、あなたがLLMです!そして、電話の向こうにいる人を通して「ツールを呼び出して」いるのです。

重要:モデル選択

ツール使用のために選択されたモデルは、パフォーマンスに大きく影響します。

モデル選択に関する一般的なガイダンス

  • すべてのモデルがインテリジェントなツール使用に対応しているわけではありません
  • 大きい方が良い(つまり、7Bパラメータのモデルは一般的に3Bパラメータのモデルよりも性能が良い)
  • 私たちは、Qwen2.5-7B-Instructが様々なケースで良好な性能を示すことを確認しました。
  • このガイダンスは変更される可能性があります

例:複数のツール

以下のコードは、単一の.act()呼び出しで複数のツールを提供する方法を示しています。

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

const client = new LMStudioClient();

const additionTool = tool({
  name: "add",
  description: "Given two numbers a and b. Returns the sum of them.",
  parameters: { a: z.number(), b: z.number() },
  implementation: ({ a, b }) => a + b,
});

const isPrimeTool = tool({
  name: "isPrime",
  description: "Given a number n. Returns true if n is a prime number.",
  parameters: { n: z.number() },
  implementation: ({ n }) => {
    if (n < 2) return false;
    const sqrt = Math.sqrt(n);
    for (let i = 2; i <= sqrt; i++) {
      if (n % i === 0) return false;
    }
    return true;
  },
});

const model = await client.llm.model("qwen2.5-7b-instruct");
await model.act(
  "Is the result of 12345 + 45668 a prime? Think step by step.",
  [additionTool, isPrimeTool],
  { onMessage: (message) => console.info(message.toString()) },
);

例:ファイル作成ツールを使ったチャットループ

以下のコードは、ファイルを作成できるLLMエージェントとの会話ループを作成します。

import { Chat, LMStudioClient, tool } from "@lmstudio/sdk";
import { existsSync } from "fs";
import { writeFile } from "fs/promises";
import { createInterface } from "readline/promises";
import { z } from "zod";

const rl = createInterface({ input: process.stdin, output: process.stdout });
const client = new LMStudioClient();
const model = await client.llm.model();
const chat = Chat.empty();

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.";
  },
});

while (true) {
  const input = await rl.question("You: ");
  // Append the user input to the chat
  chat.append("user", input);

  process.stdout.write("Bot: ");
  await model.act(chat, [createFileTool], {
    // When the model finish the entire message, push it to the chat
    onMessage: (message) => chat.append(message),
    onPredictionFragment: ({ content }) => {
      process.stdout.write(content);
    },
  });
  process.stdout.write("\n");
}