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