|
| 1 | +--- |
| 2 | +title: Perplexity |
| 3 | +id: perplexity-adapter |
| 4 | +order: 10 |
| 5 | +description: "Use the Perplexity Search API and OpenAI-compatible chat completions with TanStack AI via @tanstack/ai-perplexity." |
| 6 | +keywords: |
| 7 | + - tanstack ai |
| 8 | + - perplexity |
| 9 | + - search api |
| 10 | + - web search |
| 11 | + - adapter |
| 12 | +--- |
| 13 | + |
| 14 | +`@tanstack/ai-perplexity` integrates [Perplexity](https://www.perplexity.ai) with TanStack AI: |
| 15 | + |
| 16 | +- A **Search API tool** that grounds your agent on the live web (`POST https://api.perplexity.ai/search`). |
| 17 | +- An **OpenAI-compatible chat client** that points the `openai` SDK at Perplexity's chat-completions endpoint, so existing OpenAI code can target Perplexity by swapping the base URL. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install @tanstack/ai-perplexity |
| 23 | +``` |
| 24 | + |
| 25 | +Set your API key (get one at <https://www.perplexity.ai/account/api/keys>): |
| 26 | + |
| 27 | +```bash |
| 28 | +export PERPLEXITY_API_KEY=... |
| 29 | +# PPLX_API_KEY is also accepted |
| 30 | +``` |
| 31 | + |
| 32 | +## Search tool |
| 33 | + |
| 34 | +Wrap the Search API as a TanStack AI tool and pass it to a chat agent so the model can fetch up-to-date web results: |
| 35 | + |
| 36 | +```ts |
| 37 | +import { chat } from '@tanstack/ai' |
| 38 | +import { perplexitySearchTool } from '@tanstack/ai-perplexity' |
| 39 | + |
| 40 | +const search = perplexitySearchTool({ |
| 41 | + // optional: applied when the model omits max_results |
| 42 | + defaultMaxResults: 5, |
| 43 | +}) |
| 44 | + |
| 45 | +const stream = chat({ |
| 46 | + // ... your text adapter ... |
| 47 | + tools: [search], |
| 48 | + messages: [ |
| 49 | + { role: 'user', content: 'What were the top AI papers this week?' }, |
| 50 | + ], |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +The tool input schema accepts: |
| 55 | + |
| 56 | +| Field | Type | Notes | |
| 57 | +| --------------------------- | ------------------------------------------------- | ------------------------------------------------------------------ | |
| 58 | +| `query` | `string` (required) | The search query. | |
| 59 | +| `max_results` | `integer` (1–20) | Defaults to API default (10), or `defaultMaxResults` if configured.| |
| 60 | +| `search_domain_filter` | `string[]` | Allowlist (`"nytimes.com"`) **or** denylist (`"-pinterest.com"`) — never both. | |
| 61 | +| `search_recency_filter` | `"hour" \| "day" \| "week" \| "month" \| "year"` | Recency window. | |
| 62 | +| `search_after_date_filter` | `string` | `m/d/yyyy` — only results on/after this date. | |
| 63 | +| `search_before_date_filter` | `string` | `m/d/yyyy` — only results on/before this date. | |
| 64 | + |
| 65 | +Each result is `{ title, url, snippet, date? }`. |
| 66 | + |
| 67 | +### Direct client |
| 68 | + |
| 69 | +If you want to call the Search API outside an agent loop: |
| 70 | + |
| 71 | +```ts |
| 72 | +import { PerplexitySearchClient } from '@tanstack/ai-perplexity' |
| 73 | + |
| 74 | +const client = new PerplexitySearchClient() |
| 75 | +const { results } = await client.search({ |
| 76 | + query: 'mars sample return mission', |
| 77 | + max_results: 5, |
| 78 | + search_recency_filter: 'month', |
| 79 | +}) |
| 80 | +``` |
| 81 | + |
| 82 | +## Chat (OpenAI-compatible) |
| 83 | + |
| 84 | +Perplexity exposes `POST /v1/chat/completions` with the standard OpenAI Chat Completions shape. `createPerplexityChatClient` returns an `openai` SDK instance pointed at `https://api.perplexity.ai`: |
| 85 | + |
| 86 | +```ts |
| 87 | +import { createPerplexityChatClient } from '@tanstack/ai-perplexity/chat' |
| 88 | + |
| 89 | +const client = createPerplexityChatClient() |
| 90 | +const completion = await client.chat.completions.create({ |
| 91 | + model: 'sonar', |
| 92 | + messages: [ |
| 93 | + { role: 'user', content: 'What is the latest on the Mars rover?' }, |
| 94 | + ], |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +## Configuration |
| 99 | + |
| 100 | +```ts |
| 101 | +import { PerplexitySearchClient } from '@tanstack/ai-perplexity' |
| 102 | + |
| 103 | +const client = new PerplexitySearchClient({ |
| 104 | + apiKey: process.env.PERPLEXITY_API_KEY, // explicit key (optional) |
| 105 | + baseURL: 'https://api.perplexity.ai', // override (optional) |
| 106 | + fetch: globalThis.fetch, // custom fetch (optional) |
| 107 | +}) |
| 108 | +``` |
| 109 | + |
| 110 | +## References |
| 111 | + |
| 112 | +- Search quickstart: <https://docs.perplexity.ai/docs/search/quickstart> |
| 113 | +- Search API reference: <https://docs.perplexity.ai/api-reference/search-post> |
| 114 | +- Domain filters: <https://docs.perplexity.ai/docs/search/filters/domain-filter> |
| 115 | +- Date / recency filters: <https://docs.perplexity.ai/docs/search/filters/date-time-filters> |
0 commit comments