modelfusion 0.71.0 → 0.72.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/README.md +57 -45
- package/core/FunctionEvent.d.ts +5 -5
- package/guard/guard.cjs +5 -4
- package/guard/guard.d.ts +1 -1
- package/guard/guard.js +5 -4
- package/model-function/ModelCallEvent.d.ts +2 -2
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +2 -2
- package/model-function/generate-structure/StructureParseError.cjs +10 -0
- package/model-function/generate-structure/StructureParseError.d.ts +8 -0
- package/model-function/generate-structure/StructureParseError.js +10 -0
- package/model-function/generate-structure/index.cjs +0 -3
- package/model-function/generate-structure/index.d.ts +0 -3
- package/model-function/generate-structure/index.js +0 -3
- package/model-function/index.cjs +0 -1
- package/model-function/index.d.ts +0 -1
- package/model-function/index.js +0 -1
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +16 -25
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +10 -28
- package/model-provider/openai/chat/OpenAIChatMessage.js +16 -25
- package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -48
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +15 -107
- package/model-provider/openai/chat/OpenAIChatModel.js +2 -48
- package/package.json +1 -1
- package/tool/Tool.cjs +5 -16
- package/tool/Tool.d.ts +9 -14
- package/tool/Tool.js +5 -16
- package/tool/ToolCall.d.ts +15 -0
- package/tool/ToolCallArgumentsValidationError.cjs +49 -0
- package/tool/ToolCallArgumentsValidationError.d.ts +23 -0
- package/tool/ToolCallArgumentsValidationError.js +45 -0
- package/tool/ToolCallError.cjs +34 -0
- package/tool/ToolCallError.d.ts +17 -0
- package/tool/ToolCallError.js +30 -0
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.cjs +4 -4
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.d.ts +1 -1
- package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.js +2 -2
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.cjs → tool/ToolCallParseError.cjs} +13 -13
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.d.ts → tool/ToolCallParseError.d.ts} +6 -6
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.js → tool/ToolCallParseError.js} +11 -11
- package/tool/ToolCallResult.d.ts +13 -0
- package/{model-function/generate-tool-call → tool}/ToolDefinition.d.ts +2 -2
- package/tool/ToolExecutionError.cjs +5 -4
- package/tool/ToolExecutionError.d.ts +4 -4
- package/tool/ToolExecutionError.js +5 -4
- package/tool/WebSearchTool.cjs +4 -4
- package/tool/WebSearchTool.d.ts +2 -2
- package/tool/WebSearchTool.js +4 -4
- package/tool/{ExecuteToolEvent.d.ts → execute-tool/ExecuteToolEvent.d.ts} +1 -1
- package/tool/{executeTool.cjs → execute-tool/executeTool.cjs} +15 -15
- package/tool/{executeTool.d.ts → execute-tool/executeTool.d.ts} +6 -6
- package/tool/{executeTool.js → execute-tool/executeTool.js} +15 -15
- package/tool/execute-tool/index.cjs +18 -0
- package/tool/execute-tool/index.d.ts +2 -0
- package/tool/execute-tool/index.js +2 -0
- package/tool/execute-tool/safeExecuteToolCall.cjs +34 -0
- package/tool/execute-tool/safeExecuteToolCall.d.ts +5 -0
- package/tool/execute-tool/safeExecuteToolCall.js +30 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +59 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +35 -0
- package/tool/generate-tool-call/TextGenerationToolCallModel.js +55 -0
- package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.d.ts +1 -1
- package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.d.ts +3 -3
- package/{model-function → tool}/generate-tool-call/generateToolCall.cjs +11 -11
- package/{model-function → tool}/generate-tool-call/generateToolCall.d.ts +3 -3
- package/{model-function → tool}/generate-tool-call/generateToolCall.js +11 -11
- package/tool/generate-tool-call/index.cjs +20 -0
- package/tool/generate-tool-call/index.d.ts +4 -0
- package/tool/generate-tool-call/index.js +4 -0
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.d.ts +1 -1
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.d.ts +3 -3
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.cjs +8 -8
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.d.ts +3 -3
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/generateToolCallsOrText.js +8 -8
- package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/index.cjs +0 -7
- package/tool/generate-tool-calls-or-text/index.d.ts +3 -0
- package/tool/generate-tool-calls-or-text/index.js +3 -0
- package/tool/index.cjs +12 -7
- package/tool/index.d.ts +12 -7
- package/tool/index.js +12 -7
- package/tool/{UseToolEvent.d.ts → use-tool/UseToolEvent.d.ts} +1 -1
- package/tool/use-tool/index.cjs +18 -0
- package/tool/use-tool/index.d.ts +2 -0
- package/tool/use-tool/index.js +2 -0
- package/tool/use-tool/useTool.cjs +30 -0
- package/tool/use-tool/useTool.d.ts +16 -0
- package/tool/use-tool/useTool.js +26 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +7 -0
- package/tool/use-tools-or-generate-text/index.cjs +18 -0
- package/tool/use-tools-or-generate-text/index.d.ts +2 -0
- package/tool/use-tools-or-generate-text/index.js +2 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.cjs +48 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.d.ts +18 -0
- package/tool/use-tools-or-generate-text/useToolsOrGenerateText.js +44 -0
- package/util/getErrorMessage.cjs +3 -0
- package/util/getErrorMessage.d.ts +1 -1
- package/util/getErrorMessage.js +3 -0
- package/model-function/generate-structure/NoSuchStructureError.cjs +0 -17
- package/model-function/generate-structure/NoSuchStructureError.d.ts +0 -4
- package/model-function/generate-structure/NoSuchStructureError.js +0 -13
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +0 -26
- package/model-function/generate-structure/generateStructureOrText.cjs +0 -54
- package/model-function/generate-structure/generateStructureOrText.d.ts +0 -97
- package/model-function/generate-structure/generateStructureOrText.js +0 -50
- package/model-function/generate-tool-call/ToolCall.d.ts +0 -5
- package/model-function/generate-tool-call/index.d.ts +0 -10
- package/model-function/generate-tool-call/index.js +0 -10
- package/tool/NoSuchToolError.cjs +0 -25
- package/tool/NoSuchToolError.d.ts +0 -10
- package/tool/NoSuchToolError.js +0 -21
- package/tool/UseToolOrGenerateTextEvent.d.ts +0 -7
- package/tool/useTool.cjs +0 -40
- package/tool/useTool.d.ts +0 -15
- package/tool/useTool.js +0 -36
- package/tool/useToolOrGenerateText.cjs +0 -50
- package/tool/useToolOrGenerateText.d.ts +0 -23
- package/tool/useToolOrGenerateText.js +0 -46
- /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.cjs +0 -0
- /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.d.ts +0 -0
- /package/{model-function/generate-tool-call → tool}/NoSuchToolDefinitionError.js +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolCall.cjs +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolCall.js +0 -0
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.cjs → tool/ToolCallResult.cjs} +0 -0
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.js → tool/ToolCallResult.js} +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolDefinition.cjs +0 -0
- /package/{model-function/generate-tool-call → tool}/ToolDefinition.js +0 -0
- /package/tool/{ExecuteToolEvent.cjs → execute-tool/ExecuteToolEvent.cjs} +0 -0
- /package/tool/{ExecuteToolEvent.js → execute-tool/ExecuteToolEvent.js} +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.cjs +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.js +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.cjs +0 -0
- /package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.js +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.cjs +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationEvent.js +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.cjs +0 -0
- /package/{model-function/generate-tool-call → tool/generate-tool-calls-or-text}/ToolCallsOrTextGenerationModel.js +0 -0
- /package/tool/{UseToolEvent.cjs → use-tool/UseToolEvent.cjs} +0 -0
- /package/tool/{UseToolEvent.js → use-tool/UseToolEvent.js} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.cjs → use-tools-or-generate-text/UseToolsOrGenerateTextEvent.cjs} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.js → use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js} +0 -0
@@ -315,52 +315,6 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
315
315
|
],
|
316
316
|
});
|
317
317
|
}
|
318
|
-
async doGenerateStructureOrText(structureDefinitions, prompt, options) {
|
319
|
-
const response = await this.callAPI(prompt, {
|
320
|
-
...options,
|
321
|
-
responseFormat: exports.OpenAIChatResponseFormat.json,
|
322
|
-
functionCall: "auto",
|
323
|
-
functions: structureDefinitions.map((structureDefinition) => ({
|
324
|
-
name: structureDefinition.name,
|
325
|
-
description: structureDefinition.description,
|
326
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
327
|
-
})),
|
328
|
-
});
|
329
|
-
const message = response.choices[0].message;
|
330
|
-
const content = message.content;
|
331
|
-
const functionCall = message.function_call;
|
332
|
-
if (functionCall == null) {
|
333
|
-
return {
|
334
|
-
response,
|
335
|
-
structureAndText: {
|
336
|
-
structure: null,
|
337
|
-
value: null,
|
338
|
-
valueText: null,
|
339
|
-
text: content ?? "",
|
340
|
-
},
|
341
|
-
usage: this.extractUsage(response),
|
342
|
-
};
|
343
|
-
}
|
344
|
-
try {
|
345
|
-
return {
|
346
|
-
response,
|
347
|
-
structureAndText: {
|
348
|
-
structure: functionCall.name,
|
349
|
-
value: secure_json_parse_1.default.parse(functionCall.arguments),
|
350
|
-
valueText: functionCall.arguments,
|
351
|
-
text: content,
|
352
|
-
},
|
353
|
-
usage: this.extractUsage(response),
|
354
|
-
};
|
355
|
-
}
|
356
|
-
catch (error) {
|
357
|
-
throw new StructureParseError_js_1.StructureParseError({
|
358
|
-
structureName: functionCall.name,
|
359
|
-
valueText: functionCall.arguments,
|
360
|
-
cause: error,
|
361
|
-
});
|
362
|
-
}
|
363
|
-
}
|
364
318
|
async doGenerateToolCall(tool, prompt, options) {
|
365
319
|
const response = await this.callAPI(prompt, {
|
366
320
|
...options,
|
@@ -387,7 +341,7 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
387
341
|
? null
|
388
342
|
: {
|
389
343
|
id: toolCalls[0].id,
|
390
|
-
|
344
|
+
args: (0, parseJSON_js_1.parseJSON)({ text: toolCalls[0].function.arguments }),
|
391
345
|
},
|
392
346
|
usage: this.extractUsage(response),
|
393
347
|
};
|
@@ -413,7 +367,7 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
413
367
|
toolCalls: message.tool_calls?.map((toolCall) => ({
|
414
368
|
id: toolCall.id,
|
415
369
|
name: toolCall.function.name,
|
416
|
-
|
370
|
+
args: (0, parseJSON_js_1.parseJSON)({ text: toolCall.function.arguments }),
|
417
371
|
})) ?? null,
|
418
372
|
usage: this.extractUsage(response),
|
419
373
|
};
|
@@ -6,13 +6,12 @@ import { StructureDefinition } from "../../../core/schema/StructureDefinition.js
|
|
6
6
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
7
7
|
import { Delta } from "../../../model-function/Delta.js";
|
8
8
|
import { StructureGenerationModel } from "../../../model-function/generate-structure/StructureGenerationModel.js";
|
9
|
-
import { StructureOrTextGenerationModel } from "../../../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
10
9
|
import { PromptFormatTextStreamingModel } from "../../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
11
10
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../../model-function/generate-text/TextGenerationModel.js";
|
12
11
|
import { TextGenerationPromptFormat } from "../../../model-function/generate-text/TextGenerationPromptFormat.js";
|
13
|
-
import {
|
14
|
-
import {
|
15
|
-
import {
|
12
|
+
import { ToolDefinition } from "../../../tool/ToolDefinition.js";
|
13
|
+
import { ToolCallGenerationModel } from "../../../tool/generate-tool-call/ToolCallGenerationModel.js";
|
14
|
+
import { ToolCallsOrTextGenerationModel } from "../../../tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js";
|
16
15
|
import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
|
17
16
|
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
18
17
|
export declare const OPENAI_CHAT_MODELS: {
|
@@ -169,7 +168,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, Omit<Op
|
|
169
168
|
* ),
|
170
169
|
* ]);
|
171
170
|
*/
|
172
|
-
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextStreamingModel<OpenAIChatMessage[], OpenAIChatSettings>, StructureGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>,
|
171
|
+
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextStreamingModel<OpenAIChatMessage[], OpenAIChatSettings>, StructureGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>, ToolCallGenerationModel<OpenAIChatMessage[], OpenAIChatSettings>, ToolCallsOrTextGenerationModel<OpenAIChatMessage[], OpenAIChatSettings> {
|
173
172
|
constructor(settings: OpenAIChatSettings);
|
174
173
|
readonly provider: "openai";
|
175
174
|
get modelName(): OpenAIChatModelType;
|
@@ -218,7 +217,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
218
217
|
};
|
219
218
|
index: number;
|
220
219
|
logprobs?: any;
|
221
|
-
finish_reason?: "length" | "stop" | "
|
220
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
222
221
|
}[];
|
223
222
|
created: number;
|
224
223
|
system_fingerprint?: string | undefined;
|
@@ -267,7 +266,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
267
266
|
};
|
268
267
|
index: number;
|
269
268
|
logprobs?: any;
|
270
|
-
finish_reason?: "length" | "stop" | "
|
269
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
271
270
|
}[];
|
272
271
|
created: number;
|
273
272
|
system_fingerprint?: string | undefined;
|
@@ -281,97 +280,6 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
281
280
|
};
|
282
281
|
}>;
|
283
282
|
doStreamStructure(structureDefinition: StructureDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<AsyncIterable<Delta<unknown>>>;
|
284
|
-
doGenerateStructureOrText(structureDefinitions: Array<StructureDefinition<string, unknown>>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
285
|
-
response: {
|
286
|
-
object: "chat.completion";
|
287
|
-
usage: {
|
288
|
-
prompt_tokens: number;
|
289
|
-
total_tokens: number;
|
290
|
-
completion_tokens: number;
|
291
|
-
};
|
292
|
-
model: string;
|
293
|
-
id: string;
|
294
|
-
choices: {
|
295
|
-
message: {
|
296
|
-
role: "assistant";
|
297
|
-
content: string | null;
|
298
|
-
function_call?: {
|
299
|
-
name: string;
|
300
|
-
arguments: string;
|
301
|
-
} | undefined;
|
302
|
-
tool_calls?: {
|
303
|
-
function: {
|
304
|
-
name: string;
|
305
|
-
arguments: string;
|
306
|
-
};
|
307
|
-
type: "function";
|
308
|
-
id: string;
|
309
|
-
}[] | undefined;
|
310
|
-
};
|
311
|
-
index: number;
|
312
|
-
logprobs?: any;
|
313
|
-
finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
|
314
|
-
}[];
|
315
|
-
created: number;
|
316
|
-
system_fingerprint?: string | undefined;
|
317
|
-
};
|
318
|
-
structureAndText: {
|
319
|
-
structure: null;
|
320
|
-
value: null;
|
321
|
-
valueText: null;
|
322
|
-
text: string;
|
323
|
-
};
|
324
|
-
usage: {
|
325
|
-
promptTokens: number;
|
326
|
-
completionTokens: number;
|
327
|
-
totalTokens: number;
|
328
|
-
};
|
329
|
-
} | {
|
330
|
-
response: {
|
331
|
-
object: "chat.completion";
|
332
|
-
usage: {
|
333
|
-
prompt_tokens: number;
|
334
|
-
total_tokens: number;
|
335
|
-
completion_tokens: number;
|
336
|
-
};
|
337
|
-
model: string;
|
338
|
-
id: string;
|
339
|
-
choices: {
|
340
|
-
message: {
|
341
|
-
role: "assistant";
|
342
|
-
content: string | null;
|
343
|
-
function_call?: {
|
344
|
-
name: string;
|
345
|
-
arguments: string;
|
346
|
-
} | undefined;
|
347
|
-
tool_calls?: {
|
348
|
-
function: {
|
349
|
-
name: string;
|
350
|
-
arguments: string;
|
351
|
-
};
|
352
|
-
type: "function";
|
353
|
-
id: string;
|
354
|
-
}[] | undefined;
|
355
|
-
};
|
356
|
-
index: number;
|
357
|
-
logprobs?: any;
|
358
|
-
finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
|
359
|
-
}[];
|
360
|
-
created: number;
|
361
|
-
system_fingerprint?: string | undefined;
|
362
|
-
};
|
363
|
-
structureAndText: {
|
364
|
-
structure: string;
|
365
|
-
value: any;
|
366
|
-
valueText: string;
|
367
|
-
text: string | null;
|
368
|
-
};
|
369
|
-
usage: {
|
370
|
-
promptTokens: number;
|
371
|
-
completionTokens: number;
|
372
|
-
totalTokens: number;
|
373
|
-
};
|
374
|
-
}>;
|
375
283
|
doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
376
284
|
response: {
|
377
285
|
object: "chat.completion";
|
@@ -401,14 +309,14 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
401
309
|
};
|
402
310
|
index: number;
|
403
311
|
logprobs?: any;
|
404
|
-
finish_reason?: "length" | "stop" | "
|
312
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
405
313
|
}[];
|
406
314
|
created: number;
|
407
315
|
system_fingerprint?: string | undefined;
|
408
316
|
};
|
409
317
|
toolCall: {
|
410
318
|
id: string;
|
411
|
-
|
319
|
+
args: unknown;
|
412
320
|
} | null;
|
413
321
|
usage: {
|
414
322
|
promptTokens: number;
|
@@ -445,7 +353,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
445
353
|
};
|
446
354
|
index: number;
|
447
355
|
logprobs?: any;
|
448
|
-
finish_reason?: "length" | "stop" | "
|
356
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
449
357
|
}[];
|
450
358
|
created: number;
|
451
359
|
system_fingerprint?: string | undefined;
|
@@ -454,7 +362,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
454
362
|
toolCalls: {
|
455
363
|
id: string;
|
456
364
|
name: string;
|
457
|
-
|
365
|
+
args: unknown;
|
458
366
|
}[] | null;
|
459
367
|
usage: {
|
460
368
|
promptTokens: number;
|
@@ -575,7 +483,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
575
483
|
};
|
576
484
|
index: number;
|
577
485
|
logprobs?: any;
|
578
|
-
finish_reason?: "length" | "stop" | "
|
486
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
579
487
|
}, {
|
580
488
|
message: {
|
581
489
|
role: "assistant";
|
@@ -595,7 +503,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
595
503
|
};
|
596
504
|
index: number;
|
597
505
|
logprobs?: any;
|
598
|
-
finish_reason?: "length" | "stop" | "
|
506
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
599
507
|
}>, "many">;
|
600
508
|
created: z.ZodNumber;
|
601
509
|
model: z.ZodString;
|
@@ -642,7 +550,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
642
550
|
};
|
643
551
|
index: number;
|
644
552
|
logprobs?: any;
|
645
|
-
finish_reason?: "length" | "stop" | "
|
553
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
646
554
|
}[];
|
647
555
|
created: number;
|
648
556
|
system_fingerprint?: string | undefined;
|
@@ -674,7 +582,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
674
582
|
};
|
675
583
|
index: number;
|
676
584
|
logprobs?: any;
|
677
|
-
finish_reason?: "length" | "stop" | "
|
585
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
678
586
|
}[];
|
679
587
|
created: number;
|
680
588
|
system_fingerprint?: string | undefined;
|
@@ -718,7 +626,7 @@ export declare const OpenAIChatResponseFormat: {
|
|
718
626
|
};
|
719
627
|
index: number;
|
720
628
|
logprobs?: any;
|
721
|
-
finish_reason?: "length" | "stop" | "
|
629
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
722
630
|
}[];
|
723
631
|
created: number;
|
724
632
|
system_fingerprint?: string | undefined;
|
@@ -306,52 +306,6 @@ export class OpenAIChatModel extends AbstractModel {
|
|
306
306
|
],
|
307
307
|
});
|
308
308
|
}
|
309
|
-
async doGenerateStructureOrText(structureDefinitions, prompt, options) {
|
310
|
-
const response = await this.callAPI(prompt, {
|
311
|
-
...options,
|
312
|
-
responseFormat: OpenAIChatResponseFormat.json,
|
313
|
-
functionCall: "auto",
|
314
|
-
functions: structureDefinitions.map((structureDefinition) => ({
|
315
|
-
name: structureDefinition.name,
|
316
|
-
description: structureDefinition.description,
|
317
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
318
|
-
})),
|
319
|
-
});
|
320
|
-
const message = response.choices[0].message;
|
321
|
-
const content = message.content;
|
322
|
-
const functionCall = message.function_call;
|
323
|
-
if (functionCall == null) {
|
324
|
-
return {
|
325
|
-
response,
|
326
|
-
structureAndText: {
|
327
|
-
structure: null,
|
328
|
-
value: null,
|
329
|
-
valueText: null,
|
330
|
-
text: content ?? "",
|
331
|
-
},
|
332
|
-
usage: this.extractUsage(response),
|
333
|
-
};
|
334
|
-
}
|
335
|
-
try {
|
336
|
-
return {
|
337
|
-
response,
|
338
|
-
structureAndText: {
|
339
|
-
structure: functionCall.name,
|
340
|
-
value: SecureJSON.parse(functionCall.arguments),
|
341
|
-
valueText: functionCall.arguments,
|
342
|
-
text: content,
|
343
|
-
},
|
344
|
-
usage: this.extractUsage(response),
|
345
|
-
};
|
346
|
-
}
|
347
|
-
catch (error) {
|
348
|
-
throw new StructureParseError({
|
349
|
-
structureName: functionCall.name,
|
350
|
-
valueText: functionCall.arguments,
|
351
|
-
cause: error,
|
352
|
-
});
|
353
|
-
}
|
354
|
-
}
|
355
309
|
async doGenerateToolCall(tool, prompt, options) {
|
356
310
|
const response = await this.callAPI(prompt, {
|
357
311
|
...options,
|
@@ -378,7 +332,7 @@ export class OpenAIChatModel extends AbstractModel {
|
|
378
332
|
? null
|
379
333
|
: {
|
380
334
|
id: toolCalls[0].id,
|
381
|
-
|
335
|
+
args: parseJSON({ text: toolCalls[0].function.arguments }),
|
382
336
|
},
|
383
337
|
usage: this.extractUsage(response),
|
384
338
|
};
|
@@ -404,7 +358,7 @@ export class OpenAIChatModel extends AbstractModel {
|
|
404
358
|
toolCalls: message.tool_calls?.map((toolCall) => ({
|
405
359
|
id: toolCall.id,
|
406
360
|
name: toolCall.function.name,
|
407
|
-
|
361
|
+
args: parseJSON({ text: toolCall.function.arguments }),
|
408
362
|
})) ?? null,
|
409
363
|
usage: this.extractUsage(response),
|
410
364
|
};
|
package/package.json
CHANGED
package/tool/Tool.cjs
CHANGED
@@ -8,7 +8,7 @@ const namePattern = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
8
8
|
* by agents and chatbots.
|
9
9
|
*/
|
10
10
|
class Tool {
|
11
|
-
constructor({ name, description,
|
11
|
+
constructor({ name, description, parameters, returnType, execute, }) {
|
12
12
|
/**
|
13
13
|
* The name of the tool.
|
14
14
|
* It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
|
@@ -33,7 +33,7 @@ class Tool {
|
|
33
33
|
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
34
34
|
* Use descriptions to make the input understandable for the language model.
|
35
35
|
*/
|
36
|
-
Object.defineProperty(this, "
|
36
|
+
Object.defineProperty(this, "parameters", {
|
37
37
|
enumerable: true,
|
38
38
|
configurable: true,
|
39
39
|
writable: true,
|
@@ -42,7 +42,7 @@ class Tool {
|
|
42
42
|
/**
|
43
43
|
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
44
44
|
*/
|
45
|
-
Object.defineProperty(this, "
|
45
|
+
Object.defineProperty(this, "returnType", {
|
46
46
|
enumerable: true,
|
47
47
|
configurable: true,
|
48
48
|
writable: true,
|
@@ -66,20 +66,9 @@ class Tool {
|
|
66
66
|
}
|
67
67
|
this.name = name;
|
68
68
|
this.description = description;
|
69
|
-
this.
|
70
|
-
this.
|
69
|
+
this.parameters = parameters;
|
70
|
+
this.returnType = returnType;
|
71
71
|
this.execute = execute;
|
72
72
|
}
|
73
|
-
/**
|
74
|
-
* Provides a structure definition with the name, description and schema of the input.
|
75
|
-
* This is used by `useTool`.
|
76
|
-
*/
|
77
|
-
get inputStructureDefinition() {
|
78
|
-
return {
|
79
|
-
name: this.name,
|
80
|
-
description: this.description,
|
81
|
-
schema: this.inputSchema,
|
82
|
-
};
|
83
|
-
}
|
84
73
|
}
|
85
74
|
exports.Tool = Tool;
|
package/tool/Tool.d.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
2
|
import { JsonSchemaProducer } from "../core/schema/JsonSchemaProducer.js";
|
3
3
|
import { Schema } from "../core/schema/Schema.js";
|
4
|
-
import {
|
4
|
+
import { ToolDefinition } from "./ToolDefinition.js";
|
5
5
|
/**
|
6
6
|
* A tool is a function with a name, description and defined inputs that can be used
|
7
7
|
* by agents and chatbots.
|
8
8
|
*/
|
9
|
-
export declare class Tool<NAME extends string,
|
9
|
+
export declare class Tool<NAME extends string, PARAMETERS, RESULT> implements ToolDefinition<NAME, PARAMETERS> {
|
10
10
|
/**
|
11
11
|
* The name of the tool.
|
12
12
|
* It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
|
@@ -21,25 +21,20 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
|
21
21
|
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
22
22
|
* Use descriptions to make the input understandable for the language model.
|
23
23
|
*/
|
24
|
-
readonly
|
24
|
+
readonly parameters: Schema<PARAMETERS> & JsonSchemaProducer;
|
25
25
|
/**
|
26
26
|
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
27
27
|
*/
|
28
|
-
readonly
|
28
|
+
readonly returnType?: Schema<RESULT>;
|
29
29
|
/**
|
30
30
|
* The actual execution function of the tool.
|
31
31
|
*/
|
32
|
-
readonly execute: (
|
33
|
-
constructor({ name, description,
|
32
|
+
readonly execute: (args: PARAMETERS, options?: FunctionOptions) => PromiseLike<RESULT>;
|
33
|
+
constructor({ name, description, parameters, returnType, execute, }: {
|
34
34
|
name: NAME;
|
35
35
|
description: string;
|
36
|
-
|
37
|
-
|
38
|
-
execute(
|
36
|
+
parameters: Schema<PARAMETERS> & JsonSchemaProducer;
|
37
|
+
returnType?: Schema<RESULT>;
|
38
|
+
execute(args: PARAMETERS, options?: FunctionOptions): PromiseLike<RESULT>;
|
39
39
|
});
|
40
|
-
/**
|
41
|
-
* Provides a structure definition with the name, description and schema of the input.
|
42
|
-
* This is used by `useTool`.
|
43
|
-
*/
|
44
|
-
get inputStructureDefinition(): StructureDefinition<NAME, INPUT>;
|
45
40
|
}
|
package/tool/Tool.js
CHANGED
@@ -5,7 +5,7 @@ const namePattern = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
5
5
|
* by agents and chatbots.
|
6
6
|
*/
|
7
7
|
export class Tool {
|
8
|
-
constructor({ name, description,
|
8
|
+
constructor({ name, description, parameters, returnType, execute, }) {
|
9
9
|
/**
|
10
10
|
* The name of the tool.
|
11
11
|
* It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
|
@@ -30,7 +30,7 @@ export class Tool {
|
|
30
30
|
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
31
31
|
* Use descriptions to make the input understandable for the language model.
|
32
32
|
*/
|
33
|
-
Object.defineProperty(this, "
|
33
|
+
Object.defineProperty(this, "parameters", {
|
34
34
|
enumerable: true,
|
35
35
|
configurable: true,
|
36
36
|
writable: true,
|
@@ -39,7 +39,7 @@ export class Tool {
|
|
39
39
|
/**
|
40
40
|
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
41
41
|
*/
|
42
|
-
Object.defineProperty(this, "
|
42
|
+
Object.defineProperty(this, "returnType", {
|
43
43
|
enumerable: true,
|
44
44
|
configurable: true,
|
45
45
|
writable: true,
|
@@ -63,19 +63,8 @@ export class Tool {
|
|
63
63
|
}
|
64
64
|
this.name = name;
|
65
65
|
this.description = description;
|
66
|
-
this.
|
67
|
-
this.
|
66
|
+
this.parameters = parameters;
|
67
|
+
this.returnType = returnType;
|
68
68
|
this.execute = execute;
|
69
69
|
}
|
70
|
-
/**
|
71
|
-
* Provides a structure definition with the name, description and schema of the input.
|
72
|
-
* This is used by `useTool`.
|
73
|
-
*/
|
74
|
-
get inputStructureDefinition() {
|
75
|
-
return {
|
76
|
-
name: this.name,
|
77
|
-
description: this.description,
|
78
|
-
schema: this.inputSchema,
|
79
|
-
};
|
80
|
-
}
|
81
70
|
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* A tool call contains all information required to execute a tool.
|
3
|
+
* It can be generated by a language model.
|
4
|
+
*
|
5
|
+
* @template NAME - The name of the tool as a string.
|
6
|
+
* @template PARAMETERS - The parameters for the tool.
|
7
|
+
*/
|
8
|
+
export interface ToolCall<NAME extends string, PARAMETERS> {
|
9
|
+
/** The id of the tool call. */
|
10
|
+
id: string;
|
11
|
+
/** The name of the tool. */
|
12
|
+
name: NAME;
|
13
|
+
/** The arguments for the tool. */
|
14
|
+
args: PARAMETERS;
|
15
|
+
}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ToolCallArgumentsValidationError = void 0;
|
4
|
+
const getErrorMessage_js_1 = require("../util/getErrorMessage.cjs");
|
5
|
+
/**
|
6
|
+
* Thrown when the arguments of a tool call are invalid.
|
7
|
+
*
|
8
|
+
* This typically means they don't match the parameters schema that is expected the tool.
|
9
|
+
*/
|
10
|
+
class ToolCallArgumentsValidationError extends Error {
|
11
|
+
constructor({ toolName, args, cause, }) {
|
12
|
+
super(`Argument validation failed for tool '${toolName}'.\n` +
|
13
|
+
`Arguments: ${JSON.stringify(args)}.\n` +
|
14
|
+
`Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
|
15
|
+
Object.defineProperty(this, "toolName", {
|
16
|
+
enumerable: true,
|
17
|
+
configurable: true,
|
18
|
+
writable: true,
|
19
|
+
value: void 0
|
20
|
+
});
|
21
|
+
Object.defineProperty(this, "cause", {
|
22
|
+
enumerable: true,
|
23
|
+
configurable: true,
|
24
|
+
writable: true,
|
25
|
+
value: void 0
|
26
|
+
});
|
27
|
+
Object.defineProperty(this, "args", {
|
28
|
+
enumerable: true,
|
29
|
+
configurable: true,
|
30
|
+
writable: true,
|
31
|
+
value: void 0
|
32
|
+
});
|
33
|
+
this.name = "ToolCallArgumentsValidationError";
|
34
|
+
this.toolName = toolName;
|
35
|
+
this.cause = cause;
|
36
|
+
this.args = args;
|
37
|
+
}
|
38
|
+
toJSON() {
|
39
|
+
return {
|
40
|
+
name: this.name,
|
41
|
+
message: this.message,
|
42
|
+
cause: this.cause,
|
43
|
+
stack: this.stack,
|
44
|
+
toolName: this.toolName,
|
45
|
+
args: this.args,
|
46
|
+
};
|
47
|
+
}
|
48
|
+
}
|
49
|
+
exports.ToolCallArgumentsValidationError = ToolCallArgumentsValidationError;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
/**
|
2
|
+
* Thrown when the arguments of a tool call are invalid.
|
3
|
+
*
|
4
|
+
* This typically means they don't match the parameters schema that is expected the tool.
|
5
|
+
*/
|
6
|
+
export declare class ToolCallArgumentsValidationError extends Error {
|
7
|
+
readonly toolName: string;
|
8
|
+
readonly cause: unknown;
|
9
|
+
readonly args: unknown;
|
10
|
+
constructor({ toolName, args, cause, }: {
|
11
|
+
toolName: string;
|
12
|
+
args: unknown;
|
13
|
+
cause: unknown;
|
14
|
+
});
|
15
|
+
toJSON(): {
|
16
|
+
name: string;
|
17
|
+
message: string;
|
18
|
+
cause: unknown;
|
19
|
+
stack: string | undefined;
|
20
|
+
toolName: string;
|
21
|
+
args: unknown;
|
22
|
+
};
|
23
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { getErrorMessage } from "../util/getErrorMessage.js";
|
2
|
+
/**
|
3
|
+
* Thrown when the arguments of a tool call are invalid.
|
4
|
+
*
|
5
|
+
* This typically means they don't match the parameters schema that is expected the tool.
|
6
|
+
*/
|
7
|
+
export class ToolCallArgumentsValidationError extends Error {
|
8
|
+
constructor({ toolName, args, cause, }) {
|
9
|
+
super(`Argument validation failed for tool '${toolName}'.\n` +
|
10
|
+
`Arguments: ${JSON.stringify(args)}.\n` +
|
11
|
+
`Error message: ${getErrorMessage(cause)}`);
|
12
|
+
Object.defineProperty(this, "toolName", {
|
13
|
+
enumerable: true,
|
14
|
+
configurable: true,
|
15
|
+
writable: true,
|
16
|
+
value: void 0
|
17
|
+
});
|
18
|
+
Object.defineProperty(this, "cause", {
|
19
|
+
enumerable: true,
|
20
|
+
configurable: true,
|
21
|
+
writable: true,
|
22
|
+
value: void 0
|
23
|
+
});
|
24
|
+
Object.defineProperty(this, "args", {
|
25
|
+
enumerable: true,
|
26
|
+
configurable: true,
|
27
|
+
writable: true,
|
28
|
+
value: void 0
|
29
|
+
});
|
30
|
+
this.name = "ToolCallArgumentsValidationError";
|
31
|
+
this.toolName = toolName;
|
32
|
+
this.cause = cause;
|
33
|
+
this.args = args;
|
34
|
+
}
|
35
|
+
toJSON() {
|
36
|
+
return {
|
37
|
+
name: this.name,
|
38
|
+
message: this.message,
|
39
|
+
cause: this.cause,
|
40
|
+
stack: this.stack,
|
41
|
+
toolName: this.toolName,
|
42
|
+
args: this.args,
|
43
|
+
};
|
44
|
+
}
|
45
|
+
}
|