modelfusion 0.70.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 +61 -49
- 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 +4 -3
- 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 +14 -29
- package/model-provider/openai/chat/OpenAIChatMessage.js +16 -25
- package/model-provider/openai/chat/OpenAIChatModel.cjs +28 -48
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +23 -68
- package/model-provider/openai/chat/OpenAIChatModel.js +28 -48
- package/package.json +2 -2
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.js → tool/NoSuchToolDefinitionError.cjs} +9 -8
- package/{model-function/generate-tool-call/ToolCallParametersValidationError.d.ts → tool/NoSuchToolDefinitionError.d.ts} +2 -3
- package/tool/NoSuchToolDefinitionError.js +37 -0
- 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/tool/ToolCallParseError.d.ts +18 -0
- package/tool/ToolCallParseError.js +40 -0
- package/tool/ToolCallResult.d.ts +13 -0
- package/tool/ToolDefinition.d.ts +7 -0
- 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 +5 -5
- package/{model-function → tool}/generate-tool-call/generateToolCall.cjs +13 -12
- package/{model-function → tool}/generate-tool-call/generateToolCall.d.ts +7 -12
- package/{model-function → tool}/generate-tool-call/generateToolCall.js +13 -12
- package/{model-function → tool}/generate-tool-call/index.cjs +1 -2
- package/{model-function → tool}/generate-tool-call/index.d.ts +1 -2
- package/{model-function → tool}/generate-tool-call/index.js +1 -2
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.d.ts +23 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.cjs +2 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.d.ts +21 -0
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js +1 -0
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.cjs +63 -0
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.d.ts +33 -0
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.js +59 -0
- package/tool/generate-tool-calls-or-text/index.cjs +19 -0
- 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/use-tool/UseToolEvent.cjs +2 -0
- package/tool/{UseToolEvent.d.ts → use-tool/UseToolEvent.d.ts} +1 -1
- package/tool/use-tool/UseToolEvent.js +1 -0
- 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.cjs +2 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +7 -0
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js +1 -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/ToolCallDefinition.d.ts +0 -7
- 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-structure/StructureOrTextGenerationModel.cjs → tool/ToolCall.cjs} +0 -0
- /package/{model-function/generate-structure/StructureOrTextGenerationModel.js → tool/ToolCall.js} +0 -0
- /package/{model-function/generate-tool-call/ToolCallDefinition.cjs → tool/ToolCallResult.cjs} +0 -0
- /package/{model-function/generate-tool-call/ToolCallDefinition.js → tool/ToolCallResult.js} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationEvent.cjs → tool/ToolDefinition.cjs} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationEvent.js → 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/generate-tool-call/ToolCallGenerationModel.cjs → tool/generate-tool-call/ToolCallGenerationEvent.cjs} +0 -0
- /package/{model-function/generate-tool-call/ToolCallGenerationModel.js → tool/generate-tool-call/ToolCallGenerationEvent.js} +0 -0
- /package/tool/{UseToolEvent.cjs → generate-tool-call/ToolCallGenerationModel.cjs} +0 -0
- /package/tool/{UseToolEvent.js → generate-tool-call/ToolCallGenerationModel.js} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.cjs → generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.cjs} +0 -0
- /package/tool/{UseToolOrGenerateTextEvent.js → generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.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,
|
@@ -383,12 +337,38 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
383
337
|
const toolCalls = response.choices[0]?.message.tool_calls;
|
384
338
|
return {
|
385
339
|
response,
|
386
|
-
|
340
|
+
toolCall: toolCalls == null || toolCalls.length === 0
|
387
341
|
? null
|
388
342
|
: {
|
389
343
|
id: toolCalls[0].id,
|
390
|
-
|
344
|
+
args: (0, parseJSON_js_1.parseJSON)({ text: toolCalls[0].function.arguments }),
|
345
|
+
},
|
346
|
+
usage: this.extractUsage(response),
|
347
|
+
};
|
348
|
+
}
|
349
|
+
async doGenerateToolCallsOrText(tools, prompt, options) {
|
350
|
+
const response = await this.callAPI(prompt, {
|
351
|
+
...options,
|
352
|
+
responseFormat: exports.OpenAIChatResponseFormat.json,
|
353
|
+
toolChoice: "auto",
|
354
|
+
tools: tools.map((tool) => ({
|
355
|
+
type: "function",
|
356
|
+
function: {
|
357
|
+
name: tool.name,
|
358
|
+
description: tool.description,
|
359
|
+
parameters: tool.parameters.getJsonSchema(),
|
391
360
|
},
|
361
|
+
})),
|
362
|
+
});
|
363
|
+
const message = response.choices[0]?.message;
|
364
|
+
return {
|
365
|
+
response,
|
366
|
+
text: message.content ?? null,
|
367
|
+
toolCalls: message.tool_calls?.map((toolCall) => ({
|
368
|
+
id: toolCall.id,
|
369
|
+
name: toolCall.function.name,
|
370
|
+
args: (0, parseJSON_js_1.parseJSON)({ text: toolCall.function.arguments }),
|
371
|
+
})) ?? null,
|
392
372
|
usage: this.extractUsage(response),
|
393
373
|
};
|
394
374
|
}
|
@@ -6,12 +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 { ToolCallGenerationModel } from "../../../
|
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";
|
15
15
|
import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
|
16
16
|
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
17
17
|
export declare const OPENAI_CHAT_MODELS: {
|
@@ -168,7 +168,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, Omit<Op
|
|
168
168
|
* ),
|
169
169
|
* ]);
|
170
170
|
*/
|
171
|
-
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> {
|
172
172
|
constructor(settings: OpenAIChatSettings);
|
173
173
|
readonly provider: "openai";
|
174
174
|
get modelName(): OpenAIChatModelType;
|
@@ -217,7 +217,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
217
217
|
};
|
218
218
|
index: number;
|
219
219
|
logprobs?: any;
|
220
|
-
finish_reason?: "length" | "stop" | "
|
220
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
221
221
|
}[];
|
222
222
|
created: number;
|
223
223
|
system_fingerprint?: string | undefined;
|
@@ -266,7 +266,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
266
266
|
};
|
267
267
|
index: number;
|
268
268
|
logprobs?: any;
|
269
|
-
finish_reason?: "length" | "stop" | "
|
269
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
270
270
|
}[];
|
271
271
|
created: number;
|
272
272
|
system_fingerprint?: string | undefined;
|
@@ -280,7 +280,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
280
280
|
};
|
281
281
|
}>;
|
282
282
|
doStreamStructure(structureDefinition: StructureDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<AsyncIterable<Delta<unknown>>>;
|
283
|
-
|
283
|
+
doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
284
284
|
response: {
|
285
285
|
object: "chat.completion";
|
286
286
|
usage: {
|
@@ -309,69 +309,22 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
309
309
|
};
|
310
310
|
index: number;
|
311
311
|
logprobs?: any;
|
312
|
-
finish_reason?: "length" | "stop" | "
|
312
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
313
313
|
}[];
|
314
314
|
created: number;
|
315
315
|
system_fingerprint?: string | undefined;
|
316
316
|
};
|
317
|
-
|
318
|
-
structure: null;
|
319
|
-
value: null;
|
320
|
-
valueText: null;
|
321
|
-
text: string;
|
322
|
-
};
|
323
|
-
usage: {
|
324
|
-
promptTokens: number;
|
325
|
-
completionTokens: number;
|
326
|
-
totalTokens: number;
|
327
|
-
};
|
328
|
-
} | {
|
329
|
-
response: {
|
330
|
-
object: "chat.completion";
|
331
|
-
usage: {
|
332
|
-
prompt_tokens: number;
|
333
|
-
total_tokens: number;
|
334
|
-
completion_tokens: number;
|
335
|
-
};
|
336
|
-
model: string;
|
317
|
+
toolCall: {
|
337
318
|
id: string;
|
338
|
-
|
339
|
-
|
340
|
-
role: "assistant";
|
341
|
-
content: string | null;
|
342
|
-
function_call?: {
|
343
|
-
name: string;
|
344
|
-
arguments: string;
|
345
|
-
} | undefined;
|
346
|
-
tool_calls?: {
|
347
|
-
function: {
|
348
|
-
name: string;
|
349
|
-
arguments: string;
|
350
|
-
};
|
351
|
-
type: "function";
|
352
|
-
id: string;
|
353
|
-
}[] | undefined;
|
354
|
-
};
|
355
|
-
index: number;
|
356
|
-
logprobs?: any;
|
357
|
-
finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
|
358
|
-
}[];
|
359
|
-
created: number;
|
360
|
-
system_fingerprint?: string | undefined;
|
361
|
-
};
|
362
|
-
structureAndText: {
|
363
|
-
structure: string;
|
364
|
-
value: any;
|
365
|
-
valueText: string;
|
366
|
-
text: string | null;
|
367
|
-
};
|
319
|
+
args: unknown;
|
320
|
+
} | null;
|
368
321
|
usage: {
|
369
322
|
promptTokens: number;
|
370
323
|
completionTokens: number;
|
371
324
|
totalTokens: number;
|
372
325
|
};
|
373
326
|
}>;
|
374
|
-
|
327
|
+
doGenerateToolCallsOrText(tools: Array<ToolDefinition<string, unknown>>, prompt: OpenAIChatMessage[], options?: FunctionOptions): Promise<{
|
375
328
|
response: {
|
376
329
|
object: "chat.completion";
|
377
330
|
usage: {
|
@@ -400,15 +353,17 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
400
353
|
};
|
401
354
|
index: number;
|
402
355
|
logprobs?: any;
|
403
|
-
finish_reason?: "length" | "stop" | "
|
356
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
404
357
|
}[];
|
405
358
|
created: number;
|
406
359
|
system_fingerprint?: string | undefined;
|
407
360
|
};
|
408
|
-
|
361
|
+
text: string | null;
|
362
|
+
toolCalls: {
|
409
363
|
id: string;
|
410
|
-
|
411
|
-
|
364
|
+
name: string;
|
365
|
+
args: unknown;
|
366
|
+
}[] | null;
|
412
367
|
usage: {
|
413
368
|
promptTokens: number;
|
414
369
|
completionTokens: number;
|
@@ -528,7 +483,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
528
483
|
};
|
529
484
|
index: number;
|
530
485
|
logprobs?: any;
|
531
|
-
finish_reason?: "length" | "stop" | "
|
486
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
532
487
|
}, {
|
533
488
|
message: {
|
534
489
|
role: "assistant";
|
@@ -548,7 +503,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
548
503
|
};
|
549
504
|
index: number;
|
550
505
|
logprobs?: any;
|
551
|
-
finish_reason?: "length" | "stop" | "
|
506
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
552
507
|
}>, "many">;
|
553
508
|
created: z.ZodNumber;
|
554
509
|
model: z.ZodString;
|
@@ -595,7 +550,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
595
550
|
};
|
596
551
|
index: number;
|
597
552
|
logprobs?: any;
|
598
|
-
finish_reason?: "length" | "stop" | "
|
553
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
599
554
|
}[];
|
600
555
|
created: number;
|
601
556
|
system_fingerprint?: string | undefined;
|
@@ -627,7 +582,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
627
582
|
};
|
628
583
|
index: number;
|
629
584
|
logprobs?: any;
|
630
|
-
finish_reason?: "length" | "stop" | "
|
585
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
631
586
|
}[];
|
632
587
|
created: number;
|
633
588
|
system_fingerprint?: string | undefined;
|
@@ -671,7 +626,7 @@ export declare const OpenAIChatResponseFormat: {
|
|
671
626
|
};
|
672
627
|
index: number;
|
673
628
|
logprobs?: any;
|
674
|
-
finish_reason?: "length" | "stop" | "
|
629
|
+
finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
|
675
630
|
}[];
|
676
631
|
created: number;
|
677
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,
|
@@ -374,12 +328,38 @@ export class OpenAIChatModel extends AbstractModel {
|
|
374
328
|
const toolCalls = response.choices[0]?.message.tool_calls;
|
375
329
|
return {
|
376
330
|
response,
|
377
|
-
|
331
|
+
toolCall: toolCalls == null || toolCalls.length === 0
|
378
332
|
? null
|
379
333
|
: {
|
380
334
|
id: toolCalls[0].id,
|
381
|
-
|
335
|
+
args: parseJSON({ text: toolCalls[0].function.arguments }),
|
336
|
+
},
|
337
|
+
usage: this.extractUsage(response),
|
338
|
+
};
|
339
|
+
}
|
340
|
+
async doGenerateToolCallsOrText(tools, prompt, options) {
|
341
|
+
const response = await this.callAPI(prompt, {
|
342
|
+
...options,
|
343
|
+
responseFormat: OpenAIChatResponseFormat.json,
|
344
|
+
toolChoice: "auto",
|
345
|
+
tools: tools.map((tool) => ({
|
346
|
+
type: "function",
|
347
|
+
function: {
|
348
|
+
name: tool.name,
|
349
|
+
description: tool.description,
|
350
|
+
parameters: tool.parameters.getJsonSchema(),
|
382
351
|
},
|
352
|
+
})),
|
353
|
+
});
|
354
|
+
const message = response.choices[0]?.message;
|
355
|
+
return {
|
356
|
+
response,
|
357
|
+
text: message.content ?? null,
|
358
|
+
toolCalls: message.tool_calls?.map((toolCall) => ({
|
359
|
+
id: toolCall.id,
|
360
|
+
name: toolCall.function.name,
|
361
|
+
args: parseJSON({ text: toolCall.function.arguments }),
|
362
|
+
})) ?? null,
|
383
363
|
usage: this.extractUsage(response),
|
384
364
|
};
|
385
365
|
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
|
-
"description": "
|
4
|
-
"version": "0.
|
3
|
+
"description": "The TypeScript library for building multi-modal AI applications.",
|
4
|
+
"version": "0.72.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -1,9 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NoSuchToolDefinitionError = void 0;
|
4
|
+
class NoSuchToolDefinitionError extends Error {
|
5
|
+
constructor({ toolName, parameters, }) {
|
6
|
+
super(`Tool definition '${toolName}' not found. ` +
|
7
|
+
`Parameters: ${JSON.stringify(parameters)}.`);
|
7
8
|
Object.defineProperty(this, "toolName", {
|
8
9
|
enumerable: true,
|
9
10
|
configurable: true,
|
@@ -22,9 +23,8 @@ export class ToolCallParametersValidationError extends Error {
|
|
22
23
|
writable: true,
|
23
24
|
value: void 0
|
24
25
|
});
|
25
|
-
this.name = "
|
26
|
+
this.name = "NoSuchToolDefinitionError";
|
26
27
|
this.toolName = toolName;
|
27
|
-
this.cause = cause;
|
28
28
|
this.parameters = parameters;
|
29
29
|
}
|
30
30
|
toJSON() {
|
@@ -38,3 +38,4 @@ export class ToolCallParametersValidationError extends Error {
|
|
38
38
|
};
|
39
39
|
}
|
40
40
|
}
|
41
|
+
exports.NoSuchToolDefinitionError = NoSuchToolDefinitionError;
|
@@ -1,11 +1,10 @@
|
|
1
|
-
export declare class
|
1
|
+
export declare class NoSuchToolDefinitionError extends Error {
|
2
2
|
readonly toolName: string;
|
3
3
|
readonly cause: unknown;
|
4
4
|
readonly parameters: unknown;
|
5
|
-
constructor({ toolName, parameters,
|
5
|
+
constructor({ toolName, parameters, }: {
|
6
6
|
toolName: string;
|
7
7
|
parameters: unknown;
|
8
|
-
cause: unknown;
|
9
8
|
});
|
10
9
|
toJSON(): {
|
11
10
|
name: string;
|
@@ -0,0 +1,37 @@
|
|
1
|
+
export class NoSuchToolDefinitionError extends Error {
|
2
|
+
constructor({ toolName, parameters, }) {
|
3
|
+
super(`Tool definition '${toolName}' not found. ` +
|
4
|
+
`Parameters: ${JSON.stringify(parameters)}.`);
|
5
|
+
Object.defineProperty(this, "toolName", {
|
6
|
+
enumerable: true,
|
7
|
+
configurable: true,
|
8
|
+
writable: true,
|
9
|
+
value: void 0
|
10
|
+
});
|
11
|
+
Object.defineProperty(this, "cause", {
|
12
|
+
enumerable: true,
|
13
|
+
configurable: true,
|
14
|
+
writable: true,
|
15
|
+
value: void 0
|
16
|
+
});
|
17
|
+
Object.defineProperty(this, "parameters", {
|
18
|
+
enumerable: true,
|
19
|
+
configurable: true,
|
20
|
+
writable: true,
|
21
|
+
value: void 0
|
22
|
+
});
|
23
|
+
this.name = "NoSuchToolDefinitionError";
|
24
|
+
this.toolName = toolName;
|
25
|
+
this.parameters = parameters;
|
26
|
+
}
|
27
|
+
toJSON() {
|
28
|
+
return {
|
29
|
+
name: this.name,
|
30
|
+
message: this.message,
|
31
|
+
cause: this.cause,
|
32
|
+
stack: this.stack,
|
33
|
+
toolName: this.toolName,
|
34
|
+
parameter: this.parameters,
|
35
|
+
};
|
36
|
+
}
|
37
|
+
}
|
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
|
-
* Provdes 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
|
-
* Provdes 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
|
-
* Provdes 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
|
+
}
|