@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.
- package/CHANGELOG.md +5 -0
- package/LICENSE +6 -6
- package/README.md +7 -7
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/chat_models.cjs +416 -441
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +421 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +87 -68
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +415 -437
- package/dist/chat_models.js.map +1 -0
- package/dist/index.cjs +3 -17
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/package.json +44 -58
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/chat_models.js
CHANGED
|
@@ -1,439 +1,417 @@
|
|
|
1
1
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
2
|
-
import { ChatOpenAICompletions
|
|
2
|
+
import { ChatOpenAICompletions } from "@langchain/openai";
|
|
3
|
+
|
|
4
|
+
//#region src/chat_models.ts
|
|
3
5
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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
|