@langchain/xai 0.0.3 → 1.0.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.
@@ -1,443 +1,418 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatXAI = void 0;
4
- const env_1 = require("@langchain/core/utils/env");
5
- const openai_1 = require("@langchain/openai");
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
3
+ const __langchain_openai = require_rolldown_runtime.__toESM(require("@langchain/openai"));
4
+
5
+ //#region src/chat_models.ts
6
6
  /**
7
- * xAI chat model integration.
8
- *
9
- * The xAI API is compatible to the OpenAI API with some limitations.
10
- *
11
- * Setup:
12
- * Install `@langchain/xai` and set an environment variable named `XAI_API_KEY`.
13
- *
14
- * ```bash
15
- * npm install @langchain/xai
16
- * export XAI_API_KEY="your-api-key"
17
- * ```
18
- *
19
- * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)
20
- *
21
- * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)
22
- *
23
- * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
24
- * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
25
- *
26
- * ```typescript
27
- * // When calling `.withConfig`, call options should be passed via the first argument
28
- * const llmWithArgsBound = llm.withConfig({
29
- * stop: ["\n"],
30
- * tools: [...],
31
- * });
32
- *
33
- * // When calling `.bindTools`, call options should be passed via the second argument
34
- * const llmWithTools = llm.bindTools(
35
- * [...],
36
- * {
37
- * tool_choice: "auto",
38
- * }
39
- * );
40
- * ```
41
- *
42
- * ## Examples
43
- *
44
- * <details open>
45
- * <summary><strong>Instantiate</strong></summary>
46
- *
47
- * ```typescript
48
- * import { ChatXAI } from '@langchain/xai';
49
- *
50
- * const llm = new ChatXAI({
51
- * model: "grok-beta",
52
- * temperature: 0,
53
- * // other params...
54
- * });
55
- * ```
56
- * </details>
57
- *
58
- * <br />
59
- *
60
- * <details>
61
- * <summary><strong>Invoking</strong></summary>
62
- *
63
- * ```typescript
64
- * const input = `Translate "I love programming" into French.`;
65
- *
66
- * // Models also accept a list of chat messages or a formatted prompt
67
- * const result = await llm.invoke(input);
68
- * console.log(result);
69
- * ```
70
- *
71
- * ```txt
72
- * AIMessage {
73
- * "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.",
74
- * "additional_kwargs": {},
75
- * "response_metadata": {
76
- * "tokenUsage": {
77
- * "completionTokens": 82,
78
- * "promptTokens": 20,
79
- * "totalTokens": 102
80
- * },
81
- * "finish_reason": "stop"
82
- * },
83
- * "tool_calls": [],
84
- * "invalid_tool_calls": []
85
- * }
86
- * ```
87
- * </details>
88
- *
89
- * <br />
90
- *
91
- * <details>
92
- * <summary><strong>Streaming Chunks</strong></summary>
93
- *
94
- * ```typescript
95
- * for await (const chunk of await llm.stream(input)) {
96
- * console.log(chunk);
97
- * }
98
- * ```
99
- *
100
- * ```txt
101
- * AIMessageChunk {
102
- * "content": "",
103
- * "additional_kwargs": {},
104
- * "response_metadata": {
105
- * "finishReason": null
106
- * },
107
- * "tool_calls": [],
108
- * "tool_call_chunks": [],
109
- * "invalid_tool_calls": []
110
- * }
111
- * AIMessageChunk {
112
- * "content": "The",
113
- * "additional_kwargs": {},
114
- * "response_metadata": {
115
- * "finishReason": null
116
- * },
117
- * "tool_calls": [],
118
- * "tool_call_chunks": [],
119
- * "invalid_tool_calls": []
120
- * }
121
- * AIMessageChunk {
122
- * "content": " French",
123
- * "additional_kwargs": {},
124
- * "response_metadata": {
125
- * "finishReason": null
126
- * },
127
- * "tool_calls": [],
128
- * "tool_call_chunks": [],
129
- * "invalid_tool_calls": []
130
- * }
131
- * AIMessageChunk {
132
- * "content": " translation",
133
- * "additional_kwargs": {},
134
- * "response_metadata": {
135
- * "finishReason": null
136
- * },
137
- * "tool_calls": [],
138
- * "tool_call_chunks": [],
139
- * "invalid_tool_calls": []
140
- * }
141
- * AIMessageChunk {
142
- * "content": " of",
143
- * "additional_kwargs": {},
144
- * "response_metadata": {
145
- * "finishReason": null
146
- * },
147
- * "tool_calls": [],
148
- * "tool_call_chunks": [],
149
- * "invalid_tool_calls": []
150
- * }
151
- * AIMessageChunk {
152
- * "content": " \"",
153
- * "additional_kwargs": {},
154
- * "response_metadata": {
155
- * "finishReason": null
156
- * },
157
- * "tool_calls": [],
158
- * "tool_call_chunks": [],
159
- * "invalid_tool_calls": []
160
- * }
161
- * AIMessageChunk {
162
- * "content": "I",
163
- * "additional_kwargs": {},
164
- * "response_metadata": {
165
- * "finishReason": null
166
- * },
167
- * "tool_calls": [],
168
- * "tool_call_chunks": [],
169
- * "invalid_tool_calls": []
170
- * }
171
- * AIMessageChunk {
172
- * "content": " love",
173
- * "additional_kwargs": {},
174
- * "response_metadata": {
175
- * "finishReason": null
176
- * },
177
- * "tool_calls": [],
178
- * "tool_call_chunks": [],
179
- * "invalid_tool_calls": []
180
- * }
181
- * ...
182
- * AIMessageChunk {
183
- * "content": ".",
184
- * "additional_kwargs": {},
185
- * "response_metadata": {
186
- * "finishReason": null
187
- * },
188
- * "tool_calls": [],
189
- * "tool_call_chunks": [],
190
- * "invalid_tool_calls": []
191
- * }
192
- * AIMessageChunk {
193
- * "content": "",
194
- * "additional_kwargs": {},
195
- * "response_metadata": {
196
- * "finishReason": "stop"
197
- * },
198
- * "tool_calls": [],
199
- * "tool_call_chunks": [],
200
- * "invalid_tool_calls": []
201
- * }
202
- * ```
203
- * </details>
204
- *
205
- * <br />
206
- *
207
- * <details>
208
- * <summary><strong>Aggregate Streamed Chunks</strong></summary>
209
- *
210
- * ```typescript
211
- * import { AIMessageChunk } from '@langchain/core/messages';
212
- * import { concat } from '@langchain/core/utils/stream';
213
- *
214
- * const stream = await llm.stream(input);
215
- * let full: AIMessageChunk | undefined;
216
- * for await (const chunk of stream) {
217
- * full = !full ? chunk : concat(full, chunk);
218
- * }
219
- * console.log(full);
220
- * ```
221
- *
222
- * ```txt
223
- * AIMessageChunk {
224
- * "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.",
225
- * "additional_kwargs": {},
226
- * "response_metadata": {
227
- * "finishReason": "stop"
228
- * },
229
- * "tool_calls": [],
230
- * "tool_call_chunks": [],
231
- * "invalid_tool_calls": []
232
- * }
233
- * ```
234
- * </details>
235
- *
236
- * <br />
237
- *
238
- * <details>
239
- * <summary><strong>Bind tools</strong></summary>
240
- *
241
- * ```typescript
242
- * import { z } from 'zod';
243
- *
244
- * const llmForToolCalling = new ChatXAI({
245
- * model: "grok-beta",
246
- * temperature: 0,
247
- * // other params...
248
- * });
249
- *
250
- * const GetWeather = {
251
- * name: "GetWeather",
252
- * description: "Get the current weather in a given location",
253
- * schema: z.object({
254
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
255
- * }),
256
- * }
257
- *
258
- * const GetPopulation = {
259
- * name: "GetPopulation",
260
- * description: "Get the current population in a given location",
261
- * schema: z.object({
262
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
263
- * }),
264
- * }
265
- *
266
- * const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);
267
- * const aiMsg = await llmWithTools.invoke(
268
- * "Which city is hotter today and which is bigger: LA or NY?"
269
- * );
270
- * console.log(aiMsg.tool_calls);
271
- * ```
272
- *
273
- * ```txt
274
- * [
275
- * {
276
- * name: 'GetWeather',
277
- * args: { location: 'Los Angeles, CA' },
278
- * type: 'tool_call',
279
- * id: 'call_cd34'
280
- * },
281
- * {
282
- * name: 'GetWeather',
283
- * args: { location: 'New York, NY' },
284
- * type: 'tool_call',
285
- * id: 'call_68rf'
286
- * },
287
- * {
288
- * name: 'GetPopulation',
289
- * args: { location: 'Los Angeles, CA' },
290
- * type: 'tool_call',
291
- * id: 'call_f81z'
292
- * },
293
- * {
294
- * name: 'GetPopulation',
295
- * args: { location: 'New York, NY' },
296
- * type: 'tool_call',
297
- * id: 'call_8byt'
298
- * }
299
- * ]
300
- * ```
301
- * </details>
302
- *
303
- * <br />
304
- *
305
- * <details>
306
- * <summary><strong>Structured Output</strong></summary>
307
- *
308
- * ```typescript
309
- * import { z } from 'zod';
310
- *
311
- * const Joke = z.object({
312
- * setup: z.string().describe("The setup of the joke"),
313
- * punchline: z.string().describe("The punchline to the joke"),
314
- * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
315
- * }).describe('Joke to tell user.');
316
- *
317
- * const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: "Joke" });
318
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
319
- * console.log(jokeResult);
320
- * ```
321
- *
322
- * ```txt
323
- * {
324
- * setup: "Why don't cats play poker in the wild?",
325
- * punchline: 'Because there are too many cheetahs.'
326
- * }
327
- * ```
328
- * </details>
329
- *
330
- * <br />
331
- */
332
- class ChatXAI extends openai_1.ChatOpenAI {
333
- static lc_name() {
334
- return "ChatXAI";
335
- }
336
- _llmType() {
337
- return "xai";
338
- }
339
- get lc_secrets() {
340
- return {
341
- apiKey: "XAI_API_KEY",
342
- };
343
- }
344
- constructor(fields) {
345
- const apiKey = fields?.apiKey || (0, env_1.getEnvironmentVariable)("XAI_API_KEY");
346
- if (!apiKey) {
347
- throw new Error(`xAI API key not found. Please set the XAI_API_KEY environment variable or provide the key into "apiKey" field.`);
348
- }
349
- super({
350
- ...fields,
351
- model: fields?.model || "grok-beta",
352
- apiKey,
353
- configuration: {
354
- baseURL: "https://api.x.ai/v1",
355
- },
356
- });
357
- Object.defineProperty(this, "lc_serializable", {
358
- enumerable: true,
359
- configurable: true,
360
- writable: true,
361
- value: true
362
- });
363
- Object.defineProperty(this, "lc_namespace", {
364
- enumerable: true,
365
- configurable: true,
366
- writable: true,
367
- value: ["langchain", "chat_models", "xai"]
368
- });
369
- }
370
- toJSON() {
371
- const result = super.toJSON();
372
- if ("kwargs" in result &&
373
- typeof result.kwargs === "object" &&
374
- result.kwargs != null) {
375
- delete result.kwargs.openai_api_key;
376
- delete result.kwargs.configuration;
377
- }
378
- return result;
379
- }
380
- getLsParams(options) {
381
- const params = super.getLsParams(options);
382
- params.ls_provider = "xai";
383
- return params;
384
- }
385
- /**
386
- * Calls the xAI API with retry logic in case of failures.
387
- * @param request The request to send to the xAI API.
388
- * @param options Optional configuration for the API call.
389
- * @returns The response from the xAI API.
390
- */
391
- async completionWithRetry(request, options) {
392
- delete request.frequency_penalty;
393
- delete request.presence_penalty;
394
- delete request.logit_bias;
395
- delete request.functions;
396
- const newRequestMessages = request.messages.map((msg) => {
397
- if (!msg.content) {
398
- return {
399
- ...msg,
400
- content: "",
401
- };
402
- }
403
- return msg;
404
- });
405
- const newRequest = {
406
- ...request,
407
- messages: newRequestMessages,
408
- };
409
- if (newRequest.stream === true) {
410
- return super.completionWithRetry(newRequest, options);
411
- }
412
- return super.completionWithRetry(newRequest, options);
413
- }
414
- _convertOpenAIDeltaToBaseMessageChunk(
415
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
416
- delta, rawResponse, defaultRole) {
417
- const messageChunk = super._convertOpenAIDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
418
- // Make concatenating chunks work without merge warning
419
- if (!rawResponse.choices[0]?.finish_reason) {
420
- delete messageChunk.response_metadata.usage;
421
- delete messageChunk.usage_metadata;
422
- }
423
- else {
424
- messageChunk.usage_metadata = messageChunk.response_metadata.usage;
425
- }
426
- return messageChunk;
427
- }
428
- _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
429
- const langChainMessage = super._convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse);
430
- langChainMessage.additional_kwargs.reasoning_content =
431
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
432
- message.reasoning_content;
433
- return langChainMessage;
434
- }
435
- withStructuredOutput(outputSchema, config) {
436
- const ensuredConfig = { ...config };
437
- if (ensuredConfig?.method === undefined) {
438
- ensuredConfig.method = "functionCalling";
439
- }
440
- return super.withStructuredOutput(outputSchema, ensuredConfig);
441
- }
442
- }
7
+ * xAI chat model integration.
8
+ *
9
+ * The xAI API is compatible to the OpenAI API with some limitations.
10
+ *
11
+ * Setup:
12
+ * Install `@langchain/xai` and set an environment variable named `XAI_API_KEY`.
13
+ *
14
+ * ```bash
15
+ * npm install @langchain/xai
16
+ * export XAI_API_KEY="your-api-key"
17
+ * ```
18
+ *
19
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)
20
+ *
21
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)
22
+ *
23
+ * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
24
+ * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
25
+ *
26
+ * ```typescript
27
+ * // When calling `.withConfig`, call options should be passed via the first argument
28
+ * const llmWithArgsBound = llm.withConfig({
29
+ * stop: ["\n"],
30
+ * tools: [...],
31
+ * });
32
+ *
33
+ * // When calling `.bindTools`, call options should be passed via the second argument
34
+ * const llmWithTools = llm.bindTools(
35
+ * [...],
36
+ * {
37
+ * tool_choice: "auto",
38
+ * }
39
+ * );
40
+ * ```
41
+ *
42
+ * ## Examples
43
+ *
44
+ * <details open>
45
+ * <summary><strong>Instantiate</strong></summary>
46
+ *
47
+ * ```typescript
48
+ * import { ChatXAI } from '@langchain/xai';
49
+ *
50
+ * const llm = new ChatXAI({
51
+ * model: "grok-beta",
52
+ * temperature: 0,
53
+ * // other params...
54
+ * });
55
+ * ```
56
+ * </details>
57
+ *
58
+ * <br />
59
+ *
60
+ * <details>
61
+ * <summary><strong>Invoking</strong></summary>
62
+ *
63
+ * ```typescript
64
+ * const input = `Translate "I love programming" into French.`;
65
+ *
66
+ * // Models also accept a list of chat messages or a formatted prompt
67
+ * const result = await llm.invoke(input);
68
+ * console.log(result);
69
+ * ```
70
+ *
71
+ * ```txt
72
+ * AIMessage {
73
+ * "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.",
74
+ * "additional_kwargs": {},
75
+ * "response_metadata": {
76
+ * "tokenUsage": {
77
+ * "completionTokens": 82,
78
+ * "promptTokens": 20,
79
+ * "totalTokens": 102
80
+ * },
81
+ * "finish_reason": "stop"
82
+ * },
83
+ * "tool_calls": [],
84
+ * "invalid_tool_calls": []
85
+ * }
86
+ * ```
87
+ * </details>
88
+ *
89
+ * <br />
90
+ *
91
+ * <details>
92
+ * <summary><strong>Streaming Chunks</strong></summary>
93
+ *
94
+ * ```typescript
95
+ * for await (const chunk of await llm.stream(input)) {
96
+ * console.log(chunk);
97
+ * }
98
+ * ```
99
+ *
100
+ * ```txt
101
+ * AIMessageChunk {
102
+ * "content": "",
103
+ * "additional_kwargs": {},
104
+ * "response_metadata": {
105
+ * "finishReason": null
106
+ * },
107
+ * "tool_calls": [],
108
+ * "tool_call_chunks": [],
109
+ * "invalid_tool_calls": []
110
+ * }
111
+ * AIMessageChunk {
112
+ * "content": "The",
113
+ * "additional_kwargs": {},
114
+ * "response_metadata": {
115
+ * "finishReason": null
116
+ * },
117
+ * "tool_calls": [],
118
+ * "tool_call_chunks": [],
119
+ * "invalid_tool_calls": []
120
+ * }
121
+ * AIMessageChunk {
122
+ * "content": " French",
123
+ * "additional_kwargs": {},
124
+ * "response_metadata": {
125
+ * "finishReason": null
126
+ * },
127
+ * "tool_calls": [],
128
+ * "tool_call_chunks": [],
129
+ * "invalid_tool_calls": []
130
+ * }
131
+ * AIMessageChunk {
132
+ * "content": " translation",
133
+ * "additional_kwargs": {},
134
+ * "response_metadata": {
135
+ * "finishReason": null
136
+ * },
137
+ * "tool_calls": [],
138
+ * "tool_call_chunks": [],
139
+ * "invalid_tool_calls": []
140
+ * }
141
+ * AIMessageChunk {
142
+ * "content": " of",
143
+ * "additional_kwargs": {},
144
+ * "response_metadata": {
145
+ * "finishReason": null
146
+ * },
147
+ * "tool_calls": [],
148
+ * "tool_call_chunks": [],
149
+ * "invalid_tool_calls": []
150
+ * }
151
+ * AIMessageChunk {
152
+ * "content": " \"",
153
+ * "additional_kwargs": {},
154
+ * "response_metadata": {
155
+ * "finishReason": null
156
+ * },
157
+ * "tool_calls": [],
158
+ * "tool_call_chunks": [],
159
+ * "invalid_tool_calls": []
160
+ * }
161
+ * AIMessageChunk {
162
+ * "content": "I",
163
+ * "additional_kwargs": {},
164
+ * "response_metadata": {
165
+ * "finishReason": null
166
+ * },
167
+ * "tool_calls": [],
168
+ * "tool_call_chunks": [],
169
+ * "invalid_tool_calls": []
170
+ * }
171
+ * AIMessageChunk {
172
+ * "content": " love",
173
+ * "additional_kwargs": {},
174
+ * "response_metadata": {
175
+ * "finishReason": null
176
+ * },
177
+ * "tool_calls": [],
178
+ * "tool_call_chunks": [],
179
+ * "invalid_tool_calls": []
180
+ * }
181
+ * ...
182
+ * AIMessageChunk {
183
+ * "content": ".",
184
+ * "additional_kwargs": {},
185
+ * "response_metadata": {
186
+ * "finishReason": null
187
+ * },
188
+ * "tool_calls": [],
189
+ * "tool_call_chunks": [],
190
+ * "invalid_tool_calls": []
191
+ * }
192
+ * AIMessageChunk {
193
+ * "content": "",
194
+ * "additional_kwargs": {},
195
+ * "response_metadata": {
196
+ * "finishReason": "stop"
197
+ * },
198
+ * "tool_calls": [],
199
+ * "tool_call_chunks": [],
200
+ * "invalid_tool_calls": []
201
+ * }
202
+ * ```
203
+ * </details>
204
+ *
205
+ * <br />
206
+ *
207
+ * <details>
208
+ * <summary><strong>Aggregate Streamed Chunks</strong></summary>
209
+ *
210
+ * ```typescript
211
+ * import { AIMessageChunk } from '@langchain/core/messages';
212
+ * import { concat } from '@langchain/core/utils/stream';
213
+ *
214
+ * const stream = await llm.stream(input);
215
+ * let full: AIMessageChunk | undefined;
216
+ * for await (const chunk of stream) {
217
+ * full = !full ? chunk : concat(full, chunk);
218
+ * }
219
+ * console.log(full);
220
+ * ```
221
+ *
222
+ * ```txt
223
+ * AIMessageChunk {
224
+ * "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.",
225
+ * "additional_kwargs": {},
226
+ * "response_metadata": {
227
+ * "finishReason": "stop"
228
+ * },
229
+ * "tool_calls": [],
230
+ * "tool_call_chunks": [],
231
+ * "invalid_tool_calls": []
232
+ * }
233
+ * ```
234
+ * </details>
235
+ *
236
+ * <br />
237
+ *
238
+ * <details>
239
+ * <summary><strong>Bind tools</strong></summary>
240
+ *
241
+ * ```typescript
242
+ * import { z } from 'zod';
243
+ *
244
+ * const llmForToolCalling = new ChatXAI({
245
+ * model: "grok-beta",
246
+ * temperature: 0,
247
+ * // other params...
248
+ * });
249
+ *
250
+ * const GetWeather = {
251
+ * name: "GetWeather",
252
+ * description: "Get the current weather in a given location",
253
+ * schema: z.object({
254
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
255
+ * }),
256
+ * }
257
+ *
258
+ * const GetPopulation = {
259
+ * name: "GetPopulation",
260
+ * description: "Get the current population in a given location",
261
+ * schema: z.object({
262
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
263
+ * }),
264
+ * }
265
+ *
266
+ * const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);
267
+ * const aiMsg = await llmWithTools.invoke(
268
+ * "Which city is hotter today and which is bigger: LA or NY?"
269
+ * );
270
+ * console.log(aiMsg.tool_calls);
271
+ * ```
272
+ *
273
+ * ```txt
274
+ * [
275
+ * {
276
+ * name: 'GetWeather',
277
+ * args: { location: 'Los Angeles, CA' },
278
+ * type: 'tool_call',
279
+ * id: 'call_cd34'
280
+ * },
281
+ * {
282
+ * name: 'GetWeather',
283
+ * args: { location: 'New York, NY' },
284
+ * type: 'tool_call',
285
+ * id: 'call_68rf'
286
+ * },
287
+ * {
288
+ * name: 'GetPopulation',
289
+ * args: { location: 'Los Angeles, CA' },
290
+ * type: 'tool_call',
291
+ * id: 'call_f81z'
292
+ * },
293
+ * {
294
+ * name: 'GetPopulation',
295
+ * args: { location: 'New York, NY' },
296
+ * type: 'tool_call',
297
+ * id: 'call_8byt'
298
+ * }
299
+ * ]
300
+ * ```
301
+ * </details>
302
+ *
303
+ * <br />
304
+ *
305
+ * <details>
306
+ * <summary><strong>Structured Output</strong></summary>
307
+ *
308
+ * ```typescript
309
+ * import { z } from 'zod';
310
+ *
311
+ * const Joke = z.object({
312
+ * setup: z.string().describe("The setup of the joke"),
313
+ * punchline: z.string().describe("The punchline to the joke"),
314
+ * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
315
+ * }).describe('Joke to tell user.');
316
+ *
317
+ * const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: "Joke" });
318
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
319
+ * console.log(jokeResult);
320
+ * ```
321
+ *
322
+ * ```txt
323
+ * {
324
+ * setup: "Why don't cats play poker in the wild?",
325
+ * punchline: 'Because there are too many cheetahs.'
326
+ * }
327
+ * ```
328
+ * </details>
329
+ *
330
+ * <br />
331
+ */
332
+ var ChatXAI = class extends __langchain_openai.ChatOpenAICompletions {
333
+ static lc_name() {
334
+ return "ChatXAI";
335
+ }
336
+ _llmType() {
337
+ return "xai";
338
+ }
339
+ get lc_secrets() {
340
+ return { apiKey: "XAI_API_KEY" };
341
+ }
342
+ lc_serializable = true;
343
+ lc_namespace = [
344
+ "langchain",
345
+ "chat_models",
346
+ "xai"
347
+ ];
348
+ constructor(fields) {
349
+ const apiKey = fields?.apiKey || (0, __langchain_core_utils_env.getEnvironmentVariable)("XAI_API_KEY");
350
+ 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.`);
351
+ super({
352
+ ...fields,
353
+ model: fields?.model || "grok-beta",
354
+ apiKey,
355
+ configuration: { baseURL: "https://api.x.ai/v1" }
356
+ });
357
+ }
358
+ toJSON() {
359
+ const result = super.toJSON();
360
+ if ("kwargs" in result && typeof result.kwargs === "object" && result.kwargs != null) {
361
+ delete result.kwargs.openai_api_key;
362
+ delete result.kwargs.configuration;
363
+ }
364
+ return result;
365
+ }
366
+ getLsParams(options) {
367
+ const params = super.getLsParams(options);
368
+ params.ls_provider = "xai";
369
+ return params;
370
+ }
371
+ /**
372
+ * Calls the xAI API with retry logic in case of failures.
373
+ * @param request The request to send to the xAI API.
374
+ * @param options Optional configuration for the API call.
375
+ * @returns The response from the xAI API.
376
+ */
377
+ async completionWithRetry(request, options) {
378
+ delete request.frequency_penalty;
379
+ delete request.presence_penalty;
380
+ delete request.logit_bias;
381
+ delete request.functions;
382
+ const newRequestMessages = request.messages.map((msg) => {
383
+ if (!msg.content) return {
384
+ ...msg,
385
+ content: ""
386
+ };
387
+ return msg;
388
+ });
389
+ const newRequest = {
390
+ ...request,
391
+ messages: newRequestMessages
392
+ };
393
+ if (newRequest.stream === true) return super.completionWithRetry(newRequest, options);
394
+ return super.completionWithRetry(newRequest, options);
395
+ }
396
+ _convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
397
+ const messageChunk = super._convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
398
+ if (!rawResponse.choices[0]?.finish_reason) {
399
+ delete messageChunk.response_metadata.usage;
400
+ delete messageChunk.usage_metadata;
401
+ } else messageChunk.usage_metadata = messageChunk.response_metadata.usage;
402
+ return messageChunk;
403
+ }
404
+ _convertCompletionsMessageToBaseMessage(message, rawResponse) {
405
+ const langChainMessage = super._convertCompletionsMessageToBaseMessage(message, rawResponse);
406
+ langChainMessage.additional_kwargs.reasoning_content = message.reasoning_content;
407
+ return langChainMessage;
408
+ }
409
+ withStructuredOutput(outputSchema, config) {
410
+ const ensuredConfig = { ...config };
411
+ if (ensuredConfig?.method === void 0) ensuredConfig.method = "functionCalling";
412
+ return super.withStructuredOutput(outputSchema, ensuredConfig);
413
+ }
414
+ };
415
+
416
+ //#endregion
443
417
  exports.ChatXAI = ChatXAI;
418
+ //# sourceMappingURL=chat_models.cjs.map