Perplexity tools
@mastra/perplexity パッケージは、Perplexity Search API をMastra互換のToolとしてラップします。このパッケージは、createTool() で作成されたToolと完全なZod入出力スキーマを返すファクトリ関数を公開します。
Perplexityを利用したチャット補完やエージェント型Workflowには、Mastra組み込みのPerplexityモデルProviderまたはPerplexity Agent Providerを使用してください。これらは、このSearch Toolとは別のものです。
インストールインストールへの直接リンク
Zodと一緒にパッケージをインストールします。
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/perplexity zod
pnpm add @mastra/perplexity zod
yarn add @mastra/perplexity zod
bun add @mastra/perplexity zod
使用例使用例への直接リンク
次の例では、デフォルト設定でSearch Toolを作成します。デフォルトでは、Toolは環境から PERPLEXITY_API_KEY(フォールバックとして PPLX_API_KEY)を読み取ります。上書きするには { apiKey } を明示的に渡します。
src/mastra/tools/index.ts
import { createPerplexitySearchTool } from '@mastra/perplexity'
const searchTool = createPerplexitySearchTool()
APIキーを明示的に渡すには、次のようにします。
src/mastra/tools/index.ts
import { createPerplexitySearchTool } from '@mastra/perplexity'
const searchTool = createPerplexitySearchTool({ apiKey: 'pplx-...' })
設定設定への直接リンク
すべてのファクトリ関数は PerplexityClientOptions オブジェクトを受け取ります。
apiKey?:
string
Perplexity APIキー。
PERPLEXITY_API_KEY、次にPPLX_API_KEY環境変数へフォールバックします。baseUrl?:
string
= 'https://api.perplexity.ai'
APIのベースURLを上書きします。
fetch?:
typeof fetch
カスタム
fetch実装。テスト、再試行、計測に役立ちます。メソッドメソッドへの直接リンク
ファクトリ関数ファクトリ関数への直接リンク
createPerplexityTools(config?)createperplexitytoolsconfigへの直接リンク
指定された設定を共有するすべてのPerplexity Toolを含むオブジェクトを返します。
import { createPerplexityTools } from '@mastra/perplexity'
const tools = createPerplexityTools({ apiKey: 'pplx-...' })
// tools.perplexitySearch
戻り値: { perplexitySearch }
createPerplexitySearchTool(config?)createperplexitysearchtoolconfigへの直接リンク
Perplexity Search APIを使用してWebを検索するToolを作成します。タイトル、URL、スニペット、および任意の公開日を含む、順位付けされた結果を返します。
ToolはID perplexity-search で登録されます。
import { createPerplexitySearchTool } from '@mastra/perplexity'
const searchTool = createPerplexitySearchTool()
入力入力への直接リンク
query:
string
検索クエリ。
maxResults?:
number
返す結果の最大数(1〜20)。
searchDomainFilter?:
string[]
ドメインによって結果を限定(または除外)します。除外するにはドメインの先頭に
-を付けます(例: -pinterest.com)。同じ呼び出しで許可リストと拒否リストのエントリを混在させないでください。searchRecencyFilter?:
'hour' | 'day' | 'week' | 'month' | 'year'
指定した直近期間内の結果のみを返します。
searchAfterDateFilter?:
string
この日付以降に公開された結果のみを返します。形式: m/d/yyyy。
searchBeforeDateFilter?:
string
この日付以前に公開された結果のみを返します。形式: m/d/yyyy。
出力出力への直接リンク
query:
string
元の検索クエリ。
results:
SearchResult[]
検索結果の配列。
SearchResult
title:
string
結果のタイトル。
url:
string
結果のURL。
snippet:
string
内容のスニペット。
date?:
string
取得できる場合は公開日。
Agentの例Agentの例への直接リンク
次の例では、回答前に最新のWeb検索結果を取得できるよう、Search ToolをAgentに登録します。
src/mastra/agents/index.ts
import { Agent } from '@mastra/core/agent'
import { createPerplexitySearchTool } from '@mastra/perplexity'
const agent = new Agent({
id: 'research-agent',
name: 'Research Agent',
model: 'anthropic/claude-sonnet-4-6',
instructions:
'You are a research assistant. Use the perplexity-search tool to find up-to-date information from the web before answering.',
tools: {
search: createPerplexitySearchTool(),
},
})
環境変数環境変数への直接リンク
| 変数 | 説明 |
|---|---|
PERPLEXITY_API_KEY | Perplexity APIキー。ファクトリ関数にapiKeyを渡さない場合のデフォルトとして使用されます。 |
PPLX_API_KEY | PERPLEXITY_API_KEYが設定されていない場合に使用されるフォールバック。 |