Skip to content

Commit 49250c1

Browse files
Adds native Live Search support to xAI provider (#9537)
Co-authored-by: Christian Bromann <git@bromann.dev>
1 parent ade8b8a commit 49250c1

File tree

11 files changed

+1759
-21
lines changed

11 files changed

+1759
-21
lines changed

.changeset/ninety-paws-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/xai": minor
3+
---
4+
5+
Adds native Live Search support to xAI provider

libs/providers/langchain-xai/README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,156 @@ const message = new HumanMessage("What color is the sky?");
3131
const res = await model.invoke([message]);
3232
```
3333

34+
## Server Tool Calling (Live Search)
35+
36+
xAI supports server-side tools that are executed by the API rather than requiring client-side execution. The `live_search` tool enables the model to search the web for real-time information.
37+
38+
### Using the built-in live_search tool
39+
40+
```typescript
41+
import { ChatXAI, tools } from "@langchain/xai";
42+
43+
const model = new ChatXAI({
44+
model: "grok-2-1212",
45+
});
46+
47+
// Create the built-in live_search tool with optional parameters
48+
const searchTool = tools.xaiLiveSearch({
49+
maxSearchResults: 5,
50+
returnCitations: true,
51+
});
52+
53+
// Bind the live_search tool to the model
54+
const modelWithSearch = model.bindTools([searchTool]);
55+
56+
// The model will search the web for real-time information
57+
const result = await modelWithSearch.invoke(
58+
"What happened in tech news today?"
59+
);
60+
console.log(result.content);
61+
```
62+
63+
### Using searchParameters for more control
64+
65+
```typescript
66+
import { ChatXAI } from "@langchain/xai";
67+
68+
const model = new ChatXAI({
69+
model: "grok-2-1212",
70+
searchParameters: {
71+
mode: "auto", // "auto" | "on" | "off"
72+
max_search_results: 5,
73+
from_date: "2024-01-01", // ISO date string
74+
return_citations: true,
75+
},
76+
});
77+
78+
const result = await model.invoke("What are the latest AI developments?");
79+
```
80+
81+
### Override search parameters per request
82+
83+
```typescript
84+
const result = await model.invoke("Find recent news about SpaceX", {
85+
searchParameters: {
86+
mode: "on",
87+
max_search_results: 10,
88+
sources: [
89+
{
90+
type: "web",
91+
allowed_websites: ["spacex.com", "nasa.gov"],
92+
},
93+
],
94+
},
95+
});
96+
```
97+
98+
### Configuring data sources with `sources`
99+
100+
You can configure which data sources Live Search should use via the `sources` field
101+
in `searchParameters`. Each entry corresponds to one of the sources described in the
102+
official xAI Live Search docs (`web`, `news`, `x`, `rss`).
103+
104+
```typescript
105+
const result = await model.invoke(
106+
"What are the latest updates from xAI and related news?",
107+
{
108+
searchParameters: {
109+
mode: "on",
110+
sources: [
111+
{
112+
type: "web",
113+
// Only search on these websites
114+
allowed_websites: ["x.ai"],
115+
},
116+
{
117+
type: "news",
118+
// Exclude specific news websites
119+
excluded_websites: ["bbc.co.uk"],
120+
},
121+
{
122+
type: "x",
123+
// Focus on specific X handles
124+
included_x_handles: ["xai"],
125+
},
126+
],
127+
},
128+
}
129+
);
130+
```
131+
132+
You can also use RSS feeds as a data source:
133+
134+
```typescript
135+
const result = await model.invoke("Summarize the latest posts from this feed", {
136+
searchParameters: {
137+
mode: "on",
138+
sources: [
139+
{
140+
type: "rss",
141+
links: ["https://example.com/feed.rss"],
142+
},
143+
],
144+
},
145+
});
146+
```
147+
148+
> Notes:
149+
>
150+
> - The `xaiLiveSearch` tool options use **camelCase** field names in TypeScript
151+
> (for example `maxSearchResults`, `fromDate`, `returnCitations`,
152+
> `allowedWebsites`, `excludedWebsites`, `includedXHandles`). These are
153+
> automatically mapped to the underlying JSON API's `search_parameters`
154+
> object, which uses `snake_case` field names as documented in the official
155+
> xAI Live Search docs.
156+
157+
### Combining live_search with custom tools
158+
159+
```typescript
160+
import { ChatXAI, tools } from "@langchain/xai";
161+
162+
const model = new ChatXAI({ model: "grok-2-1212" });
163+
164+
const modelWithTools = model.bindTools([
165+
tools.xaiLiveSearch(), // Built-in server tool
166+
{
167+
// Custom function tool
168+
type: "function",
169+
function: {
170+
name: "get_stock_price",
171+
description: "Get the current stock price",
172+
parameters: {
173+
type: "object",
174+
properties: {
175+
symbol: { type: "string" },
176+
},
177+
required: ["symbol"],
178+
},
179+
},
180+
},
181+
]);
182+
```
183+
34184
## Development
35185

36186
To develop the `@langchain/xai` package, you'll need to follow these instructions:

0 commit comments

Comments
 (0)