Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

perplexity.md

139 lines6.0 KB

1---
2title: Perplexity Search
3id: perplexity-adapter
4order: 10
5description: "Ground TanStack AI agents on the live web with the Perplexity Search API via @tanstack/ai-perplexity."
6keywords:
7 - tanstack ai
8 - perplexity
9 - search api
10 - web search
11 - grounding
12---
13​
14`@tanstack/ai-perplexity` is a **Search API** package. It wraps `POST https://api.perplexity.ai/search` as a TanStack AI tool (and a low-level HTTP client) so an agent can fetch ranked web results for citation and grounding.
15​
16It does **not** ship a TanStack text adapter. Pair the search tool with a function-calling adapter such as `openaiText` or `anthropicText`. Sonar `chat()` still goes through [`openaiCompatible`](./openai-compatible.md) — Sonar already searches the web and does not accept custom tools.
17​
18## Installation
19​
20<!-- ::start:tabs variant="package-manager" mode="install" -->
21​
22react: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
23vue: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
24solid: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
25svelte: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
26preact: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
27angular: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
28vanilla: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
29octane: @tanstack/ai @tanstack/ai-openai @tanstack/ai-perplexity
30​
31<!-- ::end:tabs -->
32​
33Set your API key (get one at <https://console.perplexity.ai/group/keys>):
34​
35```bash
36export PERPLEXITY_API_KEY=...
37# PPLX_API_KEY is also accepted
38```
39​
40## Search tool
41​
42Use the tool with a first-class function-calling adapter. Do not pass it to Sonar — Sonar Chat Completions does not register custom tools.
43​
44```ts
45import { chat } from '@tanstack/ai'
46import { openaiText } from '@tanstack/ai-openai'
47import { perplexitySearchTool } from '@tanstack/ai-perplexity'
48​
49const search = perplexitySearchTool({
50 defaultMaxResults: 5,
51})
52​
53const stream = chat({
54 adapter: openaiText('gpt-5.2'),
55 tools: [search],
56 messages: [
57 { role: 'user', content: 'What were the top AI papers this week?' },
58 ],
59})
60```
61​
62Swap `openaiText` for `anthropicText` (or any other function-calling adapter) the same way.
63​
64The tool input schema accepts:
65​
66| Field | Type | Notes |
67| --------------------------- | ------------------------------------------------ | ------------------------------------------------------------------- |
68| `query` | `string` (required) | The search query. |
69| `max_results` | `integer` (1–20) | Defaults to `defaultMaxResults` when set, otherwise the API default (10). |
70| `search_domain_filter` | `string[]` | Max 20. Allowlist (`"nytimes.com"`) **or** denylist (`"-pinterest.com"`) — never both. Hostnames, optional paths, or TLDs. |
71| `search_recency_filter` | `"hour" \| "day" \| "week" \| "month" \| "year"` | Recency window. |
72| `search_after_date_filter` | `string` | `m/d/yyyy` — only results on/after this date. |
73| `search_before_date_filter` | `string` | `m/d/yyyy` — only results on/before this date. |
74​
75Output: `{ results: Array<{ title, url, snippet, date?, last_updated? }> }`. The wrapper keeps those citation fields and the optional response `id` on `client.search()`; it does not surface `server_time`.
76​
77The tool exposes a subset of Search API filters (`query` is a single string). `PerplexitySearchClient` also accepts `max_tokens_per_page` and up to 5 queries as `string[]`.
78​
79### Direct client
80​
81If you want to call the Search API outside an agent loop:
82​
83```ts
84import { PerplexitySearchClient } from '@tanstack/ai-perplexity'
85​
86const client = new PerplexitySearchClient()
87const { results } = await client.search({
88 query: 'mars sample return mission',
89 max_results: 5,
90 search_recency_filter: 'month',
91})
92```
93​
94## Configuration
95​
96```ts
97import { PerplexitySearchClient } from '@tanstack/ai-perplexity'
98​
99const client = new PerplexitySearchClient({
100 apiKey: process.env.PERPLEXITY_API_KEY, // explicit key (optional)
101 baseURL: 'https://api.perplexity.ai', // override (optional)
102 fetch: globalThis.fetch, // custom fetch (optional)
103})
104```
105​
106## Chat (Sonar)
107​
108Sonar already grounds answers on the web. Use [`openaiCompatible`](./openai-compatible.md) from `@tanstack/ai-openai/compatible` — this package does not wrap that adapter, and you should not pass `perplexitySearchTool` here.
109​
110```ts
111import { chat } from '@tanstack/ai'
112import { openaiCompatible } from '@tanstack/ai-openai/compatible'
113import { getPerplexityIntegrationHeaders } from '@tanstack/ai-perplexity'
114​
115const perplexity = openaiCompatible({
116 name: 'perplexity',
117 baseURL: 'https://api.perplexity.ai',
118 apiKey: process.env.PERPLEXITY_API_KEY!,
119 models: ['sonar', 'sonar-pro'],
120 defaultHeaders: getPerplexityIntegrationHeaders(),
121})
122​
123const stream = chat({
124 adapter: perplexity('sonar'),
125 messages: [{ role: 'user', content: 'What is the latest on the Mars rover?' }],
126})
127```
128​
129`getPerplexityIntegrationHeaders()` is optional. It adds Perplexity's `X-Pplx-Integration` attribution header (`tanstack/<package-version>`). The Search client sends it automatically; pass it into `openaiCompatible` if you want the same header on Sonar chat requests.
130​
131The OpenAI SDK then calls `POST https://api.perplexity.ai/chat/completions` (Perplexity's OpenAI-compatible alias for Sonar).
132​
133## References
134​
135- Search quickstart: <https://docs.perplexity.ai/docs/search/quickstart>
136- Search API reference: <https://docs.perplexity.ai/api-reference/search-post>
137- Domain filters: <https://docs.perplexity.ai/docs/search/filters/domain-filter>
138- Date / recency filters: <https://docs.perplexity.ai/docs/search/filters/date-time-filters>
139- [OpenAI-compatible adapter](./openai-compatible.md) — Sonar `chat()`