@langchain/xai 0.0.1
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/LICENSE +21 -0
- package/README.md +76 -0
- package/dist/chat_models.cjs +415 -0
- package/dist/chat_models.d.ts +387 -0
- package/dist/chat_models.js +411 -0
- package/dist/index.cjs +17 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/index.cjs +1 -0
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +94 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 LangChain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @langchain/xai
|
|
2
|
+
|
|
3
|
+
This package contains the LangChain.js integrations for xAI.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash npm2yarn
|
|
8
|
+
npm install @langchain/xai @langchain/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Chat models
|
|
12
|
+
|
|
13
|
+
This package adds support for xAI chat model inference.
|
|
14
|
+
|
|
15
|
+
Set the necessary environment variable (or pass it in via the constructor):
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
export XAI_API_KEY=
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { ChatXAI } from "@langchain/xai";
|
|
23
|
+
import { HumanMessage } from "@langchain/core/messages";
|
|
24
|
+
|
|
25
|
+
const model = new ChatXAI({
|
|
26
|
+
apiKey: process.env.XAI_API_KEY, // Default value.
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const message = new HumanMessage("What color is the sky?");
|
|
30
|
+
|
|
31
|
+
const res = await model.invoke([message]);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Development
|
|
35
|
+
|
|
36
|
+
To develop the `@langchain/xai` package, you'll need to follow these instructions:
|
|
37
|
+
|
|
38
|
+
### Install dependencies
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
yarn install
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Build the package
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
yarn build
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or from the repo root:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
yarn build --filter=@langchain/xai
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Run tests
|
|
57
|
+
|
|
58
|
+
Test files should live within a `tests/` file in the `src/` folder. Unit tests should end in `.test.ts` and integration tests should
|
|
59
|
+
end in `.int.test.ts`:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
$ yarn test
|
|
63
|
+
$ yarn test:int
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Lint & Format
|
|
67
|
+
|
|
68
|
+
Run the linter & formatter to ensure your code is up to standard:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
yarn lint && yarn format
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Adding new entrypoints
|
|
75
|
+
|
|
76
|
+
If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to the `entrypoints` field in the `config` variable located inside `langchain.config.js` and run `yarn build` to generate the new entrypoint.
|
|
@@ -0,0 +1,415 @@
|
|
|
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");
|
|
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 `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
25
|
+
*
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // When calling `.bind`, call options should be passed via the first argument
|
|
28
|
+
* const llmWithArgsBound = llm.bind({
|
|
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
|
+
}
|
|
415
|
+
exports.ChatXAI = ChatXAI;
|