@nocobase/plugin-ai 2.1.0-beta.25 → 2.1.0-beta.26
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/dist/client/{559.c119db3f985a695f.js → 559.39872901b9053629.js} +1 -1
- package/dist/client/index.js +3 -3
- package/dist/client/llm-providers/xai/ModelSettings.d.ts +10 -0
- package/dist/client/llm-providers/xai/index.d.ts +10 -0
- package/dist/externalVersion.js +15 -15
- package/dist/node_modules/@langchain/xai/LICENSE +21 -0
- package/dist/node_modules/@langchain/xai/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/completions.cjs +568 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/completions.d.cts +619 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/completions.d.ts +619 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/completions.js +566 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/index.cjs +2 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/index.d.ts +3 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/index.js +2 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/responses-types.d.cts +1178 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/responses-types.d.ts +1178 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/responses.cjs +233 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/responses.d.cts +70 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/responses.d.ts +70 -0
- package/dist/node_modules/@langchain/xai/dist/chat_models/responses.js +232 -0
- package/dist/node_modules/@langchain/xai/dist/converters/responses.cjs +168 -0
- package/dist/node_modules/@langchain/xai/dist/converters/responses.js +164 -0
- package/dist/node_modules/@langchain/xai/dist/index.cjs +7 -0
- package/dist/node_modules/@langchain/xai/dist/index.d.cts +5 -0
- package/dist/node_modules/@langchain/xai/dist/index.d.ts +6 -0
- package/dist/node_modules/@langchain/xai/dist/index.js +6 -0
- package/dist/node_modules/@langchain/xai/dist/live_search.cjs +54 -0
- package/dist/node_modules/@langchain/xai/dist/live_search.d.cts +145 -0
- package/dist/node_modules/@langchain/xai/dist/live_search.d.ts +145 -0
- package/dist/node_modules/@langchain/xai/dist/live_search.js +51 -0
- package/dist/node_modules/@langchain/xai/dist/profiles.cjs +289 -0
- package/dist/node_modules/@langchain/xai/dist/profiles.js +288 -0
- package/dist/node_modules/@langchain/xai/dist/tools/code_execution.cjs +52 -0
- package/dist/node_modules/@langchain/xai/dist/tools/code_execution.d.cts +64 -0
- package/dist/node_modules/@langchain/xai/dist/tools/code_execution.d.ts +64 -0
- package/dist/node_modules/@langchain/xai/dist/tools/code_execution.js +50 -0
- package/dist/node_modules/@langchain/xai/dist/tools/collections_search.cjs +60 -0
- package/dist/node_modules/@langchain/xai/dist/tools/collections_search.d.cts +90 -0
- package/dist/node_modules/@langchain/xai/dist/tools/collections_search.d.ts +90 -0
- package/dist/node_modules/@langchain/xai/dist/tools/collections_search.js +58 -0
- package/dist/node_modules/@langchain/xai/dist/tools/index.cjs +18 -0
- package/dist/node_modules/@langchain/xai/dist/tools/index.d.cts +18 -0
- package/dist/node_modules/@langchain/xai/dist/tools/index.d.ts +18 -0
- package/dist/node_modules/@langchain/xai/dist/tools/index.js +18 -0
- package/dist/node_modules/@langchain/xai/dist/tools/live_search.cjs +94 -0
- package/dist/node_modules/@langchain/xai/dist/tools/live_search.d.cts +149 -0
- package/dist/node_modules/@langchain/xai/dist/tools/live_search.d.ts +149 -0
- package/dist/node_modules/@langchain/xai/dist/tools/live_search.js +91 -0
- package/dist/node_modules/@langchain/xai/dist/tools/web_search.cjs +57 -0
- package/dist/node_modules/@langchain/xai/dist/tools/web_search.d.cts +104 -0
- package/dist/node_modules/@langchain/xai/dist/tools/web_search.d.ts +104 -0
- package/dist/node_modules/@langchain/xai/dist/tools/web_search.js +55 -0
- package/dist/node_modules/@langchain/xai/dist/tools/x_search.cjs +63 -0
- package/dist/node_modules/@langchain/xai/dist/tools/x_search.d.cts +145 -0
- package/dist/node_modules/@langchain/xai/dist/tools/x_search.d.ts +145 -0
- package/dist/node_modules/@langchain/xai/dist/tools/x_search.js +61 -0
- package/dist/node_modules/@langchain/xai/package.json +1 -0
- package/dist/node_modules/fast-glob/package.json +1 -1
- package/dist/node_modules/flexsearch/package.json +1 -1
- package/dist/node_modules/fs-extra/package.json +1 -1
- package/dist/node_modules/jsonrepair/package.json +1 -1
- package/dist/node_modules/nodejs-snowflake/package.json +1 -1
- package/dist/node_modules/openai/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/ai-employees/ai-employee.js +11 -7
- package/dist/server/llm-providers/xai.d.ts +17 -0
- package/dist/server/llm-providers/xai.js +88 -0
- package/dist/server/plugin.js +3 -0
- package/dist/server/workflow/nodes/employee/files.js +7 -4
- package/dist/server/workflow/nodes/employee/index.js +4 -3
- package/dist/server/workflow/nodes/employee/types.d.ts +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
import { buildSearchParametersPayload, filterXAIBuiltInTools, mergeSearchParams } from "../live_search.js";
|
|
2
|
+
import profiles_default from "../profiles.js";
|
|
3
|
+
import { XAI_LIVE_SEARCH_TOOL_TYPE } from "../tools/live_search.js";
|
|
4
|
+
import { convertToOpenAITool, isLangChainTool } from "@langchain/core/utils/function_calling";
|
|
5
|
+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
6
|
+
import { ChatOpenAICompletions } from "@langchain/openai";
|
|
7
|
+
|
|
8
|
+
//#region src/chat_models/completions.ts
|
|
9
|
+
/**
|
|
10
|
+
* Set of all supported xAI built-in server-side tool types.
|
|
11
|
+
* This allows us to easily extend support for future built-in tools
|
|
12
|
+
* without changing the core detection logic.
|
|
13
|
+
*/
|
|
14
|
+
const XAI_BUILT_IN_TOOL_TYPES = new Set([XAI_LIVE_SEARCH_TOOL_TYPE]);
|
|
15
|
+
/**
|
|
16
|
+
* Checks if a tool is an xAI built-in tool (like live_search).
|
|
17
|
+
* Built-in tools are executed server-side by the xAI API.
|
|
18
|
+
*
|
|
19
|
+
* @param tool - The tool to check
|
|
20
|
+
* @returns true if the tool is an xAI built-in tool
|
|
21
|
+
*/
|
|
22
|
+
function isXAIBuiltInTool(tool) {
|
|
23
|
+
return typeof tool === "object" && tool !== null && "type" in tool && typeof tool.type === "string" && XAI_BUILT_IN_TOOL_TYPES.has(tool.type);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* xAI chat model integration.
|
|
27
|
+
*
|
|
28
|
+
* The xAI API is compatible to the OpenAI API with some limitations.
|
|
29
|
+
*
|
|
30
|
+
* Setup:
|
|
31
|
+
* Install `@langchain/xai` and set an environment variable named `XAI_API_KEY`.
|
|
32
|
+
*
|
|
33
|
+
* ```bash
|
|
34
|
+
* npm install @langchain/xai
|
|
35
|
+
* export XAI_API_KEY="your-api-key"
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)
|
|
39
|
+
*
|
|
40
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)
|
|
41
|
+
*
|
|
42
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
43
|
+
* They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
44
|
+
*
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // When calling `.withConfig`, call options should be passed via the first argument
|
|
47
|
+
* const llmWithArgsBound = llm.withConfig({
|
|
48
|
+
* stop: ["\n"],
|
|
49
|
+
* tools: [...],
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
53
|
+
* const llmWithTools = llm.bindTools(
|
|
54
|
+
* [...],
|
|
55
|
+
* {
|
|
56
|
+
* tool_choice: "auto",
|
|
57
|
+
* }
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* ## Examples
|
|
62
|
+
*
|
|
63
|
+
* <details open>
|
|
64
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
65
|
+
*
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import { ChatXAI } from '@langchain/xai';
|
|
68
|
+
*
|
|
69
|
+
* const llm = new ChatXAI({
|
|
70
|
+
* model: "grok-beta",
|
|
71
|
+
* temperature: 0,
|
|
72
|
+
* // other params...
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
* </details>
|
|
76
|
+
*
|
|
77
|
+
* <br />
|
|
78
|
+
*
|
|
79
|
+
* <details>
|
|
80
|
+
* <summary><strong>Invoking</strong></summary>
|
|
81
|
+
*
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const input = `Translate "I love programming" into French.`;
|
|
84
|
+
*
|
|
85
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
86
|
+
* const result = await llm.invoke(input);
|
|
87
|
+
* console.log(result);
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* ```txt
|
|
91
|
+
* AIMessage {
|
|
92
|
+
* "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.",
|
|
93
|
+
* "additional_kwargs": {},
|
|
94
|
+
* "response_metadata": {
|
|
95
|
+
* "tokenUsage": {
|
|
96
|
+
* "completionTokens": 82,
|
|
97
|
+
* "promptTokens": 20,
|
|
98
|
+
* "totalTokens": 102
|
|
99
|
+
* },
|
|
100
|
+
* "finish_reason": "stop"
|
|
101
|
+
* },
|
|
102
|
+
* "tool_calls": [],
|
|
103
|
+
* "invalid_tool_calls": []
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
* </details>
|
|
107
|
+
*
|
|
108
|
+
* <br />
|
|
109
|
+
*
|
|
110
|
+
* <details>
|
|
111
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
112
|
+
*
|
|
113
|
+
* ```typescript
|
|
114
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
115
|
+
* console.log(chunk);
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* ```txt
|
|
120
|
+
* AIMessageChunk {
|
|
121
|
+
* "content": "",
|
|
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": "The",
|
|
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": " French",
|
|
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": " translation",
|
|
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": " of",
|
|
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": " \"",
|
|
172
|
+
* "additional_kwargs": {},
|
|
173
|
+
* "response_metadata": {
|
|
174
|
+
* "finishReason": null
|
|
175
|
+
* },
|
|
176
|
+
* "tool_calls": [],
|
|
177
|
+
* "tool_call_chunks": [],
|
|
178
|
+
* "invalid_tool_calls": []
|
|
179
|
+
* }
|
|
180
|
+
* AIMessageChunk {
|
|
181
|
+
* "content": "I",
|
|
182
|
+
* "additional_kwargs": {},
|
|
183
|
+
* "response_metadata": {
|
|
184
|
+
* "finishReason": null
|
|
185
|
+
* },
|
|
186
|
+
* "tool_calls": [],
|
|
187
|
+
* "tool_call_chunks": [],
|
|
188
|
+
* "invalid_tool_calls": []
|
|
189
|
+
* }
|
|
190
|
+
* AIMessageChunk {
|
|
191
|
+
* "content": " love",
|
|
192
|
+
* "additional_kwargs": {},
|
|
193
|
+
* "response_metadata": {
|
|
194
|
+
* "finishReason": null
|
|
195
|
+
* },
|
|
196
|
+
* "tool_calls": [],
|
|
197
|
+
* "tool_call_chunks": [],
|
|
198
|
+
* "invalid_tool_calls": []
|
|
199
|
+
* }
|
|
200
|
+
* ...
|
|
201
|
+
* AIMessageChunk {
|
|
202
|
+
* "content": ".",
|
|
203
|
+
* "additional_kwargs": {},
|
|
204
|
+
* "response_metadata": {
|
|
205
|
+
* "finishReason": null
|
|
206
|
+
* },
|
|
207
|
+
* "tool_calls": [],
|
|
208
|
+
* "tool_call_chunks": [],
|
|
209
|
+
* "invalid_tool_calls": []
|
|
210
|
+
* }
|
|
211
|
+
* AIMessageChunk {
|
|
212
|
+
* "content": "",
|
|
213
|
+
* "additional_kwargs": {},
|
|
214
|
+
* "response_metadata": {
|
|
215
|
+
* "finishReason": "stop"
|
|
216
|
+
* },
|
|
217
|
+
* "tool_calls": [],
|
|
218
|
+
* "tool_call_chunks": [],
|
|
219
|
+
* "invalid_tool_calls": []
|
|
220
|
+
* }
|
|
221
|
+
* ```
|
|
222
|
+
* </details>
|
|
223
|
+
*
|
|
224
|
+
* <br />
|
|
225
|
+
*
|
|
226
|
+
* <details>
|
|
227
|
+
* <summary><strong>Aggregate Streamed Chunks</strong></summary>
|
|
228
|
+
*
|
|
229
|
+
* ```typescript
|
|
230
|
+
* import { AIMessageChunk } from '@langchain/core/messages';
|
|
231
|
+
* import { concat } from '@langchain/core/utils/stream';
|
|
232
|
+
*
|
|
233
|
+
* const stream = await llm.stream(input);
|
|
234
|
+
* let full: AIMessageChunk | undefined;
|
|
235
|
+
* for await (const chunk of stream) {
|
|
236
|
+
* full = !full ? chunk : concat(full, chunk);
|
|
237
|
+
* }
|
|
238
|
+
* console.log(full);
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* ```txt
|
|
242
|
+
* AIMessageChunk {
|
|
243
|
+
* "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.",
|
|
244
|
+
* "additional_kwargs": {},
|
|
245
|
+
* "response_metadata": {
|
|
246
|
+
* "finishReason": "stop"
|
|
247
|
+
* },
|
|
248
|
+
* "tool_calls": [],
|
|
249
|
+
* "tool_call_chunks": [],
|
|
250
|
+
* "invalid_tool_calls": []
|
|
251
|
+
* }
|
|
252
|
+
* ```
|
|
253
|
+
* </details>
|
|
254
|
+
*
|
|
255
|
+
* <br />
|
|
256
|
+
*
|
|
257
|
+
* <details>
|
|
258
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
259
|
+
*
|
|
260
|
+
* ```typescript
|
|
261
|
+
* import { z } from 'zod';
|
|
262
|
+
*
|
|
263
|
+
* const llmForToolCalling = new ChatXAI({
|
|
264
|
+
* model: "grok-beta",
|
|
265
|
+
* temperature: 0,
|
|
266
|
+
* // other params...
|
|
267
|
+
* });
|
|
268
|
+
*
|
|
269
|
+
* const GetWeather = {
|
|
270
|
+
* name: "GetWeather",
|
|
271
|
+
* description: "Get the current weather in a given location",
|
|
272
|
+
* schema: z.object({
|
|
273
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
274
|
+
* }),
|
|
275
|
+
* }
|
|
276
|
+
*
|
|
277
|
+
* const GetPopulation = {
|
|
278
|
+
* name: "GetPopulation",
|
|
279
|
+
* description: "Get the current population in a given location",
|
|
280
|
+
* schema: z.object({
|
|
281
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
282
|
+
* }),
|
|
283
|
+
* }
|
|
284
|
+
*
|
|
285
|
+
* const llmWithTools = llmForToolCalling.bindTools([GetWeather, GetPopulation]);
|
|
286
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
287
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
288
|
+
* );
|
|
289
|
+
* console.log(aiMsg.tool_calls);
|
|
290
|
+
* ```
|
|
291
|
+
*
|
|
292
|
+
* ```txt
|
|
293
|
+
* [
|
|
294
|
+
* {
|
|
295
|
+
* name: 'GetWeather',
|
|
296
|
+
* args: { location: 'Los Angeles, CA' },
|
|
297
|
+
* type: 'tool_call',
|
|
298
|
+
* id: 'call_cd34'
|
|
299
|
+
* },
|
|
300
|
+
* {
|
|
301
|
+
* name: 'GetWeather',
|
|
302
|
+
* args: { location: 'New York, NY' },
|
|
303
|
+
* type: 'tool_call',
|
|
304
|
+
* id: 'call_68rf'
|
|
305
|
+
* },
|
|
306
|
+
* {
|
|
307
|
+
* name: 'GetPopulation',
|
|
308
|
+
* args: { location: 'Los Angeles, CA' },
|
|
309
|
+
* type: 'tool_call',
|
|
310
|
+
* id: 'call_f81z'
|
|
311
|
+
* },
|
|
312
|
+
* {
|
|
313
|
+
* name: 'GetPopulation',
|
|
314
|
+
* args: { location: 'New York, NY' },
|
|
315
|
+
* type: 'tool_call',
|
|
316
|
+
* id: 'call_8byt'
|
|
317
|
+
* }
|
|
318
|
+
* ]
|
|
319
|
+
* ```
|
|
320
|
+
* </details>
|
|
321
|
+
*
|
|
322
|
+
* <br />
|
|
323
|
+
*
|
|
324
|
+
* <details>
|
|
325
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
326
|
+
*
|
|
327
|
+
* ```typescript
|
|
328
|
+
* import { z } from 'zod';
|
|
329
|
+
*
|
|
330
|
+
* const Joke = z.object({
|
|
331
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
332
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
333
|
+
* rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
|
|
334
|
+
* }).describe('Joke to tell user.');
|
|
335
|
+
*
|
|
336
|
+
* const structuredLlm = llmForToolCalling.withStructuredOutput(Joke, { name: "Joke" });
|
|
337
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
338
|
+
* console.log(jokeResult);
|
|
339
|
+
* ```
|
|
340
|
+
*
|
|
341
|
+
* ```txt
|
|
342
|
+
* {
|
|
343
|
+
* setup: "Why don't cats play poker in the wild?",
|
|
344
|
+
* punchline: 'Because there are too many cheetahs.'
|
|
345
|
+
* }
|
|
346
|
+
* ```
|
|
347
|
+
* </details>
|
|
348
|
+
*
|
|
349
|
+
* <br />
|
|
350
|
+
*
|
|
351
|
+
* <details>
|
|
352
|
+
* <summary><strong>Server Tool Calling (Live Search)</strong></summary>
|
|
353
|
+
*
|
|
354
|
+
* xAI supports server-side tools that are executed by the API rather than
|
|
355
|
+
* requiring client-side execution. The `live_search` tool enables the model
|
|
356
|
+
* to search the web for real-time information.
|
|
357
|
+
*
|
|
358
|
+
* ```typescript
|
|
359
|
+
* // Method 1: Using the built-in live_search tool
|
|
360
|
+
* const llm = new ChatXAI({
|
|
361
|
+
* model: "grok-beta",
|
|
362
|
+
* temperature: 0,
|
|
363
|
+
* });
|
|
364
|
+
*
|
|
365
|
+
* const llmWithSearch = llm.bindTools([{ type: "live_search" }]);
|
|
366
|
+
* const result = await llmWithSearch.invoke("What happened in tech news today?");
|
|
367
|
+
* console.log(result.content);
|
|
368
|
+
* // The model will search the web and include real-time information in its response
|
|
369
|
+
* ```
|
|
370
|
+
*
|
|
371
|
+
* ```typescript
|
|
372
|
+
* // Method 2: Using searchParameters for more control
|
|
373
|
+
* const llm = new ChatXAI({
|
|
374
|
+
* model: "grok-beta",
|
|
375
|
+
* searchParameters: {
|
|
376
|
+
* mode: "auto", // "auto" | "on" | "off"
|
|
377
|
+
* max_search_results: 5,
|
|
378
|
+
* from_date: "2024-01-01", // ISO date string
|
|
379
|
+
* return_citations: true,
|
|
380
|
+
* }
|
|
381
|
+
* });
|
|
382
|
+
*
|
|
383
|
+
* const result = await llm.invoke("What are the latest AI developments?");
|
|
384
|
+
* ```
|
|
385
|
+
*
|
|
386
|
+
* ```typescript
|
|
387
|
+
* // Method 3: Override search parameters per request
|
|
388
|
+
* const result = await llm.invoke("Find recent news about SpaceX", {
|
|
389
|
+
* searchParameters: {
|
|
390
|
+
* mode: "on",
|
|
391
|
+
* max_search_results: 10,
|
|
392
|
+
* sources: [
|
|
393
|
+
* { type: "web", allowed_websites: ["spacex.com", "nasa.gov"] },
|
|
394
|
+
* ],
|
|
395
|
+
* }
|
|
396
|
+
* });
|
|
397
|
+
* ```
|
|
398
|
+
* </details>
|
|
399
|
+
*
|
|
400
|
+
* <br />
|
|
401
|
+
*/
|
|
402
|
+
var ChatXAI = class extends ChatOpenAICompletions {
|
|
403
|
+
static lc_name() {
|
|
404
|
+
return "ChatXAI";
|
|
405
|
+
}
|
|
406
|
+
_llmType() {
|
|
407
|
+
return "xai";
|
|
408
|
+
}
|
|
409
|
+
get lc_secrets() {
|
|
410
|
+
return { apiKey: "XAI_API_KEY" };
|
|
411
|
+
}
|
|
412
|
+
lc_serializable = true;
|
|
413
|
+
lc_namespace = [
|
|
414
|
+
"langchain",
|
|
415
|
+
"chat_models",
|
|
416
|
+
"xai"
|
|
417
|
+
];
|
|
418
|
+
/**
|
|
419
|
+
* Default search parameters for the Live Search API.
|
|
420
|
+
*/
|
|
421
|
+
searchParameters;
|
|
422
|
+
constructor(fields) {
|
|
423
|
+
const apiKey = fields?.apiKey || getEnvironmentVariable("XAI_API_KEY");
|
|
424
|
+
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.`);
|
|
425
|
+
super({
|
|
426
|
+
...fields,
|
|
427
|
+
model: fields?.model || "grok-beta",
|
|
428
|
+
apiKey,
|
|
429
|
+
configuration: { baseURL: fields?.baseURL ?? "https://api.x.ai/v1" }
|
|
430
|
+
});
|
|
431
|
+
this.searchParameters = fields?.searchParameters;
|
|
432
|
+
}
|
|
433
|
+
toJSON() {
|
|
434
|
+
const result = super.toJSON();
|
|
435
|
+
if ("kwargs" in result && typeof result.kwargs === "object" && result.kwargs != null) {
|
|
436
|
+
delete result.kwargs.openai_api_key;
|
|
437
|
+
delete result.kwargs.configuration;
|
|
438
|
+
}
|
|
439
|
+
return result;
|
|
440
|
+
}
|
|
441
|
+
getLsParams(options) {
|
|
442
|
+
const params = super.getLsParams(options);
|
|
443
|
+
params.ls_provider = "xai";
|
|
444
|
+
return params;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Get the effective search parameters, merging defaults with call options.
|
|
448
|
+
* @param options Call options that may contain search parameters
|
|
449
|
+
* @returns Merged search parameters or undefined if none are configured
|
|
450
|
+
*/
|
|
451
|
+
_getEffectiveSearchParameters(options) {
|
|
452
|
+
return mergeSearchParams(this.searchParameters, options?.searchParameters);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Check if any built-in tools (like live_search) are in the tools list.
|
|
456
|
+
* @param tools List of tools to check
|
|
457
|
+
* @returns true if any built-in tools are present
|
|
458
|
+
*/
|
|
459
|
+
_hasBuiltInTools(tools) {
|
|
460
|
+
return tools?.some(isXAIBuiltInTool) ?? false;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Formats tools to xAI/OpenAI format, preserving provider-specific definitions.
|
|
464
|
+
*
|
|
465
|
+
* @param tools The tools to format
|
|
466
|
+
* @returns The formatted tools
|
|
467
|
+
*/
|
|
468
|
+
formatStructuredToolToXAI(tools) {
|
|
469
|
+
if (!tools || !tools.length) return void 0;
|
|
470
|
+
return tools.map((tool) => {
|
|
471
|
+
if (isLangChainTool(tool) && tool.extras?.providerToolDefinition) return tool.extras.providerToolDefinition;
|
|
472
|
+
if (isXAIBuiltInTool(tool)) return tool;
|
|
473
|
+
return convertToOpenAITool(tool);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
bindTools(tools, kwargs) {
|
|
477
|
+
return this.withConfig({
|
|
478
|
+
tools: this.formatStructuredToolToXAI(tools),
|
|
479
|
+
...kwargs
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
/** @internal */
|
|
483
|
+
invocationParams(options, extra) {
|
|
484
|
+
const baseParams = super.invocationParams(options, extra);
|
|
485
|
+
const params = { ...baseParams };
|
|
486
|
+
const liveSearchTool = options?.tools?.find(isXAIBuiltInTool);
|
|
487
|
+
const mergedSearchParams = mergeSearchParams(this.searchParameters, options?.searchParameters, liveSearchTool);
|
|
488
|
+
if (mergedSearchParams) params.search_parameters = buildSearchParametersPayload(mergedSearchParams);
|
|
489
|
+
return params;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Calls the xAI API with retry logic in case of failures.
|
|
493
|
+
* @param request The request to send to the xAI API.
|
|
494
|
+
* @param options Optional configuration for the API call.
|
|
495
|
+
* @returns The response from the xAI API.
|
|
496
|
+
*/
|
|
497
|
+
async completionWithRetry(request, options) {
|
|
498
|
+
delete request.frequency_penalty;
|
|
499
|
+
delete request.presence_penalty;
|
|
500
|
+
delete request.logit_bias;
|
|
501
|
+
delete request.functions;
|
|
502
|
+
const newRequestMessages = request.messages.map((msg) => {
|
|
503
|
+
if (!msg.content) return {
|
|
504
|
+
...msg,
|
|
505
|
+
content: ""
|
|
506
|
+
};
|
|
507
|
+
return msg;
|
|
508
|
+
});
|
|
509
|
+
let filteredTools;
|
|
510
|
+
if (request.tools) filteredTools = filterXAIBuiltInTools({
|
|
511
|
+
tools: request.tools,
|
|
512
|
+
excludedTypes: [XAI_LIVE_SEARCH_TOOL_TYPE]
|
|
513
|
+
});
|
|
514
|
+
const newRequest = {
|
|
515
|
+
...request,
|
|
516
|
+
messages: newRequestMessages,
|
|
517
|
+
tools: filteredTools
|
|
518
|
+
};
|
|
519
|
+
if (newRequest.stream === true) return super.completionWithRetry(newRequest, options);
|
|
520
|
+
return super.completionWithRetry(newRequest, options);
|
|
521
|
+
}
|
|
522
|
+
_convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
|
|
523
|
+
const messageChunk = super._convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
|
|
524
|
+
const responseMetadata = messageChunk.response_metadata;
|
|
525
|
+
if (!rawResponse.choices[0]?.finish_reason) {
|
|
526
|
+
delete responseMetadata.usage;
|
|
527
|
+
delete messageChunk.usage_metadata;
|
|
528
|
+
} else messageChunk.usage_metadata = responseMetadata.usage;
|
|
529
|
+
return messageChunk;
|
|
530
|
+
}
|
|
531
|
+
_convertCompletionsMessageToBaseMessage(message, rawResponse) {
|
|
532
|
+
const langChainMessage = super._convertCompletionsMessageToBaseMessage(message, rawResponse);
|
|
533
|
+
const additionalKwargs = langChainMessage.additional_kwargs;
|
|
534
|
+
additionalKwargs.reasoning_content = message.reasoning_content;
|
|
535
|
+
return langChainMessage;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Return profiling information for the model.
|
|
539
|
+
*
|
|
540
|
+
* Provides information about the model's capabilities and constraints,
|
|
541
|
+
* including token limits, multimodal support, and advanced features like
|
|
542
|
+
* tool calling and structured output.
|
|
543
|
+
*
|
|
544
|
+
* @returns {ModelProfile} An object describing the model's capabilities and constraints
|
|
545
|
+
*
|
|
546
|
+
* @example
|
|
547
|
+
* ```typescript
|
|
548
|
+
* const model = new ChatXAI({ model: "grok-beta" });
|
|
549
|
+
* const profile = model.profile;
|
|
550
|
+
* console.log(profile.maxInputTokens); // 128000
|
|
551
|
+
* console.log(profile.imageInputs); // true
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
get profile() {
|
|
555
|
+
return profiles_default[this.model] ?? {};
|
|
556
|
+
}
|
|
557
|
+
withStructuredOutput(outputSchema, config) {
|
|
558
|
+
const ensuredConfig = { ...config };
|
|
559
|
+
if (ensuredConfig?.method === void 0) ensuredConfig.method = "functionCalling";
|
|
560
|
+
return super.withStructuredOutput(outputSchema, ensuredConfig);
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
//#endregion
|
|
565
|
+
export { ChatXAI, isXAIBuiltInTool };
|
|
566
|
+
//# sourceMappingURL=completions.js.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ChatXAI, ChatXAICallOptions, ChatXAICompletionsInvocationParams, ChatXAIInput, OpenAIToolChoice, XAIAdditionalKwargs, XAIBuiltInTool, XAIResponseMetadata, isXAIBuiltInTool } from "./completions.js";
|
|
2
|
+
import { ChatXAIResponsesCallOptions, ChatXAIResponsesInput, ChatXAIResponsesInvocationParams, XAIResponse, XAIResponsesAnnotation, XAIResponsesCodeInterpreterResult, XAIResponsesCodeInterpreterTool, XAIResponsesCodeInterpreterToolCall, XAIResponsesCreateParams, XAIResponsesCreateParamsNonStreaming, XAIResponsesCreateParamsStreaming, XAIResponsesDebugOutput, XAIResponsesFileCitationAnnotation, XAIResponsesFileSearchResult, XAIResponsesFileSearchTool, XAIResponsesFunctionTool, XAIResponsesFunctionToolCall, XAIResponsesFunctionToolFunction, XAIResponsesFunctionToolOutput, XAIResponsesInclude, XAIResponsesIncompleteDetails, XAIResponsesIncompleteReason, XAIResponsesInput, XAIResponsesInputContentItem, XAIResponsesInputFileItem, XAIResponsesInputImageItem, XAIResponsesInputItem, XAIResponsesInputTextItem, XAIResponsesLogprobContent, XAIResponsesLogprobToken, XAIResponsesLogprobs, XAIResponsesMcpTool, XAIResponsesMcpToolCall, XAIResponsesMessage, XAIResponsesMessageRole, XAIResponsesObjectType, XAIResponsesOutputCodeInterpreterCall, XAIResponsesOutputContent, XAIResponsesOutputFileSearchCall, XAIResponsesOutputFunctionCall, XAIResponsesOutputItem, XAIResponsesOutputMcpCall, XAIResponsesOutputReasoning, XAIResponsesOutputRefusalContent, XAIResponsesOutputTextContent, XAIResponsesOutputTextItem, XAIResponsesOutputWebSearchCall, XAIResponsesPreviousResponse, XAIResponsesReasoning, XAIResponsesReasoningEffort, XAIResponsesReasoningResponse, XAIResponsesReasoningSummary, XAIResponsesReasoningSummaryItem, XAIResponsesSearchMode, XAIResponsesSearchParameters, XAIResponsesSearchSource, XAIResponsesSearchSourceRss, XAIResponsesSearchSourceWeb, XAIResponsesSearchSourceX, XAIResponsesStatus, XAIResponsesStreamEvent, XAIResponsesStreamEventBase, XAIResponsesStreamEventCompleted, XAIResponsesStreamEventContentPartAdded, XAIResponsesStreamEventContentPartDone, XAIResponsesStreamEventCreated, XAIResponsesStreamEventError, XAIResponsesStreamEventFailed, XAIResponsesStreamEventFunctionCallArgumentsDelta, XAIResponsesStreamEventFunctionCallArgumentsDone, XAIResponsesStreamEventInProgress, XAIResponsesStreamEventIncomplete, XAIResponsesStreamEventOutputItemAdded, XAIResponsesStreamEventOutputItemDone, XAIResponsesStreamEventReasoningSummaryTextDelta, XAIResponsesStreamEventReasoningSummaryTextDone, XAIResponsesStreamEventTextDelta, XAIResponsesStreamEventTextDone, XAIResponsesText, XAIResponsesTextFormat, XAIResponsesTextFormatJsonObject, XAIResponsesTextFormatJsonSchema, XAIResponsesTextFormatText, XAIResponsesTool, XAIResponsesToolCall, XAIResponsesToolChoice, XAIResponsesToolChoiceFunction, XAIResponsesToolChoiceString, XAIResponsesToolOutput, XAIResponsesUrlCitationAnnotation, XAIResponsesUsage, XAIResponsesWebSearchTool, XAIResponsesWebSearchToolCall, XAIResponsesXSearchTool } from "./responses-types.js";
|
|
3
|
+
import { ChatXAIResponses } from "./responses.js";
|