modelfusion 0.130.1 → 0.131.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/CHANGELOG.md +58 -0
- package/README.md +19 -19
- package/core/api/postToApi.cjs +1 -1
- package/core/api/postToApi.js +1 -1
- package/core/schema/JsonSchemaProducer.d.ts +1 -1
- package/core/schema/Schema.d.ts +7 -7
- package/core/schema/TypeValidationError.cjs +5 -5
- package/core/schema/TypeValidationError.d.ts +4 -4
- package/core/schema/TypeValidationError.js +5 -5
- package/core/schema/UncheckedSchema.cjs +2 -2
- package/core/schema/UncheckedSchema.d.ts +5 -5
- package/core/schema/UncheckedSchema.js +2 -2
- package/core/schema/ZodSchema.cjs +5 -2
- package/core/schema/ZodSchema.d.ts +8 -8
- package/core/schema/ZodSchema.js +5 -2
- package/core/schema/parseJSON.cjs +6 -6
- package/core/schema/parseJSON.d.ts +3 -3
- package/core/schema/parseJSON.js +6 -6
- package/core/schema/validateTypes.cjs +13 -13
- package/core/schema/validateTypes.d.ts +9 -9
- package/core/schema/validateTypes.js +13 -13
- package/model-function/ModelCallEvent.d.ts +4 -4
- package/model-function/PromptTemplate.d.ts +2 -2
- package/model-function/{generate-structure/StructureFromTextGenerationModel.cjs → generate-object/ObjectFromTextGenerationModel.cjs} +8 -8
- package/model-function/{generate-structure/StructureFromTextGenerationModel.d.ts → generate-object/ObjectFromTextGenerationModel.d.ts} +6 -6
- package/model-function/{generate-structure/StructureFromTextGenerationModel.js → generate-object/ObjectFromTextGenerationModel.js} +6 -6
- package/model-function/{generate-structure/StructureFromTextPromptTemplate.d.ts → generate-object/ObjectFromTextPromptTemplate.d.ts} +4 -4
- package/model-function/{generate-structure/StructureFromTextStreamingModel.cjs → generate-object/ObjectFromTextStreamingModel.cjs} +10 -10
- package/model-function/generate-object/ObjectFromTextStreamingModel.d.ts +19 -0
- package/model-function/{generate-structure/StructureFromTextStreamingModel.js → generate-object/ObjectFromTextStreamingModel.js} +8 -8
- package/model-function/{generate-structure/StructureGenerationEvent.d.ts → generate-object/ObjectGenerationEvent.d.ts} +6 -6
- package/model-function/generate-object/ObjectGenerationModel.d.ts +24 -0
- package/model-function/{generate-structure/StructureParseError.cjs → generate-object/ObjectParseError.cjs} +5 -5
- package/model-function/{generate-structure/StructureParseError.d.ts → generate-object/ObjectParseError.d.ts} +1 -1
- package/model-function/{generate-structure/StructureParseError.js → generate-object/ObjectParseError.js} +3 -3
- package/model-function/generate-object/ObjectStream.cjs +43 -0
- package/model-function/generate-object/ObjectStream.d.ts +18 -0
- package/model-function/generate-object/ObjectStream.js +38 -0
- package/model-function/generate-object/ObjectStreamingEvent.d.ts +7 -0
- package/model-function/{generate-structure/StructureValidationError.cjs → generate-object/ObjectValidationError.cjs} +5 -5
- package/model-function/{generate-structure/StructureValidationError.d.ts → generate-object/ObjectValidationError.d.ts} +1 -1
- package/model-function/{generate-structure/StructureValidationError.js → generate-object/ObjectValidationError.js} +3 -3
- package/model-function/{generate-structure/generateStructure.cjs → generate-object/generateObject.cjs} +11 -12
- package/model-function/generate-object/generateObject.d.ts +56 -0
- package/model-function/{generate-structure/generateStructure.js → generate-object/generateObject.js} +9 -10
- package/model-function/{generate-structure → generate-object}/index.cjs +12 -11
- package/model-function/generate-object/index.d.ts +12 -0
- package/model-function/generate-object/index.js +12 -0
- package/model-function/{generate-structure/jsonStructurePrompt.cjs → generate-object/jsonObjectPrompt.cjs} +6 -6
- package/model-function/{generate-structure/jsonStructurePrompt.d.ts → generate-object/jsonObjectPrompt.d.ts} +6 -6
- package/model-function/{generate-structure/jsonStructurePrompt.js → generate-object/jsonObjectPrompt.js} +5 -5
- package/model-function/generate-object/streamObject.cjs +80 -0
- package/model-function/generate-object/streamObject.d.ts +57 -0
- package/model-function/generate-object/streamObject.js +76 -0
- package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +3 -3
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +3 -3
- package/model-function/generate-text/PromptTemplateTextGenerationModel.js +3 -3
- package/model-function/generate-text/PromptTemplateTextStreamingModel.cjs +3 -3
- package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +3 -3
- package/model-function/generate-text/PromptTemplateTextStreamingModel.js +3 -3
- package/model-function/generate-text/TextGenerationPromptTemplate.d.ts +1 -1
- package/model-function/index.cjs +1 -1
- package/model-function/index.d.ts +1 -1
- package/model-function/index.js +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.js +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +1 -1
- package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.cjs +1 -1
- package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.js +1 -1
- package/model-provider/llamacpp/LlamaCppCompletionModel.cjs +5 -5
- package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +3 -3
- package/model-provider/llamacpp/LlamaCppCompletionModel.js +5 -5
- package/model-provider/mistral/MistralChatModel.cjs +1 -1
- package/model-provider/mistral/MistralChatModel.js +1 -1
- package/model-provider/ollama/OllamaChatModel.cjs +7 -7
- package/model-provider/ollama/OllamaChatModel.d.ts +3 -3
- package/model-provider/ollama/OllamaChatModel.js +7 -7
- package/model-provider/ollama/OllamaCompletionModel.cjs +7 -7
- package/model-provider/ollama/OllamaCompletionModel.d.ts +3 -3
- package/model-provider/ollama/OllamaCompletionModel.js +7 -7
- package/model-provider/ollama/OllamaCompletionModel.test.cjs +8 -6
- package/model-provider/ollama/OllamaCompletionModel.test.js +8 -6
- package/model-provider/openai/AbstractOpenAIChatModel.cjs +1 -1
- package/model-provider/openai/AbstractOpenAIChatModel.js +1 -1
- package/model-provider/openai/AbstractOpenAICompletionModel.cjs +1 -1
- package/model-provider/openai/AbstractOpenAICompletionModel.js +1 -1
- package/model-provider/openai/{OpenAIChatFunctionCallStructureGenerationModel.cjs → OpenAIChatFunctionCallObjectGenerationModel.cjs} +12 -12
- package/model-provider/openai/{OpenAIChatFunctionCallStructureGenerationModel.d.ts → OpenAIChatFunctionCallObjectGenerationModel.d.ts} +10 -10
- package/model-provider/openai/{OpenAIChatFunctionCallStructureGenerationModel.js → OpenAIChatFunctionCallObjectGenerationModel.js} +10 -10
- package/model-provider/openai/OpenAIChatModel.cjs +7 -7
- package/model-provider/openai/OpenAIChatModel.d.ts +6 -6
- package/model-provider/openai/OpenAIChatModel.js +7 -7
- package/model-provider/openai/OpenAIChatModel.test.cjs +7 -5
- package/model-provider/openai/OpenAIChatModel.test.js +7 -5
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +4 -4
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +3 -3
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +4 -4
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +3 -3
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +3 -3
- package/package.json +1 -1
- package/tool/WebSearchTool.cjs +1 -1
- package/tool/WebSearchTool.js +1 -1
- package/tool/generate-tool-call/generateToolCall.cjs +1 -1
- package/tool/generate-tool-call/generateToolCall.js +1 -1
- package/tool/generate-tool-calls/generateToolCalls.cjs +1 -1
- package/tool/generate-tool-calls/generateToolCalls.js +1 -1
- package/{model-function/generate-structure → util}/fixJson.test.cjs +1 -1
- package/{model-function/generate-structure → util}/fixJson.test.js +1 -1
- package/util/isDeepEqualData.cjs +1 -1
- package/util/isDeepEqualData.d.ts +1 -1
- package/util/isDeepEqualData.js +1 -1
- package/util/streaming/parseEventSourceStreamAsAsyncIterable.cjs +1 -1
- package/util/streaming/parseEventSourceStreamAsAsyncIterable.js +1 -1
- package/vector-index/memory/MemoryVectorIndex.cjs +1 -1
- package/vector-index/memory/MemoryVectorIndex.js +1 -1
- package/model-function/generate-structure/StructureFromTextStreamingModel.d.ts +0 -19
- package/model-function/generate-structure/StructureGenerationModel.d.ts +0 -24
- package/model-function/generate-structure/StructureStreamingEvent.d.ts +0 -7
- package/model-function/generate-structure/generateStructure.d.ts +0 -56
- package/model-function/generate-structure/index.d.ts +0 -11
- package/model-function/generate-structure/index.js +0 -11
- package/model-function/generate-structure/streamStructure.cjs +0 -61
- package/model-function/generate-structure/streamStructure.d.ts +0 -67
- package/model-function/generate-structure/streamStructure.js +0 -57
- /package/model-function/{generate-structure/StructureFromTextPromptTemplate.cjs → generate-object/ObjectFromTextPromptTemplate.cjs} +0 -0
- /package/model-function/{generate-structure/StructureFromTextPromptTemplate.js → generate-object/ObjectFromTextPromptTemplate.js} +0 -0
- /package/model-function/{generate-structure/StructureGenerationEvent.cjs → generate-object/ObjectGenerationEvent.cjs} +0 -0
- /package/model-function/{generate-structure/StructureGenerationEvent.js → generate-object/ObjectGenerationEvent.js} +0 -0
- /package/model-function/{generate-structure/StructureGenerationModel.cjs → generate-object/ObjectGenerationModel.cjs} +0 -0
- /package/model-function/{generate-structure/StructureGenerationModel.js → generate-object/ObjectGenerationModel.js} +0 -0
- /package/model-function/{generate-structure/StructureStreamingEvent.cjs → generate-object/ObjectStreamingEvent.cjs} +0 -0
- /package/model-function/{generate-structure/StructureStreamingEvent.js → generate-object/ObjectStreamingEvent.js} +0 -0
- /package/{model-function/generate-structure → util}/fixJson.cjs +0 -0
- /package/{model-function/generate-structure → util}/fixJson.d.ts +0 -0
- /package/{model-function/generate-structure → util}/fixJson.js +0 -0
- /package/{model-function/generate-structure → util}/fixJson.test.d.ts +0 -0
- /package/{model-function/generate-structure → util}/parsePartialJson.cjs +0 -0
- /package/{model-function/generate-structure → util}/parsePartialJson.d.ts +0 -0
- /package/{model-function/generate-structure → util}/parsePartialJson.js +0 -0
@@ -148,7 +148,7 @@ const fixJson_js_1 = require("./fixJson.cjs");
|
|
148
148
|
(0, vitest_1.test)("should handle nested arrays and objects", () => {
|
149
149
|
node_assert_1.default.strictEqual((0, fixJson_js_1.fixJson)('{"a": {"b": ["c", {"d": "e",'), '{"a": {"b": ["c", {"d": "e"}]}}');
|
150
150
|
});
|
151
|
-
(0, vitest_1.test)("should handle deeply nested
|
151
|
+
(0, vitest_1.test)("should handle deeply nested objects", () => {
|
152
152
|
node_assert_1.default.strictEqual((0, fixJson_js_1.fixJson)('{"a": {"b": {"c": {"d":'), '{"a": {"b": {"c": {}}}}');
|
153
153
|
});
|
154
154
|
(0, vitest_1.test)("should handle potential nested arrays or objects", () => {
|
@@ -143,7 +143,7 @@ describe("nesting", () => {
|
|
143
143
|
test("should handle nested arrays and objects", () => {
|
144
144
|
assert.strictEqual(fixJson('{"a": {"b": ["c", {"d": "e",'), '{"a": {"b": ["c", {"d": "e"}]}}');
|
145
145
|
});
|
146
|
-
test("should handle deeply nested
|
146
|
+
test("should handle deeply nested objects", () => {
|
147
147
|
assert.strictEqual(fixJson('{"a": {"b": {"c": {"d":'), '{"a": {"b": {"c": {}}}}');
|
148
148
|
});
|
149
149
|
test("should handle potential nested arrays or objects", () => {
|
package/util/isDeepEqualData.cjs
CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isDeepEqualData = void 0;
|
4
4
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
5
5
|
/**
|
6
|
-
* Performs a deep-equal comparison of two parsed JSON
|
6
|
+
* Performs a deep-equal comparison of two parsed JSON objects.
|
7
7
|
*
|
8
8
|
* @param {any} obj1 - The first object to compare.
|
9
9
|
* @param {any} obj2 - The second object to compare.
|
package/util/isDeepEqualData.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
2
|
/**
|
3
|
-
* Performs a deep-equal comparison of two parsed JSON
|
3
|
+
* Performs a deep-equal comparison of two parsed JSON objects.
|
4
4
|
*
|
5
5
|
* @param {any} obj1 - The first object to compare.
|
6
6
|
* @param {any} obj2 - The second object to compare.
|
@@ -29,7 +29,7 @@ async function parseEventSourceStreamAsAsyncIterable({ stream, schema, }) {
|
|
29
29
|
// chunks that are not parsable, and ModelFusion should be resilient to that.
|
30
30
|
continue;
|
31
31
|
}
|
32
|
-
const completionChunk = parseResult.
|
32
|
+
const completionChunk = parseResult.value;
|
33
33
|
queue.push({
|
34
34
|
type: "delta",
|
35
35
|
deltaValue: completionChunk,
|
@@ -26,7 +26,7 @@ export async function parseEventSourceStreamAsAsyncIterable({ stream, schema, })
|
|
26
26
|
// chunks that are not parsable, and ModelFusion should be resilient to that.
|
27
27
|
continue;
|
28
28
|
}
|
29
|
-
const completionChunk = parseResult.
|
29
|
+
const completionChunk = parseResult.value;
|
30
30
|
queue.push({
|
31
31
|
type: "delta",
|
32
32
|
deltaValue: completionChunk,
|
@@ -25,7 +25,7 @@ class MemoryVectorIndex {
|
|
25
25
|
});
|
26
26
|
}
|
27
27
|
static async deserialize({ serializedData, schema, }) {
|
28
|
-
// validate the outer
|
28
|
+
// validate the outer value:
|
29
29
|
const json = (0, parseJSON_js_1.parseJSON)({ text: serializedData, schema: jsonDataSchema });
|
30
30
|
if (schema != null) {
|
31
31
|
// when a schema is provided, validate all entries:
|
@@ -22,7 +22,7 @@ export class MemoryVectorIndex {
|
|
22
22
|
});
|
23
23
|
}
|
24
24
|
static async deserialize({ serializedData, schema, }) {
|
25
|
-
// validate the outer
|
25
|
+
// validate the outer value:
|
26
26
|
const json = parseJSON({ text: serializedData, schema: jsonDataSchema });
|
27
27
|
if (schema != null) {
|
28
28
|
// when a schema is provided, validate all entries:
|
@@ -1,19 +0,0 @@
|
|
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 { StructureFromTextPromptTemplate } from "./StructureFromTextPromptTemplate.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
|
-
template: StructureFromTextPromptTemplate<SOURCE_PROMPT, TARGET_PROMPT>;
|
14
|
-
});
|
15
|
-
doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<AsyncQueue<Delta<string>>>;
|
16
|
-
extractStructureTextDelta(delta: unknown): string;
|
17
|
-
parseAccumulatedStructureText(accumulatedText: string): unknown;
|
18
|
-
withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
|
19
|
-
}
|
@@ -1,24 +0,0 @@
|
|
1
|
-
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
2
|
-
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
3
|
-
import { Schema } from "../../core/schema/Schema.js";
|
4
|
-
import { Delta } from "../../model-function/Delta.js";
|
5
|
-
import { Model, ModelSettings } from "../Model.js";
|
6
|
-
export interface StructureGenerationModelSettings extends ModelSettings {
|
7
|
-
}
|
8
|
-
export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends Model<SETTINGS> {
|
9
|
-
doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
10
|
-
rawResponse: unknown;
|
11
|
-
valueText: string;
|
12
|
-
value: unknown;
|
13
|
-
usage?: {
|
14
|
-
promptTokens: number;
|
15
|
-
completionTokens: number;
|
16
|
-
totalTokens: number;
|
17
|
-
};
|
18
|
-
}>;
|
19
|
-
}
|
20
|
-
export interface StructureStreamingModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends StructureGenerationModel<PROMPT, SETTINGS> {
|
21
|
-
doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<unknown>>>;
|
22
|
-
extractStructureTextDelta(delta: unknown): string | undefined;
|
23
|
-
parseAccumulatedStructureText(accumulatedText: string): unknown;
|
24
|
-
}
|
@@ -1,7 +0,0 @@
|
|
1
|
-
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
-
export interface StructureStreamingStartedEvent extends BaseModelCallStartedEvent {
|
3
|
-
functionType: "stream-structure";
|
4
|
-
}
|
5
|
-
export interface StructureStreamingFinishedEvent extends BaseModelCallFinishedEvent {
|
6
|
-
functionType: "stream-structure";
|
7
|
-
}
|
@@ -1,56 +0,0 @@
|
|
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 { ModelCallMetadata } from "../ModelCallMetadata.js";
|
5
|
-
import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
|
6
|
-
/**
|
7
|
-
* Generate a typed object for a prompt and a schema.
|
8
|
-
*
|
9
|
-
* @see https://modelfusion.dev/guide/function/generate-structure
|
10
|
-
*
|
11
|
-
* @example
|
12
|
-
* const sentiment = await generateStructure({
|
13
|
-
* model: openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
|
14
|
-
*
|
15
|
-
* schema: zodSchema(z.object({
|
16
|
-
* sentiment: z
|
17
|
-
* .enum(["positive", "neutral", "negative"])
|
18
|
-
* .describe("Sentiment."),
|
19
|
-
* })),
|
20
|
-
*
|
21
|
-
* prompt: [
|
22
|
-
* openai.ChatMessage.system(
|
23
|
-
* "You are a sentiment evaluator. " +
|
24
|
-
* "Analyze the sentiment of the following product review:"
|
25
|
-
* ),
|
26
|
-
* openai.ChatMessage.user(
|
27
|
-
* "After I opened the package, I was met by a very unpleasant smell " +
|
28
|
-
* "that did not disappear even after washing. Never again!"
|
29
|
-
* ),
|
30
|
-
* ]
|
31
|
-
* });
|
32
|
-
*
|
33
|
-
* @param {StructureGenerationModel<PROMPT, SETTINGS>} model - The model to generate the structure.
|
34
|
-
* @param {Schema<STRUCTURE>} schema - The schema to be used.
|
35
|
-
* @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
|
36
|
-
* The prompt to be used.
|
37
|
-
* You can also pass a function that takes the schema as an argument and returns the prompt.
|
38
|
-
*
|
39
|
-
* @returns {Promise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
|
40
|
-
*/
|
41
|
-
export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(args: {
|
42
|
-
model: StructureGenerationModel<PROMPT, SETTINGS>;
|
43
|
-
schema: Schema<STRUCTURE> & JsonSchemaProducer;
|
44
|
-
prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
|
45
|
-
fullResponse?: false;
|
46
|
-
} & FunctionOptions): Promise<STRUCTURE>;
|
47
|
-
export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(args: {
|
48
|
-
model: StructureGenerationModel<PROMPT, SETTINGS>;
|
49
|
-
schema: Schema<STRUCTURE> & JsonSchemaProducer;
|
50
|
-
prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
|
51
|
-
fullResponse: true;
|
52
|
-
} & FunctionOptions): Promise<{
|
53
|
-
structure: STRUCTURE;
|
54
|
-
rawResponse: unknown;
|
55
|
-
metadata: ModelCallMetadata;
|
56
|
-
}>;
|
@@ -1,11 +0,0 @@
|
|
1
|
-
export * from "./StructureFromTextGenerationModel.js";
|
2
|
-
export * from "./StructureFromTextPromptTemplate.js";
|
3
|
-
export * from "./StructureFromTextStreamingModel.js";
|
4
|
-
export * from "./StructureGenerationEvent.js";
|
5
|
-
export * from "./StructureGenerationModel.js";
|
6
|
-
export * from "./StructureParseError.js";
|
7
|
-
export * from "./StructureStreamingEvent.js";
|
8
|
-
export * from "./StructureValidationError.js";
|
9
|
-
export * from "./generateStructure.js";
|
10
|
-
export * from "./jsonStructurePrompt.js";
|
11
|
-
export * from "./streamStructure.js";
|
@@ -1,11 +0,0 @@
|
|
1
|
-
export * from "./StructureFromTextGenerationModel.js";
|
2
|
-
export * from "./StructureFromTextPromptTemplate.js";
|
3
|
-
export * from "./StructureFromTextStreamingModel.js";
|
4
|
-
export * from "./StructureGenerationEvent.js";
|
5
|
-
export * from "./StructureGenerationModel.js";
|
6
|
-
export * from "./StructureParseError.js";
|
7
|
-
export * from "./StructureStreamingEvent.js";
|
8
|
-
export * from "./StructureValidationError.js";
|
9
|
-
export * from "./generateStructure.js";
|
10
|
-
export * from "./jsonStructurePrompt.js";
|
11
|
-
export * from "./streamStructure.js";
|
@@ -1,61 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.streamStructure = void 0;
|
4
|
-
const isDeepEqualData_js_1 = require("../../util/isDeepEqualData.cjs");
|
5
|
-
const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
|
6
|
-
async function streamStructure({ model, schema, prompt, fullResponse, ...options }) {
|
7
|
-
// Note: PROMPT must not be a function.
|
8
|
-
const expandedPrompt = typeof prompt === "function"
|
9
|
-
? prompt(schema)
|
10
|
-
: prompt;
|
11
|
-
let accumulatedText = "";
|
12
|
-
let lastStructure;
|
13
|
-
let resolveStructure;
|
14
|
-
let rejectStructure;
|
15
|
-
const structurePromise = new Promise((resolve, reject) => {
|
16
|
-
resolveStructure = resolve;
|
17
|
-
rejectStructure = reject;
|
18
|
-
});
|
19
|
-
const callResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
|
20
|
-
functionType: "stream-structure",
|
21
|
-
input: {
|
22
|
-
schema,
|
23
|
-
prompt: expandedPrompt,
|
24
|
-
},
|
25
|
-
model,
|
26
|
-
options,
|
27
|
-
startStream: async (options) => model.doStreamStructure(schema, expandedPrompt, options),
|
28
|
-
processDelta: (delta) => {
|
29
|
-
const textDelta = model.extractStructureTextDelta(delta.deltaValue);
|
30
|
-
if (textDelta == null) {
|
31
|
-
return undefined;
|
32
|
-
}
|
33
|
-
accumulatedText += textDelta;
|
34
|
-
const latestStructure = model.parseAccumulatedStructureText(accumulatedText);
|
35
|
-
// only send a new part into the stream when the partial structure has changed:
|
36
|
-
if (!(0, isDeepEqualData_js_1.isDeepEqualData)(lastStructure, latestStructure)) {
|
37
|
-
lastStructure = latestStructure;
|
38
|
-
return lastStructure;
|
39
|
-
}
|
40
|
-
return undefined;
|
41
|
-
},
|
42
|
-
onDone: () => {
|
43
|
-
// process the final result (full type validation):
|
44
|
-
const parseResult = schema.validate(lastStructure);
|
45
|
-
if (parseResult.success) {
|
46
|
-
resolveStructure(parseResult.data);
|
47
|
-
}
|
48
|
-
else {
|
49
|
-
rejectStructure(parseResult.error);
|
50
|
-
}
|
51
|
-
},
|
52
|
-
});
|
53
|
-
return fullResponse
|
54
|
-
? {
|
55
|
-
structureStream: callResponse.value,
|
56
|
-
structurePromise,
|
57
|
-
metadata: callResponse.metadata,
|
58
|
-
}
|
59
|
-
: callResponse.value;
|
60
|
-
}
|
61
|
-
exports.streamStructure = streamStructure;
|
@@ -1,67 +0,0 @@
|
|
1
|
-
import type { PartialDeep } from "type-fest";
|
2
|
-
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
3
|
-
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
4
|
-
import { Schema } from "../../core/schema/Schema.js";
|
5
|
-
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
6
|
-
import { StructureStreamingModel } from "./StructureGenerationModel.js";
|
7
|
-
/**
|
8
|
-
* Generate and stream an object for a prompt and a structure definition.
|
9
|
-
*
|
10
|
-
* For the OpenAI chat model, this generates and parses a function call with a single function.
|
11
|
-
*
|
12
|
-
* @see https://modelfusion.dev/guide/function/generate-structure
|
13
|
-
*
|
14
|
-
* @example
|
15
|
-
* const structureStream = await streamStructure({
|
16
|
-
* structureGenerator: openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
|
17
|
-
* schema: zodSchema(
|
18
|
-
* z.array(
|
19
|
-
* z.object({
|
20
|
-
* name: z.string(),
|
21
|
-
* class: z
|
22
|
-
* .string()
|
23
|
-
* .describe("Character class, e.g. warrior, mage, or thief."),
|
24
|
-
* description: z.string(),
|
25
|
-
* })
|
26
|
-
* ),
|
27
|
-
* prompt: [
|
28
|
-
* openai.ChatMessage.user(
|
29
|
-
* "Generate 3 character descriptions for a fantasy role playing game."
|
30
|
-
* ),
|
31
|
-
* ]
|
32
|
-
* });
|
33
|
-
*
|
34
|
-
* for await (const partialStructure of structureStream) {
|
35
|
-
* // ...
|
36
|
-
* }
|
37
|
-
*
|
38
|
-
* @param {StructureStreamingModel<PROMPT>} structureGenerator - The model to use for streaming
|
39
|
-
* @param {Schema<STRUCTURE>} schema - The schema to be used.
|
40
|
-
* @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
|
41
|
-
* The prompt to be used.
|
42
|
-
* You can also pass a function that takes the schema as an argument and returns the prompt.
|
43
|
-
*
|
44
|
-
* @returns {AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>}
|
45
|
-
* The async iterable result promise.
|
46
|
-
* Each part of the stream is a partial structure.
|
47
|
-
*/
|
48
|
-
export declare function streamStructure<STRUCTURE, PROMPT>(args: {
|
49
|
-
model: StructureStreamingModel<PROMPT>;
|
50
|
-
schema: Schema<STRUCTURE> & JsonSchemaProducer;
|
51
|
-
prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
|
52
|
-
fullResponse?: false;
|
53
|
-
} & FunctionOptions): Promise<AsyncIterable<PartialDeep<STRUCTURE, {
|
54
|
-
recurseIntoArrays: true;
|
55
|
-
}>>>;
|
56
|
-
export declare function streamStructure<STRUCTURE, PROMPT>(args: {
|
57
|
-
model: StructureStreamingModel<PROMPT>;
|
58
|
-
schema: Schema<STRUCTURE> & JsonSchemaProducer;
|
59
|
-
prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
|
60
|
-
fullResponse: true;
|
61
|
-
} & FunctionOptions): Promise<{
|
62
|
-
structureStream: AsyncIterable<PartialDeep<STRUCTURE, {
|
63
|
-
recurseIntoArrays: true;
|
64
|
-
}>>;
|
65
|
-
structurePromise: PromiseLike<STRUCTURE>;
|
66
|
-
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
67
|
-
}>;
|
@@ -1,57 +0,0 @@
|
|
1
|
-
import { isDeepEqualData } from "../../util/isDeepEqualData.js";
|
2
|
-
import { executeStreamCall } from "../executeStreamCall.js";
|
3
|
-
export async function streamStructure({ model, schema, prompt, fullResponse, ...options }) {
|
4
|
-
// Note: PROMPT must not be a function.
|
5
|
-
const expandedPrompt = typeof prompt === "function"
|
6
|
-
? prompt(schema)
|
7
|
-
: prompt;
|
8
|
-
let accumulatedText = "";
|
9
|
-
let lastStructure;
|
10
|
-
let resolveStructure;
|
11
|
-
let rejectStructure;
|
12
|
-
const structurePromise = new Promise((resolve, reject) => {
|
13
|
-
resolveStructure = resolve;
|
14
|
-
rejectStructure = reject;
|
15
|
-
});
|
16
|
-
const callResponse = await executeStreamCall({
|
17
|
-
functionType: "stream-structure",
|
18
|
-
input: {
|
19
|
-
schema,
|
20
|
-
prompt: expandedPrompt,
|
21
|
-
},
|
22
|
-
model,
|
23
|
-
options,
|
24
|
-
startStream: async (options) => model.doStreamStructure(schema, expandedPrompt, options),
|
25
|
-
processDelta: (delta) => {
|
26
|
-
const textDelta = model.extractStructureTextDelta(delta.deltaValue);
|
27
|
-
if (textDelta == null) {
|
28
|
-
return undefined;
|
29
|
-
}
|
30
|
-
accumulatedText += textDelta;
|
31
|
-
const latestStructure = model.parseAccumulatedStructureText(accumulatedText);
|
32
|
-
// only send a new part into the stream when the partial structure has changed:
|
33
|
-
if (!isDeepEqualData(lastStructure, latestStructure)) {
|
34
|
-
lastStructure = latestStructure;
|
35
|
-
return lastStructure;
|
36
|
-
}
|
37
|
-
return undefined;
|
38
|
-
},
|
39
|
-
onDone: () => {
|
40
|
-
// process the final result (full type validation):
|
41
|
-
const parseResult = schema.validate(lastStructure);
|
42
|
-
if (parseResult.success) {
|
43
|
-
resolveStructure(parseResult.data);
|
44
|
-
}
|
45
|
-
else {
|
46
|
-
rejectStructure(parseResult.error);
|
47
|
-
}
|
48
|
-
},
|
49
|
-
});
|
50
|
-
return fullResponse
|
51
|
-
? {
|
52
|
-
structureStream: callResponse.value,
|
53
|
-
structurePromise,
|
54
|
-
metadata: callResponse.metadata,
|
55
|
-
}
|
56
|
-
: callResponse.value;
|
57
|
-
}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|