@ai-sdk/xai 0.0.0-1c33ba03-20260114162300 → 0.0.0-4115c213-20260122152721
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +65 -4
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/01-xai.mdx +697 -0
- package/package.json +15 -6
- package/src/convert-to-xai-chat-messages.ts +142 -0
- package/src/convert-xai-chat-usage.ts +23 -0
- package/src/get-response-metadata.ts +19 -0
- package/src/index.ts +14 -0
- package/src/map-xai-finish-reason.ts +19 -0
- package/src/responses/convert-to-xai-responses-input.ts +206 -0
- package/src/responses/convert-xai-responses-usage.ts +24 -0
- package/src/responses/map-xai-responses-finish-reason.ts +20 -0
- package/src/responses/xai-responses-api.ts +393 -0
- package/src/responses/xai-responses-language-model.ts +732 -0
- package/src/responses/xai-responses-options.ts +34 -0
- package/src/responses/xai-responses-prepare-tools.ts +226 -0
- package/src/tool/code-execution.ts +17 -0
- package/src/tool/index.ts +15 -0
- package/src/tool/view-image.ts +20 -0
- package/src/tool/view-x-video.ts +18 -0
- package/src/tool/web-search.ts +56 -0
- package/src/tool/x-search.ts +63 -0
- package/src/version.ts +6 -0
- package/src/xai-chat-language-model.ts +681 -0
- package/src/xai-chat-options.ts +131 -0
- package/src/xai-chat-prompt.ts +44 -0
- package/src/xai-error.ts +19 -0
- package/src/xai-image-settings.ts +1 -0
- package/src/xai-prepare-tools.ts +95 -0
- package/src/xai-provider.ts +162 -0
package/docs/01-xai.mdx
ADDED
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: xAI Grok
|
|
3
|
+
description: Learn how to use xAI Grok.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# xAI Grok Provider
|
|
7
|
+
|
|
8
|
+
The [xAI Grok](https://x.ai) provider contains language model support for the [xAI API](https://x.ai/api).
|
|
9
|
+
|
|
10
|
+
## Setup
|
|
11
|
+
|
|
12
|
+
The xAI Grok provider is available via the `@ai-sdk/xai` module. You can
|
|
13
|
+
install it with
|
|
14
|
+
|
|
15
|
+
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
|
|
16
|
+
<Tab>
|
|
17
|
+
<Snippet text="pnpm add @ai-sdk/xai" dark />
|
|
18
|
+
</Tab>
|
|
19
|
+
<Tab>
|
|
20
|
+
<Snippet text="npm install @ai-sdk/xai" dark />
|
|
21
|
+
</Tab>
|
|
22
|
+
<Tab>
|
|
23
|
+
<Snippet text="yarn add @ai-sdk/xai" dark />
|
|
24
|
+
</Tab>
|
|
25
|
+
|
|
26
|
+
<Tab>
|
|
27
|
+
<Snippet text="bun add @ai-sdk/xai" dark />
|
|
28
|
+
</Tab>
|
|
29
|
+
</Tabs>
|
|
30
|
+
|
|
31
|
+
## Provider Instance
|
|
32
|
+
|
|
33
|
+
You can import the default provider instance `xai` from `@ai-sdk/xai`:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { xai } from '@ai-sdk/xai';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If you need a customized setup, you can import `createXai` from `@ai-sdk/xai`
|
|
40
|
+
and create a provider instance with your settings:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createXai } from '@ai-sdk/xai';
|
|
44
|
+
|
|
45
|
+
const xai = createXai({
|
|
46
|
+
apiKey: 'your-api-key',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
You can use the following optional settings to customize the xAI provider instance:
|
|
51
|
+
|
|
52
|
+
- **baseURL** _string_
|
|
53
|
+
|
|
54
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
55
|
+
The default prefix is `https://api.x.ai/v1`.
|
|
56
|
+
|
|
57
|
+
- **apiKey** _string_
|
|
58
|
+
|
|
59
|
+
API key that is being sent using the `Authorization` header. It defaults to
|
|
60
|
+
the `XAI_API_KEY` environment variable.
|
|
61
|
+
|
|
62
|
+
- **headers** _Record<string,string>_
|
|
63
|
+
|
|
64
|
+
Custom headers to include in the requests.
|
|
65
|
+
|
|
66
|
+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
|
67
|
+
|
|
68
|
+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
|
69
|
+
Defaults to the global `fetch` function.
|
|
70
|
+
You can use it as a middleware to intercept requests,
|
|
71
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
72
|
+
|
|
73
|
+
## Language Models
|
|
74
|
+
|
|
75
|
+
You can create [xAI models](https://console.x.ai) using a provider instance. The
|
|
76
|
+
first argument is the model id, e.g. `grok-3`.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const model = xai('grok-3');
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
By default, `xai(modelId)` uses the Chat API. To use the Responses API with server-side agentic tools, explicitly use `xai.responses(modelId)`.
|
|
83
|
+
|
|
84
|
+
### Example
|
|
85
|
+
|
|
86
|
+
You can use xAI language models to generate text with the `generateText` function:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { xai } from '@ai-sdk/xai';
|
|
90
|
+
import { generateText } from 'ai';
|
|
91
|
+
|
|
92
|
+
const { text } = await generateText({
|
|
93
|
+
model: xai('grok-3'),
|
|
94
|
+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
xAI language models can also be used in the `streamText`, `generateObject`, and `streamObject` functions
|
|
99
|
+
(see [AI SDK Core](/docs/ai-sdk-core)).
|
|
100
|
+
|
|
101
|
+
### Provider Options
|
|
102
|
+
|
|
103
|
+
xAI chat models support additional provider options that are not part of
|
|
104
|
+
the [standard call settings](/docs/ai-sdk-core/settings). You can pass them in the `providerOptions` argument:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const model = xai('grok-3-mini');
|
|
108
|
+
|
|
109
|
+
await generateText({
|
|
110
|
+
model,
|
|
111
|
+
providerOptions: {
|
|
112
|
+
xai: {
|
|
113
|
+
reasoningEffort: 'high',
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The following optional provider options are available for xAI chat models:
|
|
120
|
+
|
|
121
|
+
- **reasoningEffort** _'low' | 'medium' | 'high'_
|
|
122
|
+
|
|
123
|
+
Reasoning effort for reasoning models.
|
|
124
|
+
|
|
125
|
+
- **store** _boolean_
|
|
126
|
+
|
|
127
|
+
Whether to store the generation. Defaults to `true`.
|
|
128
|
+
|
|
129
|
+
- **previousResponseId** _string_
|
|
130
|
+
|
|
131
|
+
The ID of the previous response. You can use it to continue a conversation. Defaults to `undefined`.
|
|
132
|
+
|
|
133
|
+
## Responses API (Agentic Tools)
|
|
134
|
+
|
|
135
|
+
You can use the xAI Responses API with the `xai.responses(modelId)` factory method for server-side agentic tool calling. This enables the model to autonomously orchestrate tool calls and research on xAI's servers.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
const model = xai.responses('grok-4-fast');
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The Responses API provides server-side tools that the model can autonomously execute during its reasoning process:
|
|
142
|
+
|
|
143
|
+
- **web_search**: Real-time web search and page browsing
|
|
144
|
+
- **x_search**: Search X (Twitter) posts, users, and threads
|
|
145
|
+
- **code_execution**: Execute Python code for calculations and data analysis
|
|
146
|
+
|
|
147
|
+
### Vision
|
|
148
|
+
|
|
149
|
+
The Responses API supports image input with vision models:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { xai } from '@ai-sdk/xai';
|
|
153
|
+
import { generateText } from 'ai';
|
|
154
|
+
|
|
155
|
+
const { text } = await generateText({
|
|
156
|
+
model: xai.responses('grok-2-vision-1212'),
|
|
157
|
+
messages: [
|
|
158
|
+
{
|
|
159
|
+
role: 'user',
|
|
160
|
+
content: [
|
|
161
|
+
{ type: 'text', text: 'What do you see in this image?' },
|
|
162
|
+
{ type: 'image', image: fs.readFileSync('./image.png') },
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Web Search Tool
|
|
170
|
+
|
|
171
|
+
The web search tool enables autonomous web research with optional domain filtering and image understanding:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { xai } from '@ai-sdk/xai';
|
|
175
|
+
import { generateText } from 'ai';
|
|
176
|
+
|
|
177
|
+
const { text, sources } = await generateText({
|
|
178
|
+
model: xai.responses('grok-4-fast'),
|
|
179
|
+
prompt: 'What are the latest developments in AI?',
|
|
180
|
+
tools: {
|
|
181
|
+
web_search: xai.tools.webSearch({
|
|
182
|
+
allowedDomains: ['arxiv.org', 'openai.com'],
|
|
183
|
+
enableImageUnderstanding: true,
|
|
184
|
+
}),
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
console.log(text);
|
|
189
|
+
console.log('Citations:', sources);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Web Search Parameters
|
|
193
|
+
|
|
194
|
+
- **allowedDomains** _string[]_
|
|
195
|
+
|
|
196
|
+
Only search within specified domains (max 5). Cannot be used with `excludedDomains`.
|
|
197
|
+
|
|
198
|
+
- **excludedDomains** _string[]_
|
|
199
|
+
|
|
200
|
+
Exclude specified domains from search (max 5). Cannot be used with `allowedDomains`.
|
|
201
|
+
|
|
202
|
+
- **enableImageUnderstanding** _boolean_
|
|
203
|
+
|
|
204
|
+
Enable the model to view and analyze images found during search. Increases token usage.
|
|
205
|
+
|
|
206
|
+
### X Search Tool
|
|
207
|
+
|
|
208
|
+
The X search tool enables searching X (Twitter) for posts, with filtering by handles and date ranges:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
const { text, sources } = await generateText({
|
|
212
|
+
model: xai.responses('grok-4-fast'),
|
|
213
|
+
prompt: 'What are people saying about AI on X this week?',
|
|
214
|
+
tools: {
|
|
215
|
+
x_search: xai.tools.xSearch({
|
|
216
|
+
allowedXHandles: ['elonmusk', 'xai'],
|
|
217
|
+
fromDate: '2025-10-23',
|
|
218
|
+
toDate: '2025-10-30',
|
|
219
|
+
enableImageUnderstanding: true,
|
|
220
|
+
enableVideoUnderstanding: true,
|
|
221
|
+
}),
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### X Search Parameters
|
|
227
|
+
|
|
228
|
+
- **allowedXHandles** _string[]_
|
|
229
|
+
|
|
230
|
+
Only search posts from specified X handles (max 10). Cannot be used with `excludedXHandles`.
|
|
231
|
+
|
|
232
|
+
- **excludedXHandles** _string[]_
|
|
233
|
+
|
|
234
|
+
Exclude posts from specified X handles (max 10). Cannot be used with `allowedXHandles`.
|
|
235
|
+
|
|
236
|
+
- **fromDate** _string_
|
|
237
|
+
|
|
238
|
+
Start date for posts in ISO8601 format (`YYYY-MM-DD`).
|
|
239
|
+
|
|
240
|
+
- **toDate** _string_
|
|
241
|
+
|
|
242
|
+
End date for posts in ISO8601 format (`YYYY-MM-DD`).
|
|
243
|
+
|
|
244
|
+
- **enableImageUnderstanding** _boolean_
|
|
245
|
+
|
|
246
|
+
Enable the model to view and analyze images in X posts.
|
|
247
|
+
|
|
248
|
+
- **enableVideoUnderstanding** _boolean_
|
|
249
|
+
|
|
250
|
+
Enable the model to view and analyze videos in X posts.
|
|
251
|
+
|
|
252
|
+
### Code Execution Tool
|
|
253
|
+
|
|
254
|
+
The code execution tool enables the model to write and execute Python code for calculations and data analysis:
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
const { text } = await generateText({
|
|
258
|
+
model: xai.responses('grok-4-fast'),
|
|
259
|
+
prompt:
|
|
260
|
+
'Calculate the compound interest for $10,000 at 5% annually for 10 years',
|
|
261
|
+
tools: {
|
|
262
|
+
code_execution: xai.tools.codeExecution(),
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### File Search Tool
|
|
268
|
+
|
|
269
|
+
xAI supports file search through OpenAI compatibility. You can use the OpenAI provider with xAI's base URL to search vector stores:
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
import { createOpenAI } from '@ai-sdk/openai';
|
|
273
|
+
import { streamText } from 'ai';
|
|
274
|
+
|
|
275
|
+
const openai = createOpenAI({
|
|
276
|
+
baseURL: 'https://api.x.ai/v1',
|
|
277
|
+
apiKey: process.env.XAI_API_KEY,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const result = streamText({
|
|
281
|
+
model: openai('grok-4'),
|
|
282
|
+
prompt: 'What documents do you have access to?',
|
|
283
|
+
tools: {
|
|
284
|
+
file_search: openai.tools.fileSearch({
|
|
285
|
+
vectorStoreIds: ['your-vector-store-id'],
|
|
286
|
+
maxNumResults: 5,
|
|
287
|
+
}),
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
<Note>
|
|
293
|
+
File search requires grok-4 family models. See the [OpenAI
|
|
294
|
+
provider](/providers/ai-sdk-providers/openai) documentation for additional
|
|
295
|
+
file search options like filters and ranking.
|
|
296
|
+
</Note>
|
|
297
|
+
|
|
298
|
+
### Multiple Tools
|
|
299
|
+
|
|
300
|
+
You can combine multiple server-side tools for comprehensive research:
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
import { xai } from '@ai-sdk/xai';
|
|
304
|
+
import { streamText } from 'ai';
|
|
305
|
+
|
|
306
|
+
const { fullStream } = streamText({
|
|
307
|
+
model: xai.responses('grok-4-fast'),
|
|
308
|
+
prompt: 'Research AI safety developments and calculate risk metrics',
|
|
309
|
+
tools: {
|
|
310
|
+
web_search: xai.tools.webSearch(),
|
|
311
|
+
x_search: xai.tools.xSearch(),
|
|
312
|
+
code_execution: xai.tools.codeExecution(),
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
for await (const part of fullStream) {
|
|
317
|
+
if (part.type === 'text-delta') {
|
|
318
|
+
process.stdout.write(part.text);
|
|
319
|
+
} else if (part.type === 'source' && part.sourceType === 'url') {
|
|
320
|
+
console.log('\nSource:', part.url);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Provider Options
|
|
326
|
+
|
|
327
|
+
The Responses API supports the following provider options:
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
import { xai } from '@ai-sdk/xai';
|
|
331
|
+
import { generateText } from 'ai';
|
|
332
|
+
|
|
333
|
+
const result = await generateText({
|
|
334
|
+
model: xai.responses('grok-4-fast'),
|
|
335
|
+
providerOptions: {
|
|
336
|
+
xai: {
|
|
337
|
+
reasoningEffort: 'high',
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
// ...
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
The following provider options are available:
|
|
345
|
+
|
|
346
|
+
- **reasoningEffort** _'low' | 'high'_
|
|
347
|
+
|
|
348
|
+
Control the reasoning effort for the model. Higher effort may produce more thorough results at the cost of increased latency and token usage.
|
|
349
|
+
|
|
350
|
+
<Note>
|
|
351
|
+
The Responses API only supports server-side tools. You cannot mix server-side
|
|
352
|
+
tools with client-side function tools in the same request.
|
|
353
|
+
</Note>
|
|
354
|
+
|
|
355
|
+
## Live Search
|
|
356
|
+
|
|
357
|
+
xAI models support Live Search functionality, allowing them to query real-time data from various sources and include it in responses with citations.
|
|
358
|
+
|
|
359
|
+
### Basic Search
|
|
360
|
+
|
|
361
|
+
To enable search, specify `searchParameters` with a search mode:
|
|
362
|
+
|
|
363
|
+
```ts
|
|
364
|
+
import { xai } from '@ai-sdk/xai';
|
|
365
|
+
import { generateText } from 'ai';
|
|
366
|
+
|
|
367
|
+
const { text, sources } = await generateText({
|
|
368
|
+
model: xai('grok-3-latest'),
|
|
369
|
+
prompt: 'What are the latest developments in AI?',
|
|
370
|
+
providerOptions: {
|
|
371
|
+
xai: {
|
|
372
|
+
searchParameters: {
|
|
373
|
+
mode: 'auto', // 'auto', 'on', or 'off'
|
|
374
|
+
returnCitations: true,
|
|
375
|
+
maxSearchResults: 5,
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
console.log(text);
|
|
382
|
+
console.log('Sources:', sources);
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### Search Parameters
|
|
386
|
+
|
|
387
|
+
The following search parameters are available:
|
|
388
|
+
|
|
389
|
+
- **mode** _'auto' | 'on' | 'off'_
|
|
390
|
+
|
|
391
|
+
Search mode preference:
|
|
392
|
+
|
|
393
|
+
- `'auto'` (default): Model decides whether to search
|
|
394
|
+
- `'on'`: Always enables search
|
|
395
|
+
- `'off'`: Disables search completely
|
|
396
|
+
|
|
397
|
+
- **returnCitations** _boolean_
|
|
398
|
+
|
|
399
|
+
Whether to return citations in the response. Defaults to `true`.
|
|
400
|
+
|
|
401
|
+
- **fromDate** _string_
|
|
402
|
+
|
|
403
|
+
Start date for search data in ISO8601 format (`YYYY-MM-DD`).
|
|
404
|
+
|
|
405
|
+
- **toDate** _string_
|
|
406
|
+
|
|
407
|
+
End date for search data in ISO8601 format (`YYYY-MM-DD`).
|
|
408
|
+
|
|
409
|
+
- **maxSearchResults** _number_
|
|
410
|
+
|
|
411
|
+
Maximum number of search results to consider. Defaults to 20, max 50.
|
|
412
|
+
|
|
413
|
+
- **sources** _Array<SearchSource>_
|
|
414
|
+
|
|
415
|
+
Data sources to search from. Defaults to `["web", "x"]` if not specified.
|
|
416
|
+
|
|
417
|
+
### Search Sources
|
|
418
|
+
|
|
419
|
+
You can specify different types of data sources for search:
|
|
420
|
+
|
|
421
|
+
#### Web Search
|
|
422
|
+
|
|
423
|
+
```ts
|
|
424
|
+
const result = await generateText({
|
|
425
|
+
model: xai('grok-3-latest'),
|
|
426
|
+
prompt: 'Best ski resorts in Switzerland',
|
|
427
|
+
providerOptions: {
|
|
428
|
+
xai: {
|
|
429
|
+
searchParameters: {
|
|
430
|
+
mode: 'on',
|
|
431
|
+
sources: [
|
|
432
|
+
{
|
|
433
|
+
type: 'web',
|
|
434
|
+
country: 'CH', // ISO alpha-2 country code
|
|
435
|
+
allowedWebsites: ['ski.com', 'snow-forecast.com'],
|
|
436
|
+
safeSearch: true,
|
|
437
|
+
},
|
|
438
|
+
],
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
#### Web source parameters
|
|
446
|
+
|
|
447
|
+
- **country** _string_: ISO alpha-2 country code
|
|
448
|
+
- **allowedWebsites** _string[]_: Max 5 allowed websites
|
|
449
|
+
- **excludedWebsites** _string[]_: Max 5 excluded websites
|
|
450
|
+
- **safeSearch** _boolean_: Enable safe search (default: true)
|
|
451
|
+
|
|
452
|
+
#### X (Twitter) Search
|
|
453
|
+
|
|
454
|
+
```ts
|
|
455
|
+
const result = await generateText({
|
|
456
|
+
model: xai('grok-3-latest'),
|
|
457
|
+
prompt: 'Latest updates on Grok AI',
|
|
458
|
+
providerOptions: {
|
|
459
|
+
xai: {
|
|
460
|
+
searchParameters: {
|
|
461
|
+
mode: 'on',
|
|
462
|
+
sources: [
|
|
463
|
+
{
|
|
464
|
+
type: 'x',
|
|
465
|
+
includedXHandles: ['grok', 'xai'],
|
|
466
|
+
excludedXHandles: ['openai'],
|
|
467
|
+
postFavoriteCount: 10,
|
|
468
|
+
postViewCount: 100,
|
|
469
|
+
},
|
|
470
|
+
],
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
});
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
#### X source parameters
|
|
478
|
+
|
|
479
|
+
- **includedXHandles** _string[]_: Array of X handles to search (without @ symbol)
|
|
480
|
+
- **excludedXHandles** _string[]_: Array of X handles to exclude from search (without @ symbol)
|
|
481
|
+
- **postFavoriteCount** _number_: Minimum favorite count of the X posts to consider.
|
|
482
|
+
- **postViewCount** _number_: Minimum view count of the X posts to consider.
|
|
483
|
+
|
|
484
|
+
#### News Search
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
const result = await generateText({
|
|
488
|
+
model: xai('grok-3-latest'),
|
|
489
|
+
prompt: 'Recent tech industry news',
|
|
490
|
+
providerOptions: {
|
|
491
|
+
xai: {
|
|
492
|
+
searchParameters: {
|
|
493
|
+
mode: 'on',
|
|
494
|
+
sources: [
|
|
495
|
+
{
|
|
496
|
+
type: 'news',
|
|
497
|
+
country: 'US',
|
|
498
|
+
excludedWebsites: ['tabloid.com'],
|
|
499
|
+
safeSearch: true,
|
|
500
|
+
},
|
|
501
|
+
],
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
});
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
#### News source parameters
|
|
509
|
+
|
|
510
|
+
- **country** _string_: ISO alpha-2 country code
|
|
511
|
+
- **excludedWebsites** _string[]_: Max 5 excluded websites
|
|
512
|
+
- **safeSearch** _boolean_: Enable safe search (default: true)
|
|
513
|
+
|
|
514
|
+
#### RSS Feed Search
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
const result = await generateText({
|
|
518
|
+
model: xai('grok-3-latest'),
|
|
519
|
+
prompt: 'Latest status updates',
|
|
520
|
+
providerOptions: {
|
|
521
|
+
xai: {
|
|
522
|
+
searchParameters: {
|
|
523
|
+
mode: 'on',
|
|
524
|
+
sources: [
|
|
525
|
+
{
|
|
526
|
+
type: 'rss',
|
|
527
|
+
links: ['https://status.x.ai/feed.xml'],
|
|
528
|
+
},
|
|
529
|
+
],
|
|
530
|
+
},
|
|
531
|
+
},
|
|
532
|
+
},
|
|
533
|
+
});
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
#### RSS source parameters
|
|
537
|
+
|
|
538
|
+
- **links** _string[]_: Array of RSS feed URLs (max 1 currently supported)
|
|
539
|
+
|
|
540
|
+
### Multiple Sources
|
|
541
|
+
|
|
542
|
+
You can combine multiple data sources in a single search:
|
|
543
|
+
|
|
544
|
+
```ts
|
|
545
|
+
const result = await generateText({
|
|
546
|
+
model: xai('grok-3-latest'),
|
|
547
|
+
prompt: 'Comprehensive overview of recent AI breakthroughs',
|
|
548
|
+
providerOptions: {
|
|
549
|
+
xai: {
|
|
550
|
+
searchParameters: {
|
|
551
|
+
mode: 'on',
|
|
552
|
+
returnCitations: true,
|
|
553
|
+
maxSearchResults: 15,
|
|
554
|
+
sources: [
|
|
555
|
+
{
|
|
556
|
+
type: 'web',
|
|
557
|
+
allowedWebsites: ['arxiv.org', 'openai.com'],
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
type: 'news',
|
|
561
|
+
country: 'US',
|
|
562
|
+
},
|
|
563
|
+
{
|
|
564
|
+
type: 'x',
|
|
565
|
+
includedXHandles: ['openai', 'deepmind'],
|
|
566
|
+
},
|
|
567
|
+
],
|
|
568
|
+
},
|
|
569
|
+
},
|
|
570
|
+
},
|
|
571
|
+
});
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### Sources and Citations
|
|
575
|
+
|
|
576
|
+
When search is enabled with `returnCitations: true`, the response includes sources that were used to generate the answer:
|
|
577
|
+
|
|
578
|
+
```ts
|
|
579
|
+
const { text, sources } = await generateText({
|
|
580
|
+
model: xai('grok-3-latest'),
|
|
581
|
+
prompt: 'What are the latest developments in AI?',
|
|
582
|
+
providerOptions: {
|
|
583
|
+
xai: {
|
|
584
|
+
searchParameters: {
|
|
585
|
+
mode: 'auto',
|
|
586
|
+
returnCitations: true,
|
|
587
|
+
},
|
|
588
|
+
},
|
|
589
|
+
},
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
// Access the sources used
|
|
593
|
+
for (const source of sources) {
|
|
594
|
+
if (source.sourceType === 'url') {
|
|
595
|
+
console.log('Source:', source.url);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### Streaming with Search
|
|
601
|
+
|
|
602
|
+
Live Search works with streaming responses. Citations are included when the stream completes:
|
|
603
|
+
|
|
604
|
+
```ts
|
|
605
|
+
import { streamText } from 'ai';
|
|
606
|
+
|
|
607
|
+
const result = streamText({
|
|
608
|
+
model: xai('grok-3-latest'),
|
|
609
|
+
prompt: 'What has happened in tech recently?',
|
|
610
|
+
providerOptions: {
|
|
611
|
+
xai: {
|
|
612
|
+
searchParameters: {
|
|
613
|
+
mode: 'auto',
|
|
614
|
+
returnCitations: true,
|
|
615
|
+
},
|
|
616
|
+
},
|
|
617
|
+
},
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
for await (const textPart of result.textStream) {
|
|
621
|
+
process.stdout.write(textPart);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
console.log('Sources:', await result.sources);
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
## Model Capabilities
|
|
628
|
+
|
|
629
|
+
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming | Reasoning |
|
|
630
|
+
| --------------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|
|
631
|
+
| `grok-4-fast-non-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
632
|
+
| `grok-4-fast-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
633
|
+
| `grok-code-fast-1` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
634
|
+
| `grok-4` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
635
|
+
| `grok-3` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
636
|
+
| `grok-3-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
637
|
+
| `grok-3-fast` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
638
|
+
| `grok-3-fast-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
639
|
+
| `grok-3-mini` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
640
|
+
| `grok-3-mini-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
641
|
+
| `grok-3-mini-fast` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
642
|
+
| `grok-3-mini-fast-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
643
|
+
| `grok-2` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
644
|
+
| `grok-2-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
645
|
+
| `grok-2-1212` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
646
|
+
| `grok-2-vision` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
647
|
+
| `grok-2-vision-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
648
|
+
| `grok-2-vision-1212` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
649
|
+
| `grok-beta` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
650
|
+
| `grok-vision-beta` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
|
|
651
|
+
|
|
652
|
+
<Note>
|
|
653
|
+
The table above lists popular models. Please see the [xAI
|
|
654
|
+
docs](https://docs.x.ai/docs#models) for a full list of available models. You
|
|
655
|
+
can also pass any available provider model ID as a string if needed.
|
|
656
|
+
</Note>
|
|
657
|
+
|
|
658
|
+
## Image Models
|
|
659
|
+
|
|
660
|
+
You can create xAI image models using the `.image()` factory method. For more on image generation with the AI SDK see [generateImage()](/docs/reference/ai-sdk-core/generate-image).
|
|
661
|
+
|
|
662
|
+
```ts
|
|
663
|
+
import { xai } from '@ai-sdk/xai';
|
|
664
|
+
import { generateImage } from 'ai';
|
|
665
|
+
|
|
666
|
+
const { image } = await generateImage({
|
|
667
|
+
model: xai.image('grok-2-image'),
|
|
668
|
+
prompt: 'A futuristic cityscape at sunset',
|
|
669
|
+
});
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
<Note>
|
|
673
|
+
The xAI image model does not currently support the `aspectRatio` or `size`
|
|
674
|
+
parameters. Image size defaults to 1024x768.
|
|
675
|
+
</Note>
|
|
676
|
+
|
|
677
|
+
### Model-specific options
|
|
678
|
+
|
|
679
|
+
You can customize the image generation behavior with model-specific settings:
|
|
680
|
+
|
|
681
|
+
```ts
|
|
682
|
+
import { xai } from '@ai-sdk/xai';
|
|
683
|
+
import { generateImage } from 'ai';
|
|
684
|
+
|
|
685
|
+
const { images } = await generateImage({
|
|
686
|
+
model: xai.image('grok-2-image'),
|
|
687
|
+
prompt: 'A futuristic cityscape at sunset',
|
|
688
|
+
maxImagesPerCall: 5, // Default is 10
|
|
689
|
+
n: 2, // Generate 2 images
|
|
690
|
+
});
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
### Model Capabilities
|
|
694
|
+
|
|
695
|
+
| Model | Sizes | Notes |
|
|
696
|
+
| -------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
697
|
+
| `grok-2-image` | 1024x768 (default) | xAI's text-to-image generation model, designed to create high-quality images from text prompts. It's trained on a diverse dataset and can generate images across various styles, subjects, and settings. |
|