@langchain/xai 1.0.3-dev-1765432861398 → 1.1.0
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 +11 -0
- package/README.md +150 -0
- package/dist/chat_models.cjs +133 -4
- package/dist/chat_models.cjs.map +1 -1
- package/dist/chat_models.d.cts +195 -7
- package/dist/chat_models.d.cts.map +1 -1
- package/dist/chat_models.d.ts +195 -7
- package/dist/chat_models.d.ts.map +1 -1
- package/dist/chat_models.js +133 -5
- package/dist/chat_models.js.map +1 -1
- package/dist/index.cjs +4 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/live_search.cjs +54 -0
- package/dist/live_search.cjs.map +1 -0
- package/dist/live_search.d.cts +145 -0
- package/dist/live_search.d.cts.map +1 -0
- package/dist/live_search.d.ts +145 -0
- package/dist/live_search.d.ts.map +1 -0
- package/dist/live_search.js +51 -0
- package/dist/live_search.js.map +1 -0
- package/dist/tools/index.cjs +8 -0
- package/dist/tools/index.cjs.map +1 -0
- package/dist/tools/index.d.cts +9 -0
- package/dist/tools/index.d.cts.map +1 -0
- package/dist/tools/index.d.ts +9 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/live_search.cjs +80 -0
- package/dist/tools/live_search.cjs.map +1 -0
- package/dist/tools/live_search.d.cts +136 -0
- package/dist/tools/live_search.d.cts.map +1 -0
- package/dist/tools/live_search.d.ts +136 -0
- package/dist/tools/live_search.d.ts.map +1 -0
- package/dist/tools/live_search.js +78 -0
- package/dist/tools/live_search.js.map +1 -0
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @langchain/xai
|
|
2
2
|
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#9537](https://github.com/langchain-ai/langchainjs/pull/9537) [`49250c1`](https://github.com/langchain-ai/langchainjs/commit/49250c156edeb2952c25e3f17d4de5df44753835) Thanks [@pawel-twardziak](https://github.com/pawel-twardziak)! - Adds native Live Search support to xAI provider
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`5a01b5b`](https://github.com/langchain-ai/langchainjs/commit/5a01b5b705f6933958f61318b22f00b5f4763be8), [`eab88a5`](https://github.com/langchain-ai/langchainjs/commit/eab88a5ab7610f5b63212f753ebcbeee2f393622), [`5f79bc5`](https://github.com/langchain-ai/langchainjs/commit/5f79bc50aebc093c90b6716c0aebf5c4813d0171), [`7b301c0`](https://github.com/langchain-ai/langchainjs/commit/7b301c00ac851c286a13c2a908757cb40180c768), [`bb2f422`](https://github.com/langchain-ai/langchainjs/commit/bb2f422cd8e0d709d82baca44565980abb57120f), [`2a5ba50`](https://github.com/langchain-ai/langchainjs/commit/2a5ba50d240e7d6181546facf088142fbb7b4977), [`47edf3f`](https://github.com/langchain-ai/langchainjs/commit/47edf3fc673eb0627ec585a3a5c2b9381e234527), [`2e563e3`](https://github.com/langchain-ai/langchainjs/commit/2e563e332772aa0468f610c334cbedd7f3513ce8), [`72795fe`](https://github.com/langchain-ai/langchainjs/commit/72795fe76b515d9edc7d78fb28db59df844ce0c3), [`f97b488`](https://github.com/langchain-ai/langchainjs/commit/f97b488200b34c485b15a743277984ecacc62160), [`29a8480`](https://github.com/langchain-ai/langchainjs/commit/29a8480799d4c3534892a29cef4a135c437deb9b), [`3ecc1e7`](https://github.com/langchain-ai/langchainjs/commit/3ecc1e716704a032e941e670d1d9fbf5370d57aa), [`6baa851`](https://github.com/langchain-ai/langchainjs/commit/6baa851176b5dde5da19891df114a4645dfe7481), [`a552cad`](https://github.com/langchain-ai/langchainjs/commit/a552cad1a463239a0d1d1b5da7798978722738cf), [`69a1045`](https://github.com/langchain-ai/langchainjs/commit/69a1045e1e14aed9273a1a4085ac35e601a1ecc7)]:
|
|
12
|
+
- @langchain/openai@1.2.0
|
|
13
|
+
|
|
3
14
|
## 1.0.2
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -31,6 +31,156 @@ const message = new HumanMessage("What color is the sky?");
|
|
|
31
31
|
const res = await model.invoke([message]);
|
|
32
32
|
```
|
|
33
33
|
|
|
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
|
+
|
|
34
184
|
## Development
|
|
35
185
|
|
|
36
186
|
To develop the `@langchain/xai` package, you'll need to follow these instructions:
|
package/dist/chat_models.cjs
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_live_search = require('./live_search.cjs');
|
|
2
3
|
const require_profiles = require('./profiles.cjs');
|
|
4
|
+
const require_live_search$1 = require('./tools/live_search.cjs');
|
|
5
|
+
const __langchain_core_utils_function_calling = require_rolldown_runtime.__toESM(require("@langchain/core/utils/function_calling"));
|
|
3
6
|
const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
|
|
4
7
|
const __langchain_openai = require_rolldown_runtime.__toESM(require("@langchain/openai"));
|
|
5
8
|
|
|
6
9
|
//#region src/chat_models.ts
|
|
7
10
|
/**
|
|
11
|
+
* Set of all supported xAI built-in server-side tool types.
|
|
12
|
+
* This allows us to easily extend support for future built-in tools
|
|
13
|
+
* without changing the core detection logic.
|
|
14
|
+
*/
|
|
15
|
+
const XAI_BUILT_IN_TOOL_TYPES = new Set([require_live_search$1.XAI_LIVE_SEARCH_TOOL_TYPE]);
|
|
16
|
+
/**
|
|
17
|
+
* Checks if a tool is an xAI built-in tool (like live_search).
|
|
18
|
+
* Built-in tools are executed server-side by the xAI API.
|
|
19
|
+
*
|
|
20
|
+
* @param tool - The tool to check
|
|
21
|
+
* @returns true if the tool is an xAI built-in tool
|
|
22
|
+
*/
|
|
23
|
+
function isXAIBuiltInTool(tool) {
|
|
24
|
+
return typeof tool === "object" && tool !== null && "type" in tool && typeof tool.type === "string" && XAI_BUILT_IN_TOOL_TYPES.has(tool.type);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
8
27
|
* xAI chat model integration.
|
|
9
28
|
*
|
|
10
29
|
* The xAI API is compatible to the OpenAI API with some limitations.
|
|
@@ -329,6 +348,57 @@ const __langchain_openai = require_rolldown_runtime.__toESM(require("@langchain/
|
|
|
329
348
|
* </details>
|
|
330
349
|
*
|
|
331
350
|
* <br />
|
|
351
|
+
*
|
|
352
|
+
* <details>
|
|
353
|
+
* <summary><strong>Server Tool Calling (Live Search)</strong></summary>
|
|
354
|
+
*
|
|
355
|
+
* xAI supports server-side tools that are executed by the API rather than
|
|
356
|
+
* requiring client-side execution. The `live_search` tool enables the model
|
|
357
|
+
* to search the web for real-time information.
|
|
358
|
+
*
|
|
359
|
+
* ```typescript
|
|
360
|
+
* // Method 1: Using the built-in live_search tool
|
|
361
|
+
* const llm = new ChatXAI({
|
|
362
|
+
* model: "grok-beta",
|
|
363
|
+
* temperature: 0,
|
|
364
|
+
* });
|
|
365
|
+
*
|
|
366
|
+
* const llmWithSearch = llm.bindTools([{ type: "live_search" }]);
|
|
367
|
+
* const result = await llmWithSearch.invoke("What happened in tech news today?");
|
|
368
|
+
* console.log(result.content);
|
|
369
|
+
* // The model will search the web and include real-time information in its response
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
* ```typescript
|
|
373
|
+
* // Method 2: Using searchParameters for more control
|
|
374
|
+
* const llm = new ChatXAI({
|
|
375
|
+
* model: "grok-beta",
|
|
376
|
+
* searchParameters: {
|
|
377
|
+
* mode: "auto", // "auto" | "on" | "off"
|
|
378
|
+
* max_search_results: 5,
|
|
379
|
+
* from_date: "2024-01-01", // ISO date string
|
|
380
|
+
* return_citations: true,
|
|
381
|
+
* }
|
|
382
|
+
* });
|
|
383
|
+
*
|
|
384
|
+
* const result = await llm.invoke("What are the latest AI developments?");
|
|
385
|
+
* ```
|
|
386
|
+
*
|
|
387
|
+
* ```typescript
|
|
388
|
+
* // Method 3: Override search parameters per request
|
|
389
|
+
* const result = await llm.invoke("Find recent news about SpaceX", {
|
|
390
|
+
* searchParameters: {
|
|
391
|
+
* mode: "on",
|
|
392
|
+
* max_search_results: 10,
|
|
393
|
+
* sources: [
|
|
394
|
+
* { type: "web", allowed_websites: ["spacex.com", "nasa.gov"] },
|
|
395
|
+
* ],
|
|
396
|
+
* }
|
|
397
|
+
* });
|
|
398
|
+
* ```
|
|
399
|
+
* </details>
|
|
400
|
+
*
|
|
401
|
+
* <br />
|
|
332
402
|
*/
|
|
333
403
|
var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
|
|
334
404
|
static lc_name() {
|
|
@@ -346,6 +416,10 @@ var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
|
|
|
346
416
|
"chat_models",
|
|
347
417
|
"xai"
|
|
348
418
|
];
|
|
419
|
+
/**
|
|
420
|
+
* Default search parameters for the Live Search API.
|
|
421
|
+
*/
|
|
422
|
+
searchParameters;
|
|
349
423
|
constructor(fields) {
|
|
350
424
|
const apiKey = fields?.apiKey || (0, __langchain_core_utils_env.getEnvironmentVariable)("XAI_API_KEY");
|
|
351
425
|
if (!apiKey) throw new Error(`xAI API key not found. Please set the XAI_API_KEY environment variable or provide the key into "apiKey" field.`);
|
|
@@ -355,6 +429,7 @@ var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
|
|
|
355
429
|
apiKey,
|
|
356
430
|
configuration: { baseURL: "https://api.x.ai/v1" }
|
|
357
431
|
});
|
|
432
|
+
this.searchParameters = fields?.searchParameters;
|
|
358
433
|
}
|
|
359
434
|
toJSON() {
|
|
360
435
|
const result = super.toJSON();
|
|
@@ -370,6 +445,51 @@ var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
|
|
|
370
445
|
return params;
|
|
371
446
|
}
|
|
372
447
|
/**
|
|
448
|
+
* Get the effective search parameters, merging defaults with call options.
|
|
449
|
+
* @param options Call options that may contain search parameters
|
|
450
|
+
* @returns Merged search parameters or undefined if none are configured
|
|
451
|
+
*/
|
|
452
|
+
_getEffectiveSearchParameters(options) {
|
|
453
|
+
return require_live_search.mergeSearchParams(this.searchParameters, options?.searchParameters);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Check if any built-in tools (like live_search) are in the tools list.
|
|
457
|
+
* @param tools List of tools to check
|
|
458
|
+
* @returns true if any built-in tools are present
|
|
459
|
+
*/
|
|
460
|
+
_hasBuiltInTools(tools) {
|
|
461
|
+
return tools?.some(isXAIBuiltInTool) ?? false;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Formats tools to xAI/OpenAI format, preserving provider-specific definitions.
|
|
465
|
+
*
|
|
466
|
+
* @param tools The tools to format
|
|
467
|
+
* @returns The formatted tools
|
|
468
|
+
*/
|
|
469
|
+
formatStructuredToolToXAI(tools) {
|
|
470
|
+
if (!tools || !tools.length) return void 0;
|
|
471
|
+
return tools.map((tool) => {
|
|
472
|
+
if ((0, __langchain_core_utils_function_calling.isLangChainTool)(tool) && tool.extras?.providerToolDefinition) return tool.extras.providerToolDefinition;
|
|
473
|
+
if (isXAIBuiltInTool(tool)) return tool;
|
|
474
|
+
return (0, __langchain_core_utils_function_calling.convertToOpenAITool)(tool);
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
bindTools(tools, kwargs) {
|
|
478
|
+
return this.withConfig({
|
|
479
|
+
tools: this.formatStructuredToolToXAI(tools),
|
|
480
|
+
...kwargs
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
/** @internal */
|
|
484
|
+
invocationParams(options, extra) {
|
|
485
|
+
const baseParams = super.invocationParams(options, extra);
|
|
486
|
+
const params = { ...baseParams };
|
|
487
|
+
const liveSearchTool = options?.tools?.find(isXAIBuiltInTool);
|
|
488
|
+
const mergedSearchParams = require_live_search.mergeSearchParams(this.searchParameters, options?.searchParameters, liveSearchTool);
|
|
489
|
+
if (mergedSearchParams) params.search_parameters = require_live_search.buildSearchParametersPayload(mergedSearchParams);
|
|
490
|
+
return params;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
373
493
|
* Calls the xAI API with retry logic in case of failures.
|
|
374
494
|
* @param request The request to send to the xAI API.
|
|
375
495
|
* @param options Optional configuration for the API call.
|
|
@@ -387,24 +507,32 @@ var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
|
|
|
387
507
|
};
|
|
388
508
|
return msg;
|
|
389
509
|
});
|
|
510
|
+
let filteredTools;
|
|
511
|
+
if (request.tools) filteredTools = require_live_search.filterXAIBuiltInTools({
|
|
512
|
+
tools: request.tools,
|
|
513
|
+
excludedTypes: [require_live_search$1.XAI_LIVE_SEARCH_TOOL_TYPE]
|
|
514
|
+
});
|
|
390
515
|
const newRequest = {
|
|
391
516
|
...request,
|
|
392
|
-
messages: newRequestMessages
|
|
517
|
+
messages: newRequestMessages,
|
|
518
|
+
tools: filteredTools
|
|
393
519
|
};
|
|
394
520
|
if (newRequest.stream === true) return super.completionWithRetry(newRequest, options);
|
|
395
521
|
return super.completionWithRetry(newRequest, options);
|
|
396
522
|
}
|
|
397
523
|
_convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
|
|
398
524
|
const messageChunk = super._convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
|
|
525
|
+
const responseMetadata = messageChunk.response_metadata;
|
|
399
526
|
if (!rawResponse.choices[0]?.finish_reason) {
|
|
400
|
-
delete
|
|
527
|
+
delete responseMetadata.usage;
|
|
401
528
|
delete messageChunk.usage_metadata;
|
|
402
|
-
} else messageChunk.usage_metadata =
|
|
529
|
+
} else messageChunk.usage_metadata = responseMetadata.usage;
|
|
403
530
|
return messageChunk;
|
|
404
531
|
}
|
|
405
532
|
_convertCompletionsMessageToBaseMessage(message, rawResponse) {
|
|
406
533
|
const langChainMessage = super._convertCompletionsMessageToBaseMessage(message, rawResponse);
|
|
407
|
-
|
|
534
|
+
const additionalKwargs = langChainMessage.additional_kwargs;
|
|
535
|
+
additionalKwargs.reasoning_content = message.reasoning_content;
|
|
408
536
|
return langChainMessage;
|
|
409
537
|
}
|
|
410
538
|
/**
|
|
@@ -436,4 +564,5 @@ var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
|
|
|
436
564
|
|
|
437
565
|
//#endregion
|
|
438
566
|
exports.ChatXAI = ChatXAI;
|
|
567
|
+
exports.isXAIBuiltInTool = isXAIBuiltInTool;
|
|
439
568
|
//# sourceMappingURL=chat_models.cjs.map
|
package/dist/chat_models.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat_models.cjs","names":["ChatOpenAICompletions","fields?: Partial<ChatXAIInput>","options: this[\"ParsedCallOptions\"]","request:\n | OpenAIClient.Chat.ChatCompletionCreateParamsStreaming\n | OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming","options?: OpenAICoreRequestOptions","delta: Record<string, any>","rawResponse: OpenAIClient.ChatCompletionChunk","defaultRole?:\n | \"function\"\n | \"user\"\n | \"system\"\n | \"developer\"\n | \"assistant\"\n | \"tool\"","messageChunk: AIMessageChunk","message: OpenAIClient.ChatCompletionMessage","rawResponse: OpenAIClient.ChatCompletion","PROFILES","outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>","config?: StructuredOutputMethodOptions<boolean>"],"sources":["../src/chat_models.ts"],"sourcesContent":["import {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n} from \"@langchain/core/language_models/base\";\nimport {\n BaseChatModelCallOptions,\n BindToolsInput,\n LangSmithParams,\n type BaseChatModelParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport { ModelProfile } from \"@langchain/core/language_models/profile\";\nimport { Serialized } from \"@langchain/core/load/serializable\";\nimport { AIMessageChunk, BaseMessage } from \"@langchain/core/messages\";\nimport { Runnable } from \"@langchain/core/runnables\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { InteropZodType } from \"@langchain/core/utils/types\";\nimport {\n type OpenAICoreRequestOptions,\n type OpenAIClient,\n OpenAIToolChoice,\n ChatOpenAICompletions,\n} from \"@langchain/openai\";\nimport PROFILES from \"./profiles.js\";\n\ntype ChatXAIToolType = BindToolsInput | OpenAIClient.ChatCompletionTool;\n\nexport interface ChatXAICallOptions extends BaseChatModelCallOptions {\n headers?: Record<string, string>;\n tools?: ChatXAIToolType[];\n tool_choice?: OpenAIToolChoice | string | \"auto\" | \"any\";\n}\n\nexport interface ChatXAIInput extends BaseChatModelParams {\n /**\n * The xAI API key to use for requests.\n * @default process.env.XAI_API_KEY\n */\n apiKey?: string;\n /**\n * The name of the model to use.\n * @default \"grok-beta\"\n */\n model?: string;\n /**\n * Up to 4 sequences where the API will stop generating further tokens. The\n * returned text will not contain the stop sequence.\n * Alias for `stopSequences`\n */\n stop?: Array<string>;\n /**\n * Up to 4 sequences where the API will stop generating further tokens. The\n * returned text will not contain the stop sequence.\n */\n stopSequences?: Array<string>;\n /**\n * Whether or not to stream responses.\n */\n streaming?: boolean;\n /**\n * The temperature to use for sampling.\n * @default 0.7\n */\n temperature?: number;\n /**\n * The maximum number of tokens that the model can process in a single response.\n * This limits ensures computational efficiency and resource management.\n */\n maxTokens?: number;\n}\n\n/**\n * xAI chat model integration.\n *\n * The xAI API is compatible to the OpenAI API with some limitations.\n *\n * Setup:\n * Install `@langchain/xai` and set an environment variable named `XAI_API_KEY`.\n *\n * ```bash\n * npm install @langchain/xai\n * export XAI_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * tool_choice: \"auto\",\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatXAI } from '@langchain/xai';\n *\n * const llm = new ChatXAI({\n * model: \"grok-beta\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"content\": \"The French translation of \\\"I love programming\\\" is \\\"J'aime programmer\\\". In this sentence, \\\"J'aime\\\" is the first person singular conjugation of the French verb \\\"aimer\\\" which means \\\"to love\\\", and \\\"programmer\\\" is the French infinitive for \\\"to program\\\". I hope this helps! Let me know if you have any other questions.\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"tokenUsage\": {\n * \"completionTokens\": 82,\n * \"promptTokens\": 20,\n * \"totalTokens\": 102\n * },\n * \"finish_reason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"invalid_tool_calls\": []\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"The\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" French\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" translation\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" of\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" \\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"I\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" love\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ...\n * AIMessageChunk {\n * \"content\": \".\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"The French translation of \\\"I love programming\\\" is \\\"J'aime programmer\\\". In this sentence, \\\"J'aime\\\" is the first person singular conjugation of the French verb \\\"aimer\\\" which means \\\"to love\\\", and \\\"programmer\\\" is the French infinitive for \\\"to program\\\". I hope this helps! Let me know if you have any other questions.\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const llmForToolCalling = new ChatXAI({\n * model: \"grok-beta\",\n * temperature: 0,\n * // other params...\n * });\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call',\n * id: 'call_cd34'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call',\n * id: 'call_68rf'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call',\n * id: 'call_f81z'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call',\n * id: 'call_8byt'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: \"Why don't cats play poker in the wild?\",\n * punchline: 'Because there are too many cheetahs.'\n * }\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatXAI extends ChatOpenAICompletions<ChatXAICallOptions> {\n static lc_name() {\n return \"ChatXAI\";\n }\n\n _llmType() {\n return \"xai\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"XAI_API_KEY\",\n };\n }\n\n lc_serializable = true;\n\n lc_namespace = [\"langchain\", \"chat_models\", \"xai\"];\n\n constructor(fields?: Partial<ChatXAIInput>) {\n const apiKey = fields?.apiKey || getEnvironmentVariable(\"XAI_API_KEY\");\n if (!apiKey) {\n throw new Error(\n `xAI API key not found. Please set the XAI_API_KEY environment variable or provide the key into \"apiKey\" field.`\n );\n }\n\n super({\n ...fields,\n model: fields?.model || \"grok-beta\",\n apiKey,\n configuration: {\n baseURL: \"https://api.x.ai/v1\",\n },\n });\n }\n\n toJSON(): Serialized {\n const result = super.toJSON();\n\n if (\n \"kwargs\" in result &&\n typeof result.kwargs === \"object\" &&\n result.kwargs != null\n ) {\n delete result.kwargs.openai_api_key;\n delete result.kwargs.configuration;\n }\n\n return result;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n const params = super.getLsParams(options);\n params.ls_provider = \"xai\";\n return params;\n }\n\n async completionWithRetry(\n request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;\n\n async completionWithRetry(\n request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;\n\n /**\n * Calls the xAI API with retry logic in case of failures.\n * @param request The request to send to the xAI API.\n * @param options Optional configuration for the API call.\n * @returns The response from the xAI API.\n */\n async completionWithRetry(\n request:\n | OpenAIClient.Chat.ChatCompletionCreateParamsStreaming\n | OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<\n | AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>\n | OpenAIClient.Chat.Completions.ChatCompletion\n > {\n delete request.frequency_penalty;\n delete request.presence_penalty;\n delete request.logit_bias;\n delete request.functions;\n\n const newRequestMessages = request.messages.map((msg) => {\n if (!msg.content) {\n return {\n ...msg,\n content: \"\",\n };\n }\n return msg;\n });\n\n const newRequest = {\n ...request,\n messages: newRequestMessages,\n };\n\n if (newRequest.stream === true) {\n return super.completionWithRetry(newRequest, options);\n }\n\n return super.completionWithRetry(newRequest, options);\n }\n\n protected override _convertCompletionsDeltaToBaseMessageChunk(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n delta: Record<string, any>,\n rawResponse: OpenAIClient.ChatCompletionChunk,\n defaultRole?:\n | \"function\"\n | \"user\"\n | \"system\"\n | \"developer\"\n | \"assistant\"\n | \"tool\"\n ) {\n const messageChunk: AIMessageChunk =\n super._convertCompletionsDeltaToBaseMessageChunk(\n delta,\n rawResponse,\n defaultRole\n );\n // Make concatenating chunks work without merge warning\n if (!rawResponse.choices[0]?.finish_reason) {\n delete messageChunk.response_metadata.usage;\n delete messageChunk.usage_metadata;\n } else {\n messageChunk.usage_metadata = messageChunk.response_metadata.usage;\n }\n return messageChunk;\n }\n\n protected override _convertCompletionsMessageToBaseMessage(\n message: OpenAIClient.ChatCompletionMessage,\n rawResponse: OpenAIClient.ChatCompletion\n ) {\n const langChainMessage = super._convertCompletionsMessageToBaseMessage(\n message,\n rawResponse\n );\n langChainMessage.additional_kwargs.reasoning_content =\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (message as any).reasoning_content;\n return langChainMessage;\n }\n\n /**\n * Return profiling information for the model.\n *\n * Provides information about the model's capabilities and constraints,\n * including token limits, multimodal support, and advanced features like\n * tool calling and structured output.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n *\n * @example\n * ```typescript\n * const model = new ChatXAI({ model: \"grok-beta\" });\n * const profile = model.profile;\n * console.log(profile.maxInputTokens); // 128000\n * console.log(profile.imageInputs); // true\n * ```\n */\n get profile(): ModelProfile {\n return PROFILES[this.model] ?? {};\n }\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n const ensuredConfig = { ...config };\n if (ensuredConfig?.method === undefined) {\n ensuredConfig.method = \"functionCalling\";\n }\n return super.withStructuredOutput<RunOutput>(outputSchema, ensuredConfig);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4YA,IAAa,UAAb,cAA6BA,yCAA0C;CACrE,OAAO,UAAU;AACf,SAAO;CACR;CAED,WAAW;AACT,SAAO;CACR;CAED,IAAI,aAAoD;AACtD,SAAO,EACL,QAAQ,cACT;CACF;CAED,kBAAkB;CAElB,eAAe;EAAC;EAAa;EAAe;CAAM;CAElD,YAAYC,QAAgC;EAC1C,MAAM,SAAS,QAAQ,iEAAiC,cAAc;AACtE,MAAI,CAAC,OACH,OAAM,IAAI,MACR,CAAC,8GAA8G,CAAC;EAIpH,MAAM;GACJ,GAAG;GACH,OAAO,QAAQ,SAAS;GACxB;GACA,eAAe,EACb,SAAS,sBACV;EACF,EAAC;CACH;CAED,SAAqB;EACnB,MAAM,SAAS,MAAM,QAAQ;AAE7B,MACE,YAAY,UACZ,OAAO,OAAO,WAAW,YACzB,OAAO,UAAU,MACjB;GACA,OAAO,OAAO,OAAO;GACrB,OAAO,OAAO,OAAO;EACtB;AAED,SAAO;CACR;CAED,YAAYC,SAAqD;EAC/D,MAAM,SAAS,MAAM,YAAY,QAAQ;EACzC,OAAO,cAAc;AACrB,SAAO;CACR;;;;;;;CAkBD,MAAM,oBACJC,SAGAC,SAIA;EACA,OAAO,QAAQ;EACf,OAAO,QAAQ;EACf,OAAO,QAAQ;EACf,OAAO,QAAQ;EAEf,MAAM,qBAAqB,QAAQ,SAAS,IAAI,CAAC,QAAQ;AACvD,OAAI,CAAC,IAAI,QACP,QAAO;IACL,GAAG;IACH,SAAS;GACV;AAEH,UAAO;EACR,EAAC;EAEF,MAAM,aAAa;GACjB,GAAG;GACH,UAAU;EACX;AAED,MAAI,WAAW,WAAW,KACxB,QAAO,MAAM,oBAAoB,YAAY,QAAQ;AAGvD,SAAO,MAAM,oBAAoB,YAAY,QAAQ;CACtD;CAED,AAAmB,2CAEjBC,OACAC,aACAC,aAOA;EACA,MAAMC,eACJ,MAAM,2CACJ,OACA,aACA,YACD;AAEH,MAAI,CAAC,YAAY,QAAQ,IAAI,eAAe;GAC1C,OAAO,aAAa,kBAAkB;GACtC,OAAO,aAAa;EACrB,OACC,aAAa,iBAAiB,aAAa,kBAAkB;AAE/D,SAAO;CACR;CAED,AAAmB,wCACjBC,SACAC,aACA;EACA,MAAM,mBAAmB,MAAM,wCAC7B,SACA,YACD;EACD,iBAAiB,kBAAkB,oBAEhC,QAAgB;AACnB,SAAO;CACR;;;;;;;;;;;;;;;;;;CAmBD,IAAI,UAAwB;AAC1B,SAAOC,yBAAS,KAAK,UAAU,CAAE;CAClC;CAqCD,AAAS,qBAIPC,cAIAC,QAMI;EACJ,MAAM,gBAAgB,EAAE,GAAG,OAAQ;AACnC,MAAI,eAAe,WAAW,QAC5B,cAAc,SAAS;AAEzB,SAAO,MAAM,qBAAgC,cAAc,cAAc;CAC1E;AACF"}
|
|
1
|
+
{"version":3,"file":"chat_models.cjs","names":["XAI_LIVE_SEARCH_TOOL_TYPE","tool: ChatXAIToolType","ChatOpenAICompletions","fields?: Partial<ChatXAIInput>","options: this[\"ParsedCallOptions\"]","options?: this[\"ParsedCallOptions\"]","mergeSearchParams","tools?: ChatXAIToolType[]","tools: ChatXAIToolType[]","kwargs?: Partial<ChatXAICallOptions>","extra?: { streaming?: boolean }","params: ChatXAICompletionsInvocationParams","buildSearchParametersPayload","request:\n | OpenAIClient.Chat.ChatCompletionCreateParamsStreaming\n | OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming","options?: OpenAICoreRequestOptions","filteredTools: OpenAIClient.ChatCompletionTool[] | undefined","filterXAIBuiltInTools","delta: Record<string, any>","rawResponse: OpenAIClient.ChatCompletionChunk","defaultRole?:\n | \"function\"\n | \"user\"\n | \"system\"\n | \"developer\"\n | \"assistant\"\n | \"tool\"","message: OpenAIClient.ChatCompletionMessage & {\n reasoning_content?: string;\n }","rawResponse: OpenAIClient.ChatCompletion","PROFILES","outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>","config?: StructuredOutputMethodOptions<boolean>"],"sources":["../src/chat_models.ts"],"sourcesContent":["import {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n} from \"@langchain/core/language_models/base\";\nimport {\n BaseChatModelCallOptions,\n BindToolsInput,\n LangSmithParams,\n type BaseChatModelParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport {\n isLangChainTool,\n convertToOpenAITool,\n} from \"@langchain/core/utils/function_calling\";\nimport { ModelProfile } from \"@langchain/core/language_models/profile\";\nimport { Serialized } from \"@langchain/core/load/serializable\";\nimport {\n AIMessageChunk,\n BaseMessage,\n type UsageMetadata,\n} from \"@langchain/core/messages\";\nimport { Runnable } from \"@langchain/core/runnables\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport { InteropZodType } from \"@langchain/core/utils/types\";\nimport {\n type OpenAICoreRequestOptions,\n type OpenAIClient,\n ChatOpenAICompletions,\n} from \"@langchain/openai\";\nimport {\n buildSearchParametersPayload,\n filterXAIBuiltInTools,\n mergeSearchParams,\n type XAISearchParameters,\n type XAISearchParametersPayload,\n} from \"./live_search.js\";\nimport PROFILES from \"./profiles.js\";\nimport {\n XAI_LIVE_SEARCH_TOOL_TYPE,\n XAILiveSearchTool,\n} from \"./tools/live_search.js\";\n\nexport type OpenAIToolChoice =\n | OpenAIClient.ChatCompletionToolChoiceOption\n | \"any\"\n | string;\n\n/**\n * Union type for all xAI built-in server-side tools.\n */\nexport type XAIBuiltInTool = XAILiveSearchTool;\n\n/**\n * Set of all supported xAI built-in server-side tool types.\n * This allows us to easily extend support for future built-in tools\n * without changing the core detection logic.\n */\nconst XAI_BUILT_IN_TOOL_TYPES = new Set<XAILiveSearchTool[\"type\"] | string>([\n XAI_LIVE_SEARCH_TOOL_TYPE,\n]);\n\n/**\n * Tool type that includes both standard tools and xAI built-in tools.\n */\ntype ChatXAIToolType =\n | BindToolsInput\n | OpenAIClient.ChatCompletionTool\n | XAIBuiltInTool;\n\n/**\n * xAI-specific invocation parameters that extend the OpenAI completion params\n * with xAI's search_parameters field.\n */\nexport type ChatXAICompletionsInvocationParams = Omit<\n OpenAIClient.Chat.Completions.ChatCompletionCreateParams,\n \"messages\"\n> & {\n /**\n * Search parameters for xAI's Live Search API.\n * When present, enables the model to search the web for real-time information.\n */\n search_parameters?: XAISearchParametersPayload;\n};\n\n/**\n * xAI-specific additional kwargs that may be present on AI messages.\n * Includes xAI-specific fields like reasoning_content.\n */\nexport interface XAIAdditionalKwargs {\n /**\n * The reasoning content from xAI models that support chain-of-thought reasoning.\n * This contains the model's internal reasoning process.\n */\n reasoning_content?: string;\n /**\n * Tool calls made by the model.\n */\n tool_calls?: OpenAIClient.ChatCompletionMessageToolCall[];\n /**\n * Additional properties that may be present.\n */\n [key: string]: unknown;\n}\n\n/**\n * xAI-specific response metadata that may include usage information.\n */\nexport interface XAIResponseMetadata {\n /**\n * Token usage information.\n */\n usage?: UsageMetadata;\n /**\n * Additional metadata properties.\n */\n [key: string]: unknown;\n}\n\n/**\n * Checks if a tool is an xAI built-in tool (like live_search).\n * Built-in tools are executed server-side by the xAI API.\n *\n * @param tool - The tool to check\n * @returns true if the tool is an xAI built-in tool\n */\nexport function isXAIBuiltInTool(\n tool: ChatXAIToolType\n): tool is XAIBuiltInTool {\n return (\n typeof tool === \"object\" &&\n tool !== null &&\n \"type\" in tool &&\n typeof (tool as { type?: unknown }).type === \"string\" &&\n XAI_BUILT_IN_TOOL_TYPES.has((tool as { type: string }).type)\n );\n}\n\nexport interface ChatXAICallOptions extends BaseChatModelCallOptions {\n headers?: Record<string, string>;\n /**\n * A list of tools the model may call.\n * Can include standard function tools and xAI built-in tools like `{ type: \"live_search\" }`.\n *\n * @example\n * ```typescript\n * // Using built-in live_search tool\n * const llm = new ChatXAI().bindTools([{ type: \"live_search\" }]);\n * const result = await llm.invoke(\"What happened in tech news today?\");\n * ```\n */\n tools?: ChatXAIToolType[];\n tool_choice?: OpenAIToolChoice | string | \"auto\" | \"any\";\n /**\n * Search parameters for xAI's Live Search API.\n * Enables the model to search the web for real-time information.\n *\n * @note This is an alternative to using `tools: [{ type: \"live_search\" }]`.\n * The Live Search API parameters approach may be deprecated in favor of\n * the tool-based approach.\n *\n * @example\n * ```typescript\n * const result = await llm.invoke(\"What's the latest news?\", {\n * searchParameters: {\n * mode: \"auto\",\n * max_search_results: 5,\n * }\n * });\n * ```\n */\n searchParameters?: XAISearchParameters;\n}\n\nexport interface ChatXAIInput extends BaseChatModelParams {\n /**\n * The xAI API key to use for requests.\n * @default process.env.XAI_API_KEY\n */\n apiKey?: string;\n /**\n * The name of the model to use.\n * @default \"grok-beta\"\n */\n model?: string;\n /**\n * Up to 4 sequences where the API will stop generating further tokens. The\n * returned text will not contain the stop sequence.\n * Alias for `stopSequences`\n */\n stop?: Array<string>;\n /**\n * Up to 4 sequences where the API will stop generating further tokens. The\n * returned text will not contain the stop sequence.\n */\n stopSequences?: Array<string>;\n /**\n * Whether or not to stream responses.\n */\n streaming?: boolean;\n /**\n * The temperature to use for sampling.\n * @default 0.7\n */\n temperature?: number;\n /**\n * The maximum number of tokens that the model can process in a single response.\n * This limits ensures computational efficiency and resource management.\n */\n maxTokens?: number;\n /**\n * Default search parameters for xAI's Live Search API.\n * When set, these parameters will be applied to all requests unless\n * overridden in the call options.\n *\n * @example\n * ```typescript\n * const llm = new ChatXAI({\n * model: \"grok-beta\",\n * searchParameters: {\n * mode: \"auto\",\n * max_search_results: 5,\n * }\n * });\n * ```\n */\n searchParameters?: XAISearchParameters;\n}\n\n/**\n * xAI chat model integration.\n *\n * The xAI API is compatible to the OpenAI API with some limitations.\n *\n * Setup:\n * Install `@langchain/xai` and set an environment variable named `XAI_API_KEY`.\n *\n * ```bash\n * npm install @langchain/xai\n * export XAI_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * tool_choice: \"auto\",\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatXAI } from '@langchain/xai';\n *\n * const llm = new ChatXAI({\n * model: \"grok-beta\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"content\": \"The French translation of \\\"I love programming\\\" is \\\"J'aime programmer\\\". In this sentence, \\\"J'aime\\\" is the first person singular conjugation of the French verb \\\"aimer\\\" which means \\\"to love\\\", and \\\"programmer\\\" is the French infinitive for \\\"to program\\\". I hope this helps! Let me know if you have any other questions.\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"tokenUsage\": {\n * \"completionTokens\": 82,\n * \"promptTokens\": 20,\n * \"totalTokens\": 102\n * },\n * \"finish_reason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"invalid_tool_calls\": []\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"The\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" French\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" translation\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" of\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" \\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"I\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \" love\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ...\n * AIMessageChunk {\n * \"content\": \".\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": null\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"The French translation of \\\"I love programming\\\" is \\\"J'aime programmer\\\". In this sentence, \\\"J'aime\\\" is the first person singular conjugation of the French verb \\\"aimer\\\" which means \\\"to love\\\", and \\\"programmer\\\" is the French infinitive for \\\"to program\\\". I hope this helps! Let me know if you have any other questions.\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const llmForToolCalling = new ChatXAI({\n * model: \"grok-beta\",\n * temperature: 0,\n * // other params...\n * });\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call',\n * id: 'call_cd34'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call',\n * id: 'call_68rf'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call',\n * id: 'call_f81z'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call',\n * id: 'call_8byt'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: \"Why don't cats play poker in the wild?\",\n * punchline: 'Because there are too many cheetahs.'\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Server Tool Calling (Live Search)</strong></summary>\n *\n * xAI supports server-side tools that are executed by the API rather than\n * requiring client-side execution. The `live_search` tool enables the model\n * to search the web for real-time information.\n *\n * ```typescript\n * // Method 1: Using the built-in live_search tool\n * const llm = new ChatXAI({\n * model: \"grok-beta\",\n * temperature: 0,\n * });\n *\n * const llmWithSearch = llm.bindTools([{ type: \"live_search\" }]);\n * const result = await llmWithSearch.invoke(\"What happened in tech news today?\");\n * console.log(result.content);\n * // The model will search the web and include real-time information in its response\n * ```\n *\n * ```typescript\n * // Method 2: Using searchParameters for more control\n * const llm = new ChatXAI({\n * model: \"grok-beta\",\n * searchParameters: {\n * mode: \"auto\", // \"auto\" | \"on\" | \"off\"\n * max_search_results: 5,\n * from_date: \"2024-01-01\", // ISO date string\n * return_citations: true,\n * }\n * });\n *\n * const result = await llm.invoke(\"What are the latest AI developments?\");\n * ```\n *\n * ```typescript\n * // Method 3: Override search parameters per request\n * const result = await llm.invoke(\"Find recent news about SpaceX\", {\n * searchParameters: {\n * mode: \"on\",\n * max_search_results: 10,\n * sources: [\n * { type: \"web\", allowed_websites: [\"spacex.com\", \"nasa.gov\"] },\n * ],\n * }\n * });\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatXAI extends ChatOpenAICompletions<ChatXAICallOptions> {\n static lc_name() {\n return \"ChatXAI\";\n }\n\n _llmType() {\n return \"xai\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"XAI_API_KEY\",\n };\n }\n\n lc_serializable = true;\n\n lc_namespace = [\"langchain\", \"chat_models\", \"xai\"];\n\n /**\n * Default search parameters for the Live Search API.\n */\n searchParameters?: XAISearchParameters;\n\n constructor(fields?: Partial<ChatXAIInput>) {\n const apiKey = fields?.apiKey || getEnvironmentVariable(\"XAI_API_KEY\");\n if (!apiKey) {\n throw new Error(\n `xAI API key not found. Please set the XAI_API_KEY environment variable or provide the key into \"apiKey\" field.`\n );\n }\n\n super({\n ...fields,\n model: fields?.model || \"grok-beta\",\n apiKey,\n configuration: {\n baseURL: \"https://api.x.ai/v1\",\n },\n });\n\n this.searchParameters = fields?.searchParameters;\n }\n\n toJSON(): Serialized {\n const result = super.toJSON();\n\n if (\n \"kwargs\" in result &&\n typeof result.kwargs === \"object\" &&\n result.kwargs != null\n ) {\n delete result.kwargs.openai_api_key;\n delete result.kwargs.configuration;\n }\n\n return result;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n const params = super.getLsParams(options);\n params.ls_provider = \"xai\";\n return params;\n }\n\n /**\n * Get the effective search parameters, merging defaults with call options.\n * @param options Call options that may contain search parameters\n * @returns Merged search parameters or undefined if none are configured\n */\n protected _getEffectiveSearchParameters(\n options?: this[\"ParsedCallOptions\"]\n ): XAISearchParameters | undefined {\n return mergeSearchParams(this.searchParameters, options?.searchParameters);\n }\n\n /**\n * Check if any built-in tools (like live_search) are in the tools list.\n * @param tools List of tools to check\n * @returns true if any built-in tools are present\n */\n protected _hasBuiltInTools(tools?: ChatXAIToolType[]): boolean {\n return tools?.some(isXAIBuiltInTool) ?? false;\n }\n\n /**\n * Formats tools to xAI/OpenAI format, preserving provider-specific definitions.\n *\n * @param tools The tools to format\n * @returns The formatted tools\n */\n formatStructuredToolToXAI(\n tools: ChatXAIToolType[]\n ): (OpenAIClient.ChatCompletionTool | XAIBuiltInTool)[] | undefined {\n if (!tools || !tools.length) {\n return undefined;\n }\n return tools.map((tool) => {\n // 1. Check for provider definition first (from xaiLiveSearch factory)\n if (isLangChainTool(tool) && tool.extras?.providerToolDefinition) {\n return tool.extras.providerToolDefinition as XAIBuiltInTool;\n }\n // 2. Check for built-in tools (legacy { type: \"live_search\" })\n if (isXAIBuiltInTool(tool)) {\n return tool;\n }\n // 3. Convert standard tools to OpenAI format\n return convertToOpenAITool(tool) as OpenAIClient.ChatCompletionTool;\n });\n }\n\n override bindTools(\n tools: ChatXAIToolType[],\n kwargs?: Partial<ChatXAICallOptions>\n ): Runnable<BaseLanguageModelInput, AIMessageChunk, ChatXAICallOptions> {\n return this.withConfig({\n tools: this.formatStructuredToolToXAI(tools),\n ...kwargs,\n } as Partial<ChatXAICallOptions>);\n }\n\n /** @internal */\n override invocationParams(\n options?: this[\"ParsedCallOptions\"],\n extra?: { streaming?: boolean }\n ): ChatXAICompletionsInvocationParams {\n const baseParams = super.invocationParams(options, extra);\n\n // Cast to xAI-specific params type\n const params: ChatXAICompletionsInvocationParams = { ...baseParams };\n\n // Check if live_search tool is being used\n // We also need to extract params from the tool definition if present\n const liveSearchTool = options?.tools?.find(isXAIBuiltInTool) as\n | XAILiveSearchTool\n | undefined;\n\n const mergedSearchParams = mergeSearchParams(\n this.searchParameters,\n options?.searchParameters,\n liveSearchTool\n );\n\n // Add search_parameters if needed\n if (mergedSearchParams) {\n params.search_parameters =\n buildSearchParametersPayload(mergedSearchParams);\n }\n\n return params;\n }\n\n async completionWithRetry(\n request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;\n\n async completionWithRetry(\n request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;\n\n /**\n * Calls the xAI API with retry logic in case of failures.\n * @param request The request to send to the xAI API.\n * @param options Optional configuration for the API call.\n * @returns The response from the xAI API.\n */\n async completionWithRetry(\n request:\n | OpenAIClient.Chat.ChatCompletionCreateParamsStreaming\n | OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,\n options?: OpenAICoreRequestOptions\n ): Promise<\n | AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>\n | OpenAIClient.Chat.Completions.ChatCompletion\n > {\n delete request.frequency_penalty;\n delete request.presence_penalty;\n delete request.logit_bias;\n delete request.functions;\n\n const newRequestMessages = request.messages.map((msg) => {\n if (!msg.content) {\n return {\n ...msg,\n content: \"\",\n };\n }\n return msg;\n });\n\n let filteredTools: OpenAIClient.ChatCompletionTool[] | undefined;\n if (request.tools) {\n filteredTools = filterXAIBuiltInTools({\n tools: request.tools,\n excludedTypes: [XAI_LIVE_SEARCH_TOOL_TYPE],\n }) as OpenAIClient.ChatCompletionTool[] | undefined;\n }\n\n const newRequest = {\n ...request,\n messages: newRequestMessages,\n tools: filteredTools,\n };\n\n if (newRequest.stream === true) {\n return super.completionWithRetry(newRequest, options);\n }\n\n return super.completionWithRetry(newRequest, options);\n }\n\n protected override _convertCompletionsDeltaToBaseMessageChunk(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n delta: Record<string, any>,\n rawResponse: OpenAIClient.ChatCompletionChunk,\n defaultRole?:\n | \"function\"\n | \"user\"\n | \"system\"\n | \"developer\"\n | \"assistant\"\n | \"tool\"\n ): AIMessageChunk {\n const messageChunk = super._convertCompletionsDeltaToBaseMessageChunk(\n delta,\n rawResponse,\n defaultRole\n ) as AIMessageChunk;\n\n // Cast to xAI-specific types for proper typing\n const responseMetadata =\n messageChunk.response_metadata as XAIResponseMetadata;\n\n // Make concatenating chunks work without merge warning\n if (!rawResponse.choices[0]?.finish_reason) {\n delete responseMetadata.usage;\n delete messageChunk.usage_metadata;\n } else {\n messageChunk.usage_metadata = responseMetadata.usage;\n }\n return messageChunk;\n }\n\n protected override _convertCompletionsMessageToBaseMessage(\n message: OpenAIClient.ChatCompletionMessage & {\n reasoning_content?: string;\n },\n rawResponse: OpenAIClient.ChatCompletion\n ): AIMessageChunk {\n const langChainMessage = super._convertCompletionsMessageToBaseMessage(\n message,\n rawResponse\n ) as AIMessageChunk;\n\n // Cast additional_kwargs to xAI-specific type and add reasoning_content\n const additionalKwargs =\n langChainMessage.additional_kwargs as XAIAdditionalKwargs;\n additionalKwargs.reasoning_content = message.reasoning_content;\n\n return langChainMessage;\n }\n\n /**\n * Return profiling information for the model.\n *\n * Provides information about the model's capabilities and constraints,\n * including token limits, multimodal support, and advanced features like\n * tool calling and structured output.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n *\n * @example\n * ```typescript\n * const model = new ChatXAI({ model: \"grok-beta\" });\n * const profile = model.profile;\n * console.log(profile.maxInputTokens); // 128000\n * console.log(profile.imageInputs); // true\n * ```\n */\n get profile(): ModelProfile {\n return PROFILES[this.model] ?? {};\n }\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n override withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n const ensuredConfig = { ...config };\n if (ensuredConfig?.method === undefined) {\n ensuredConfig.method = \"functionCalling\";\n }\n return super.withStructuredOutput<RunOutput>(outputSchema, ensuredConfig);\n }\n}\n"],"mappings":"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"}
|