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
package/README.md
CHANGED
@@ -29,12 +29,6 @@ ModelFusion is a library for building AI apps, chatbots, and agents. It provides
|
|
29
29
|
npm install modelfusion
|
30
30
|
```
|
31
31
|
|
32
|
-
You need to install `zod` and a matching version of `zod-to-json-schema` (peer dependencies):
|
33
|
-
|
34
|
-
```sh
|
35
|
-
npm install zod zod-to-json-schema
|
36
|
-
```
|
37
|
-
|
38
32
|
Or use a template: [ModelFusion terminal app starter](https://github.com/lgrammel/modelfusion-terminal-app-starter)
|
39
33
|
|
40
34
|
## Usage Examples
|
@@ -141,27 +135,27 @@ for (const choice of response.choices) {
|
|
141
135
|
console.log(`Duration: ${metadata.durationInMs}ms`);
|
142
136
|
```
|
143
137
|
|
144
|
-
### [Generate
|
138
|
+
### [Generate Structure](https://modelfusion.dev/guide/function/generate-structure)
|
145
139
|
|
146
|
-
Generate
|
140
|
+
Generate a structure that matches a schema.
|
147
141
|
|
148
142
|
```ts
|
149
|
-
const
|
143
|
+
const sentiment = await generateStructure(
|
150
144
|
new OpenAIChatModel({
|
151
145
|
model: "gpt-3.5-turbo",
|
152
146
|
temperature: 0,
|
153
147
|
maxCompletionTokens: 50,
|
154
148
|
}),
|
155
|
-
{
|
156
|
-
name: "sentiment"
|
149
|
+
new ZodStructureDefinition({
|
150
|
+
name: "sentiment",
|
157
151
|
description: "Write the sentiment analysis",
|
158
152
|
schema: z.object({
|
159
153
|
sentiment: z
|
160
154
|
.enum(["positive", "neutral", "negative"])
|
161
155
|
.describe("Sentiment."),
|
162
156
|
}),
|
163
|
-
},
|
164
|
-
OpenAIChatFunctionPrompt.
|
157
|
+
}),
|
158
|
+
OpenAIChatFunctionPrompt.forStructureCurried([
|
165
159
|
OpenAIChatMessage.system(
|
166
160
|
"You are a sentiment evaluator. " +
|
167
161
|
"Analyze the sentiment of the following product review:"
|
@@ -176,19 +170,16 @@ const value = await generateJson(
|
|
176
170
|
|
177
171
|
Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
|
178
172
|
|
179
|
-
### [Generate
|
173
|
+
### [Generate Structure or Text](https://modelfusion.dev/guide/function/generate-structure-or-text)
|
180
174
|
|
181
|
-
Generate
|
175
|
+
Generate a structure (or text as a fallback) using a prompt and multiple schemas.
|
182
176
|
It either matches one of the schemas or is text reponse.
|
183
177
|
|
184
178
|
```ts
|
185
|
-
const {
|
186
|
-
new OpenAIChatModel({
|
187
|
-
model: "gpt-3.5-turbo",
|
188
|
-
maxCompletionTokens: 1000,
|
189
|
-
}),
|
179
|
+
const { structure, value, text } = await generateStructureOrText(
|
180
|
+
new OpenAIChatModel({ model: "gpt-3.5-turbo", maxCompletionTokens: 1000 }),
|
190
181
|
[
|
191
|
-
{
|
182
|
+
new ZodStructureDefinition({
|
192
183
|
name: "getCurrentWeather" as const, // mark 'as const' for type inference
|
193
184
|
description: "Get the current weather in a given location",
|
194
185
|
schema: z.object({
|
@@ -197,16 +188,16 @@ const { schema, value, text } = await generateJsonOrText(
|
|
197
188
|
.describe("The city and state, e.g. San Francisco, CA"),
|
198
189
|
unit: z.enum(["celsius", "fahrenheit"]).optional(),
|
199
190
|
}),
|
200
|
-
},
|
201
|
-
{
|
191
|
+
}),
|
192
|
+
new ZodStructureDefinition({
|
202
193
|
name: "getContactInformation" as const,
|
203
194
|
description: "Get the contact information for a given person",
|
204
195
|
schema: z.object({
|
205
196
|
name: z.string().describe("The name of the person"),
|
206
197
|
}),
|
207
|
-
},
|
198
|
+
}),
|
208
199
|
],
|
209
|
-
OpenAIChatFunctionPrompt.
|
200
|
+
OpenAIChatFunctionPrompt.forStructuresCurried([OpenAIChatMessage.user(query)])
|
210
201
|
);
|
211
202
|
```
|
212
203
|
|
@@ -227,11 +218,15 @@ const calculator = new Tool({
|
|
227
218
|
name: "calculator",
|
228
219
|
description: "Execute a calculation",
|
229
220
|
|
230
|
-
inputSchema:
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
221
|
+
inputSchema: new ZodSchema(
|
222
|
+
z.object({
|
223
|
+
a: z.number().describe("The first number."),
|
224
|
+
b: z.number().describe("The second number."),
|
225
|
+
operator: z
|
226
|
+
.enum(["+", "-", "*", "/"])
|
227
|
+
.describe("The operator (+, -, *, /)."),
|
228
|
+
})
|
229
|
+
),
|
235
230
|
|
236
231
|
execute: async ({ a, b, operator }) => {
|
237
232
|
switch (operator) {
|
@@ -421,8 +416,8 @@ Integrations: [Helicone](https://modelfusion.dev/integration/observability/helic
|
|
421
416
|
|
422
417
|
- [Model Functions](https://modelfusion.dev/guide/function/)
|
423
418
|
- [Generate and stream text](https://modelfusion.dev/guide/function/generate-text)
|
424
|
-
- [Generate
|
425
|
-
- [Generate
|
419
|
+
- [Generate structure](https://modelfusion.dev/guide/function/generate-structure)
|
420
|
+
- [Generate structure or text](https://modelfusion.dev/guide/function/generate-structure-or-text)
|
426
421
|
- [Embed Text](https://modelfusion.dev/guide/function/embed-text)
|
427
422
|
- [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
|
428
423
|
- [Transcribe Speech](https://modelfusion.dev/guide/function/transcribe-speech)
|
package/core/index.cjs
CHANGED
@@ -24,3 +24,4 @@ __exportStar(require("./GlobalFunctionObservers.cjs"), exports);
|
|
24
24
|
__exportStar(require("./Run.cjs"), exports);
|
25
25
|
__exportStar(require("./Vector.cjs"), exports);
|
26
26
|
__exportStar(require("./api/index.cjs"), exports);
|
27
|
+
__exportStar(require("./structure/index.cjs"), exports);
|
package/core/index.d.ts
CHANGED
package/core/index.js
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
/**
|
2
|
+
* Schema defines a structure for a JSON object.
|
3
|
+
*/
|
4
|
+
export interface Schema<STRUCTURE> {
|
5
|
+
/**
|
6
|
+
* Validates that the structure of `value` matches the structure of this schema,
|
7
|
+
* and returns a typed version of value if it does.
|
8
|
+
*/
|
9
|
+
validate(value: unknown): {
|
10
|
+
success: true;
|
11
|
+
value: STRUCTURE;
|
12
|
+
} | {
|
13
|
+
success: false;
|
14
|
+
error: unknown;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* Returns the JSON schema for this schema. The schema has to be a valid JSON schema in object form.
|
18
|
+
*/
|
19
|
+
getJsonSchema(): unknown;
|
20
|
+
/**
|
21
|
+
* Only used for type inference.
|
22
|
+
*/
|
23
|
+
readonly _type: STRUCTURE;
|
24
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.UncheckedJsonSchemaSchema = void 0;
|
4
|
+
class UncheckedJsonSchemaSchema {
|
5
|
+
constructor(jsonSchema) {
|
6
|
+
Object.defineProperty(this, "jsonSchema", {
|
7
|
+
enumerable: true,
|
8
|
+
configurable: true,
|
9
|
+
writable: true,
|
10
|
+
value: jsonSchema
|
11
|
+
});
|
12
|
+
Object.defineProperty(this, "_type", {
|
13
|
+
enumerable: true,
|
14
|
+
configurable: true,
|
15
|
+
writable: true,
|
16
|
+
value: void 0
|
17
|
+
});
|
18
|
+
}
|
19
|
+
validate(value) {
|
20
|
+
return { success: true, value: value };
|
21
|
+
}
|
22
|
+
getJsonSchema() {
|
23
|
+
return this.jsonSchema;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
exports.UncheckedJsonSchemaSchema = UncheckedJsonSchemaSchema;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { Schema } from "./Schema.js";
|
2
|
+
export declare class UncheckedJsonSchemaSchema<STRUCTURE> implements Schema<STRUCTURE> {
|
3
|
+
private readonly jsonSchema;
|
4
|
+
constructor(jsonSchema: unknown);
|
5
|
+
validate(value: unknown): {
|
6
|
+
success: true;
|
7
|
+
value: STRUCTURE;
|
8
|
+
} | {
|
9
|
+
success: false;
|
10
|
+
error: unknown;
|
11
|
+
};
|
12
|
+
getJsonSchema(): unknown;
|
13
|
+
readonly _type: STRUCTURE;
|
14
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
export class UncheckedJsonSchemaSchema {
|
2
|
+
constructor(jsonSchema) {
|
3
|
+
Object.defineProperty(this, "jsonSchema", {
|
4
|
+
enumerable: true,
|
5
|
+
configurable: true,
|
6
|
+
writable: true,
|
7
|
+
value: jsonSchema
|
8
|
+
});
|
9
|
+
Object.defineProperty(this, "_type", {
|
10
|
+
enumerable: true,
|
11
|
+
configurable: true,
|
12
|
+
writable: true,
|
13
|
+
value: void 0
|
14
|
+
});
|
15
|
+
}
|
16
|
+
validate(value) {
|
17
|
+
return { success: true, value: value };
|
18
|
+
}
|
19
|
+
getJsonSchema() {
|
20
|
+
return this.jsonSchema;
|
21
|
+
}
|
22
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.UncheckedJsonSchemaStructureDefinition = void 0;
|
4
|
+
const UncheckedJsonSchemaSchema_js_1 = require("./UncheckedJsonSchemaSchema.cjs");
|
5
|
+
class UncheckedJsonSchemaStructureDefinition {
|
6
|
+
constructor({ name, description, jsonSchema, }) {
|
7
|
+
Object.defineProperty(this, "name", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "description", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
Object.defineProperty(this, "schema", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
this.name = name;
|
26
|
+
this.description = description;
|
27
|
+
this.schema = new UncheckedJsonSchemaSchema_js_1.UncheckedJsonSchemaSchema(jsonSchema);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.UncheckedJsonSchemaStructureDefinition = UncheckedJsonSchemaStructureDefinition;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { Schema } from "./Schema.js";
|
2
|
+
import { StructureDefinition } from "./StructureDefinition.js";
|
3
|
+
export declare class UncheckedJsonSchemaStructureDefinition<NAME extends string, STRUCTURE> implements StructureDefinition<NAME, STRUCTURE> {
|
4
|
+
name: NAME;
|
5
|
+
description?: string;
|
6
|
+
schema: Schema<STRUCTURE>;
|
7
|
+
constructor({ name, description, jsonSchema, }: {
|
8
|
+
name: NAME;
|
9
|
+
description?: string;
|
10
|
+
jsonSchema: unknown;
|
11
|
+
});
|
12
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { UncheckedJsonSchemaSchema } from "./UncheckedJsonSchemaSchema.js";
|
2
|
+
export class UncheckedJsonSchemaStructureDefinition {
|
3
|
+
constructor({ name, description, jsonSchema, }) {
|
4
|
+
Object.defineProperty(this, "name", {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value: void 0
|
9
|
+
});
|
10
|
+
Object.defineProperty(this, "description", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
Object.defineProperty(this, "schema", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: void 0
|
21
|
+
});
|
22
|
+
this.name = name;
|
23
|
+
this.description = description;
|
24
|
+
this.schema = new UncheckedJsonSchemaSchema(jsonSchema);
|
25
|
+
}
|
26
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ZodSchema = void 0;
|
4
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
5
|
+
class ZodSchema {
|
6
|
+
constructor(zodSchema) {
|
7
|
+
Object.defineProperty(this, "zodSchema", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "_type", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
this.zodSchema = zodSchema;
|
20
|
+
}
|
21
|
+
validate(value) {
|
22
|
+
const parseResult = this.zodSchema.safeParse(value);
|
23
|
+
return parseResult.success
|
24
|
+
? { success: true, value: parseResult.data }
|
25
|
+
: parseResult;
|
26
|
+
}
|
27
|
+
getJsonSchema() {
|
28
|
+
return (0, zod_to_json_schema_1.zodToJsonSchema)(this.zodSchema);
|
29
|
+
}
|
30
|
+
}
|
31
|
+
exports.ZodSchema = ZodSchema;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { Schema } from "./Schema.js";
|
3
|
+
export declare class ZodSchema<STRUCTURE> implements Schema<STRUCTURE> {
|
4
|
+
readonly zodSchema: z.Schema<STRUCTURE>;
|
5
|
+
constructor(zodSchema: z.Schema<STRUCTURE>);
|
6
|
+
validate(value: unknown): {
|
7
|
+
success: true;
|
8
|
+
value: STRUCTURE;
|
9
|
+
} | {
|
10
|
+
success: false;
|
11
|
+
error: unknown;
|
12
|
+
};
|
13
|
+
getJsonSchema(): unknown;
|
14
|
+
readonly _type: STRUCTURE;
|
15
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
2
|
+
export class ZodSchema {
|
3
|
+
constructor(zodSchema) {
|
4
|
+
Object.defineProperty(this, "zodSchema", {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value: void 0
|
9
|
+
});
|
10
|
+
Object.defineProperty(this, "_type", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
this.zodSchema = zodSchema;
|
17
|
+
}
|
18
|
+
validate(value) {
|
19
|
+
const parseResult = this.zodSchema.safeParse(value);
|
20
|
+
return parseResult.success
|
21
|
+
? { success: true, value: parseResult.data }
|
22
|
+
: parseResult;
|
23
|
+
}
|
24
|
+
getJsonSchema() {
|
25
|
+
return zodToJsonSchema(this.zodSchema);
|
26
|
+
}
|
27
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ZodStructureDefinition = void 0;
|
4
|
+
const ZodSchema_js_1 = require("./ZodSchema.cjs");
|
5
|
+
class ZodStructureDefinition {
|
6
|
+
constructor({ name, description, schema, }) {
|
7
|
+
Object.defineProperty(this, "name", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "description", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
Object.defineProperty(this, "schema", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
this.name = name;
|
26
|
+
this.description = description;
|
27
|
+
this.schema = new ZodSchema_js_1.ZodSchema(schema);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.ZodStructureDefinition = ZodStructureDefinition;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { Schema } from "./Schema.js";
|
3
|
+
import { StructureDefinition } from "./StructureDefinition.js";
|
4
|
+
export declare class ZodStructureDefinition<NAME extends string, STRUCTURE> implements StructureDefinition<NAME, STRUCTURE> {
|
5
|
+
name: NAME;
|
6
|
+
description?: string;
|
7
|
+
schema: Schema<STRUCTURE>;
|
8
|
+
constructor({ name, description, schema, }: {
|
9
|
+
name: NAME;
|
10
|
+
description?: string;
|
11
|
+
schema: z.Schema<STRUCTURE>;
|
12
|
+
});
|
13
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { ZodSchema } from "./ZodSchema.js";
|
2
|
+
export class ZodStructureDefinition {
|
3
|
+
constructor({ name, description, schema, }) {
|
4
|
+
Object.defineProperty(this, "name", {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value: void 0
|
9
|
+
});
|
10
|
+
Object.defineProperty(this, "description", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
Object.defineProperty(this, "schema", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: void 0
|
21
|
+
});
|
22
|
+
this.name = name;
|
23
|
+
this.description = description;
|
24
|
+
this.schema = new ZodSchema(schema);
|
25
|
+
}
|
26
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./Schema.cjs"), exports);
|
18
|
+
__exportStar(require("./StructureDefinition.cjs"), exports);
|
19
|
+
__exportStar(require("./UncheckedJsonSchemaSchema.cjs"), exports);
|
20
|
+
__exportStar(require("./UncheckedJsonSchemaStructureDefinition.cjs"), exports);
|
21
|
+
__exportStar(require("./ZodSchema.cjs"), exports);
|
22
|
+
__exportStar(require("./ZodStructureDefinition.cjs"), exports);
|
@@ -3,7 +3,7 @@ import { ModelInformation } from "./ModelInformation.js";
|
|
3
3
|
import { ImageDescriptionFinishedEvent, ImageDescriptionStartedEvent } from "./describe-image/ImageDescriptionEvent.js";
|
4
4
|
import { TextEmbeddingFinishedEvent, TextEmbeddingStartedEvent } from "./embed-text/TextEmbeddingEvent.js";
|
5
5
|
import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
|
6
|
-
import {
|
6
|
+
import { StructureGenerationFinishedEvent, StructureGenerationStartedEvent } from "./generate-structure/StructureGenerationEvent.js";
|
7
7
|
import { TextGenerationFinishedEvent, TextGenerationStartedEvent } from "./generate-text/TextGenerationEvent.js";
|
8
8
|
import { TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextStreamingEvent.js";
|
9
9
|
import { SpeechSynthesisFinishedEvent, SpeechSynthesisStartedEvent } from "./synthesize-speech/SpeechSynthesisEvent.js";
|
@@ -48,5 +48,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
|
|
48
48
|
*/
|
49
49
|
result: BaseModelCallFinishedEventResult;
|
50
50
|
}
|
51
|
-
export type ModelCallStartedEvent = ImageDescriptionStartedEvent | ImageGenerationStartedEvent |
|
52
|
-
export type ModelCallFinishedEvent = ImageDescriptionFinishedEvent | ImageGenerationFinishedEvent |
|
51
|
+
export type ModelCallStartedEvent = ImageDescriptionStartedEvent | ImageGenerationStartedEvent | StructureGenerationStartedEvent | SpeechSynthesisStartedEvent | TextEmbeddingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
|
52
|
+
export type ModelCallFinishedEvent = ImageDescriptionFinishedEvent | ImageGenerationFinishedEvent | StructureGenerationFinishedEvent | SpeechSynthesisFinishedEvent | TextEmbeddingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { FunctionEvent } from "../core/FunctionEvent.js";
|
2
2
|
import { ModelInformation } from "./ModelInformation.js";
|
3
3
|
export type SuccessfulModelCall = {
|
4
|
-
type: "image-description" | "image-generation" | "
|
4
|
+
type: "image-description" | "image-generation" | "structure-generation" | "structure-or-text-generation" | "speech-synthesis" | "text-embedding" | "text-generation" | "text-streaming" | "transcription";
|
5
5
|
model: ModelInformation;
|
6
6
|
settings: unknown;
|
7
7
|
response: unknown;
|
@@ -12,7 +12,10 @@ export interface TextEmbeddingModel<RESPONSE, SETTINGS extends TextEmbeddingMode
|
|
12
12
|
* The size of the embedding vector.
|
13
13
|
*/
|
14
14
|
readonly embeddingDimensions: number | undefined;
|
15
|
-
|
15
|
+
/**
|
16
|
+
* Limit of how many texts can be sent in a single API call.
|
17
|
+
*/
|
18
|
+
readonly maxTextsPerCall: number | undefined;
|
16
19
|
generateEmbeddingResponse(texts: string[], options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
17
20
|
extractEmbeddings(response: RESPONSE): Vector[];
|
18
21
|
}
|
@@ -24,8 +24,13 @@ function embedTexts(model, texts, options) {
|
|
24
24
|
// split the texts into groups that are small enough to be sent in one call:
|
25
25
|
const maxTextsPerCall = model.maxTextsPerCall;
|
26
26
|
const textGroups = [];
|
27
|
-
|
28
|
-
textGroups.push(texts
|
27
|
+
if (maxTextsPerCall == null) {
|
28
|
+
textGroups.push(texts);
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
for (let i = 0; i < texts.length; i += maxTextsPerCall) {
|
32
|
+
textGroups.push(texts.slice(i, i + maxTextsPerCall));
|
33
|
+
}
|
29
34
|
}
|
30
35
|
return Promise.all(textGroups.map((textGroup) => model.generateEmbeddingResponse(textGroup, options)));
|
31
36
|
},
|
@@ -21,8 +21,13 @@ export function embedTexts(model, texts, options) {
|
|
21
21
|
// split the texts into groups that are small enough to be sent in one call:
|
22
22
|
const maxTextsPerCall = model.maxTextsPerCall;
|
23
23
|
const textGroups = [];
|
24
|
-
|
25
|
-
textGroups.push(texts
|
24
|
+
if (maxTextsPerCall == null) {
|
25
|
+
textGroups.push(texts);
|
26
|
+
}
|
27
|
+
else {
|
28
|
+
for (let i = 0; i < texts.length; i += maxTextsPerCall) {
|
29
|
+
textGroups.push(texts.slice(i, i + maxTextsPerCall));
|
30
|
+
}
|
26
31
|
}
|
27
32
|
return Promise.all(textGroups.map((textGroup) => model.generateEmbeddingResponse(textGroup, options)));
|
28
33
|
},
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.InstructionWithStructurePrompt = void 0;
|
4
|
+
exports.InstructionWithStructurePrompt = {
|
5
|
+
forStructure({ instruction, structure, }) {
|
6
|
+
return { structure, instruction };
|
7
|
+
},
|
8
|
+
forTool({ instruction, tool, }) {
|
9
|
+
return exports.InstructionWithStructurePrompt.forStructure({
|
10
|
+
instruction,
|
11
|
+
structure: tool.inputStructureDefinition,
|
12
|
+
});
|
13
|
+
},
|
14
|
+
forToolCurried(instruction) {
|
15
|
+
return (tool) => this.forTool({ instruction, tool });
|
16
|
+
},
|
17
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { Tool } from "../../tool/Tool.js";
|
2
|
+
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
3
|
+
export type InstructionWithStructure<NAME extends string, STRUCTURE> = {
|
4
|
+
instruction: string;
|
5
|
+
structure: StructureDefinition<NAME, STRUCTURE>;
|
6
|
+
};
|
7
|
+
export declare const InstructionWithStructurePrompt: {
|
8
|
+
forStructure<STRUCTURE>({ instruction, structure, }: {
|
9
|
+
instruction: string;
|
10
|
+
structure: StructureDefinition<string, STRUCTURE>;
|
11
|
+
}): InstructionWithStructure<string, STRUCTURE>;
|
12
|
+
forTool<INPUT, OUTPUT>({ instruction, tool, }: {
|
13
|
+
instruction: string;
|
14
|
+
tool: Tool<string, INPUT, OUTPUT>;
|
15
|
+
}): InstructionWithStructure<string, INPUT>;
|
16
|
+
forToolCurried<INPUT_1, OUTPUT_1>(instruction: string): (tool: Tool<string, INPUT_1, OUTPUT_1>) => InstructionWithStructure<string, INPUT_1>;
|
17
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export const InstructionWithStructurePrompt = {
|
2
|
+
forStructure({ instruction, structure, }) {
|
3
|
+
return { structure, instruction };
|
4
|
+
},
|
5
|
+
forTool({ instruction, tool, }) {
|
6
|
+
return InstructionWithStructurePrompt.forStructure({
|
7
|
+
instruction,
|
8
|
+
structure: tool.inputStructureDefinition,
|
9
|
+
});
|
10
|
+
},
|
11
|
+
forToolCurried(instruction) {
|
12
|
+
return (tool) => this.forTool({ instruction, tool });
|
13
|
+
},
|
14
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.NoSuchStructureError = void 0;
|
4
|
+
class NoSuchStructureError extends Error {
|
5
|
+
constructor(structureName) {
|
6
|
+
super(`No such structure: ${structureName}`);
|
7
|
+
Object.defineProperty(this, "structureName", {
|
8
|
+
enumerable: true,
|
9
|
+
configurable: true,
|
10
|
+
writable: true,
|
11
|
+
value: void 0
|
12
|
+
});
|
13
|
+
this.name = "NoSuchStructureError";
|
14
|
+
this.structureName = structureName;
|
15
|
+
}
|
16
|
+
}
|
17
|
+
exports.NoSuchStructureError = NoSuchStructureError;
|