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
@@ -1,15 +1,9 @@
|
|
1
1
|
import { getErrorMessage } from "../../util/getErrorMessage.js";
|
2
2
|
export class StructureParseError extends Error {
|
3
|
-
constructor({
|
4
|
-
super(`Structure parsing failed
|
3
|
+
constructor({ valueText, cause }) {
|
4
|
+
super(`Structure parsing failed. ` +
|
5
5
|
`Value: ${valueText}.\n` +
|
6
6
|
`Error message: ${getErrorMessage(cause)}`);
|
7
|
-
Object.defineProperty(this, "structureName", {
|
8
|
-
enumerable: true,
|
9
|
-
configurable: true,
|
10
|
-
writable: true,
|
11
|
-
value: void 0
|
12
|
-
});
|
13
7
|
Object.defineProperty(this, "cause", {
|
14
8
|
enumerable: true,
|
15
9
|
configurable: true,
|
@@ -23,7 +17,6 @@ export class StructureParseError extends Error {
|
|
23
17
|
value: void 0
|
24
18
|
});
|
25
19
|
this.name = "StructureParseError";
|
26
|
-
this.structureName = structureName;
|
27
20
|
this.cause = cause;
|
28
21
|
this.valueText = valueText;
|
29
22
|
}
|
@@ -33,7 +26,6 @@ export class StructureParseError extends Error {
|
|
33
26
|
cause: this.cause,
|
34
27
|
message: this.message,
|
35
28
|
stack: this.stack,
|
36
|
-
structureName: this.structureName,
|
37
29
|
valueText: this.valueText,
|
38
30
|
};
|
39
31
|
}
|
@@ -3,16 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StructureValidationError = void 0;
|
4
4
|
const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
|
5
5
|
class StructureValidationError extends Error {
|
6
|
-
constructor({
|
7
|
-
super(`Structure validation failed
|
6
|
+
constructor({ value, valueText, cause, }) {
|
7
|
+
super(`Structure validation 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,
|
@@ -32,7 +26,6 @@ class StructureValidationError extends Error {
|
|
32
26
|
value: void 0
|
33
27
|
});
|
34
28
|
this.name = "StructureValidationError";
|
35
|
-
this.structureName = structureName;
|
36
29
|
this.cause = cause;
|
37
30
|
this.value = value;
|
38
31
|
this.valueText = valueText;
|
@@ -43,7 +36,6 @@ class StructureValidationError extends Error {
|
|
43
36
|
message: this.message,
|
44
37
|
cause: this.cause,
|
45
38
|
stack: this.stack,
|
46
|
-
structureName: this.structureName,
|
47
39
|
value: this.value,
|
48
40
|
valueText: this.valueText,
|
49
41
|
};
|
@@ -1,10 +1,8 @@
|
|
1
1
|
export declare class StructureValidationError extends Error {
|
2
|
-
readonly structureName: string;
|
3
2
|
readonly cause: unknown;
|
4
3
|
readonly valueText: string;
|
5
4
|
readonly value: unknown;
|
6
|
-
constructor({
|
7
|
-
structureName: string;
|
5
|
+
constructor({ value, valueText, cause, }: {
|
8
6
|
value: unknown;
|
9
7
|
valueText: string;
|
10
8
|
cause: unknown;
|
@@ -14,7 +12,6 @@ export declare class StructureValidationError extends Error {
|
|
14
12
|
message: string;
|
15
13
|
cause: unknown;
|
16
14
|
stack: string | undefined;
|
17
|
-
structureName: string;
|
18
15
|
value: unknown;
|
19
16
|
valueText: string;
|
20
17
|
};
|
@@ -1,15 +1,9 @@
|
|
1
1
|
import { getErrorMessage } from "../../util/getErrorMessage.js";
|
2
2
|
export class StructureValidationError extends Error {
|
3
|
-
constructor({
|
4
|
-
super(`Structure validation failed
|
3
|
+
constructor({ value, valueText, cause, }) {
|
4
|
+
super(`Structure validation failed. ` +
|
5
5
|
`Value: ${valueText}.\n` +
|
6
6
|
`Error message: ${getErrorMessage(cause)}`);
|
7
|
-
Object.defineProperty(this, "structureName", {
|
8
|
-
enumerable: true,
|
9
|
-
configurable: true,
|
10
|
-
writable: true,
|
11
|
-
value: void 0
|
12
|
-
});
|
13
7
|
Object.defineProperty(this, "cause", {
|
14
8
|
enumerable: true,
|
15
9
|
configurable: true,
|
@@ -29,7 +23,6 @@ export class StructureValidationError extends Error {
|
|
29
23
|
value: void 0
|
30
24
|
});
|
31
25
|
this.name = "StructureValidationError";
|
32
|
-
this.structureName = structureName;
|
33
26
|
this.cause = cause;
|
34
27
|
this.value = value;
|
35
28
|
this.valueText = valueText;
|
@@ -40,7 +33,6 @@ export class StructureValidationError extends Error {
|
|
40
33
|
message: this.message,
|
41
34
|
cause: this.cause,
|
42
35
|
stack: this.stack,
|
43
|
-
structureName: this.structureName,
|
44
36
|
value: this.value,
|
45
37
|
valueText: this.valueText,
|
46
38
|
};
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateStructure = void 0;
|
4
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
5
|
const StructureValidationError_js_1 = require("./StructureValidationError.cjs");
|
6
|
-
async function generateStructure(model,
|
6
|
+
async function generateStructure(model, schema, prompt, options) {
|
7
7
|
// Note: PROMPT must not be a function.
|
8
8
|
const expandedPrompt = typeof prompt === "function"
|
9
|
-
? prompt(
|
9
|
+
? prompt(schema)
|
10
10
|
: prompt;
|
11
11
|
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
12
12
|
functionType: "generate-structure",
|
@@ -14,12 +14,11 @@ async function generateStructure(model, structureDefinition, prompt, options) {
|
|
14
14
|
model,
|
15
15
|
options,
|
16
16
|
generateResponse: async (options) => {
|
17
|
-
const result = await model.doGenerateStructure(
|
17
|
+
const result = await model.doGenerateStructure(schema, expandedPrompt, options);
|
18
18
|
const structure = result.value;
|
19
|
-
const parseResult =
|
19
|
+
const parseResult = schema.validate(structure);
|
20
20
|
if (!parseResult.success) {
|
21
21
|
throw new StructureValidationError_js_1.StructureValidationError({
|
22
|
-
structureName: structureDefinition.name,
|
23
22
|
valueText: result.valueText,
|
24
23
|
value: structure,
|
25
24
|
cause: parseResult.error,
|
@@ -1,27 +1,21 @@
|
|
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 { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
5
|
import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
|
5
6
|
/**
|
6
|
-
* Generate a typed object for a prompt and a
|
7
|
-
* The structure definition is used as part of the final prompt.
|
8
|
-
*
|
9
|
-
* For the OpenAI chat model, this generates and parses a function call with a single function.
|
7
|
+
* Generate a typed object for a prompt and a schema.
|
10
8
|
*
|
11
9
|
* @see https://modelfusion.dev/guide/function/generate-structure
|
12
10
|
*
|
13
11
|
* @example
|
14
12
|
* const sentiment = await generateStructure(
|
15
|
-
* openai.ChatTextGenerator(...),
|
16
|
-
* new
|
17
|
-
*
|
18
|
-
*
|
19
|
-
*
|
20
|
-
*
|
21
|
-
* .enum(["positive", "neutral", "negative"])
|
22
|
-
* .describe("Sentiment."),
|
23
|
-
* }),
|
24
|
-
* }),
|
13
|
+
* openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
|
14
|
+
* new ZodSchema(z.object({
|
15
|
+
* sentiment: z
|
16
|
+
* .enum(["positive", "neutral", "negative"])
|
17
|
+
* .describe("Sentiment."),
|
18
|
+
* })),
|
25
19
|
* [
|
26
20
|
* OpenAIChatMessage.system(
|
27
21
|
* "You are a sentiment evaluator. " +
|
@@ -35,18 +29,18 @@ import { StructureGenerationModel, StructureGenerationModelSettings } from "./St
|
|
35
29
|
* );
|
36
30
|
*
|
37
31
|
* @param {StructureGenerationModel<PROMPT, SETTINGS>} model - The model to generate the structure.
|
38
|
-
* @param {
|
39
|
-
* @param {PROMPT | ((
|
32
|
+
* @param {Schema<STRUCTURE>} schema - The schema to be used.
|
33
|
+
* @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
|
40
34
|
* The prompt to be used.
|
41
|
-
* You can also pass a function that takes the
|
35
|
+
* You can also pass a function that takes the schema as an argument and returns the prompt.
|
42
36
|
* @param {FunctionOptions} [options] - Optional function options.
|
43
37
|
*
|
44
38
|
* @returns {Promise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
|
45
39
|
*/
|
46
|
-
export declare function generateStructure<STRUCTURE, PROMPT,
|
40
|
+
export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options?: FunctionOptions & {
|
47
41
|
returnType?: "structure";
|
48
42
|
}): Promise<STRUCTURE>;
|
49
|
-
export declare function generateStructure<STRUCTURE, PROMPT,
|
43
|
+
export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options: FunctionOptions & {
|
50
44
|
returnType: "full";
|
51
45
|
}): Promise<{
|
52
46
|
value: STRUCTURE;
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { executeStandardCall } from "../executeStandardCall.js";
|
2
2
|
import { StructureValidationError } from "./StructureValidationError.js";
|
3
|
-
export async function generateStructure(model,
|
3
|
+
export async function generateStructure(model, schema, prompt, options) {
|
4
4
|
// Note: PROMPT must not be a function.
|
5
5
|
const expandedPrompt = typeof prompt === "function"
|
6
|
-
? prompt(
|
6
|
+
? prompt(schema)
|
7
7
|
: prompt;
|
8
8
|
const fullResponse = await executeStandardCall({
|
9
9
|
functionType: "generate-structure",
|
@@ -11,12 +11,11 @@ export async function generateStructure(model, structureDefinition, prompt, opti
|
|
11
11
|
model,
|
12
12
|
options,
|
13
13
|
generateResponse: async (options) => {
|
14
|
-
const result = await model.doGenerateStructure(
|
14
|
+
const result = await model.doGenerateStructure(schema, expandedPrompt, options);
|
15
15
|
const structure = result.value;
|
16
|
-
const parseResult =
|
16
|
+
const parseResult = schema.validate(structure);
|
17
17
|
if (!parseResult.success) {
|
18
18
|
throw new StructureValidationError({
|
19
|
-
structureName: structureDefinition.name,
|
20
19
|
valueText: result.valueText,
|
21
20
|
value: structure,
|
22
21
|
cause: parseResult.error,
|
@@ -15,10 +15,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
__exportStar(require("./StructureFromTextGenerationModel.cjs"), exports);
|
18
|
+
__exportStar(require("./StructureFromTextPromptFormat.cjs"), exports);
|
19
|
+
__exportStar(require("./StructureFromTextStreamingModel.cjs"), exports);
|
18
20
|
__exportStar(require("./StructureGenerationEvent.cjs"), exports);
|
19
21
|
__exportStar(require("./StructureGenerationModel.cjs"), exports);
|
20
22
|
__exportStar(require("./StructureParseError.cjs"), exports);
|
21
23
|
__exportStar(require("./StructureStreamingEvent.cjs"), exports);
|
22
24
|
__exportStar(require("./StructureValidationError.cjs"), exports);
|
23
25
|
__exportStar(require("./generateStructure.cjs"), exports);
|
26
|
+
__exportStar(require("./jsonStructurePrompt.cjs"), exports);
|
24
27
|
__exportStar(require("./streamStructure.cjs"), exports);
|
@@ -1,8 +1,11 @@
|
|
1
1
|
export * from "./StructureFromTextGenerationModel.js";
|
2
|
+
export * from "./StructureFromTextPromptFormat.js";
|
3
|
+
export * from "./StructureFromTextStreamingModel.js";
|
2
4
|
export * from "./StructureGenerationEvent.js";
|
3
5
|
export * from "./StructureGenerationModel.js";
|
4
6
|
export * from "./StructureParseError.js";
|
5
7
|
export * from "./StructureStreamingEvent.js";
|
6
8
|
export * from "./StructureValidationError.js";
|
7
9
|
export * from "./generateStructure.js";
|
10
|
+
export * from "./jsonStructurePrompt.js";
|
8
11
|
export * from "./streamStructure.js";
|
@@ -1,8 +1,11 @@
|
|
1
1
|
export * from "./StructureFromTextGenerationModel.js";
|
2
|
+
export * from "./StructureFromTextPromptFormat.js";
|
3
|
+
export * from "./StructureFromTextStreamingModel.js";
|
2
4
|
export * from "./StructureGenerationEvent.js";
|
3
5
|
export * from "./StructureGenerationModel.js";
|
4
6
|
export * from "./StructureParseError.js";
|
5
7
|
export * from "./StructureStreamingEvent.js";
|
6
8
|
export * from "./StructureValidationError.js";
|
7
9
|
export * from "./generateStructure.js";
|
10
|
+
export * from "./jsonStructurePrompt.js";
|
8
11
|
export * from "./streamStructure.js";
|
@@ -0,0 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.jsonStructurePrompt = void 0;
|
4
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
5
|
+
function jsonStructurePrompt(createPrompt) {
|
6
|
+
return {
|
7
|
+
createPrompt,
|
8
|
+
extractStructure: (response) => (0, parseJSON_js_1.parseJSON)({ text: response }),
|
9
|
+
};
|
10
|
+
}
|
11
|
+
exports.jsonStructurePrompt = jsonStructurePrompt;
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
2
|
+
import { Schema } from "../../core/schema/Schema.js";
|
3
|
+
import { StructureFromTextPromptFormat } from "./StructureFromTextPromptFormat.js";
|
4
|
+
export declare function jsonStructurePrompt<SOURCE_PROMPT, TARGET_PROMPT>(createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => TARGET_PROMPT): StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.streamStructure = void 0;
|
4
4
|
const isDeepEqualData_js_1 = require("../../util/isDeepEqualData.cjs");
|
5
5
|
const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
|
6
|
-
async function streamStructure(model,
|
6
|
+
async function streamStructure(model, schema, prompt, options) {
|
7
7
|
// Note: PROMPT must not be a function.
|
8
8
|
const expandedPrompt = typeof prompt === "function"
|
9
|
-
? prompt(
|
9
|
+
? prompt(schema)
|
10
10
|
: prompt;
|
11
11
|
let lastStructure;
|
12
12
|
let lastFullDelta;
|
@@ -15,7 +15,7 @@ async function streamStructure(model, structureDefinition, prompt, options) {
|
|
15
15
|
input: prompt,
|
16
16
|
model,
|
17
17
|
options,
|
18
|
-
startStream: async (options) => model.doStreamStructure(
|
18
|
+
startStream: async (options) => model.doStreamStructure(schema, expandedPrompt, options),
|
19
19
|
processDelta: (delta) => {
|
20
20
|
const latestFullDelta = delta.fullDelta;
|
21
21
|
const latestStructure = delta.valueDelta;
|
@@ -32,7 +32,7 @@ async function streamStructure(model, structureDefinition, prompt, options) {
|
|
32
32
|
},
|
33
33
|
processFinished: () => {
|
34
34
|
// process the final result (full type validation):
|
35
|
-
const parseResult =
|
35
|
+
const parseResult = schema.validate(lastStructure);
|
36
36
|
if (!parseResult.success) {
|
37
37
|
reportError(parseResult.error);
|
38
38
|
throw parseResult.error;
|
@@ -1,5 +1,6 @@
|
|
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 { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
5
|
import { StructureStreamingModel } from "./StructureGenerationModel.js";
|
5
6
|
export type StructureStreamPart<STRUCTURE> = {
|
@@ -25,26 +26,17 @@ export type StructureStreamPart<STRUCTURE> = {
|
|
25
26
|
*
|
26
27
|
* @example
|
27
28
|
* const structureStream = await streamStructure(
|
28
|
-
*
|
29
|
-
*
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
33
|
-
*
|
34
|
-
*
|
35
|
-
*
|
36
|
-
*
|
37
|
-
*
|
38
|
-
*
|
39
|
-
* name: z.string(),
|
40
|
-
* class: z
|
41
|
-
* .string()
|
42
|
-
* .describe("Character class, e.g. warrior, mage, or thief."),
|
43
|
-
* description: z.string(),
|
44
|
-
* })
|
45
|
-
* ),
|
46
|
-
* }),
|
47
|
-
* }),
|
29
|
+
* openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
|
30
|
+
* new ZodSchema(
|
31
|
+
* z.array(
|
32
|
+
* z.object({
|
33
|
+
* name: z.string(),
|
34
|
+
* class: z
|
35
|
+
* .string()
|
36
|
+
* .describe("Character class, e.g. warrior, mage, or thief."),
|
37
|
+
* description: z.string(),
|
38
|
+
* })
|
39
|
+
* ),
|
48
40
|
* [
|
49
41
|
* OpenAIChatMessage.user(
|
50
42
|
* "Generate 3 character descriptions for a fantasy role playing game."
|
@@ -64,10 +56,10 @@ export type StructureStreamPart<STRUCTURE> = {
|
|
64
56
|
* }
|
65
57
|
*
|
66
58
|
* @param {StructureStreamingModel<PROMPT>} model - The model to use for streaming
|
67
|
-
* @param {
|
68
|
-
* @param {PROMPT | ((
|
59
|
+
* @param {Schema<STRUCTURE>} schema - The schema to be used.
|
60
|
+
* @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
|
69
61
|
* The prompt to be used.
|
70
|
-
* You can also pass a function that takes the
|
62
|
+
* You can also pass a function that takes the schema as an argument and returns the prompt.
|
71
63
|
* @param {FunctionOptions} [options] - Optional function options
|
72
64
|
*
|
73
65
|
* @returns {AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>}
|
@@ -76,10 +68,10 @@ export type StructureStreamPart<STRUCTURE> = {
|
|
76
68
|
* It contains a isComplete flag to indicate whether the structure is complete,
|
77
69
|
* and a value that is either the partial structure or the final structure.
|
78
70
|
*/
|
79
|
-
export declare function streamStructure<STRUCTURE, PROMPT
|
71
|
+
export declare function streamStructure<STRUCTURE, PROMPT>(model: StructureStreamingModel<PROMPT>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options?: FunctionOptions & {
|
80
72
|
returnType?: "structure-stream";
|
81
73
|
}): Promise<AsyncIterable<StructureStreamPart<STRUCTURE>>>;
|
82
|
-
export declare function streamStructure<STRUCTURE, PROMPT
|
74
|
+
export declare function streamStructure<STRUCTURE, PROMPT>(model: StructureStreamingModel<PROMPT>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options: FunctionOptions & {
|
83
75
|
returnType: "full";
|
84
76
|
}): Promise<{
|
85
77
|
value: AsyncIterable<StructureStreamPart<STRUCTURE>>;
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { isDeepEqualData } from "../../util/isDeepEqualData.js";
|
2
2
|
import { executeStreamCall } from "../executeStreamCall.js";
|
3
|
-
export async function streamStructure(model,
|
3
|
+
export async function streamStructure(model, schema, prompt, options) {
|
4
4
|
// Note: PROMPT must not be a function.
|
5
5
|
const expandedPrompt = typeof prompt === "function"
|
6
|
-
? prompt(
|
6
|
+
? prompt(schema)
|
7
7
|
: prompt;
|
8
8
|
let lastStructure;
|
9
9
|
let lastFullDelta;
|
@@ -12,7 +12,7 @@ export async function streamStructure(model, structureDefinition, prompt, option
|
|
12
12
|
input: prompt,
|
13
13
|
model,
|
14
14
|
options,
|
15
|
-
startStream: async (options) => model.doStreamStructure(
|
15
|
+
startStream: async (options) => model.doStreamStructure(schema, expandedPrompt, options),
|
16
16
|
processDelta: (delta) => {
|
17
17
|
const latestFullDelta = delta.fullDelta;
|
18
18
|
const latestStructure = delta.valueDelta;
|
@@ -29,7 +29,7 @@ export async function streamStructure(model, structureDefinition, prompt, option
|
|
29
29
|
},
|
30
30
|
processFinished: () => {
|
31
31
|
// process the final result (full type validation):
|
32
|
-
const parseResult =
|
32
|
+
const parseResult = schema.validate(lastStructure);
|
33
33
|
if (!parseResult.success) {
|
34
34
|
reportError(parseResult.error);
|
35
35
|
throw parseResult.error;
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.PromptFormatTextGenerationModel = void 0;
|
4
|
+
const StructureFromTextGenerationModel_js_1 = require("../../model-function/generate-structure/StructureFromTextGenerationModel.cjs");
|
4
5
|
const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
|
5
6
|
const TextGenerationToolCallsOrGenerateTextModel_js_1 = require("../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs");
|
6
7
|
class PromptFormatTextGenerationModel {
|
@@ -58,6 +59,12 @@ class PromptFormatTextGenerationModel {
|
|
58
59
|
format: promptFormat,
|
59
60
|
});
|
60
61
|
}
|
62
|
+
asStructureGenerationModel(promptFormat) {
|
63
|
+
return new StructureFromTextGenerationModel_js_1.StructureFromTextGenerationModel({
|
64
|
+
model: this,
|
65
|
+
format: promptFormat,
|
66
|
+
});
|
67
|
+
}
|
61
68
|
withPromptFormat(promptFormat) {
|
62
69
|
return new PromptFormatTextGenerationModel({
|
63
70
|
model: this.withSettings({
|
@@ -1,4 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { StructureFromTextGenerationModel } from "../../model-function/generate-structure/StructureFromTextGenerationModel.js";
|
3
|
+
import { StructureFromTextPromptFormat } from "model-function/generate-structure/StructureFromTextPromptFormat.js";
|
2
4
|
import { TextGenerationToolCallModel, ToolCallPromptFormat } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
3
5
|
import { TextGenerationToolCallsOrGenerateTextModel, ToolCallsOrGenerateTextPromptFormat } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
4
6
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
@@ -27,6 +29,7 @@ export declare class PromptFormatTextGenerationModel<PROMPT, MODEL_PROMPT, SETTI
|
|
27
29
|
get settingsForEvent(): Partial<SETTINGS>;
|
28
30
|
asToolCallGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallPromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationToolCallModel<INPUT_PROMPT, PROMPT, this>;
|
29
31
|
asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallsOrGenerateTextPromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationToolCallsOrGenerateTextModel<INPUT_PROMPT, PROMPT, this>;
|
32
|
+
asStructureGenerationModel<INPUT_PROMPT>(promptFormat: StructureFromTextPromptFormat<INPUT_PROMPT, PROMPT>): StructureFromTextGenerationModel<INPUT_PROMPT, PROMPT, this>;
|
30
33
|
withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
|
31
34
|
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
32
35
|
}
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { StructureFromTextGenerationModel } from "../../model-function/generate-structure/StructureFromTextGenerationModel.js";
|
1
2
|
import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
2
3
|
import { TextGenerationToolCallsOrGenerateTextModel, } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
3
4
|
export class PromptFormatTextGenerationModel {
|
@@ -55,6 +56,12 @@ export class PromptFormatTextGenerationModel {
|
|
55
56
|
format: promptFormat,
|
56
57
|
});
|
57
58
|
}
|
59
|
+
asStructureGenerationModel(promptFormat) {
|
60
|
+
return new StructureFromTextGenerationModel({
|
61
|
+
model: this,
|
62
|
+
format: promptFormat,
|
63
|
+
});
|
64
|
+
}
|
58
65
|
withPromptFormat(promptFormat) {
|
59
66
|
return new PromptFormatTextGenerationModel({
|
60
67
|
model: this.withSettings({
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.PromptFormatTextStreamingModel = void 0;
|
4
|
+
const StructureFromTextStreamingModel_js_1 = require("../../model-function/generate-structure/StructureFromTextStreamingModel.cjs");
|
4
5
|
const PromptFormatTextGenerationModel_js_1 = require("./PromptFormatTextGenerationModel.cjs");
|
5
6
|
class PromptFormatTextStreamingModel extends PromptFormatTextGenerationModel_js_1.PromptFormatTextGenerationModel {
|
6
7
|
constructor(options) {
|
@@ -10,6 +11,12 @@ class PromptFormatTextStreamingModel extends PromptFormatTextGenerationModel_js_
|
|
10
11
|
const mappedPrompt = this.promptFormat.format(prompt);
|
11
12
|
return this.model.doStreamText(mappedPrompt, options);
|
12
13
|
}
|
14
|
+
asStructureGenerationModel(promptFormat) {
|
15
|
+
return new StructureFromTextStreamingModel_js_1.StructureFromTextStreamingModel({
|
16
|
+
model: this,
|
17
|
+
format: promptFormat,
|
18
|
+
});
|
19
|
+
}
|
13
20
|
withPromptFormat(promptFormat) {
|
14
21
|
return new PromptFormatTextStreamingModel({
|
15
22
|
model: this.withSettings({
|
@@ -1,4 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { StructureFromTextPromptFormat } from "model-function/generate-structure/StructureFromTextPromptFormat.js";
|
3
|
+
import { StructureFromTextStreamingModel } from "../../model-function/generate-structure/StructureFromTextStreamingModel.js";
|
2
4
|
import { PromptFormatTextGenerationModel } from "./PromptFormatTextGenerationModel.js";
|
3
5
|
import { TextGenerationModelSettings, TextStreamingModel } from "./TextGenerationModel.js";
|
4
6
|
import { TextGenerationPromptFormat } from "./TextGenerationPromptFormat.js";
|
@@ -8,6 +10,7 @@ export declare class PromptFormatTextStreamingModel<PROMPT, MODEL_PROMPT, SETTIN
|
|
8
10
|
promptFormat: TextGenerationPromptFormat<PROMPT, MODEL_PROMPT>;
|
9
11
|
});
|
10
12
|
doStreamText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<import("../Delta.js").Delta<string>>>;
|
13
|
+
asStructureGenerationModel<INPUT_PROMPT>(promptFormat: StructureFromTextPromptFormat<INPUT_PROMPT, PROMPT>): StructureFromTextStreamingModel<INPUT_PROMPT, PROMPT, this>;
|
11
14
|
withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextStreamingModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
|
12
15
|
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
13
16
|
}
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { StructureFromTextStreamingModel } from "../../model-function/generate-structure/StructureFromTextStreamingModel.js";
|
1
2
|
import { PromptFormatTextGenerationModel } from "./PromptFormatTextGenerationModel.js";
|
2
3
|
export class PromptFormatTextStreamingModel extends PromptFormatTextGenerationModel {
|
3
4
|
constructor(options) {
|
@@ -7,6 +8,12 @@ export class PromptFormatTextStreamingModel extends PromptFormatTextGenerationMo
|
|
7
8
|
const mappedPrompt = this.promptFormat.format(prompt);
|
8
9
|
return this.model.doStreamText(mappedPrompt, options);
|
9
10
|
}
|
11
|
+
asStructureGenerationModel(promptFormat) {
|
12
|
+
return new StructureFromTextStreamingModel({
|
13
|
+
model: this,
|
14
|
+
format: promptFormat,
|
15
|
+
});
|
16
|
+
}
|
10
17
|
withPromptFormat(promptFormat) {
|
11
18
|
return new PromptFormatTextStreamingModel({
|
12
19
|
model: this.withSettings({
|
@@ -9,10 +9,10 @@ const DEFAULT_SYSTEM_PROMPT_NO_INPUT = "Below is an instruction that describes a
|
|
9
9
|
function text() {
|
10
10
|
return {
|
11
11
|
stopSequences: [],
|
12
|
-
format
|
12
|
+
format(prompt) {
|
13
13
|
let text = DEFAULT_SYSTEM_PROMPT_NO_INPUT;
|
14
14
|
text += "\n\n### Instruction:\n";
|
15
|
-
text +=
|
15
|
+
text += prompt;
|
16
16
|
text += "\n\n### Response:\n";
|
17
17
|
return text;
|
18
18
|
},
|
@@ -58,18 +58,18 @@ exports.text = text;
|
|
58
58
|
function instruction() {
|
59
59
|
return {
|
60
60
|
stopSequences: [],
|
61
|
-
format
|
62
|
-
let text =
|
63
|
-
(
|
61
|
+
format(prompt) {
|
62
|
+
let text = prompt.system ??
|
63
|
+
(prompt.input != null
|
64
64
|
? DEFAULT_SYSTEM_PROMPT_INPUT
|
65
65
|
: DEFAULT_SYSTEM_PROMPT_NO_INPUT);
|
66
66
|
text += "\n\n### Instruction:\n";
|
67
|
-
if (
|
68
|
-
text += `${
|
67
|
+
if (prompt.system != null) {
|
68
|
+
text += `${prompt.system}\n`;
|
69
69
|
}
|
70
|
-
text +=
|
71
|
-
if (
|
72
|
-
text += `\n\n### Input:\n${
|
70
|
+
text += prompt.instruction;
|
71
|
+
if (prompt.input != null) {
|
72
|
+
text += `\n\n### Input:\n${prompt.input}`;
|
73
73
|
}
|
74
74
|
text += "\n\n### Response:\n";
|
75
75
|
return text;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { InstructionPrompt } from "./InstructionPrompt.js";
|
2
1
|
import { TextGenerationPromptFormat } from "../TextGenerationPromptFormat.js";
|
2
|
+
import { TextInstructionPrompt } from "./InstructionPrompt.js";
|
3
3
|
/**
|
4
4
|
* Formats a text prompt as an Alpaca prompt.
|
5
5
|
*/
|
@@ -40,7 +40,7 @@ export declare function text(): TextGenerationPromptFormat<string, string>;
|
|
40
40
|
*
|
41
41
|
* @see https://github.com/tatsu-lab/stanford_alpaca#data-release
|
42
42
|
*/
|
43
|
-
export declare function instruction(): TextGenerationPromptFormat<
|
43
|
+
export declare function instruction(): TextGenerationPromptFormat<TextInstructionPrompt & {
|
44
44
|
input?: string;
|
45
45
|
}, // optional input supported by Alpaca
|
46
46
|
string>;
|