ドキュメント
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
エージェントフロー
インテグレーション
テキスト埋め込み
トークン化
モデル情報
エージェントフロー
.act() コール
ローカルマシンでタスクを実行できる自律エージェントとしてLLMを機能させるための、`.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 は人間に対してツールを呼び出すように求め、ツールの出力を特定の形式で提供されることを期待するように訓練されていることです。
電話で誰かにコンピューターサポートを提供していると想像してください。「このコマンドを実行して... OK、それは何を出力しましたか?... OK、次にそこをクリックして、何が書かれているか教えてください...」と言うかもしれません。この場合、LLM はあなた自身であり、電話の向こう側の人物を通じて間接的に「ツールを呼び出している」のです。
ツール使用のために選択されたモデルは、パフォーマンスに大きく影響します。
モデル選択の一般的なガイダンス
以下のコードは、単一の`.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");
}
このページのソースはGitHubで入手できます。
このページについて
自動ツール呼び出し
クイック例
LLMが「ツールを使用する」とはどういう意味ですか?
重要:モデルの選択
例:複数のツール
例:ファイル作成ツールとのチャットループ