modelfusion 0.9.0 → 0.11.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 +7 -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 +2 -2
- 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/WebSearchTool.cjs +20 -12
- package/tool/WebSearchTool.d.ts +5 -6
- package/tool/WebSearchTool.js +20 -12
- 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({
|
@@ -451,6 +451,12 @@ Record audio with push-to-talk and transcribe it using Whisper, implemented as a
|
|
451
451
|
|
452
452
|
TypeScript implementation of the BabyAGI classic and BabyBeeAGI.
|
453
453
|
|
454
|
+
### [Wikipedia Agent](https://github.com/lgrammel/modelfusion/tree/main/examples/wikipedia-agent)
|
455
|
+
|
456
|
+
> _terminal app_, _ReAct agent_, _GPT-4_, _OpenAI functions_, _tools_
|
457
|
+
|
458
|
+
Get answers to questions from Wikipedia, e.g. "Who was born first, Einstein or Picasso?"
|
459
|
+
|
454
460
|
### [Middle school math agent](https://github.com/lgrammel/modelfusion/tree/main/examples/middle-school-math-agent)
|
455
461
|
|
456
462
|
> _terminal app_, _agent_, _tools_, _GPT-4_
|
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
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "Build AI applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.11.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -67,7 +67,7 @@
|
|
67
67
|
"eslint": "^8.45.0",
|
68
68
|
"eslint-config-prettier": "9.0.0",
|
69
69
|
"husky": "^8.0.3",
|
70
|
-
"lint-staged": "
|
70
|
+
"lint-staged": "14.0.0",
|
71
71
|
"prettier": "3.0.1",
|
72
72
|
"rimraf": "5.0.1",
|
73
73
|
"typescript": "5.1.6",
|
@@ -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/WebSearchTool.cjs
CHANGED
@@ -13,31 +13,39 @@ const OUTPUT_SCHEMA = zod_1.z.object({
|
|
13
13
|
snippet: zod_1.z.string(),
|
14
14
|
})),
|
15
15
|
});
|
16
|
-
/**
|
17
|
-
* @see https://serpapi.com/search-api
|
18
|
-
*/
|
19
16
|
class WebSearchTool extends Tool_js_1.Tool {
|
20
|
-
constructor(
|
17
|
+
constructor({ name, description, queryDescription = "Search query", execute, }) {
|
21
18
|
super({
|
22
|
-
name
|
23
|
-
description
|
24
|
-
inputSchema:
|
19
|
+
name,
|
20
|
+
description,
|
21
|
+
inputSchema: WebSearchTool.createInputSchema(queryDescription),
|
25
22
|
outputSchema: OUTPUT_SCHEMA,
|
26
|
-
execute
|
23
|
+
execute,
|
27
24
|
});
|
25
|
+
Object.defineProperty(this, "outputSchema", {
|
26
|
+
enumerable: true,
|
27
|
+
configurable: true,
|
28
|
+
writable: true,
|
29
|
+
value: void 0
|
30
|
+
});
|
31
|
+
this.outputSchema = OUTPUT_SCHEMA;
|
28
32
|
}
|
29
33
|
}
|
30
34
|
exports.WebSearchTool = WebSearchTool;
|
31
35
|
// expose the schemas to library consumers:
|
32
|
-
Object.defineProperty(WebSearchTool, "
|
36
|
+
Object.defineProperty(WebSearchTool, "createInputSchema", {
|
33
37
|
enumerable: true,
|
34
38
|
configurable: true,
|
35
39
|
writable: true,
|
36
|
-
value:
|
40
|
+
value: (description) =>
|
41
|
+
// same structure, but with description:
|
42
|
+
zod_1.z.object({
|
43
|
+
query: zod_1.z.string().describe(description),
|
44
|
+
})
|
37
45
|
});
|
38
|
-
Object.defineProperty(WebSearchTool, "
|
46
|
+
Object.defineProperty(WebSearchTool, "createOutputSchema", {
|
39
47
|
enumerable: true,
|
40
48
|
configurable: true,
|
41
49
|
writable: true,
|
42
|
-
value: OUTPUT_SCHEMA
|
50
|
+
value: () => OUTPUT_SCHEMA
|
43
51
|
});
|
package/tool/WebSearchTool.d.ts
CHANGED
@@ -34,18 +34,15 @@ declare const OUTPUT_SCHEMA: z.ZodObject<{
|
|
34
34
|
snippet: string;
|
35
35
|
}[];
|
36
36
|
}>;
|
37
|
-
/**
|
38
|
-
* @see https://serpapi.com/search-api
|
39
|
-
*/
|
40
37
|
export declare class WebSearchTool<NAME extends string> extends Tool<NAME, z.infer<typeof INPUT_SCHEMA>, z.infer<typeof OUTPUT_SCHEMA>> {
|
41
|
-
static readonly
|
38
|
+
static readonly createInputSchema: (description: string) => z.ZodObject<{
|
42
39
|
query: z.ZodString;
|
43
40
|
}, "strip", z.ZodTypeAny, {
|
44
41
|
query: string;
|
45
42
|
}, {
|
46
43
|
query: string;
|
47
44
|
}>;
|
48
|
-
static readonly
|
45
|
+
static readonly createOutputSchema: () => z.ZodObject<{
|
49
46
|
results: z.ZodArray<z.ZodObject<{
|
50
47
|
title: z.ZodString;
|
51
48
|
link: z.ZodString;
|
@@ -72,9 +69,11 @@ export declare class WebSearchTool<NAME extends string> extends Tool<NAME, z.inf
|
|
72
69
|
snippet: string;
|
73
70
|
}[];
|
74
71
|
}>;
|
75
|
-
|
72
|
+
readonly outputSchema: typeof OUTPUT_SCHEMA;
|
73
|
+
constructor({ name, description, queryDescription, execute, }: {
|
76
74
|
name: NAME;
|
77
75
|
description: string;
|
76
|
+
queryDescription?: string;
|
78
77
|
execute(input: z.infer<typeof INPUT_SCHEMA>): Promise<z.infer<typeof OUTPUT_SCHEMA>>;
|
79
78
|
});
|
80
79
|
}
|
package/tool/WebSearchTool.js
CHANGED
@@ -10,30 +10,38 @@ const OUTPUT_SCHEMA = z.object({
|
|
10
10
|
snippet: z.string(),
|
11
11
|
})),
|
12
12
|
});
|
13
|
-
/**
|
14
|
-
* @see https://serpapi.com/search-api
|
15
|
-
*/
|
16
13
|
export class WebSearchTool extends Tool {
|
17
|
-
constructor(
|
14
|
+
constructor({ name, description, queryDescription = "Search query", execute, }) {
|
18
15
|
super({
|
19
|
-
name
|
20
|
-
description
|
21
|
-
inputSchema:
|
16
|
+
name,
|
17
|
+
description,
|
18
|
+
inputSchema: WebSearchTool.createInputSchema(queryDescription),
|
22
19
|
outputSchema: OUTPUT_SCHEMA,
|
23
|
-
execute
|
20
|
+
execute,
|
24
21
|
});
|
22
|
+
Object.defineProperty(this, "outputSchema", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: void 0
|
27
|
+
});
|
28
|
+
this.outputSchema = OUTPUT_SCHEMA;
|
25
29
|
}
|
26
30
|
}
|
27
31
|
// expose the schemas to library consumers:
|
28
|
-
Object.defineProperty(WebSearchTool, "
|
32
|
+
Object.defineProperty(WebSearchTool, "createInputSchema", {
|
29
33
|
enumerable: true,
|
30
34
|
configurable: true,
|
31
35
|
writable: true,
|
32
|
-
value:
|
36
|
+
value: (description) =>
|
37
|
+
// same structure, but with description:
|
38
|
+
z.object({
|
39
|
+
query: z.string().describe(description),
|
40
|
+
})
|
33
41
|
});
|
34
|
-
Object.defineProperty(WebSearchTool, "
|
42
|
+
Object.defineProperty(WebSearchTool, "createOutputSchema", {
|
35
43
|
enumerable: true,
|
36
44
|
configurable: true,
|
37
45
|
writable: true,
|
38
|
-
value: OUTPUT_SCHEMA
|
46
|
+
value: () => OUTPUT_SCHEMA
|
39
47
|
});
|
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