modelfusion 0.40.1 → 0.41.1
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 +14 -7
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +3 -3
- package/core/FunctionEvent.d.ts +1 -1
- package/model-function/AsyncIterableResultPromise.d.ts +1 -1
- package/model-function/Delta.d.ts +8 -0
- package/model-function/ModelCallEvent.d.ts +1 -1
- package/model-function/ModelCallMetadata.d.ts +13 -0
- package/model-function/describe-image/ImageDescriptionEvent.d.ts +1 -1
- package/model-function/describe-image/ImageDescriptionModel.d.ts +6 -4
- package/model-function/describe-image/describeImage.cjs +7 -2
- package/model-function/describe-image/describeImage.d.ts +2 -2
- package/model-function/describe-image/describeImage.js +7 -2
- package/model-function/embed/EmbeddingEvent.d.ts +1 -1
- package/model-function/embed/EmbeddingModel.d.ts +6 -4
- package/model-function/embed/embed.cjs +16 -11
- package/model-function/embed/embed.d.ts +3 -3
- package/model-function/embed/embed.js +16 -11
- package/model-function/executeCall.cjs +26 -30
- package/model-function/executeCall.d.ts +19 -28
- package/model-function/executeCall.js +26 -30
- package/model-function/generate-image/ImageGenerationEvent.d.ts +1 -1
- package/model-function/generate-image/ImageGenerationModel.d.ts +6 -4
- package/model-function/generate-image/generateImage.cjs +7 -2
- package/model-function/generate-image/generateImage.d.ts +2 -2
- package/model-function/generate-image/generateImage.js +7 -2
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +6 -5
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +7 -5
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +6 -5
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +1 -1
- package/model-function/generate-structure/StructureGenerationModel.d.ts +15 -18
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +19 -17
- package/model-function/generate-structure/generateStructure.cjs +10 -8
- package/model-function/generate-structure/generateStructure.d.ts +2 -2
- package/model-function/generate-structure/generateStructure.js +10 -8
- package/model-function/generate-structure/generateStructureOrText.cjs +15 -8
- package/model-function/generate-structure/generateStructureOrText.d.ts +4 -4
- package/model-function/generate-structure/generateStructureOrText.js +15 -8
- package/model-function/generate-structure/streamStructure.cjs +4 -16
- package/model-function/generate-structure/streamStructure.d.ts +3 -7
- package/model-function/generate-structure/streamStructure.js +4 -16
- package/model-function/generate-text/TextGenerationEvent.d.ts +1 -1
- package/model-function/generate-text/TextGenerationModel.d.ts +18 -19
- package/model-function/generate-text/generateText.cjs +8 -9
- package/model-function/generate-text/generateText.d.ts +2 -2
- package/model-function/generate-text/generateText.js +8 -9
- package/model-function/generate-text/streamText.cjs +8 -21
- package/model-function/generate-text/streamText.d.ts +3 -7
- package/model-function/generate-text/streamText.js +8 -21
- package/model-function/index.cjs +2 -2
- package/model-function/index.d.ts +2 -2
- package/model-function/index.js +2 -2
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +1 -1
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +3 -3
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +7 -2
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -2
- package/model-function/synthesize-speech/synthesizeSpeech.js +7 -2
- package/model-function/transcribe-speech/TranscriptionEvent.d.ts +1 -1
- package/model-function/transcribe-speech/TranscriptionModel.d.ts +6 -4
- package/model-function/transcribe-speech/transcribe.cjs +7 -2
- package/model-function/transcribe-speech/transcribe.d.ts +2 -2
- package/model-function/transcribe-speech/transcribe.js +7 -2
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +14 -18
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +11 -9
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +14 -18
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +13 -16
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +12 -10
- package/model-provider/cohere/CohereTextEmbeddingModel.js +13 -16
- package/model-provider/cohere/CohereTextGenerationModel.cjs +25 -28
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +24 -22
- package/model-provider/cohere/CohereTextGenerationModel.js +25 -28
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.cjs +10 -17
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +2 -2
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.js +10 -17
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.cjs +13 -16
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +9 -7
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.js +13 -16
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +19 -25
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +8 -6
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +19 -25
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +18 -24
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +10 -8
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +18 -24
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +13 -16
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +8 -6
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +13 -16
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +27 -33
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +62 -60
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +27 -33
- package/model-provider/lmnt/LmntSpeechSynthesisModel.cjs +7 -12
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +2 -2
- package/model-provider/lmnt/LmntSpeechSynthesisModel.js +7 -12
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +8 -16
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +11 -11
- package/model-provider/openai/OpenAIImageGenerationModel.js +8 -16
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +18 -24
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +18 -16
- package/model-provider/openai/OpenAITextEmbeddingModel.js +18 -24
- package/model-provider/openai/OpenAITextGenerationModel.cjs +19 -26
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +31 -33
- package/model-provider/openai/OpenAITextGenerationModel.js +19 -26
- package/model-provider/openai/OpenAITranscriptionModel.cjs +19 -28
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +27 -7
- package/model-provider/openai/OpenAITranscriptionModel.js +19 -28
- package/model-provider/openai/chat/OpenAIChatModel.cjs +76 -85
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +127 -50
- package/model-provider/openai/chat/OpenAIChatModel.js +77 -86
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +4 -3
- package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +2 -1
- package/model-provider/stability/StabilityImageGenerationModel.cjs +16 -21
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +13 -11
- package/model-provider/stability/StabilityImageGenerationModel.js +16 -21
- package/package.json +1 -1
- package/prompt/PromptFormatTextGenerationModel.cjs +2 -18
- package/prompt/PromptFormatTextGenerationModel.d.ts +14 -10
- package/prompt/PromptFormatTextGenerationModel.js +2 -18
- package/prompt/PromptFormatTextStreamingModel.cjs +31 -0
- package/prompt/PromptFormatTextStreamingModel.d.ts +13 -0
- package/prompt/PromptFormatTextStreamingModel.js +27 -0
- package/prompt/chat/trimChatPrompt.d.ts +2 -2
- package/prompt/index.cjs +1 -0
- package/prompt/index.d.ts +1 -0
- package/prompt/index.js +1 -0
- package/retriever/Retriever.d.ts +3 -6
- package/retriever/retrieve.cjs +2 -2
- package/retriever/retrieve.d.ts +3 -3
- package/retriever/retrieve.js +2 -2
- package/tool/executeTool.cjs +2 -2
- package/tool/executeTool.js +2 -2
- package/tool/useTool.cjs +2 -4
- package/tool/useTool.d.ts +2 -2
- package/tool/useTool.js +2 -4
- package/tool/useToolOrGenerateText.d.ts +2 -2
- package/util/SafeResult.d.ts +1 -1
- package/util/runSafe.cjs +1 -1
- package/util/runSafe.js +1 -1
- package/vector-index/VectorIndexRetriever.cjs +0 -7
- package/vector-index/VectorIndexRetriever.d.ts +5 -5
- package/vector-index/VectorIndexRetriever.js +0 -7
- package/vector-index/upsertIntoVectorIndex.d.ts +4 -4
- package/model-function/DeltaEvent.d.ts +0 -7
- package/model-function/ModelFunctionOptions.d.ts +0 -4
- /package/model-function/{DeltaEvent.cjs → Delta.cjs} +0 -0
- /package/model-function/{DeltaEvent.js → Delta.js} +0 -0
- /package/model-function/{ModelFunctionOptions.cjs → ModelCallMetadata.cjs} +0 -0
- /package/model-function/{ModelFunctionOptions.js → ModelCallMetadata.js} +0 -0
@@ -1,8 +1,10 @@
|
|
1
|
-
import {
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { Model, ModelSettings } from "../Model.js";
|
3
3
|
export interface ImageGenerationModelSettings extends ModelSettings {
|
4
4
|
}
|
5
|
-
export interface ImageGenerationModel<PROMPT,
|
6
|
-
|
7
|
-
|
5
|
+
export interface ImageGenerationModel<PROMPT, SETTINGS extends ImageGenerationModelSettings = ImageGenerationModelSettings> extends Model<SETTINGS> {
|
6
|
+
doGenerateImage(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
7
|
+
response: unknown;
|
8
|
+
base64Image: string;
|
9
|
+
}>;
|
8
10
|
}
|
@@ -23,8 +23,13 @@ function generateImage(model, prompt, options) {
|
|
23
23
|
input: prompt,
|
24
24
|
model,
|
25
25
|
options,
|
26
|
-
generateResponse: (options) =>
|
27
|
-
|
26
|
+
generateResponse: async (options) => {
|
27
|
+
const result = await model.doGenerateImage(prompt, options);
|
28
|
+
return {
|
29
|
+
response: result.response,
|
30
|
+
extractedValue: result.base64Image,
|
31
|
+
};
|
32
|
+
},
|
28
33
|
});
|
29
34
|
}
|
30
35
|
exports.generateImage = generateImage;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { ModelFunctionPromise } from "../executeCall.js";
|
3
3
|
import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGenerationModel.js";
|
4
4
|
/**
|
@@ -16,4 +16,4 @@ import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGener
|
|
16
16
|
* ]
|
17
17
|
* );
|
18
18
|
*/
|
19
|
-
export declare function generateImage<PROMPT
|
19
|
+
export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT, ImageGenerationModelSettings>, prompt: PROMPT, options?: FunctionOptions): ModelFunctionPromise<string>;
|
@@ -20,7 +20,12 @@ export function generateImage(model, prompt, options) {
|
|
20
20
|
input: prompt,
|
21
21
|
model,
|
22
22
|
options,
|
23
|
-
generateResponse: (options) =>
|
24
|
-
|
23
|
+
generateResponse: async (options) => {
|
24
|
+
const result = await model.doGenerateImage(prompt, options);
|
25
|
+
return {
|
26
|
+
response: result.response,
|
27
|
+
extractedValue: result.base64Image,
|
28
|
+
};
|
29
|
+
},
|
25
30
|
});
|
26
31
|
}
|
@@ -28,11 +28,12 @@ class StructureFromTextGenerationModel {
|
|
28
28
|
get settingsForEvent() {
|
29
29
|
return this.model.settingsForEvent;
|
30
30
|
}
|
31
|
-
async
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
async doGenerateStructure(structure, prompt, options) {
|
32
|
+
const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, structure), options).asFullResponse();
|
33
|
+
return {
|
34
|
+
response,
|
35
|
+
structure: this.format.extractStructure(value),
|
36
|
+
};
|
36
37
|
}
|
37
38
|
withSettings(additionalSettings) {
|
38
39
|
return new StructureFromTextGenerationModel({
|
@@ -1,12 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
3
|
+
import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
|
3
4
|
import { StructureGenerationModel } from "./StructureGenerationModel.js";
|
4
|
-
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
5
5
|
export type StructureFromTextPromptFormat<PROMPT> = {
|
6
6
|
createPrompt: (prompt: PROMPT, structure: StructureDefinition<string, unknown>) => string;
|
7
7
|
extractStructure: (response: string) => unknown;
|
8
8
|
};
|
9
|
-
export declare class StructureFromTextGenerationModel<PROMPT, MODEL extends TextGenerationModel<string,
|
9
|
+
export declare class StructureFromTextGenerationModel<PROMPT, MODEL extends TextGenerationModel<string, TextGenerationModelSettings>> implements StructureGenerationModel<PROMPT, MODEL["settings"]> {
|
10
10
|
private readonly model;
|
11
11
|
private readonly format;
|
12
12
|
constructor({ model, format, }: {
|
@@ -16,7 +16,9 @@ export declare class StructureFromTextGenerationModel<PROMPT, MODEL extends Text
|
|
16
16
|
get modelInformation(): import("../ModelInformation.js").ModelInformation;
|
17
17
|
get settings(): TextGenerationModelSettings;
|
18
18
|
get settingsForEvent(): Partial<MODEL["settings"]>;
|
19
|
-
|
20
|
-
|
19
|
+
doGenerateStructure(structure: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): Promise<{
|
20
|
+
response: unknown;
|
21
|
+
structure: unknown;
|
22
|
+
}>;
|
21
23
|
withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
|
22
24
|
}
|
@@ -25,11 +25,12 @@ export class StructureFromTextGenerationModel {
|
|
25
25
|
get settingsForEvent() {
|
26
26
|
return this.model.settingsForEvent;
|
27
27
|
}
|
28
|
-
async
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
async doGenerateStructure(structure, prompt, options) {
|
29
|
+
const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, structure), options).asFullResponse();
|
30
|
+
return {
|
31
|
+
response,
|
32
|
+
structure: this.format.extractStructure(value),
|
33
|
+
};
|
33
34
|
}
|
34
35
|
withSettings(additionalSettings) {
|
35
36
|
return new StructureFromTextGenerationModel({
|
@@ -5,7 +5,7 @@ export interface StructureGenerationStartedEvent extends BaseModelCallStartedEve
|
|
5
5
|
export type StructureGenerationFinishedEventResult = {
|
6
6
|
status: "success";
|
7
7
|
response: unknown;
|
8
|
-
|
8
|
+
value: unknown;
|
9
9
|
usage?: {
|
10
10
|
promptTokens: number;
|
11
11
|
completionTokens: number;
|
@@ -1,23 +1,20 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
1
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
2
|
-
import {
|
3
|
+
import { Delta } from "../../model-function/Delta.js";
|
3
4
|
import { Model, ModelSettings } from "../Model.js";
|
4
|
-
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
5
5
|
export interface StructureGenerationModelSettings extends ModelSettings {
|
6
6
|
}
|
7
|
-
export interface StructureGenerationModel<PROMPT,
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
* Optional. Implement for streaming support.
|
21
|
-
*/
|
22
|
-
readonly extractPartialStructure?: (fullDelta: FULL_DELTA) => unknown | undefined;
|
7
|
+
export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends Model<SETTINGS> {
|
8
|
+
doGenerateStructure(structure: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
9
|
+
response: unknown;
|
10
|
+
structure: unknown;
|
11
|
+
usage?: {
|
12
|
+
promptTokens: number;
|
13
|
+
completionTokens: number;
|
14
|
+
totalTokens: number;
|
15
|
+
};
|
16
|
+
}>;
|
17
|
+
}
|
18
|
+
export interface StructureStreamingModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends StructureGenerationModel<PROMPT, SETTINGS> {
|
19
|
+
doStreamStructure(structureDefinition: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<unknown>>>;
|
23
20
|
}
|
@@ -1,22 +1,24 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
1
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
2
3
|
import { Model, ModelSettings } from "../Model.js";
|
3
|
-
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
4
4
|
export interface StructureOrTextGenerationModelSettings extends ModelSettings {
|
5
5
|
}
|
6
|
-
export interface StructureOrTextGenerationModel<PROMPT,
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
6
|
+
export interface StructureOrTextGenerationModel<PROMPT, SETTINGS extends StructureOrTextGenerationModelSettings> extends Model<SETTINGS> {
|
7
|
+
doGenerateStructureOrText(structureDefinitions: Array<StructureDefinition<string, unknown>>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
8
|
+
response: unknown;
|
9
|
+
structureAndText: {
|
10
|
+
structure: null;
|
11
|
+
value: null;
|
12
|
+
text: string;
|
13
|
+
} | {
|
14
|
+
structure: string;
|
15
|
+
value: unknown;
|
16
|
+
text: string | null;
|
17
|
+
};
|
18
|
+
usage?: {
|
19
|
+
promptTokens: number;
|
20
|
+
completionTokens: number;
|
21
|
+
totalTokens: number;
|
22
|
+
};
|
23
|
+
}>;
|
22
24
|
}
|
@@ -3,9 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateStructure = void 0;
|
4
4
|
const executeCall_js_1 = require("../executeCall.cjs");
|
5
5
|
const StructureValidationError_js_1 = require("./StructureValidationError.cjs");
|
6
|
-
function generateStructure(
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
8
|
-
model, structureDefinition, prompt, options) {
|
6
|
+
function generateStructure(model, structureDefinition, prompt, options) {
|
9
7
|
// Note: PROMPT must not be a function.
|
10
8
|
const expandedPrompt = typeof prompt === "function"
|
11
9
|
? prompt(structureDefinition)
|
@@ -15,9 +13,9 @@ model, structureDefinition, prompt, options) {
|
|
15
13
|
input: expandedPrompt,
|
16
14
|
model,
|
17
15
|
options,
|
18
|
-
generateResponse: (options) =>
|
19
|
-
|
20
|
-
const structure =
|
16
|
+
generateResponse: async (options) => {
|
17
|
+
const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
|
18
|
+
const structure = result.structure;
|
21
19
|
const parseResult = structureDefinition.schema.validate(structure);
|
22
20
|
if (!parseResult.success) {
|
23
21
|
throw new StructureValidationError_js_1.StructureValidationError({
|
@@ -26,9 +24,13 @@ model, structureDefinition, prompt, options) {
|
|
26
24
|
cause: parseResult.error,
|
27
25
|
});
|
28
26
|
}
|
29
|
-
|
27
|
+
const value = parseResult.value;
|
28
|
+
return {
|
29
|
+
response: result.response,
|
30
|
+
extractedValue: value,
|
31
|
+
usage: result.usage,
|
32
|
+
};
|
30
33
|
},
|
31
|
-
extractUsage: (result) => model.extractUsage?.(result),
|
32
34
|
});
|
33
35
|
}
|
34
36
|
exports.generateStructure = generateStructure;
|
@@ -1,5 +1,5 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
1
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
2
|
-
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
3
3
|
import { ModelFunctionPromise } from "../executeCall.js";
|
4
4
|
import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
|
5
|
-
export declare function generateStructure<STRUCTURE, PROMPT,
|
5
|
+
export declare function generateStructure<STRUCTURE, PROMPT, NAME extends string, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options?: FunctionOptions): ModelFunctionPromise<STRUCTURE>;
|
@@ -1,8 +1,6 @@
|
|
1
1
|
import { executeCall } from "../executeCall.js";
|
2
2
|
import { StructureValidationError } from "./StructureValidationError.js";
|
3
|
-
export function generateStructure(
|
4
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
5
|
-
model, structureDefinition, prompt, options) {
|
3
|
+
export function generateStructure(model, structureDefinition, prompt, options) {
|
6
4
|
// Note: PROMPT must not be a function.
|
7
5
|
const expandedPrompt = typeof prompt === "function"
|
8
6
|
? prompt(structureDefinition)
|
@@ -12,9 +10,9 @@ model, structureDefinition, prompt, options) {
|
|
12
10
|
input: expandedPrompt,
|
13
11
|
model,
|
14
12
|
options,
|
15
|
-
generateResponse: (options) =>
|
16
|
-
|
17
|
-
const structure =
|
13
|
+
generateResponse: async (options) => {
|
14
|
+
const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
|
15
|
+
const structure = result.structure;
|
18
16
|
const parseResult = structureDefinition.schema.validate(structure);
|
19
17
|
if (!parseResult.success) {
|
20
18
|
throw new StructureValidationError({
|
@@ -23,8 +21,12 @@ model, structureDefinition, prompt, options) {
|
|
23
21
|
cause: parseResult.error,
|
24
22
|
});
|
25
23
|
}
|
26
|
-
|
24
|
+
const value = parseResult.value;
|
25
|
+
return {
|
26
|
+
response: result.response,
|
27
|
+
extractedValue: value,
|
28
|
+
usage: result.usage,
|
29
|
+
};
|
27
30
|
},
|
28
|
-
extractUsage: (result) => model.extractUsage?.(result),
|
29
31
|
});
|
30
32
|
}
|
@@ -14,12 +14,16 @@ function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
|
14
14
|
input: expandedPrompt,
|
15
15
|
model,
|
16
16
|
options,
|
17
|
-
generateResponse: (options) =>
|
18
|
-
|
19
|
-
const { structure, value, text } =
|
17
|
+
generateResponse: async (options) => {
|
18
|
+
const result = await model.doGenerateStructureOrText(structureDefinitions, expandedPrompt, options);
|
19
|
+
const { structure, value, text } = result.structureAndText;
|
20
20
|
// text generation:
|
21
21
|
if (structure == null) {
|
22
|
-
return {
|
22
|
+
return {
|
23
|
+
response: result.response,
|
24
|
+
extractedValue: { structure, value, text },
|
25
|
+
usage: result.usage,
|
26
|
+
};
|
23
27
|
}
|
24
28
|
const definition = structureDefinitions.find((d) => d.name === structure);
|
25
29
|
if (definition == undefined) {
|
@@ -34,12 +38,15 @@ function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
|
34
38
|
});
|
35
39
|
}
|
36
40
|
return {
|
37
|
-
|
38
|
-
|
39
|
-
|
41
|
+
response: result.response,
|
42
|
+
extractedValue: {
|
43
|
+
structure: structure,
|
44
|
+
value: parseResult.value,
|
45
|
+
text: text, // text is string | null, which is part of the response for schema values
|
46
|
+
},
|
47
|
+
usage: result.usage,
|
40
48
|
};
|
41
49
|
},
|
42
|
-
extractUsage: (result) => model.extractUsage?.(result),
|
43
50
|
});
|
44
51
|
}
|
45
52
|
exports.generateStructureOrText = generateStructureOrText;
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import {
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
+
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
2
3
|
import { ModelFunctionPromise } from "../executeCall.js";
|
3
4
|
import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings } from "./StructureOrTextGenerationModel.js";
|
4
|
-
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
5
5
|
type StructureDefinitionArray<T extends StructureDefinition<any, any>[]> = T;
|
6
6
|
type ToStructureDefinitionMap<T extends StructureDefinitionArray<StructureDefinition<any, any>[]>> = {
|
7
7
|
[K in T[number]["name"]]: Extract<T[number], StructureDefinition<K, any>>;
|
@@ -14,9 +14,9 @@ type ToStructureUnion<T> = {
|
|
14
14
|
} : never;
|
15
15
|
}[keyof T];
|
16
16
|
type ToOutputValue<STRUCTURES extends StructureDefinitionArray<StructureDefinition<any, any>[]>> = ToStructureUnion<ToStructureDefinitionMap<STRUCTURES>>;
|
17
|
-
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT
|
17
|
+
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT>(model: StructureOrTextGenerationModel<PROMPT, StructureOrTextGenerationModelSettings>, structureDefinitions: STRUCTURES, prompt: PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT), options?: FunctionOptions): ModelFunctionPromise<{
|
18
18
|
structure: null;
|
19
19
|
value: null;
|
20
20
|
text: string;
|
21
|
-
} | ToOutputValue<STRUCTURES
|
21
|
+
} | ToOutputValue<STRUCTURES>>;
|
22
22
|
export {};
|
@@ -11,12 +11,16 @@ export function generateStructureOrText(model, structureDefinitions, prompt, opt
|
|
11
11
|
input: expandedPrompt,
|
12
12
|
model,
|
13
13
|
options,
|
14
|
-
generateResponse: (options) =>
|
15
|
-
|
16
|
-
const { structure, value, text } =
|
14
|
+
generateResponse: async (options) => {
|
15
|
+
const result = await model.doGenerateStructureOrText(structureDefinitions, expandedPrompt, options);
|
16
|
+
const { structure, value, text } = result.structureAndText;
|
17
17
|
// text generation:
|
18
18
|
if (structure == null) {
|
19
|
-
return {
|
19
|
+
return {
|
20
|
+
response: result.response,
|
21
|
+
extractedValue: { structure, value, text },
|
22
|
+
usage: result.usage,
|
23
|
+
};
|
20
24
|
}
|
21
25
|
const definition = structureDefinitions.find((d) => d.name === structure);
|
22
26
|
if (definition == undefined) {
|
@@ -31,11 +35,14 @@ export function generateStructureOrText(model, structureDefinitions, prompt, opt
|
|
31
35
|
});
|
32
36
|
}
|
33
37
|
return {
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
response: result.response,
|
39
|
+
extractedValue: {
|
40
|
+
structure: structure,
|
41
|
+
value: parseResult.value,
|
42
|
+
text: text, // text is string | null, which is part of the response for schema values
|
43
|
+
},
|
44
|
+
usage: result.usage,
|
37
45
|
};
|
38
46
|
},
|
39
|
-
extractUsage: (result) => model.extractUsage?.(result),
|
40
47
|
});
|
41
48
|
}
|
@@ -19,14 +19,6 @@ function streamStructure(model, structureDefinition, prompt, options) {
|
|
19
19
|
}
|
20
20
|
exports.streamStructure = streamStructure;
|
21
21
|
async function doStreamStructure(model, structureDefinition, prompt, options) {
|
22
|
-
if (options?.settings != null) {
|
23
|
-
model = model.withSettings(options.settings);
|
24
|
-
options = {
|
25
|
-
functionId: options.functionId,
|
26
|
-
observers: options.observers,
|
27
|
-
run: options.run,
|
28
|
-
};
|
29
|
-
}
|
30
22
|
const run = options?.run;
|
31
23
|
const settings = model.settings;
|
32
24
|
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
@@ -58,11 +50,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
58
50
|
...startMetadata,
|
59
51
|
});
|
60
52
|
const result = await (0, runSafe_js_1.runSafe)(async () => {
|
61
|
-
const deltaIterable = await model.
|
62
|
-
functionId: options?.functionId,
|
63
|
-
settings,
|
64
|
-
run,
|
65
|
-
});
|
53
|
+
const deltaIterable = await model.doStreamStructure(structureDefinition, prompt, options);
|
66
54
|
return (async function* () {
|
67
55
|
function reportError(error) {
|
68
56
|
const finishMetadata = {
|
@@ -95,7 +83,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
95
83
|
}
|
96
84
|
if (event?.type === "delta") {
|
97
85
|
const latestFullDelta = event.fullDelta;
|
98
|
-
const latestStructure =
|
86
|
+
const latestStructure = event.valueDelta;
|
99
87
|
// only send a new part into the stream when the partial structure has changed:
|
100
88
|
if (!(0, deep_equal_1.default)(lastStructure, latestStructure, {
|
101
89
|
strict: true,
|
@@ -130,7 +118,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
130
118
|
result: {
|
131
119
|
status: "success",
|
132
120
|
response: lastFullDelta,
|
133
|
-
|
121
|
+
value: lastStructure,
|
134
122
|
},
|
135
123
|
});
|
136
124
|
})();
|
@@ -161,7 +149,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
161
149
|
throw result.error;
|
162
150
|
}
|
163
151
|
return {
|
164
|
-
output: result.
|
152
|
+
output: result.value,
|
165
153
|
metadata: startMetadata,
|
166
154
|
};
|
167
155
|
}
|
@@ -1,8 +1,7 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
1
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
2
3
|
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
3
|
-
import {
|
4
|
-
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
5
|
-
import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
|
4
|
+
import { StructureStreamingModel } from "./StructureGenerationModel.js";
|
6
5
|
export type StructureStreamPart<STRUCTURE> = {
|
7
6
|
isComplete: false;
|
8
7
|
value: unknown;
|
@@ -10,7 +9,4 @@ export type StructureStreamPart<STRUCTURE> = {
|
|
10
9
|
isComplete: true;
|
11
10
|
value: STRUCTURE;
|
12
11
|
};
|
13
|
-
export declare function streamStructure<STRUCTURE, PROMPT,
|
14
|
-
generateStructureStreamResponse: (structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT, options: ModelFunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
|
15
|
-
extractPartialStructure: (fullDelta: FULL_DELTA) => unknown | undefined;
|
16
|
-
}, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>;
|
12
|
+
export declare function streamStructure<STRUCTURE, PROMPT, NAME extends string>(model: StructureStreamingModel<PROMPT>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT, options?: FunctionOptions): AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>;
|
@@ -12,14 +12,6 @@ export function streamStructure(model, structureDefinition, prompt, options) {
|
|
12
12
|
return new AsyncIterableResultPromise(doStreamStructure(model, structureDefinition, prompt, options));
|
13
13
|
}
|
14
14
|
async function doStreamStructure(model, structureDefinition, prompt, options) {
|
15
|
-
if (options?.settings != null) {
|
16
|
-
model = model.withSettings(options.settings);
|
17
|
-
options = {
|
18
|
-
functionId: options.functionId,
|
19
|
-
observers: options.observers,
|
20
|
-
run: options.run,
|
21
|
-
};
|
22
|
-
}
|
23
15
|
const run = options?.run;
|
24
16
|
const settings = model.settings;
|
25
17
|
const eventSource = new FunctionEventSource({
|
@@ -51,11 +43,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
51
43
|
...startMetadata,
|
52
44
|
});
|
53
45
|
const result = await runSafe(async () => {
|
54
|
-
const deltaIterable = await model.
|
55
|
-
functionId: options?.functionId,
|
56
|
-
settings,
|
57
|
-
run,
|
58
|
-
});
|
46
|
+
const deltaIterable = await model.doStreamStructure(structureDefinition, prompt, options);
|
59
47
|
return (async function* () {
|
60
48
|
function reportError(error) {
|
61
49
|
const finishMetadata = {
|
@@ -88,7 +76,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
88
76
|
}
|
89
77
|
if (event?.type === "delta") {
|
90
78
|
const latestFullDelta = event.fullDelta;
|
91
|
-
const latestStructure =
|
79
|
+
const latestStructure = event.valueDelta;
|
92
80
|
// only send a new part into the stream when the partial structure has changed:
|
93
81
|
if (!deepEqual(lastStructure, latestStructure, {
|
94
82
|
strict: true,
|
@@ -123,7 +111,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
123
111
|
result: {
|
124
112
|
status: "success",
|
125
113
|
response: lastFullDelta,
|
126
|
-
|
114
|
+
value: lastStructure,
|
127
115
|
},
|
128
116
|
});
|
129
117
|
})();
|
@@ -154,7 +142,7 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
|
|
154
142
|
throw result.error;
|
155
143
|
}
|
156
144
|
return {
|
157
|
-
output: result.
|
145
|
+
output: result.value,
|
158
146
|
metadata: startMetadata,
|
159
147
|
};
|
160
148
|
}
|
@@ -5,7 +5,7 @@ export interface TextGenerationStartedEvent extends BaseModelCallStartedEvent {
|
|
5
5
|
export type TextGenerationFinishedEventResult = {
|
6
6
|
status: "success";
|
7
7
|
response: unknown;
|
8
|
-
|
8
|
+
value: string;
|
9
9
|
usage?: {
|
10
10
|
promptTokens: number;
|
11
11
|
completionTokens: number;
|
@@ -1,8 +1,9 @@
|
|
1
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
1
2
|
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
2
3
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
3
|
-
import {
|
4
|
+
import { PromptFormatTextStreamingModel } from "../../prompt/PromptFormatTextStreamingModel.js";
|
5
|
+
import { Delta } from "../Delta.js";
|
4
6
|
import { Model, ModelSettings } from "../Model.js";
|
5
|
-
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
6
7
|
import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
|
7
8
|
export interface TextGenerationModelSettings extends ModelSettings {
|
8
9
|
/**
|
@@ -21,27 +22,25 @@ export interface TextGenerationModelSettings extends ModelSettings {
|
|
21
22
|
*/
|
22
23
|
trimWhitespace?: boolean;
|
23
24
|
}
|
24
|
-
export interface TextGenerationModel<PROMPT,
|
25
|
+
export interface TextGenerationModel<PROMPT, SETTINGS extends TextGenerationModelSettings = TextGenerationModelSettings> extends Model<SETTINGS> {
|
25
26
|
readonly contextWindowSize: number | undefined;
|
26
27
|
readonly tokenizer: BasicTokenizer | FullTokenizer | undefined;
|
27
28
|
/**
|
28
29
|
* Optional. Implement if you have a tokenizer and want to count the number of tokens in a prompt.
|
29
30
|
*/
|
30
31
|
readonly countPromptTokens: ((prompt: PROMPT) => PromiseLike<number>) | undefined;
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
};
|
46
|
-
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, RESPONSE, FULL_DELTA, SETTINGS, this>;
|
32
|
+
doGenerateText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
33
|
+
response: unknown;
|
34
|
+
text: string;
|
35
|
+
usage?: {
|
36
|
+
promptTokens: number;
|
37
|
+
completionTokens: number;
|
38
|
+
totalTokens: number;
|
39
|
+
};
|
40
|
+
}>;
|
41
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
|
42
|
+
}
|
43
|
+
export interface TextStreamingModel<PROMPT, SETTINGS extends TextGenerationModelSettings = TextGenerationModelSettings> extends TextGenerationModel<PROMPT, SETTINGS> {
|
44
|
+
doStreamText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<string>>>;
|
45
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextStreamingModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
|
47
46
|
}
|