Agent クラス
Agent
クラスは、Mastra で AI エージェントを作成するための土台となるクラスです。応答の生成、やり取りのストリーミング、音声機能の扱いに関するメソッドを提供します。
使い方の例
src/mastra/agents/test-agent.ts
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
export const agent = new Agent({
name: "test-agent",
instructions: 'エージェントへのメッセージ',
model: openai("gpt-4o")
});
コンストラクターのパラメーター
id?:
string
エージェントの任意指定の一意識別子。指定がない場合は `name` が既定値になります。
name:
string
エージェントの一意識別子。
description?:
string
エージェントの目的や機能に関する任意の説明。
instructions:
string | ({ runtimeContext: RuntimeContext }) => string | Promise<string>
エージェントの挙動を導くための指示。固定の文字列、または動的に文字列を返す関数を指定できます。
model:
MastraLanguageModel | ({ runtimeContext: RuntimeContext }) => MastraLanguageModel | Promise<MastraLanguageModel>
エージェントが使用する言語モデル。静的に指定するか、実行時に解決できます。
tools?:
ToolsInput | ({ runtimeContext: RuntimeContext }) => ToolsInput | Promise<ToolsInput>
エージェントが利用できるツール。静的に指定するか、動的に解決できます。
workflows?:
Record<string, Workflow> | ({ runtimeContext: RuntimeContext }) => Record<string, Workflow> | Promise<Record<string, Workflow>>
エージェントが実行できるワークフロー。静的または動的に解決可能です。
defaultGenerateOptions?:
AgentGenerateOptions | ({ runtimeContext: RuntimeContext }) => AgentGenerateOptions | Promise<AgentGenerateOptions>
`generate()` 呼び出し時に使用される既定のオプション。
defaultStreamOptions?:
AgentStreamOptions | ({ runtimeContext: RuntimeContext }) => AgentStreamOptions | Promise<AgentStreamOptions>
`stream()` 呼び出し時に使用される既定のオプション。
defaultVNextStreamOptions?:
AgentExecutionOptions | ({ runtimeContext: RuntimeContext }) => AgentExecutionOptions | Promise<AgentExecutionOptions>
vNext モードで `stream()` を呼び出す際に使用される既定のオプション。
mastra?:
Mastra
Mastra ランタイムインスタンスへの参照(自動的に注入されます)。
scorers?:
MastraScorers | ({ runtimeContext: RuntimeContext }) => MastraScorers | Promise<MastraScorers>
実行時評価およびテレメトリーのためのスコアリング設定。静的または動的に提供可能です。
evals?:
Record<string, Metric>
エージェントの応答を評価するための指標。
memory?:
MastraMemory | ({ runtimeContext: RuntimeContext }) => MastraMemory | Promise<MastraMemory>
状態付きコンテキストの保存・取得に用いるメモリモジュール。
voice?:
CompositeVoice
音声入出力の設定。
inputProcessors?:
Processor[] | ({ runtimeContext: RuntimeContext }) => Processor[] | Promise<Processor[]>
エージェントが処理する前にメッセージを修正または検証する入力プロセッサ。`processInput` 関数を実装する必要があります。
outputProcessors?:
Processor[] | ({ runtimeContext: RuntimeContext }) => Processor[] | Promise<Processor[]>
クライアントに送信する前にエージェントからのメッセージを修正または検証する出力プロセッサ。`processOutputResult` と `processOutputStream` のいずれか(または両方)を実装する必要があります。
返却値
agent:
Agent<TAgentId, TTools, TMetrics>
指定した設定を適用した新規の Agent インスタンス。