modelfusion 0.29.0 → 0.30.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 +27 -32
- package/core/index.cjs +1 -0
- package/core/index.d.ts +1 -0
- package/core/index.js +1 -0
- package/core/structure/Schema.d.ts +24 -0
- package/core/structure/StructureDefinition.d.ts +6 -0
- package/core/structure/UncheckedJsonSchemaSchema.cjs +26 -0
- package/core/structure/UncheckedJsonSchemaSchema.d.ts +14 -0
- package/core/structure/UncheckedJsonSchemaSchema.js +22 -0
- package/core/structure/UncheckedJsonSchemaStructureDefinition.cjs +30 -0
- package/core/structure/UncheckedJsonSchemaStructureDefinition.d.ts +12 -0
- package/core/structure/UncheckedJsonSchemaStructureDefinition.js +26 -0
- package/core/structure/ZodSchema.cjs +31 -0
- package/core/structure/ZodSchema.d.ts +15 -0
- package/core/structure/ZodSchema.js +27 -0
- package/core/structure/ZodStructureDefinition.cjs +30 -0
- package/core/structure/ZodStructureDefinition.d.ts +13 -0
- package/core/structure/ZodStructureDefinition.js +26 -0
- package/core/structure/index.cjs +22 -0
- package/core/structure/index.d.ts +6 -0
- package/core/structure/index.js +6 -0
- package/model-function/ModelCallEvent.d.ts +3 -3
- package/model-function/SuccessfulModelCall.d.ts +1 -1
- package/model-function/embed-text/TextEmbeddingModel.d.ts +4 -1
- package/model-function/embed-text/embedText.cjs +7 -2
- package/model-function/embed-text/embedText.js +7 -2
- package/model-function/generate-structure/InstructionWithStructurePrompt.cjs +17 -0
- package/model-function/generate-structure/InstructionWithStructurePrompt.d.ts +17 -0
- package/model-function/generate-structure/InstructionWithStructurePrompt.js +14 -0
- package/model-function/generate-structure/NoSuchStructureError.cjs +17 -0
- package/model-function/generate-structure/NoSuchStructureError.d.ts +4 -0
- package/model-function/generate-structure/NoSuchStructureError.js +13 -0
- package/model-function/{generate-json/JsonTextGenerationModel.cjs → generate-structure/StructureFromTextGenerationModel.cjs} +7 -7
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +26 -0
- package/model-function/{generate-json/JsonTextGenerationModel.js → generate-structure/StructureFromTextGenerationModel.js} +5 -5
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +23 -0
- package/model-function/generate-structure/StructureGenerationModel.d.ts +13 -0
- package/model-function/generate-structure/StructureOrTextGenerationModel.cjs +2 -0
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +23 -0
- package/model-function/generate-structure/StructureOrTextGenerationModel.js +1 -0
- package/model-function/generate-structure/StructureValidationError.cjs +34 -0
- package/model-function/generate-structure/StructureValidationError.d.ts +10 -0
- package/model-function/generate-structure/StructureValidationError.js +30 -0
- package/model-function/generate-structure/generateStructure.cjs +29 -0
- package/model-function/generate-structure/generateStructure.d.ts +5 -0
- package/model-function/generate-structure/generateStructure.js +25 -0
- package/model-function/generate-structure/generateStructureOrText.cjs +42 -0
- package/model-function/generate-structure/generateStructureOrText.d.ts +22 -0
- package/model-function/generate-structure/generateStructureOrText.js +38 -0
- package/model-function/index.cjs +9 -10
- package/model-function/index.d.ts +9 -10
- package/model-function/index.js +9 -10
- package/model-provider/openai/OpenAICostCalculator.cjs +1 -1
- package/model-provider/openai/OpenAICostCalculator.js +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +6 -8
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +2 -2
- package/model-provider/openai/OpenAITextEmbeddingModel.js +6 -8
- package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +5 -5
- package/model-provider/openai/chat/OpenAIChatModel.js +2 -2
- package/model-provider/openai/chat/OpenAIChatPrompt.cjs +22 -26
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +20 -30
- package/model-provider/openai/chat/OpenAIChatPrompt.js +22 -26
- package/model-provider/openai/index.d.ts +1 -1
- package/model-provider/openai/index.js +1 -1
- package/package.json +9 -12
- package/tool/Tool.cjs +2 -2
- package/tool/Tool.d.ts +8 -8
- package/tool/Tool.js +2 -2
- package/tool/WebSearchTool.cjs +5 -4
- package/tool/WebSearchTool.d.ts +2 -22
- package/tool/WebSearchTool.js +5 -4
- package/tool/executeTool.cjs +1 -1
- package/tool/executeTool.d.ts +1 -2
- package/tool/executeTool.js +1 -1
- package/tool/useTool.cjs +3 -3
- package/tool/useTool.d.ts +3 -3
- package/tool/useTool.js +3 -3
- package/tool/useToolOrGenerateText.cjs +7 -7
- package/tool/useToolOrGenerateText.d.ts +2 -2
- package/tool/useToolOrGenerateText.js +7 -7
- package/util/getErrorMessage.cjs +13 -0
- package/util/getErrorMessage.d.ts +1 -0
- package/util/getErrorMessage.js +9 -0
- package/model-function/generate-json/InstructionWithSchemaPrompt.cjs +0 -17
- package/model-function/generate-json/InstructionWithSchemaPrompt.d.ts +0 -26
- package/model-function/generate-json/InstructionWithSchemaPrompt.js +0 -14
- package/model-function/generate-json/JsonGenerationEvent.d.ts +0 -23
- package/model-function/generate-json/JsonGenerationModel.d.ts +0 -13
- package/model-function/generate-json/JsonOrTextGenerationModel.d.ts +0 -23
- package/model-function/generate-json/JsonTextGenerationModel.d.ts +0 -26
- package/model-function/generate-json/NoSuchSchemaError.cjs +0 -17
- package/model-function/generate-json/NoSuchSchemaError.d.ts +0 -4
- package/model-function/generate-json/NoSuchSchemaError.js +0 -13
- package/model-function/generate-json/SchemaDefinition.d.ts +0 -6
- package/model-function/generate-json/SchemaValidationError.cjs +0 -36
- package/model-function/generate-json/SchemaValidationError.d.ts +0 -11
- package/model-function/generate-json/SchemaValidationError.js +0 -32
- package/model-function/generate-json/generateJson.cjs +0 -29
- package/model-function/generate-json/generateJson.d.ts +0 -5
- package/model-function/generate-json/generateJson.js +0 -25
- package/model-function/generate-json/generateJsonOrText.cjs +0 -42
- package/model-function/generate-json/generateJsonOrText.d.ts +0 -22
- package/model-function/generate-json/generateJsonOrText.js +0 -38
- /package/{model-function/generate-json/JsonGenerationEvent.cjs → core/structure/Schema.cjs} +0 -0
- /package/{model-function/generate-json/JsonGenerationEvent.js → core/structure/Schema.js} +0 -0
- /package/{model-function/generate-json/JsonGenerationModel.cjs → core/structure/StructureDefinition.cjs} +0 -0
- /package/{model-function/generate-json/JsonGenerationModel.js → core/structure/StructureDefinition.js} +0 -0
- /package/model-function/{generate-json/JsonOrTextGenerationModel.cjs → generate-structure/StructureGenerationEvent.cjs} +0 -0
- /package/model-function/{generate-json/JsonOrTextGenerationModel.js → generate-structure/StructureGenerationEvent.js} +0 -0
- /package/model-function/{generate-json/SchemaDefinition.cjs → generate-structure/StructureGenerationModel.cjs} +0 -0
- /package/model-function/{generate-json/SchemaDefinition.js → generate-structure/StructureGenerationModel.js} +0 -0
@@ -50,7 +50,7 @@ export class OpenAITextEmbeddingModel extends AbstractModel {
|
|
50
50
|
enumerable: true,
|
51
51
|
configurable: true,
|
52
52
|
writable: true,
|
53
|
-
value:
|
53
|
+
value: 2048
|
54
54
|
});
|
55
55
|
Object.defineProperty(this, "embeddingDimensions", {
|
56
56
|
enumerable: true,
|
@@ -82,7 +82,7 @@ export class OpenAITextEmbeddingModel extends AbstractModel {
|
|
82
82
|
async countTokens(input) {
|
83
83
|
return countTokens(this.tokenizer, input);
|
84
84
|
}
|
85
|
-
async callAPI(
|
85
|
+
async callAPI(texts, options) {
|
86
86
|
const run = options?.run;
|
87
87
|
const settings = options?.settings;
|
88
88
|
const combinedSettings = {
|
@@ -95,7 +95,7 @@ export class OpenAITextEmbeddingModel extends AbstractModel {
|
|
95
95
|
...combinedSettings,
|
96
96
|
// other settings:
|
97
97
|
abortSignal: run?.abortSignal,
|
98
|
-
input:
|
98
|
+
input: texts,
|
99
99
|
};
|
100
100
|
return callWithRetryAndThrottle({
|
101
101
|
retry: callSettings.api?.retry,
|
@@ -110,7 +110,7 @@ export class OpenAITextEmbeddingModel extends AbstractModel {
|
|
110
110
|
if (texts.length > this.maxTextsPerCall) {
|
111
111
|
throw new Error(`The OpenAI embedding API only supports ${this.maxTextsPerCall} texts per API call.`);
|
112
112
|
}
|
113
|
-
return this.callAPI(texts
|
113
|
+
return this.callAPI(texts, options);
|
114
114
|
}
|
115
115
|
extractEmbeddings(response) {
|
116
116
|
return [response.data[0].embedding];
|
@@ -121,13 +121,11 @@ export class OpenAITextEmbeddingModel extends AbstractModel {
|
|
121
121
|
}
|
122
122
|
const openAITextEmbeddingResponseSchema = z.object({
|
123
123
|
object: z.literal("list"),
|
124
|
-
data: z
|
125
|
-
.array(z.object({
|
124
|
+
data: z.array(z.object({
|
126
125
|
object: z.literal("embedding"),
|
127
126
|
embedding: z.array(z.number()),
|
128
127
|
index: z.number(),
|
129
|
-
}))
|
130
|
-
.length(1),
|
128
|
+
})),
|
131
129
|
model: z.string(),
|
132
130
|
usage: z.object({
|
133
131
|
prompt_tokens: z.number(),
|
@@ -245,7 +245,7 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
245
245
|
*
|
246
246
|
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
247
247
|
*/
|
248
|
-
|
248
|
+
generateStructureResponse(prompt, options) {
|
249
249
|
const settingsWithFunctionCall = Object.assign({}, options, {
|
250
250
|
functionCall: prompt.functionCall,
|
251
251
|
functions: prompt.functions,
|
@@ -257,7 +257,7 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
257
257
|
run: options?.run,
|
258
258
|
});
|
259
259
|
}
|
260
|
-
|
260
|
+
extractStructure(response) {
|
261
261
|
const jsonText = response.choices[0].message.function_call.arguments;
|
262
262
|
return secure_json_parse_1.default.parse(jsonText);
|
263
263
|
}
|
@@ -2,8 +2,8 @@ import z from "zod";
|
|
2
2
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
3
3
|
import { ModelFunctionOptions } from "../../../model-function/ModelFunctionOptions.js";
|
4
4
|
import { ApiConfiguration } from "../../../core/api/ApiConfiguration.js";
|
5
|
-
import {
|
6
|
-
import {
|
5
|
+
import { StructureGenerationModel } from "../../../model-function/generate-structure/StructureGenerationModel.js";
|
6
|
+
import { StructureOrTextGenerationModel } from "../../../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
7
7
|
import { DeltaEvent } from "../../../model-function/generate-text/DeltaEvent.js";
|
8
8
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../../model-function/generate-text/TextGenerationModel.js";
|
9
9
|
import { PromptFormat } from "../../../prompt/PromptFormat.js";
|
@@ -132,7 +132,7 @@ export interface OpenAIChatSettings extends TextGenerationModelSettings, Omit<Op
|
|
132
132
|
* ),
|
133
133
|
* ]);
|
134
134
|
*/
|
135
|
-
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>,
|
135
|
+
export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> implements TextGenerationModel<OpenAIChatMessage[], OpenAIChatResponse, OpenAIChatDelta, OpenAIChatSettings>, StructureGenerationModel<OpenAIChatSingleFunctionPrompt<unknown>, OpenAIChatResponse, OpenAIChatSettings>, StructureOrTextGenerationModel<OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, OpenAIChatResponse, OpenAIChatSettings> {
|
136
136
|
constructor(settings: OpenAIChatSettings);
|
137
137
|
readonly provider: "openai";
|
138
138
|
get modelName(): OpenAIChatModelType;
|
@@ -183,8 +183,8 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
183
183
|
*
|
184
184
|
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
185
185
|
*/
|
186
|
-
|
187
|
-
|
186
|
+
generateStructureResponse(prompt: OpenAIChatSingleFunctionPrompt<unknown> | OpenAIChatAutoFunctionPrompt<Array<OpenAIFunctionDescription<unknown>>>, options?: ModelFunctionOptions<OpenAIChatSettings> | undefined): PromiseLike<OpenAIChatResponse>;
|
187
|
+
extractStructure(response: OpenAIChatResponse): unknown;
|
188
188
|
extractUsage(response: OpenAIChatResponse): {
|
189
189
|
promptTokens: number;
|
190
190
|
completionTokens: number;
|
@@ -236,7 +236,7 @@ export class OpenAIChatModel extends AbstractModel {
|
|
236
236
|
*
|
237
237
|
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
238
238
|
*/
|
239
|
-
|
239
|
+
generateStructureResponse(prompt, options) {
|
240
240
|
const settingsWithFunctionCall = Object.assign({}, options, {
|
241
241
|
functionCall: prompt.functionCall,
|
242
242
|
functions: prompt.functions,
|
@@ -248,7 +248,7 @@ export class OpenAIChatModel extends AbstractModel {
|
|
248
248
|
run: options?.run,
|
249
249
|
});
|
250
250
|
}
|
251
|
-
|
251
|
+
extractStructure(response) {
|
252
252
|
const jsonText = response.choices[0].message.function_call.arguments;
|
253
253
|
return SecureJSON.parse(jsonText);
|
254
254
|
}
|
@@ -5,31 +5,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.OpenAIChatAutoFunctionPrompt = exports.OpenAIChatSingleFunctionPrompt = exports.OpenAIChatFunctionPrompt = void 0;
|
7
7
|
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
|
-
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
9
8
|
exports.OpenAIChatFunctionPrompt = {
|
10
9
|
forOpenAIFunctionDescription(options) {
|
11
10
|
return new OpenAIChatSingleFunctionPrompt(options);
|
12
11
|
},
|
13
|
-
|
12
|
+
forStructure({ messages, structure, }) {
|
14
13
|
return this.forOpenAIFunctionDescription({
|
15
14
|
messages,
|
16
15
|
fn: {
|
17
|
-
name:
|
18
|
-
description:
|
19
|
-
parameters:
|
16
|
+
name: structure.name,
|
17
|
+
description: structure.description,
|
18
|
+
parameters: structure.schema,
|
20
19
|
},
|
21
20
|
});
|
22
21
|
},
|
23
|
-
|
24
|
-
return (
|
25
|
-
messages,
|
26
|
-
schemaDefinition,
|
27
|
-
});
|
22
|
+
forStructureCurried(messages) {
|
23
|
+
return (structure) => this.forStructure({ messages, structure });
|
28
24
|
},
|
29
25
|
forTool({ messages, tool, }) {
|
30
|
-
return this.
|
26
|
+
return this.forStructure({
|
31
27
|
messages,
|
32
|
-
|
28
|
+
structure: tool.inputStructureDefinition,
|
33
29
|
});
|
34
30
|
},
|
35
31
|
forToolCurried(messages) {
|
@@ -38,23 +34,23 @@ exports.OpenAIChatFunctionPrompt = {
|
|
38
34
|
forOpenAIFunctionDescriptions(options) {
|
39
35
|
return new OpenAIChatAutoFunctionPrompt(options);
|
40
36
|
},
|
41
|
-
|
37
|
+
forStructures({ messages, structures, }) {
|
42
38
|
return this.forOpenAIFunctionDescriptions({
|
43
39
|
messages,
|
44
|
-
fns:
|
45
|
-
name:
|
46
|
-
description:
|
47
|
-
parameters:
|
40
|
+
fns: structures.map((structure) => ({
|
41
|
+
name: structure.name,
|
42
|
+
description: structure.description,
|
43
|
+
parameters: structure.schema,
|
48
44
|
})),
|
49
45
|
});
|
50
46
|
},
|
51
|
-
|
52
|
-
return (
|
47
|
+
forStructuresCurried(messages) {
|
48
|
+
return (structures) => this.forStructures({ messages, structures });
|
53
49
|
},
|
54
50
|
forTools({ messages, tools, }) {
|
55
|
-
return this.
|
51
|
+
return this.forStructures({
|
56
52
|
messages,
|
57
|
-
|
53
|
+
structures: tools.map((tool) => tool.inputStructureDefinition),
|
58
54
|
});
|
59
55
|
},
|
60
56
|
forToolsCurried(messages) {
|
@@ -86,7 +82,7 @@ class OpenAIChatSingleFunctionPrompt {
|
|
86
82
|
{
|
87
83
|
name: this.fn.name,
|
88
84
|
description: this.fn.description,
|
89
|
-
parameters:
|
85
|
+
parameters: this.fn.parameters.getJsonSchema(),
|
90
86
|
},
|
91
87
|
];
|
92
88
|
}
|
@@ -109,18 +105,18 @@ class OpenAIChatAutoFunctionPrompt {
|
|
109
105
|
this.messages = messages;
|
110
106
|
this.fns = fns;
|
111
107
|
}
|
112
|
-
|
108
|
+
extractStructureAndText(response) {
|
113
109
|
const message = response.choices[0].message;
|
114
110
|
const content = message.content;
|
115
111
|
const functionCall = message.function_call;
|
116
112
|
return functionCall == null
|
117
113
|
? {
|
118
|
-
|
114
|
+
structure: null,
|
119
115
|
value: null,
|
120
116
|
text: content ?? "",
|
121
117
|
}
|
122
118
|
: {
|
123
|
-
|
119
|
+
structure: functionCall.name,
|
124
120
|
value: secure_json_parse_1.default.parse(functionCall.arguments),
|
125
121
|
text: content,
|
126
122
|
};
|
@@ -132,7 +128,7 @@ class OpenAIChatAutoFunctionPrompt {
|
|
132
128
|
return this.fns.map((fn) => ({
|
133
129
|
name: fn.name,
|
134
130
|
description: fn.description,
|
135
|
-
parameters:
|
131
|
+
parameters: fn.parameters.getJsonSchema(),
|
136
132
|
}));
|
137
133
|
}
|
138
134
|
}
|
@@ -1,24 +1,24 @@
|
|
1
|
-
import
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import { Schema } from "../../../core/structure/Schema.js";
|
2
|
+
import { StructureDefinition } from "../../../core/structure/StructureDefinition.js";
|
3
|
+
import { StructureOrTextGenerationPrompt } from "../../../model-function/generate-structure/StructureOrTextGenerationModel.js";
|
4
4
|
import { Tool } from "../../../tool/Tool.js";
|
5
5
|
import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
|
6
6
|
import { OpenAIChatResponse } from "./OpenAIChatModel.js";
|
7
7
|
export type OpenAIFunctionDescription<T> = {
|
8
8
|
name: string;
|
9
9
|
description?: string;
|
10
|
-
parameters:
|
10
|
+
parameters: Schema<T>;
|
11
11
|
};
|
12
12
|
export declare const OpenAIChatFunctionPrompt: {
|
13
13
|
forOpenAIFunctionDescription<T>(options: {
|
14
14
|
messages: OpenAIChatMessage[];
|
15
15
|
fn: OpenAIFunctionDescription<T>;
|
16
16
|
}): OpenAIChatSingleFunctionPrompt<T>;
|
17
|
-
|
17
|
+
forStructure<STRUCTURE>({ messages, structure, }: {
|
18
18
|
messages: OpenAIChatMessage[];
|
19
|
-
|
19
|
+
structure: StructureDefinition<any, STRUCTURE>;
|
20
20
|
}): OpenAIChatSingleFunctionPrompt<STRUCTURE>;
|
21
|
-
|
21
|
+
forStructureCurried<STRUCTURE_1>(messages: OpenAIChatMessage[]): (structure: StructureDefinition<any, STRUCTURE_1>) => OpenAIChatSingleFunctionPrompt<STRUCTURE_1>;
|
22
22
|
forTool<INPUT, OUTPUT>({ messages, tool, }: {
|
23
23
|
messages: OpenAIChatMessage[];
|
24
24
|
tool: Tool<any, INPUT, OUTPUT>;
|
@@ -28,18 +28,18 @@ export declare const OpenAIChatFunctionPrompt: {
|
|
28
28
|
messages: OpenAIChatMessage[];
|
29
29
|
fns: FUNCTIONS;
|
30
30
|
}): OpenAIChatAutoFunctionPrompt<FUNCTIONS>;
|
31
|
-
|
31
|
+
forStructures<STRUCTURES extends StructureDefinition<any, any>[]>({ messages, structures, }: {
|
32
32
|
messages: OpenAIChatMessage[];
|
33
|
-
|
33
|
+
structures: STRUCTURES;
|
34
34
|
}): OpenAIChatAutoFunctionPrompt<{
|
35
35
|
name: any;
|
36
36
|
description: string | undefined;
|
37
|
-
parameters:
|
37
|
+
parameters: Schema<any>;
|
38
38
|
}[]>;
|
39
|
-
|
39
|
+
forStructuresCurried<STRUCTURE_2 extends StructureDefinition<any, any>[]>(messages: OpenAIChatMessage[]): (structures: STRUCTURE_2) => OpenAIChatAutoFunctionPrompt<{
|
40
40
|
name: any;
|
41
41
|
description: string | undefined;
|
42
|
-
parameters:
|
42
|
+
parameters: Schema<any>;
|
43
43
|
}[]>;
|
44
44
|
forTools<TOOLS extends Tool<any, any, any>[]>({ messages, tools, }: {
|
45
45
|
messages: OpenAIChatMessage[];
|
@@ -47,12 +47,12 @@ export declare const OpenAIChatFunctionPrompt: {
|
|
47
47
|
}): OpenAIChatAutoFunctionPrompt<{
|
48
48
|
name: any;
|
49
49
|
description: string | undefined;
|
50
|
-
parameters:
|
50
|
+
parameters: Schema<any>;
|
51
51
|
}[]>;
|
52
52
|
forToolsCurried<TOOLS_1 extends Tool<any, any, any>[]>(messages: OpenAIChatMessage[]): (tools: TOOLS_1) => OpenAIChatAutoFunctionPrompt<{
|
53
53
|
name: any;
|
54
54
|
description: string | undefined;
|
55
|
-
parameters:
|
55
|
+
parameters: Schema<any>;
|
56
56
|
}[]>;
|
57
57
|
};
|
58
58
|
export declare class OpenAIChatSingleFunctionPrompt<FUNCTION> {
|
@@ -68,27 +68,22 @@ export declare class OpenAIChatSingleFunctionPrompt<FUNCTION> {
|
|
68
68
|
get functions(): {
|
69
69
|
name: string;
|
70
70
|
description: string | undefined;
|
71
|
-
parameters:
|
72
|
-
$schema?: string | undefined;
|
73
|
-
definitions?: {
|
74
|
-
[key: string]: import("zod-to-json-schema/src/parseDef.js").JsonSchema7Type;
|
75
|
-
} | undefined;
|
76
|
-
};
|
71
|
+
parameters: unknown;
|
77
72
|
}[];
|
78
73
|
}
|
79
|
-
export declare class OpenAIChatAutoFunctionPrompt<FUNCTIONS extends Array<OpenAIFunctionDescription<any>>> implements
|
74
|
+
export declare class OpenAIChatAutoFunctionPrompt<FUNCTIONS extends Array<OpenAIFunctionDescription<any>>> implements StructureOrTextGenerationPrompt<OpenAIChatResponse> {
|
80
75
|
readonly messages: OpenAIChatMessage[];
|
81
76
|
readonly fns: FUNCTIONS;
|
82
77
|
constructor({ messages, fns, }: {
|
83
78
|
messages: OpenAIChatMessage[];
|
84
79
|
fns: FUNCTIONS;
|
85
80
|
});
|
86
|
-
|
87
|
-
|
81
|
+
extractStructureAndText(response: OpenAIChatResponse): {
|
82
|
+
structure: null;
|
88
83
|
value: null;
|
89
84
|
text: string;
|
90
85
|
} | {
|
91
|
-
|
86
|
+
structure: string;
|
92
87
|
value: any;
|
93
88
|
text: string | null;
|
94
89
|
};
|
@@ -96,11 +91,6 @@ export declare class OpenAIChatAutoFunctionPrompt<FUNCTIONS extends Array<OpenAI
|
|
96
91
|
get functions(): {
|
97
92
|
name: string;
|
98
93
|
description: string | undefined;
|
99
|
-
parameters:
|
100
|
-
$schema?: string | undefined;
|
101
|
-
definitions?: {
|
102
|
-
[key: string]: import("zod-to-json-schema/src/parseDef.js").JsonSchema7Type;
|
103
|
-
} | undefined;
|
104
|
-
};
|
94
|
+
parameters: unknown;
|
105
95
|
}[];
|
106
96
|
}
|
@@ -1,29 +1,25 @@
|
|
1
1
|
import SecureJSON from "secure-json-parse";
|
2
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
3
2
|
export const OpenAIChatFunctionPrompt = {
|
4
3
|
forOpenAIFunctionDescription(options) {
|
5
4
|
return new OpenAIChatSingleFunctionPrompt(options);
|
6
5
|
},
|
7
|
-
|
6
|
+
forStructure({ messages, structure, }) {
|
8
7
|
return this.forOpenAIFunctionDescription({
|
9
8
|
messages,
|
10
9
|
fn: {
|
11
|
-
name:
|
12
|
-
description:
|
13
|
-
parameters:
|
10
|
+
name: structure.name,
|
11
|
+
description: structure.description,
|
12
|
+
parameters: structure.schema,
|
14
13
|
},
|
15
14
|
});
|
16
15
|
},
|
17
|
-
|
18
|
-
return (
|
19
|
-
messages,
|
20
|
-
schemaDefinition,
|
21
|
-
});
|
16
|
+
forStructureCurried(messages) {
|
17
|
+
return (structure) => this.forStructure({ messages, structure });
|
22
18
|
},
|
23
19
|
forTool({ messages, tool, }) {
|
24
|
-
return this.
|
20
|
+
return this.forStructure({
|
25
21
|
messages,
|
26
|
-
|
22
|
+
structure: tool.inputStructureDefinition,
|
27
23
|
});
|
28
24
|
},
|
29
25
|
forToolCurried(messages) {
|
@@ -32,23 +28,23 @@ export const OpenAIChatFunctionPrompt = {
|
|
32
28
|
forOpenAIFunctionDescriptions(options) {
|
33
29
|
return new OpenAIChatAutoFunctionPrompt(options);
|
34
30
|
},
|
35
|
-
|
31
|
+
forStructures({ messages, structures, }) {
|
36
32
|
return this.forOpenAIFunctionDescriptions({
|
37
33
|
messages,
|
38
|
-
fns:
|
39
|
-
name:
|
40
|
-
description:
|
41
|
-
parameters:
|
34
|
+
fns: structures.map((structure) => ({
|
35
|
+
name: structure.name,
|
36
|
+
description: structure.description,
|
37
|
+
parameters: structure.schema,
|
42
38
|
})),
|
43
39
|
});
|
44
40
|
},
|
45
|
-
|
46
|
-
return (
|
41
|
+
forStructuresCurried(messages) {
|
42
|
+
return (structures) => this.forStructures({ messages, structures });
|
47
43
|
},
|
48
44
|
forTools({ messages, tools, }) {
|
49
|
-
return this.
|
45
|
+
return this.forStructures({
|
50
46
|
messages,
|
51
|
-
|
47
|
+
structures: tools.map((tool) => tool.inputStructureDefinition),
|
52
48
|
});
|
53
49
|
},
|
54
50
|
forToolsCurried(messages) {
|
@@ -80,7 +76,7 @@ export class OpenAIChatSingleFunctionPrompt {
|
|
80
76
|
{
|
81
77
|
name: this.fn.name,
|
82
78
|
description: this.fn.description,
|
83
|
-
parameters:
|
79
|
+
parameters: this.fn.parameters.getJsonSchema(),
|
84
80
|
},
|
85
81
|
];
|
86
82
|
}
|
@@ -102,18 +98,18 @@ export class OpenAIChatAutoFunctionPrompt {
|
|
102
98
|
this.messages = messages;
|
103
99
|
this.fns = fns;
|
104
100
|
}
|
105
|
-
|
101
|
+
extractStructureAndText(response) {
|
106
102
|
const message = response.choices[0].message;
|
107
103
|
const content = message.content;
|
108
104
|
const functionCall = message.function_call;
|
109
105
|
return functionCall == null
|
110
106
|
? {
|
111
|
-
|
107
|
+
structure: null,
|
112
108
|
value: null,
|
113
109
|
text: content ?? "",
|
114
110
|
}
|
115
111
|
: {
|
116
|
-
|
112
|
+
structure: functionCall.name,
|
117
113
|
value: SecureJSON.parse(functionCall.arguments),
|
118
114
|
text: content,
|
119
115
|
};
|
@@ -125,7 +121,7 @@ export class OpenAIChatAutoFunctionPrompt {
|
|
125
121
|
return this.fns.map((fn) => ({
|
126
122
|
name: fn.name,
|
127
123
|
description: fn.description,
|
128
|
-
parameters:
|
124
|
+
parameters: fn.parameters.getJsonSchema(),
|
129
125
|
}));
|
130
126
|
}
|
131
127
|
}
|
@@ -9,6 +9,6 @@ export * from "./OpenAITranscriptionModel.js";
|
|
9
9
|
export * from "./TikTokenTokenizer.js";
|
10
10
|
export * from "./chat/OpenAIChatMessage.js";
|
11
11
|
export * from "./chat/OpenAIChatModel.js";
|
12
|
-
export { OpenAIChatFunctionPrompt
|
12
|
+
export { OpenAIChatFunctionPrompt } from "./chat/OpenAIChatPrompt.js";
|
13
13
|
export { OpenAIChatDelta } from "./chat/OpenAIChatStreamIterable.js";
|
14
14
|
export * from "./chat/countOpenAIChatMessageTokens.js";
|
@@ -9,5 +9,5 @@ export * from "./OpenAITranscriptionModel.js";
|
|
9
9
|
export * from "./TikTokenTokenizer.js";
|
10
10
|
export * from "./chat/OpenAIChatMessage.js";
|
11
11
|
export * from "./chat/OpenAIChatModel.js";
|
12
|
-
export { OpenAIChatFunctionPrompt
|
12
|
+
export { OpenAIChatFunctionPrompt } from "./chat/OpenAIChatPrompt.js";
|
13
13
|
export * from "./chat/countOpenAIChatMessageTokens.js";
|
package/package.json
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
|
-
"description": "Build
|
4
|
-
"version": "0.
|
3
|
+
"description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
+
"version": "0.30.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
8
8
|
"llm",
|
9
9
|
"embedding",
|
10
10
|
"openai",
|
11
|
-
"cohere",
|
12
11
|
"huggingface",
|
13
|
-
"stability-ai",
|
14
12
|
"gpt-3",
|
15
13
|
"gpt-4",
|
16
14
|
"whisper",
|
17
15
|
"chatbot",
|
18
|
-
"ai"
|
16
|
+
"ai",
|
17
|
+
"multimodal"
|
19
18
|
],
|
20
19
|
"homepage": "https://modelfusion.dev/",
|
21
20
|
"repository": {
|
@@ -55,7 +54,9 @@
|
|
55
54
|
"eventsource-parser": "1.0.0",
|
56
55
|
"js-tiktoken": "1.0.7",
|
57
56
|
"nanoid": "3.3.6",
|
58
|
-
"secure-json-parse": "2.7.0"
|
57
|
+
"secure-json-parse": "2.7.0",
|
58
|
+
"zod": "3.22.2",
|
59
|
+
"zod-to-json-schema": "3.21.4"
|
59
60
|
},
|
60
61
|
"devDependencies": {
|
61
62
|
"@pinecone-database/pinecone": "^0.1.6",
|
@@ -70,14 +71,10 @@
|
|
70
71
|
"lint-staged": "14.0.1",
|
71
72
|
"prettier": "3.0.3",
|
72
73
|
"rimraf": "5.0.1",
|
73
|
-
"typescript": "5.1.6"
|
74
|
-
"zod": "3.22.2",
|
75
|
-
"zod-to-json-schema": "3.21.4"
|
74
|
+
"typescript": "5.1.6"
|
76
75
|
},
|
77
76
|
"peerDependencies": {
|
78
|
-
"@pinecone-database/pinecone": "*"
|
79
|
-
"zod": "^3",
|
80
|
-
"zod-to-json-schema": "^3"
|
77
|
+
"@pinecone-database/pinecone": "*"
|
81
78
|
},
|
82
79
|
"peerDependenciesMeta": {
|
83
80
|
"@pinecone-database/pinecone": {
|
package/tool/Tool.cjs
CHANGED
@@ -71,10 +71,10 @@ class Tool {
|
|
71
71
|
this.execute = execute;
|
72
72
|
}
|
73
73
|
/**
|
74
|
-
* Provdes a
|
74
|
+
* Provdes a structure definition with the name, description and schema of the input.
|
75
75
|
* This is used by `useTool`.
|
76
76
|
*/
|
77
|
-
get
|
77
|
+
get inputStructureDefinition() {
|
78
78
|
return {
|
79
79
|
name: this.name,
|
80
80
|
description: this.description,
|
package/tool/Tool.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { z } from "zod";
|
2
|
-
import { SchemaDefinition } from "../model-function/generate-json/SchemaDefinition.js";
|
3
1
|
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
+
import { Schema } from "../core/structure/Schema.js";
|
3
|
+
import { StructureDefinition } from "../core/structure/StructureDefinition.js";
|
4
4
|
/**
|
5
5
|
* A tool is a function with a name, description and defined inputs that can be used
|
6
6
|
* by agents and chatbots.
|
@@ -20,11 +20,11 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
|
20
20
|
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
21
21
|
* Use descriptions to make the input understandable for the language model.
|
22
22
|
*/
|
23
|
-
readonly inputSchema:
|
23
|
+
readonly inputSchema: Schema<INPUT>;
|
24
24
|
/**
|
25
25
|
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
26
26
|
*/
|
27
|
-
readonly outputSchema?:
|
27
|
+
readonly outputSchema?: Schema<OUTPUT>;
|
28
28
|
/**
|
29
29
|
* The actual execution function of the tool.
|
30
30
|
*/
|
@@ -32,13 +32,13 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
|
32
32
|
constructor({ name, description, inputSchema, outputSchema, execute, }: {
|
33
33
|
name: NAME;
|
34
34
|
description: string;
|
35
|
-
inputSchema:
|
36
|
-
outputSchema?:
|
35
|
+
inputSchema: Schema<INPUT>;
|
36
|
+
outputSchema?: Schema<OUTPUT>;
|
37
37
|
execute(input: INPUT, options?: FunctionOptions): PromiseLike<OUTPUT>;
|
38
38
|
});
|
39
39
|
/**
|
40
|
-
* Provdes a
|
40
|
+
* Provdes a structure definition with the name, description and schema of the input.
|
41
41
|
* This is used by `useTool`.
|
42
42
|
*/
|
43
|
-
get
|
43
|
+
get inputStructureDefinition(): StructureDefinition<NAME, INPUT>;
|
44
44
|
}
|
package/tool/Tool.js
CHANGED
@@ -68,10 +68,10 @@ export class Tool {
|
|
68
68
|
this.execute = execute;
|
69
69
|
}
|
70
70
|
/**
|
71
|
-
* Provdes a
|
71
|
+
* Provdes a structure definition with the name, description and schema of the input.
|
72
72
|
* This is used by `useTool`.
|
73
73
|
*/
|
74
|
-
get
|
74
|
+
get inputStructureDefinition() {
|
75
75
|
return {
|
76
76
|
name: this.name,
|
77
77
|
description: this.description,
|
package/tool/WebSearchTool.cjs
CHANGED
@@ -2,20 +2,21 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.WebSearchTool = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
|
+
const ZodSchema_js_1 = require("../core/structure/ZodSchema.cjs");
|
5
6
|
const Tool_js_1 = require("./Tool.cjs");
|
6
|
-
const OUTPUT_SCHEMA = zod_1.z.object({
|
7
|
+
const OUTPUT_SCHEMA = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
7
8
|
results: zod_1.z.array(zod_1.z.object({
|
8
9
|
title: zod_1.z.string(),
|
9
10
|
link: zod_1.z.string().url(),
|
10
11
|
snippet: zod_1.z.string(),
|
11
12
|
})),
|
12
|
-
});
|
13
|
+
}));
|
13
14
|
// expose the schemas to library consumers:
|
14
15
|
const createInputSchema = (description) =>
|
15
16
|
// same structure, but with description:
|
16
|
-
zod_1.z.object({
|
17
|
+
new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
17
18
|
query: zod_1.z.string().describe(description),
|
18
|
-
});
|
19
|
+
}));
|
19
20
|
/**
|
20
21
|
* A tool for searching the web.
|
21
22
|
*
|
package/tool/WebSearchTool.d.ts
CHANGED
@@ -1,27 +1,7 @@
|
|
1
|
-
import { z } from "zod";
|
2
1
|
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
+
import { ZodSchema } from "../core/structure/ZodSchema.js";
|
3
3
|
import { Tool } from "./Tool.js";
|
4
|
-
declare const OUTPUT_SCHEMA:
|
5
|
-
results: z.ZodArray<z.ZodObject<{
|
6
|
-
title: z.ZodString;
|
7
|
-
link: z.ZodString;
|
8
|
-
snippet: z.ZodString;
|
9
|
-
}, "strip", z.ZodTypeAny, {
|
10
|
-
link: string;
|
11
|
-
title: string;
|
12
|
-
snippet: string;
|
13
|
-
}, {
|
14
|
-
link: string;
|
15
|
-
title: string;
|
16
|
-
snippet: string;
|
17
|
-
}>, "many">;
|
18
|
-
}, "strip", z.ZodTypeAny, {
|
19
|
-
results: {
|
20
|
-
link: string;
|
21
|
-
title: string;
|
22
|
-
snippet: string;
|
23
|
-
}[];
|
24
|
-
}, {
|
4
|
+
declare const OUTPUT_SCHEMA: ZodSchema<{
|
25
5
|
results: {
|
26
6
|
link: string;
|
27
7
|
title: string;
|