Skip to content

Commit 660933e

Browse files
authored
Update README.md
1 parent c248fb5 commit 660933e

File tree

1 file changed

+171
-14
lines changed

1 file changed

+171
-14
lines changed

README.md

Lines changed: 171 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,31 @@ npm install @perplexity-ai/perplexity_ai
1818

1919
The full API of this library can be found in [api.md](api.md).
2020

21-
<!-- prettier-ignore -->
21+
### Search API
22+
23+
Get ranked web search results with real-time information:
24+
25+
```js
26+
import Perplexity from '@perplexity-ai/perplexity_ai';
27+
28+
const client = new Perplexity({
29+
apiKey: process.env['PERPLEXITY_API_KEY'], // This is the default and can be omitted
30+
});
31+
32+
const search = await client.search.create({
33+
query: "latest AI developments 2024",
34+
maxResults: 5
35+
});
36+
37+
for (const result of search.results) {
38+
console.log(`${result.title}: ${result.url}`);
39+
}
40+
```
41+
42+
### Chat Completions
43+
44+
Get AI responses with real-time web search grounding:
45+
2246
```js
2347
import Perplexity from '@perplexity-ai/perplexity_ai';
2448

@@ -31,26 +55,121 @@ const completion = await client.chat.completions.create({
3155
model: 'sonar',
3256
});
3357

34-
console.log(completion.id);
58+
console.log(completion.choices[0].message.content);
59+
```
60+
61+
### Advanced Search Features
62+
63+
#### Multi-Query Search
64+
65+
Run multiple related searches in a single request:
66+
67+
```js
68+
const search = await client.search.create({
69+
query: [
70+
"renewable energy trends 2024",
71+
"solar power innovations",
72+
"wind energy developments"
73+
],
74+
maxResults: 10
75+
});
76+
```
77+
78+
#### Domain Filtering
79+
80+
Limit search results to specific trusted domains:
81+
82+
```js
83+
const search = await client.search.create({
84+
query: "climate change research",
85+
searchDomainFilter: [
86+
"science.org",
87+
"pnas.org",
88+
"cell.com",
89+
"nature.com"
90+
],
91+
maxResults: 10
92+
});
93+
```
94+
95+
#### Date Filtering
96+
97+
Filter results by recency or specific date ranges:
98+
99+
```js
100+
// Get results from the past week
101+
const recentSearch = await client.search.create({
102+
query: "latest AI developments",
103+
searchRecencyFilter: "week"
104+
});
105+
106+
// Search within a specific date range
107+
const dateRangeSearch = await client.search.create({
108+
query: "AI developments",
109+
searchAfterDateFilter: "01/01/2024",
110+
searchBeforeDateFilter: "12/31/2024"
111+
});
112+
```
113+
114+
#### Academic Search
115+
116+
Search academic sources for research purposes:
117+
118+
```js
119+
const academicSearch = await client.search.create({
120+
query: "machine learning algorithms",
121+
searchMode: "academic",
122+
maxResults: 10
123+
});
124+
```
125+
126+
#### Location-Based Search
127+
128+
Get geographically relevant results:
129+
130+
```js
131+
const localSearch = await client.search.create({
132+
query: "local restaurants",
133+
userLocationFilter: {
134+
latitude: 37.7749,
135+
longitude: -122.4194,
136+
radius: 10 // km
137+
},
138+
maxResults: 10
139+
});
35140
```
36141

37142
### Request & Response types
38143

39144
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
40145

41-
<!-- prettier-ignore -->
42146
```ts
43147
import Perplexity from '@perplexity-ai/perplexity_ai';
44148

45149
const client = new Perplexity({
46150
apiKey: process.env['PERPLEXITY_API_KEY'], // This is the default and can be omitted
47151
});
48152

49-
const params: Perplexity.Chat.CompletionCreateParams = {
153+
// Search API types
154+
const searchParams: Perplexity.Search.SearchCreateParams = {
155+
query: "artificial intelligence trends",
156+
maxResults: 5,
157+
searchMode: "web"
158+
};
159+
const searchResponse: Perplexity.Search.SearchCreateResponse = await client.search.create(searchParams);
160+
161+
// Content API types
162+
const contentParams: Perplexity.Content.ContentCreateParams = {
163+
urls: ["https://example.com/article"]
164+
};
165+
const contentResponse: Perplexity.Content.ContentCreateResponse = await client.content.create(contentParams);
166+
167+
// Chat Completions types
168+
const chatParams: Perplexity.Chat.CompletionCreateParams = {
50169
messages: [{ role: 'user', content: 'What is the capital of France?' }],
51170
model: 'sonar',
52171
};
53-
const completion: Perplexity.Chat.CompletionCreateResponse = await client.chat.completions.create(params);
172+
const chatResponse: Perplexity.Chat.CompletionCreateResponse = await client.chat.completions.create(chatParams);
54173
```
55174

56175
Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
@@ -61,8 +180,21 @@ When the library is unable to connect to the API,
61180
or if the API returns a non-success status code (i.e., 4xx or 5xx response),
62181
a subclass of `APIError` will be thrown:
63182

64-
<!-- prettier-ignore -->
65183
```ts
184+
// Search API error handling
185+
const search = await client.search
186+
.create({ query: "AI developments", maxResults: 5 })
187+
.catch(async (err) => {
188+
if (err instanceof Perplexity.APIError) {
189+
console.log(err.status); // 400
190+
console.log(err.name); // BadRequestError
191+
console.log(err.headers); // {server: 'nginx', ...}
192+
} else {
193+
throw err;
194+
}
195+
});
196+
197+
// Chat completions error handling
66198
const completion = await client.chat.completions
67199
.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'sonar' })
68200
.catch(async (err) => {
@@ -97,14 +229,17 @@ Connection errors (for example, due to a network connectivity problem), 408 Requ
97229

98230
You can use the `maxRetries` option to configure or disable this:
99231

100-
<!-- prettier-ignore -->
101232
```js
102233
// Configure the default for all requests:
103234
const client = new Perplexity({
104235
maxRetries: 0, // default is 2
105236
});
106237

107238
// Or, configure per-request:
239+
await client.search.create({ query: "AI developments", maxResults: 5 }, {
240+
maxRetries: 5,
241+
});
242+
108243
await client.chat.completions.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'sonar' }, {
109244
maxRetries: 5,
110245
});
@@ -114,14 +249,17 @@ await client.chat.completions.create({ messages: [{ role: 'user', content: 'What
114249

115250
Requests time out after 1 minute by default. You can configure this with a `timeout` option:
116251

117-
<!-- prettier-ignore -->
118252
```ts
119253
// Configure the default for all requests:
120254
const client = new Perplexity({
121255
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
122256
});
123257

124258
// Override per-request:
259+
await client.search.create({ query: "AI developments", maxResults: 5 }, {
260+
timeout: 5 * 1000,
261+
});
262+
125263
await client.chat.completions.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'sonar' }, {
126264
timeout: 5 * 1000,
127265
});
@@ -141,20 +279,33 @@ This method returns as soon as the headers for a successful response are receive
141279
You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
142280
Unlike `.asResponse()` this method consumes the body, returning once it is parsed.
143281

144-
<!-- prettier-ignore -->
145282
```ts
146283
const client = new Perplexity();
147284

148-
const response = await client.chat.completions
285+
// With search API
286+
const searchResponse = await client.search
287+
.create({ query: "AI developments", maxResults: 5 })
288+
.asResponse();
289+
console.log(searchResponse.headers.get('X-My-Header'));
290+
console.log(searchResponse.statusText); // access the underlying Response object
291+
292+
const { data: search, response: rawSearchResponse } = await client.search
293+
.create({ query: "AI developments", maxResults: 5 })
294+
.withResponse();
295+
console.log(rawSearchResponse.headers.get('X-My-Header'));
296+
console.log(search.results.length);
297+
298+
// With chat completions
299+
const chatResponse = await client.chat.completions
149300
.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'sonar' })
150301
.asResponse();
151-
console.log(response.headers.get('X-My-Header'));
152-
console.log(response.statusText); // access the underlying Response object
302+
console.log(chatResponse.headers.get('X-My-Header'));
303+
console.log(chatResponse.statusText); // access the underlying Response object
153304

154-
const { data: completion, response: raw } = await client.chat.completions
305+
const { data: completion, response: rawChatResponse } = await client.chat.completions
155306
.create({ messages: [{ role: 'user', content: 'What is the capital of France?' }], model: 'sonar' })
156307
.withResponse();
157-
console.log(raw.headers.get('X-My-Header'));
308+
console.log(rawChatResponse.headers.get('X-My-Header'));
158309
console.log(completion.id);
159310
```
160311

@@ -235,6 +386,12 @@ parameter. This library doesn't validate at runtime that the request matches the
235386
send will be sent as-is.
236387

237388
```ts
389+
client.search.create({
390+
// ...
391+
// @ts-expect-error baz is not yet public
392+
baz: 'undocumented option',
393+
});
394+
238395
client.chat.completions.create({
239396
// ...
240397
// @ts-expect-error baz is not yet public

0 commit comments

Comments
 (0)