@@ -31,6 +31,152 @@ const message = new HumanMessage("What color is the sky?");
3131const 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 } from " @langchain/xai" ;
42+
43+ const model = new ChatXAI ({
44+ model: " grok-2-1212" ,
45+ });
46+
47+ // Bind the live_search tool
48+ const modelWithSearch = model .bindTools ([{ type: " live_search" }]);
49+
50+ // The model will search the web for real-time information
51+ const result = await modelWithSearch .invoke (
52+ " What happened in tech news today?"
53+ );
54+ console .log (result .content );
55+ ```
56+
57+ ### Using searchParameters for more control
58+
59+ ``` typescript
60+ import { ChatXAI } from " @langchain/xai" ;
61+
62+ const model = new ChatXAI ({
63+ model: " grok-2-1212" ,
64+ searchParameters: {
65+ mode: " auto" , // "auto" | "on" | "off"
66+ max_search_results: 5 ,
67+ from_date: " 2024-01-01" , // ISO date string
68+ return_citations: true ,
69+ },
70+ });
71+
72+ const result = await model .invoke (" What are the latest AI developments?" );
73+ ```
74+
75+ ### Override search parameters per request
76+
77+ ``` typescript
78+ const result = await model .invoke (" Find recent news about SpaceX" , {
79+ searchParameters: {
80+ mode: " on" ,
81+ max_search_results: 10 ,
82+ sources: [
83+ {
84+ type: " web" ,
85+ allowed_websites: [" spacex.com" , " nasa.gov" ],
86+ },
87+ ],
88+ },
89+ });
90+ ```
91+
92+ ### Configuring data sources with ` sources `
93+
94+ You can configure which data sources Live Search should use via the ` sources ` field
95+ in ` searchParameters ` . Each entry corresponds to one of the sources described in the
96+ official xAI Live Search docs (` web ` , ` news ` , ` x ` , ` rss ` ).
97+
98+ ``` typescript
99+ const result = await model .invoke (
100+ " What are the latest updates from xAI and related news?" ,
101+ {
102+ searchParameters: {
103+ mode: " on" ,
104+ sources: [
105+ {
106+ type: " web" ,
107+ // Only search on these websites
108+ allowed_websites: [" x.ai" ],
109+ },
110+ {
111+ type: " news" ,
112+ // Exclude specific news websites
113+ excluded_websites: [" bbc.co.uk" ],
114+ },
115+ {
116+ type: " x" ,
117+ // Focus on specific X handles
118+ included_x_handles: [" xai" ],
119+ },
120+ ],
121+ },
122+ }
123+ );
124+ ```
125+
126+ You can also use RSS feeds as a data source:
127+
128+ ``` typescript
129+ const result = await model .invoke (" Summarize the latest posts from this feed" , {
130+ searchParameters: {
131+ mode: " on" ,
132+ sources: [
133+ {
134+ type: " rss" ,
135+ links: [" https://example.com/feed.rss" ],
136+ },
137+ ],
138+ },
139+ });
140+ ```
141+
142+ > Notes:
143+ >
144+ > - The previous ` allowed_domains ` / ` excluded_domains ` fields are not
145+ > supported in this provider. Use ` sources ` with ` allowed_websites ` and
146+ > ` excluded_websites ` instead.
147+ > - In TypeScript, the ` XAISearchParameters ` and ` sources ` types use the same
148+ > ` snake_case ` field names as the underlying JSON API (for example
149+ > ` allowed_websites ` , ` excluded_websites ` , ` included_x_handles ` ). There are no
150+ > separate camelCase aliases (` allowedWebsites ` , etc.), which keeps the
151+ > provider aligned with the official xAI documentation.
152+
153+ ### Combining live_search with custom tools
154+
155+ ``` typescript
156+ import { ChatXAI } from " @langchain/xai" ;
157+
158+ const model = new ChatXAI ({ model: " grok-2-1212" });
159+
160+ const modelWithTools = model .bindTools ([
161+ { type: " live_search" }, // Built-in server tool
162+ {
163+ // Custom function tool
164+ type: " function" ,
165+ function: {
166+ name: " get_stock_price" ,
167+ description: " Get the current stock price" ,
168+ parameters: {
169+ type: " object" ,
170+ properties: {
171+ symbol: { type: " string" },
172+ },
173+ required: [" symbol" ],
174+ },
175+ },
176+ },
177+ ]);
178+ ```
179+
34180## Development
35181
36182To develop the ` @langchain/xai ` package, you'll need to follow these instructions:
0 commit comments