modelfusion 0.9.0 → 0.10.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 +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +1 -4
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +1 -4
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +23 -0
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +26 -0
- package/model-provider/openai/chat/OpenAIChatMessage.js +23 -0
- package/package.json +1 -1
- package/tool/InvalidToolNameError.cjs +17 -0
- package/tool/InvalidToolNameError.d.ts +7 -0
- package/tool/InvalidToolNameError.js +13 -0
- package/tool/Tool.cjs +18 -7
- package/tool/Tool.d.ts +4 -2
- package/tool/Tool.js +18 -7
- package/tool/index.cjs +1 -0
- package/tool/index.d.ts +1 -0
- package/tool/index.js +1 -0
package/README.md
CHANGED
@@ -199,7 +199,7 @@ A tool is a function with a name, a description, and a schema for the input para
|
|
199
199
|
|
200
200
|
```ts
|
201
201
|
const calculator = new Tool({
|
202
|
-
name: "calculator"
|
202
|
+
name: "calculator",
|
203
203
|
description: "Execute a calculation",
|
204
204
|
|
205
205
|
inputSchema: z.object({
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs
CHANGED
@@ -17,10 +17,7 @@ async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, m
|
|
17
17
|
tokenizer: model.tokenizer,
|
18
18
|
maxTokensPerChunk: tokenLimit - emptyPromptTokens,
|
19
19
|
}),
|
20
|
-
summarize: async (input) =>
|
21
|
-
const text = await (0, generateText_js_1.generateText)(model, await prompt(input), options);
|
22
|
-
return text;
|
23
|
-
},
|
20
|
+
summarize: async (input) => (0, generateText_js_1.generateText)(model, await prompt(input), options),
|
24
21
|
join,
|
25
22
|
text,
|
26
23
|
}, options);
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js
CHANGED
@@ -14,10 +14,7 @@ export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({
|
|
14
14
|
tokenizer: model.tokenizer,
|
15
15
|
maxTokensPerChunk: tokenLimit - emptyPromptTokens,
|
16
16
|
}),
|
17
|
-
summarize: async (input) =>
|
18
|
-
const text = await generateText(model, await prompt(input), options);
|
19
|
-
return text;
|
20
|
-
},
|
17
|
+
summarize: async (input) => generateText(model, await prompt(input), options),
|
21
18
|
join,
|
22
19
|
text,
|
23
20
|
}, options);
|
@@ -21,4 +21,27 @@ exports.OpenAIChatMessage = {
|
|
21
21
|
functionResult(name, content) {
|
22
22
|
return { role: "function", name, content };
|
23
23
|
},
|
24
|
+
/**
|
25
|
+
* Creates a function call chat message for tool calls.
|
26
|
+
*/
|
27
|
+
toolCall({ text, tool, parameters, }) {
|
28
|
+
return {
|
29
|
+
role: "assistant",
|
30
|
+
content: text,
|
31
|
+
function_call: {
|
32
|
+
name: tool,
|
33
|
+
arguments: JSON.stringify(parameters),
|
34
|
+
},
|
35
|
+
};
|
36
|
+
},
|
37
|
+
/**
|
38
|
+
* Creates a function result chat message for tool call results.
|
39
|
+
*/
|
40
|
+
toolResult({ tool, result }) {
|
41
|
+
return {
|
42
|
+
role: "function",
|
43
|
+
name: tool,
|
44
|
+
content: JSON.stringify(result),
|
45
|
+
};
|
46
|
+
},
|
24
47
|
};
|
@@ -23,4 +23,30 @@ export declare const OpenAIChatMessage: {
|
|
23
23
|
arguments: string;
|
24
24
|
}): OpenAIChatMessage;
|
25
25
|
functionResult(name: string, content: string): OpenAIChatMessage;
|
26
|
+
/**
|
27
|
+
* Creates a function call chat message for tool calls.
|
28
|
+
*/
|
29
|
+
toolCall({ text, tool, parameters, }: {
|
30
|
+
text: string | null;
|
31
|
+
tool: string;
|
32
|
+
parameters: unknown;
|
33
|
+
}): {
|
34
|
+
role: "assistant";
|
35
|
+
content: string | null;
|
36
|
+
function_call: {
|
37
|
+
name: string;
|
38
|
+
arguments: string;
|
39
|
+
};
|
40
|
+
};
|
41
|
+
/**
|
42
|
+
* Creates a function result chat message for tool call results.
|
43
|
+
*/
|
44
|
+
toolResult({ tool, result }: {
|
45
|
+
tool: string;
|
46
|
+
result: unknown;
|
47
|
+
}): {
|
48
|
+
role: "function";
|
49
|
+
name: string;
|
50
|
+
content: string;
|
51
|
+
};
|
26
52
|
};
|
@@ -18,4 +18,27 @@ export const OpenAIChatMessage = {
|
|
18
18
|
functionResult(name, content) {
|
19
19
|
return { role: "function", name, content };
|
20
20
|
},
|
21
|
+
/**
|
22
|
+
* Creates a function call chat message for tool calls.
|
23
|
+
*/
|
24
|
+
toolCall({ text, tool, parameters, }) {
|
25
|
+
return {
|
26
|
+
role: "assistant",
|
27
|
+
content: text,
|
28
|
+
function_call: {
|
29
|
+
name: tool,
|
30
|
+
arguments: JSON.stringify(parameters),
|
31
|
+
},
|
32
|
+
};
|
33
|
+
},
|
34
|
+
/**
|
35
|
+
* Creates a function result chat message for tool call results.
|
36
|
+
*/
|
37
|
+
toolResult({ tool, result }) {
|
38
|
+
return {
|
39
|
+
role: "function",
|
40
|
+
name: tool,
|
41
|
+
content: JSON.stringify(result),
|
42
|
+
};
|
43
|
+
},
|
21
44
|
};
|
package/package.json
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.InvalidToolNameError = void 0;
|
4
|
+
class InvalidToolNameError extends Error {
|
5
|
+
constructor({ toolName, namePattern, }) {
|
6
|
+
super(`Invalid tool name '${toolName}'. The tool name must match the regular expression pattern ${namePattern}.`);
|
7
|
+
Object.defineProperty(this, "toolName", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
this.name = "InvalidToolNameError";
|
14
|
+
this.toolName = toolName;
|
15
|
+
}
|
16
|
+
}
|
17
|
+
exports.InvalidToolNameError = InvalidToolNameError;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
export class InvalidToolNameError extends Error {
|
2
|
+
constructor({ toolName, namePattern, }) {
|
3
|
+
super(`Invalid tool name '${toolName}'. The tool name must match the regular expression pattern ${namePattern}.`);
|
4
|
+
Object.defineProperty(this, "toolName", {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value: void 0
|
9
|
+
});
|
10
|
+
this.name = "InvalidToolNameError";
|
11
|
+
this.toolName = toolName;
|
12
|
+
}
|
13
|
+
}
|
package/tool/Tool.cjs
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.Tool = void 0;
|
4
|
+
const InvalidToolNameError_js_1 = require("./InvalidToolNameError.cjs");
|
5
|
+
const namePattern = /^[a-zA-Z0-9_-]{1,64}$/;
|
4
6
|
/**
|
5
7
|
* A tool is a function with a name, description and defined inputs that can be used
|
6
8
|
* by agents and chatbots.
|
7
9
|
*/
|
8
10
|
class Tool {
|
9
|
-
constructor(
|
11
|
+
constructor({ name, description, inputSchema, outputSchema, execute, }) {
|
10
12
|
/**
|
11
|
-
* The name of the tool.
|
13
|
+
* The name of the tool.
|
14
|
+
* It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
|
15
|
+
* Should be understandable for language models and unique among the tools that they know.
|
12
16
|
*/
|
13
17
|
Object.defineProperty(this, "name", {
|
14
18
|
enumerable: true,
|
@@ -53,11 +57,18 @@ class Tool {
|
|
53
57
|
writable: true,
|
54
58
|
value: void 0
|
55
59
|
});
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
// check that the name is a valid function name:
|
61
|
+
if (!namePattern.test(name)) {
|
62
|
+
throw new InvalidToolNameError_js_1.InvalidToolNameError({
|
63
|
+
toolName: name,
|
64
|
+
namePattern,
|
65
|
+
});
|
66
|
+
}
|
67
|
+
this.name = name;
|
68
|
+
this.description = description;
|
69
|
+
this.inputSchema = inputSchema;
|
70
|
+
this.outputSchema = outputSchema;
|
71
|
+
this.execute = execute;
|
61
72
|
}
|
62
73
|
/**
|
63
74
|
* Provdes a schema definition with the name, description and schema of the input.
|
package/tool/Tool.d.ts
CHANGED
@@ -7,7 +7,9 @@ import { RunFunction } from "../run/RunFunction.js";
|
|
7
7
|
*/
|
8
8
|
export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
9
9
|
/**
|
10
|
-
* The name of the tool.
|
10
|
+
* The name of the tool.
|
11
|
+
* It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
|
12
|
+
* Should be understandable for language models and unique among the tools that they know.
|
11
13
|
*/
|
12
14
|
readonly name: NAME;
|
13
15
|
/**
|
@@ -27,7 +29,7 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
|
27
29
|
* The actual execution function of the tool.
|
28
30
|
*/
|
29
31
|
readonly execute: RunFunction<INPUT, OUTPUT>;
|
30
|
-
constructor(
|
32
|
+
constructor({ name, description, inputSchema, outputSchema, execute, }: {
|
31
33
|
name: NAME;
|
32
34
|
description: string;
|
33
35
|
inputSchema: z.ZodSchema<INPUT>;
|
package/tool/Tool.js
CHANGED
@@ -1,11 +1,15 @@
|
|
1
|
+
import { InvalidToolNameError } from "./InvalidToolNameError.js";
|
2
|
+
const namePattern = /^[a-zA-Z0-9_-]{1,64}$/;
|
1
3
|
/**
|
2
4
|
* A tool is a function with a name, description and defined inputs that can be used
|
3
5
|
* by agents and chatbots.
|
4
6
|
*/
|
5
7
|
export class Tool {
|
6
|
-
constructor(
|
8
|
+
constructor({ name, description, inputSchema, outputSchema, execute, }) {
|
7
9
|
/**
|
8
|
-
* The name of the tool.
|
10
|
+
* The name of the tool.
|
11
|
+
* It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
|
12
|
+
* Should be understandable for language models and unique among the tools that they know.
|
9
13
|
*/
|
10
14
|
Object.defineProperty(this, "name", {
|
11
15
|
enumerable: true,
|
@@ -50,11 +54,18 @@ export class Tool {
|
|
50
54
|
writable: true,
|
51
55
|
value: void 0
|
52
56
|
});
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
// check that the name is a valid function name:
|
58
|
+
if (!namePattern.test(name)) {
|
59
|
+
throw new InvalidToolNameError({
|
60
|
+
toolName: name,
|
61
|
+
namePattern,
|
62
|
+
});
|
63
|
+
}
|
64
|
+
this.name = name;
|
65
|
+
this.description = description;
|
66
|
+
this.inputSchema = inputSchema;
|
67
|
+
this.outputSchema = outputSchema;
|
68
|
+
this.execute = execute;
|
58
69
|
}
|
59
70
|
/**
|
60
71
|
* Provdes a schema definition with the name, description and schema of the input.
|
package/tool/index.cjs
CHANGED
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./InvalidToolNameError.cjs"), exports);
|
17
18
|
__exportStar(require("./NoSuchToolError.cjs"), exports);
|
18
19
|
__exportStar(require("./Tool.cjs"), exports);
|
19
20
|
__exportStar(require("./ToolExecutionError.cjs"), exports);
|
package/tool/index.d.ts
CHANGED
package/tool/index.js
CHANGED