modelfusion 0.79.0 → 0.81.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 +58 -46
- package/core/schema/UncheckedSchema.d.ts +2 -1
- package/core/schema/ZodSchema.d.ts +2 -1
- package/core/schema/index.cjs +0 -3
- package/core/schema/index.d.ts +0 -3
- package/core/schema/index.js +0 -3
- package/guard/fixStructure.cjs +14 -8
- package/guard/fixStructure.d.ts +14 -8
- package/guard/fixStructure.js +14 -8
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +2 -3
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +8 -10
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +2 -3
- package/model-function/generate-structure/StructureFromTextPromptFormat.d.ts +6 -0
- package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +69 -0
- package/model-function/generate-structure/StructureFromTextStreamingModel.d.ts +22 -0
- package/model-function/generate-structure/StructureFromTextStreamingModel.js +65 -0
- package/model-function/generate-structure/StructureGenerationModel.d.ts +4 -3
- package/model-function/generate-structure/StructureParseError.cjs +2 -10
- package/model-function/generate-structure/StructureParseError.d.ts +1 -4
- package/model-function/generate-structure/StructureParseError.js +2 -10
- package/model-function/generate-structure/StructureValidationError.cjs +2 -10
- package/model-function/generate-structure/StructureValidationError.d.ts +1 -4
- package/model-function/generate-structure/StructureValidationError.js +2 -10
- package/model-function/generate-structure/generateStructure.cjs +4 -5
- package/model-function/generate-structure/generateStructure.d.ts +14 -20
- package/model-function/generate-structure/generateStructure.js +4 -5
- package/model-function/generate-structure/index.cjs +3 -0
- package/model-function/generate-structure/index.d.ts +3 -0
- package/model-function/generate-structure/index.js +3 -0
- package/model-function/generate-structure/jsonStructurePrompt.cjs +11 -0
- package/model-function/generate-structure/jsonStructurePrompt.d.ts +4 -0
- package/model-function/generate-structure/jsonStructurePrompt.js +7 -0
- package/model-function/generate-structure/streamStructure.cjs +4 -4
- package/model-function/generate-structure/streamStructure.d.ts +18 -26
- package/model-function/generate-structure/streamStructure.js +4 -4
- package/model-function/generate-text/PromptFormatTextGenerationModel.cjs +7 -0
- package/model-function/generate-text/PromptFormatTextGenerationModel.d.ts +3 -0
- package/model-function/generate-text/PromptFormatTextGenerationModel.js +7 -0
- package/model-function/generate-text/PromptFormatTextStreamingModel.cjs +7 -0
- package/model-function/generate-text/PromptFormatTextStreamingModel.d.ts +3 -0
- package/model-function/generate-text/PromptFormatTextStreamingModel.js +7 -0
- package/model-function/generate-text/prompt-format/AlpacaPromptFormat.cjs +10 -10
- package/model-function/generate-text/prompt-format/AlpacaPromptFormat.d.ts +2 -2
- package/model-function/generate-text/prompt-format/AlpacaPromptFormat.js +10 -10
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.cjs +15 -13
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.d.ts +3 -3
- package/model-function/generate-text/prompt-format/ChatMLPromptFormat.js +15 -13
- package/model-function/generate-text/prompt-format/ChatPrompt.cjs +24 -0
- package/model-function/generate-text/prompt-format/ChatPrompt.d.ts +10 -4
- package/model-function/generate-text/prompt-format/ChatPrompt.js +22 -1
- package/model-function/generate-text/prompt-format/Content.cjs +2 -0
- package/model-function/generate-text/prompt-format/Content.d.ts +20 -0
- package/model-function/generate-text/prompt-format/Content.js +1 -0
- package/model-function/generate-text/prompt-format/InstructionPrompt.d.ts +22 -17
- package/model-function/generate-text/prompt-format/InvalidPromptError.cjs +28 -0
- package/model-function/generate-text/prompt-format/InvalidPromptError.d.ts +13 -0
- package/model-function/generate-text/prompt-format/InvalidPromptError.js +24 -0
- package/model-function/generate-text/prompt-format/Llama2PromptFormat.cjs +14 -10
- package/model-function/generate-text/prompt-format/Llama2PromptFormat.d.ts +3 -3
- package/model-function/generate-text/prompt-format/Llama2PromptFormat.js +14 -10
- package/model-function/generate-text/prompt-format/TextPromptFormat.cjs +11 -11
- package/model-function/generate-text/prompt-format/TextPromptFormat.d.ts +3 -3
- package/model-function/generate-text/prompt-format/TextPromptFormat.js +11 -11
- package/model-function/generate-text/prompt-format/VicunaPromptFormat.cjs +6 -6
- package/model-function/generate-text/prompt-format/VicunaPromptFormat.d.ts +1 -1
- package/model-function/generate-text/prompt-format/VicunaPromptFormat.js +6 -6
- package/model-function/generate-text/prompt-format/index.cjs +2 -2
- package/model-function/generate-text/prompt-format/index.d.ts +2 -2
- package/model-function/generate-text/prompt-format/index.js +2 -2
- package/model-function/generate-text/prompt-format/trimChatPrompt.cjs +2 -2
- package/model-function/generate-text/prompt-format/trimChatPrompt.d.ts +1 -1
- package/model-function/generate-text/prompt-format/trimChatPrompt.js +1 -1
- package/model-provider/anthropic/AnthropicPromptFormat.cjs +10 -10
- package/model-provider/anthropic/AnthropicPromptFormat.d.ts +3 -3
- package/model-provider/anthropic/AnthropicPromptFormat.js +10 -10
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppBakLLaVA1Format.cjs +20 -11
- package/model-provider/llamacpp/LlamaCppBakLLaVA1Format.js +20 -11
- package/model-provider/openai/OpenAICompletionModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.cjs +147 -0
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -0
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.js +140 -0
- package/model-provider/openai/chat/OpenAIChatMessage.cjs +19 -14
- package/model-provider/openai/chat/OpenAIChatMessage.d.ts +2 -5
- package/model-provider/openai/chat/OpenAIChatMessage.js +19 -14
- package/model-provider/openai/chat/OpenAIChatModel.cjs +16 -56
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +10 -54
- package/model-provider/openai/chat/OpenAIChatModel.js +17 -54
- package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +19 -14
- package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +6 -2
- package/model-provider/openai/chat/OpenAIChatPromptFormat.js +17 -13
- package/package.json +1 -1
- package/tool/generate-tool-calls-or-text/generateToolCallsOrText.d.ts +2 -2
- package/core/schema/StructureDefinition.d.ts +0 -7
- package/core/schema/UncheckedStructureDefinition.cjs +0 -30
- package/core/schema/UncheckedStructureDefinition.d.ts +0 -12
- package/core/schema/UncheckedStructureDefinition.js +0 -26
- package/core/schema/ZodStructureDefinition.cjs +0 -30
- package/core/schema/ZodStructureDefinition.d.ts +0 -13
- package/core/schema/ZodStructureDefinition.js +0 -26
- package/model-function/generate-text/prompt-format/ChatPromptValidationError.cjs +0 -17
- package/model-function/generate-text/prompt-format/ChatPromptValidationError.d.ts +0 -8
- package/model-function/generate-text/prompt-format/ChatPromptValidationError.js +0 -13
- package/model-function/generate-text/prompt-format/validateChatPrompt.cjs +0 -24
- package/model-function/generate-text/prompt-format/validateChatPrompt.d.ts +0 -5
- package/model-function/generate-text/prompt-format/validateChatPrompt.js +0 -20
- /package/{core/schema/StructureDefinition.cjs → model-function/generate-structure/StructureFromTextPromptFormat.cjs} +0 -0
- /package/{core/schema/StructureDefinition.js → model-function/generate-structure/StructureFromTextPromptFormat.js} +0 -0
package/README.md
CHANGED
@@ -87,9 +87,10 @@ import { streamText, openai } from "modelfusion";
|
|
87
87
|
const textStream = await streamText(
|
88
88
|
openai.ChatTextGenerator({ model: "gpt-4-vision-preview" }),
|
89
89
|
[
|
90
|
-
OpenAIChatMessage.user(
|
91
|
-
|
92
|
-
|
90
|
+
OpenAIChatMessage.user([
|
91
|
+
{ type: "text", text: "Describe the image in detail:" },
|
92
|
+
{ type: "image", base64Image: image, mimeType: "image/png" },
|
93
|
+
]),
|
93
94
|
]
|
94
95
|
);
|
95
96
|
```
|
@@ -192,31 +193,34 @@ Generate typed objects using a language model and a schema.
|
|
192
193
|
Generate a structure that matches a schema.
|
193
194
|
|
194
195
|
```ts
|
196
|
+
import { ZodSchema, generateStructure, openai } from "modelfusion";
|
197
|
+
|
195
198
|
const sentiment = await generateStructure(
|
196
|
-
openai
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
199
|
+
openai
|
200
|
+
.ChatTextGenerator({
|
201
|
+
model: "gpt-3.5-turbo",
|
202
|
+
temperature: 0,
|
203
|
+
maxCompletionTokens: 50,
|
204
|
+
})
|
205
|
+
.asFunctionCallStructureGenerationModel({ fnName: "sentiment" })
|
206
|
+
.withInstructionPrompt(),
|
207
|
+
|
208
|
+
new ZodSchema(
|
209
|
+
z.object({
|
205
210
|
sentiment: z
|
206
211
|
.enum(["positive", "neutral", "negative"])
|
207
212
|
.describe("Sentiment."),
|
208
|
-
})
|
209
|
-
|
210
|
-
|
211
|
-
|
213
|
+
})
|
214
|
+
),
|
215
|
+
|
216
|
+
{
|
217
|
+
system:
|
212
218
|
"You are a sentiment evaluator. " +
|
213
|
-
|
214
|
-
|
215
|
-
OpenAIChatMessage.user(
|
219
|
+
"Analyze the sentiment of the following product review:",
|
220
|
+
instruction:
|
216
221
|
"After I opened the package, I was met by a very unpleasant smell " +
|
217
|
-
|
218
|
-
|
219
|
-
]
|
222
|
+
"that did not disappear even after washing. Never again!",
|
223
|
+
}
|
220
224
|
);
|
221
225
|
```
|
222
226
|
|
@@ -227,16 +231,19 @@ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
|
|
227
231
|
Stream a structure that matches a schema. Partial structures before the final part are untyped JSON.
|
228
232
|
|
229
233
|
```ts
|
234
|
+
import { ZodSchema, openai, streamStructure } from "modelfusion";
|
235
|
+
|
230
236
|
const structureStream = await streamStructure(
|
231
|
-
openai
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
237
|
+
openai
|
238
|
+
.ChatTextGenerator(/* ... */)
|
239
|
+
.asFunctionCallStructureGenerationModel({
|
240
|
+
fnName: "generateCharacter",
|
241
|
+
fnDescription: "Generate character descriptions.",
|
242
|
+
})
|
243
|
+
.withTextPrompt(),
|
244
|
+
|
245
|
+
new ZodSchema(
|
246
|
+
z.object({
|
240
247
|
characters: z.array(
|
241
248
|
z.object({
|
242
249
|
name: z.string(),
|
@@ -246,13 +253,10 @@ const structureStream = await streamStructure(
|
|
246
253
|
description: z.string(),
|
247
254
|
})
|
248
255
|
),
|
249
|
-
})
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
"Generate 3 character descriptions for a fantasy role playing game."
|
254
|
-
),
|
255
|
-
]
|
256
|
+
})
|
257
|
+
),
|
258
|
+
|
259
|
+
"Generate 3 character descriptions for a fantasy role playing game."
|
256
260
|
);
|
257
261
|
|
258
262
|
for await (const part of structureStream) {
|
@@ -319,10 +323,14 @@ Guard functions can be used to implement retry on error, redacting and changing
|
|
319
323
|
const result = await guard(
|
320
324
|
(input, options) =>
|
321
325
|
generateStructure(
|
322
|
-
openai
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
+
openai
|
327
|
+
.ChatTextGenerator({
|
328
|
+
// ...
|
329
|
+
})
|
330
|
+
.asFunctionCallStructureGenerationModel({
|
331
|
+
fnName: "myFunction",
|
332
|
+
}),
|
333
|
+
new ZodSchema({
|
326
334
|
// ...
|
327
335
|
}),
|
328
336
|
input,
|
@@ -334,10 +342,14 @@ const result = await guard(
|
|
334
342
|
fixStructure({
|
335
343
|
modifyInputForRetry: async ({ input, error }) => [
|
336
344
|
...input,
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
345
|
+
{
|
346
|
+
role: "assistant",
|
347
|
+
content: null,
|
348
|
+
function_call: {
|
349
|
+
name: "sentiment",
|
350
|
+
arguments: JSON.stringify(error.valueText),
|
351
|
+
},
|
352
|
+
} satisfies OpenAIChatMessage,
|
341
353
|
OpenAIChatMessage.user(error.message),
|
342
354
|
OpenAIChatMessage.user("Please fix the error and try again."),
|
343
355
|
],
|
@@ -1,5 +1,6 @@
|
|
1
|
+
import { JsonSchemaProducer } from "./JsonSchemaProducer.js";
|
1
2
|
import { Schema } from "./Schema.js";
|
2
|
-
export declare class UncheckedSchema<STRUCTURE> implements Schema<STRUCTURE
|
3
|
+
export declare class UncheckedSchema<STRUCTURE> implements Schema<STRUCTURE>, JsonSchemaProducer {
|
3
4
|
private readonly jsonSchema?;
|
4
5
|
constructor(jsonSchema?: unknown);
|
5
6
|
validate(data: unknown): {
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
+
import { JsonSchemaProducer } from "./JsonSchemaProducer.js";
|
2
3
|
import { Schema } from "./Schema.js";
|
3
|
-
export declare class ZodSchema<STRUCTURE> implements Schema<STRUCTURE
|
4
|
+
export declare class ZodSchema<STRUCTURE> implements Schema<STRUCTURE>, JsonSchemaProducer {
|
4
5
|
readonly zodSchema: z.Schema<STRUCTURE>;
|
5
6
|
constructor(zodSchema: z.Schema<STRUCTURE>);
|
6
7
|
validate(data: unknown): {
|
package/core/schema/index.cjs
CHANGED
@@ -17,9 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./JSONParseError.cjs"), exports);
|
18
18
|
__exportStar(require("./JsonSchemaProducer.cjs"), exports);
|
19
19
|
__exportStar(require("./Schema.cjs"), exports);
|
20
|
-
__exportStar(require("./StructureDefinition.cjs"), exports);
|
21
20
|
__exportStar(require("./UncheckedSchema.cjs"), exports);
|
22
|
-
__exportStar(require("./UncheckedStructureDefinition.cjs"), exports);
|
23
21
|
__exportStar(require("./ZodSchema.cjs"), exports);
|
24
|
-
__exportStar(require("./ZodStructureDefinition.cjs"), exports);
|
25
22
|
__exportStar(require("./parseJSON.cjs"), exports);
|
package/core/schema/index.d.ts
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
export * from "./JSONParseError.js";
|
2
2
|
export * from "./JsonSchemaProducer.js";
|
3
3
|
export * from "./Schema.js";
|
4
|
-
export * from "./StructureDefinition.js";
|
5
4
|
export * from "./UncheckedSchema.js";
|
6
|
-
export * from "./UncheckedStructureDefinition.js";
|
7
5
|
export * from "./ZodSchema.js";
|
8
|
-
export * from "./ZodStructureDefinition.js";
|
9
6
|
export * from "./parseJSON.js";
|
package/core/schema/index.js
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
export * from "./JSONParseError.js";
|
2
2
|
export * from "./JsonSchemaProducer.js";
|
3
3
|
export * from "./Schema.js";
|
4
|
-
export * from "./StructureDefinition.js";
|
5
4
|
export * from "./UncheckedSchema.js";
|
6
|
-
export * from "./UncheckedStructureDefinition.js";
|
7
5
|
export * from "./ZodSchema.js";
|
8
|
-
export * from "./ZodStructureDefinition.js";
|
9
6
|
export * from "./parseJSON.js";
|
package/guard/fixStructure.cjs
CHANGED
@@ -29,12 +29,14 @@ const StructureValidationError_js_1 = require("../model-function/generate-struct
|
|
29
29
|
* const result = await guard(
|
30
30
|
* (input) =>
|
31
31
|
* generateStructure(
|
32
|
-
* openai
|
33
|
-
*
|
34
|
-
*
|
35
|
-
*
|
32
|
+
* openai
|
33
|
+
* .ChatTextGenerator(/* ... * /)
|
34
|
+
* .asFunctionCallStructureGenerationModel(/* ... * /),
|
35
|
+
*
|
36
|
+
* new ZodSchema({
|
36
37
|
* // ...
|
37
38
|
* }),
|
39
|
+
*
|
38
40
|
* input
|
39
41
|
* ),
|
40
42
|
* [
|
@@ -43,10 +45,14 @@ const StructureValidationError_js_1 = require("../model-function/generate-struct
|
|
43
45
|
* fixStructure({
|
44
46
|
* modifyInputForRetry: async ({ input, error }) => [
|
45
47
|
* ...input,
|
46
|
-
*
|
47
|
-
*
|
48
|
-
|
49
|
-
|
48
|
+
* {
|
49
|
+
* role: "assistant",
|
50
|
+
* content: null,
|
51
|
+
function_call: {
|
52
|
+
* name: "sentiment",
|
53
|
+
* arguments: JSON.stringify(error.valueText),
|
54
|
+
* },
|
55
|
+
* } satisfies OpenAIChatMessage,
|
50
56
|
* OpenAIChatMessage.user(error.message),
|
51
57
|
* OpenAIChatMessage.user("Please fix the error and try again."),
|
52
58
|
* ],
|
package/guard/fixStructure.d.ts
CHANGED
@@ -27,12 +27,14 @@ import { Guard } from "./guard.js";
|
|
27
27
|
* const result = await guard(
|
28
28
|
* (input) =>
|
29
29
|
* generateStructure(
|
30
|
-
* openai
|
31
|
-
*
|
32
|
-
*
|
33
|
-
*
|
30
|
+
* openai
|
31
|
+
* .ChatTextGenerator(/* ... * /)
|
32
|
+
* .asFunctionCallStructureGenerationModel(/* ... * /),
|
33
|
+
*
|
34
|
+
* new ZodSchema({
|
34
35
|
* // ...
|
35
36
|
* }),
|
37
|
+
*
|
36
38
|
* input
|
37
39
|
* ),
|
38
40
|
* [
|
@@ -41,10 +43,14 @@ import { Guard } from "./guard.js";
|
|
41
43
|
* fixStructure({
|
42
44
|
* modifyInputForRetry: async ({ input, error }) => [
|
43
45
|
* ...input,
|
44
|
-
*
|
45
|
-
*
|
46
|
-
|
47
|
-
|
46
|
+
* {
|
47
|
+
* role: "assistant",
|
48
|
+
* content: null,
|
49
|
+
function_call: {
|
50
|
+
* name: "sentiment",
|
51
|
+
* arguments: JSON.stringify(error.valueText),
|
52
|
+
* },
|
53
|
+
* } satisfies OpenAIChatMessage,
|
48
54
|
* OpenAIChatMessage.user(error.message),
|
49
55
|
* OpenAIChatMessage.user("Please fix the error and try again."),
|
50
56
|
* ],
|
package/guard/fixStructure.js
CHANGED
@@ -26,12 +26,14 @@ import { StructureValidationError } from "../model-function/generate-structure/S
|
|
26
26
|
* const result = await guard(
|
27
27
|
* (input) =>
|
28
28
|
* generateStructure(
|
29
|
-
* openai
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
29
|
+
* openai
|
30
|
+
* .ChatTextGenerator(/* ... * /)
|
31
|
+
* .asFunctionCallStructureGenerationModel(/* ... * /),
|
32
|
+
*
|
33
|
+
* new ZodSchema({
|
33
34
|
* // ...
|
34
35
|
* }),
|
36
|
+
*
|
35
37
|
* input
|
36
38
|
* ),
|
37
39
|
* [
|
@@ -40,10 +42,14 @@ import { StructureValidationError } from "../model-function/generate-structure/S
|
|
40
42
|
* fixStructure({
|
41
43
|
* modifyInputForRetry: async ({ input, error }) => [
|
42
44
|
* ...input,
|
43
|
-
*
|
44
|
-
*
|
45
|
-
|
46
|
-
|
45
|
+
* {
|
46
|
+
* role: "assistant",
|
47
|
+
* content: null,
|
48
|
+
function_call: {
|
49
|
+
* name: "sentiment",
|
50
|
+
* arguments: JSON.stringify(error.valueText),
|
51
|
+
* },
|
52
|
+
* } satisfies OpenAIChatMessage,
|
47
53
|
* OpenAIChatMessage.user(error.message),
|
48
54
|
* OpenAIChatMessage.user("Please fix the error and try again."),
|
49
55
|
* ],
|
@@ -29,8 +29,8 @@ class StructureFromTextGenerationModel {
|
|
29
29
|
get settingsForEvent() {
|
30
30
|
return this.model.settingsForEvent;
|
31
31
|
}
|
32
|
-
async doGenerateStructure(
|
33
|
-
const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt,
|
32
|
+
async doGenerateStructure(schema, prompt, options) {
|
33
|
+
const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, schema), {
|
34
34
|
...options,
|
35
35
|
returnType: "full",
|
36
36
|
});
|
@@ -43,7 +43,6 @@ class StructureFromTextGenerationModel {
|
|
43
43
|
}
|
44
44
|
catch (error) {
|
45
45
|
throw new StructureParseError_js_1.StructureParseError({
|
46
|
-
structureName: structure.name,
|
47
46
|
valueText: value,
|
48
47
|
cause: error,
|
49
48
|
});
|
@@ -1,22 +1,20 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
-
import {
|
2
|
+
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
3
|
+
import { Schema } from "../../core/schema/Schema.js";
|
3
4
|
import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
|
5
|
+
import { StructureFromTextPromptFormat } from "./StructureFromTextPromptFormat.js";
|
4
6
|
import { StructureGenerationModel } from "./StructureGenerationModel.js";
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
};
|
9
|
-
export declare class StructureFromTextGenerationModel<PROMPT, MODEL extends TextGenerationModel<string, TextGenerationModelSettings>> implements StructureGenerationModel<PROMPT, MODEL["settings"]> {
|
10
|
-
private readonly model;
|
11
|
-
private readonly format;
|
7
|
+
export declare class StructureFromTextGenerationModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL extends TextGenerationModel<TARGET_PROMPT, TextGenerationModelSettings>> implements StructureGenerationModel<SOURCE_PROMPT, MODEL["settings"]> {
|
8
|
+
protected readonly model: MODEL;
|
9
|
+
protected readonly format: StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
|
12
10
|
constructor({ model, format, }: {
|
13
11
|
model: MODEL;
|
14
|
-
format: StructureFromTextPromptFormat<
|
12
|
+
format: StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
|
15
13
|
});
|
16
14
|
get modelInformation(): import("../ModelInformation.js").ModelInformation;
|
17
15
|
get settings(): TextGenerationModelSettings;
|
18
16
|
get settingsForEvent(): Partial<MODEL["settings"]>;
|
19
|
-
doGenerateStructure(
|
17
|
+
doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<{
|
20
18
|
response: unknown;
|
21
19
|
value: unknown;
|
22
20
|
valueText: string;
|
@@ -26,8 +26,8 @@ export class StructureFromTextGenerationModel {
|
|
26
26
|
get settingsForEvent() {
|
27
27
|
return this.model.settingsForEvent;
|
28
28
|
}
|
29
|
-
async doGenerateStructure(
|
30
|
-
const { response, value } = await generateText(this.model, this.format.createPrompt(prompt,
|
29
|
+
async doGenerateStructure(schema, prompt, options) {
|
30
|
+
const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, schema), {
|
31
31
|
...options,
|
32
32
|
returnType: "full",
|
33
33
|
});
|
@@ -40,7 +40,6 @@ export class StructureFromTextGenerationModel {
|
|
40
40
|
}
|
41
41
|
catch (error) {
|
42
42
|
throw new StructureParseError({
|
43
|
-
structureName: structure.name,
|
44
43
|
valueText: value,
|
45
44
|
cause: error,
|
46
45
|
});
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
2
|
+
import { Schema } from "../../core/schema/Schema.js";
|
3
|
+
export type StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT> = {
|
4
|
+
createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => TARGET_PROMPT;
|
5
|
+
extractStructure: (response: string) => unknown;
|
6
|
+
};
|
@@ -0,0 +1,69 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.StructureFromTextStreamingModel = void 0;
|
4
|
+
const streamText_js_1 = require("../../model-function/generate-text/streamText.cjs");
|
5
|
+
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
6
|
+
const generateText_js_1 = require("../generate-text/generateText.cjs");
|
7
|
+
const StructureFromTextGenerationModel_js_1 = require("./StructureFromTextGenerationModel.cjs");
|
8
|
+
const StructureParseError_js_1 = require("./StructureParseError.cjs");
|
9
|
+
const parsePartialJson_js_1 = require("./parsePartialJson.cjs");
|
10
|
+
class StructureFromTextStreamingModel extends StructureFromTextGenerationModel_js_1.StructureFromTextGenerationModel {
|
11
|
+
constructor(options) {
|
12
|
+
super(options);
|
13
|
+
}
|
14
|
+
async doStreamStructure(schema, prompt, options) {
|
15
|
+
const textStream = await (0, streamText_js_1.streamText)(this.model, this.format.createPrompt(prompt, schema), options);
|
16
|
+
const queue = new AsyncQueue_js_1.AsyncQueue();
|
17
|
+
// run async on purpose:
|
18
|
+
(async () => {
|
19
|
+
try {
|
20
|
+
let fullText = "";
|
21
|
+
for await (const deltaText of textStream) {
|
22
|
+
fullText += deltaText;
|
23
|
+
const deltaStructure = (0, parsePartialJson_js_1.parsePartialJson)(fullText);
|
24
|
+
// only publish parsable structures
|
25
|
+
if (deltaStructure != null) {
|
26
|
+
queue.push({
|
27
|
+
type: "delta",
|
28
|
+
fullDelta: fullText,
|
29
|
+
valueDelta: deltaStructure,
|
30
|
+
});
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
catch (error) {
|
35
|
+
queue.push({ type: "error", error });
|
36
|
+
}
|
37
|
+
finally {
|
38
|
+
queue.close();
|
39
|
+
}
|
40
|
+
})();
|
41
|
+
return queue;
|
42
|
+
}
|
43
|
+
async doGenerateStructure(schema, prompt, options) {
|
44
|
+
const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, schema), {
|
45
|
+
...options,
|
46
|
+
returnType: "full",
|
47
|
+
});
|
48
|
+
try {
|
49
|
+
return {
|
50
|
+
response,
|
51
|
+
value: this.format.extractStructure(value),
|
52
|
+
valueText: value,
|
53
|
+
};
|
54
|
+
}
|
55
|
+
catch (error) {
|
56
|
+
throw new StructureParseError_js_1.StructureParseError({
|
57
|
+
valueText: value,
|
58
|
+
cause: error,
|
59
|
+
});
|
60
|
+
}
|
61
|
+
}
|
62
|
+
withSettings(additionalSettings) {
|
63
|
+
return new StructureFromTextStreamingModel({
|
64
|
+
model: this.model.withSettings(additionalSettings),
|
65
|
+
format: this.format,
|
66
|
+
});
|
67
|
+
}
|
68
|
+
}
|
69
|
+
exports.StructureFromTextStreamingModel = StructureFromTextStreamingModel;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
3
|
+
import { Schema } from "../../core/schema/Schema.js";
|
4
|
+
import { Delta } from "../../model-function/Delta.js";
|
5
|
+
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
6
|
+
import { TextGenerationModelSettings, TextStreamingModel } from "../generate-text/TextGenerationModel.js";
|
7
|
+
import { StructureFromTextGenerationModel } from "./StructureFromTextGenerationModel.js";
|
8
|
+
import { StructureFromTextPromptFormat } from "./StructureFromTextPromptFormat.js";
|
9
|
+
import { StructureStreamingModel } from "./StructureGenerationModel.js";
|
10
|
+
export declare class StructureFromTextStreamingModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL extends TextStreamingModel<TARGET_PROMPT, TextGenerationModelSettings>> extends StructureFromTextGenerationModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL> implements StructureStreamingModel<SOURCE_PROMPT, MODEL["settings"]> {
|
11
|
+
constructor(options: {
|
12
|
+
model: MODEL;
|
13
|
+
format: StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
|
14
|
+
});
|
15
|
+
doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<AsyncQueue<Delta<unknown>>>;
|
16
|
+
doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<{
|
17
|
+
response: unknown;
|
18
|
+
value: unknown;
|
19
|
+
valueText: string;
|
20
|
+
}>;
|
21
|
+
withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
|
22
|
+
}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import { streamText } from "../../model-function/generate-text/streamText.js";
|
2
|
+
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
3
|
+
import { generateText } from "../generate-text/generateText.js";
|
4
|
+
import { StructureFromTextGenerationModel } from "./StructureFromTextGenerationModel.js";
|
5
|
+
import { StructureParseError } from "./StructureParseError.js";
|
6
|
+
import { parsePartialJson } from "./parsePartialJson.js";
|
7
|
+
export class StructureFromTextStreamingModel extends StructureFromTextGenerationModel {
|
8
|
+
constructor(options) {
|
9
|
+
super(options);
|
10
|
+
}
|
11
|
+
async doStreamStructure(schema, prompt, options) {
|
12
|
+
const textStream = await streamText(this.model, this.format.createPrompt(prompt, schema), options);
|
13
|
+
const queue = new AsyncQueue();
|
14
|
+
// run async on purpose:
|
15
|
+
(async () => {
|
16
|
+
try {
|
17
|
+
let fullText = "";
|
18
|
+
for await (const deltaText of textStream) {
|
19
|
+
fullText += deltaText;
|
20
|
+
const deltaStructure = parsePartialJson(fullText);
|
21
|
+
// only publish parsable structures
|
22
|
+
if (deltaStructure != null) {
|
23
|
+
queue.push({
|
24
|
+
type: "delta",
|
25
|
+
fullDelta: fullText,
|
26
|
+
valueDelta: deltaStructure,
|
27
|
+
});
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
catch (error) {
|
32
|
+
queue.push({ type: "error", error });
|
33
|
+
}
|
34
|
+
finally {
|
35
|
+
queue.close();
|
36
|
+
}
|
37
|
+
})();
|
38
|
+
return queue;
|
39
|
+
}
|
40
|
+
async doGenerateStructure(schema, prompt, options) {
|
41
|
+
const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, schema), {
|
42
|
+
...options,
|
43
|
+
returnType: "full",
|
44
|
+
});
|
45
|
+
try {
|
46
|
+
return {
|
47
|
+
response,
|
48
|
+
value: this.format.extractStructure(value),
|
49
|
+
valueText: value,
|
50
|
+
};
|
51
|
+
}
|
52
|
+
catch (error) {
|
53
|
+
throw new StructureParseError({
|
54
|
+
valueText: value,
|
55
|
+
cause: error,
|
56
|
+
});
|
57
|
+
}
|
58
|
+
}
|
59
|
+
withSettings(additionalSettings) {
|
60
|
+
return new StructureFromTextStreamingModel({
|
61
|
+
model: this.model.withSettings(additionalSettings),
|
62
|
+
format: this.format,
|
63
|
+
});
|
64
|
+
}
|
65
|
+
}
|
@@ -1,11 +1,12 @@
|
|
1
|
+
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
1
2
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
-
import {
|
3
|
+
import { Schema } from "../../core/schema/Schema.js";
|
3
4
|
import { Delta } from "../../model-function/Delta.js";
|
4
5
|
import { Model, ModelSettings } from "../Model.js";
|
5
6
|
export interface StructureGenerationModelSettings extends ModelSettings {
|
6
7
|
}
|
7
8
|
export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends Model<SETTINGS> {
|
8
|
-
doGenerateStructure(
|
9
|
+
doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
9
10
|
response: unknown;
|
10
11
|
valueText: string;
|
11
12
|
value: unknown;
|
@@ -17,5 +18,5 @@ export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGene
|
|
17
18
|
}>;
|
18
19
|
}
|
19
20
|
export interface StructureStreamingModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends StructureGenerationModel<PROMPT, SETTINGS> {
|
20
|
-
doStreamStructure(
|
21
|
+
doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<unknown>>>;
|
21
22
|
}
|
@@ -3,16 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StructureParseError = void 0;
|
4
4
|
const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
|
5
5
|
class StructureParseError extends Error {
|
6
|
-
constructor({
|
7
|
-
super(`Structure parsing failed
|
6
|
+
constructor({ valueText, cause }) {
|
7
|
+
super(`Structure parsing failed. ` +
|
8
8
|
`Value: ${valueText}.\n` +
|
9
9
|
`Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
|
10
|
-
Object.defineProperty(this, "structureName", {
|
11
|
-
enumerable: true,
|
12
|
-
configurable: true,
|
13
|
-
writable: true,
|
14
|
-
value: void 0
|
15
|
-
});
|
16
10
|
Object.defineProperty(this, "cause", {
|
17
11
|
enumerable: true,
|
18
12
|
configurable: true,
|
@@ -26,7 +20,6 @@ class StructureParseError extends Error {
|
|
26
20
|
value: void 0
|
27
21
|
});
|
28
22
|
this.name = "StructureParseError";
|
29
|
-
this.structureName = structureName;
|
30
23
|
this.cause = cause;
|
31
24
|
this.valueText = valueText;
|
32
25
|
}
|
@@ -36,7 +29,6 @@ class StructureParseError extends Error {
|
|
36
29
|
cause: this.cause,
|
37
30
|
message: this.message,
|
38
31
|
stack: this.stack,
|
39
|
-
structureName: this.structureName,
|
40
32
|
valueText: this.valueText,
|
41
33
|
};
|
42
34
|
}
|
@@ -1,9 +1,7 @@
|
|
1
1
|
export declare class StructureParseError extends Error {
|
2
|
-
readonly structureName: string;
|
3
2
|
readonly cause: unknown;
|
4
3
|
readonly valueText: string;
|
5
|
-
constructor({
|
6
|
-
structureName: string;
|
4
|
+
constructor({ valueText, cause }: {
|
7
5
|
valueText: string;
|
8
6
|
cause: unknown;
|
9
7
|
});
|
@@ -12,7 +10,6 @@ export declare class StructureParseError extends Error {
|
|
12
10
|
cause: unknown;
|
13
11
|
message: string;
|
14
12
|
stack: string | undefined;
|
15
|
-
structureName: string;
|
16
13
|
valueText: string;
|
17
14
|
};
|
18
15
|
}
|