modelfusion 0.21.0 → 0.23.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 +1 -0
- package/composed-function/summarize/SummarizationFunction.d.ts +1 -1
- package/composed-function/summarize/summarizeRecursively.d.ts +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +1 -1
- package/{run → core}/DefaultRun.cjs +1 -1
- package/{run → core}/DefaultRun.js +1 -1
- package/{run → core}/FunctionEvent.d.ts +30 -6
- package/core/FunctionOptions.d.ts +33 -0
- package/core/GlobalFunctionLogging.cjs +12 -0
- package/core/GlobalFunctionLogging.d.ts +3 -0
- package/core/GlobalFunctionLogging.js +7 -0
- package/core/getFunctionCallLogger.cjs +74 -0
- package/core/getFunctionCallLogger.d.ts +3 -0
- package/core/getFunctionCallLogger.js +70 -0
- package/{run → core}/index.cjs +1 -1
- package/{run → core}/index.d.ts +1 -1
- package/{run → core}/index.js +1 -1
- package/index.cjs +1 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/model-function/AbstractModel.d.ts +1 -0
- package/model-function/Model.d.ts +7 -2
- package/model-function/ModelCallEvent.d.ts +39 -5
- package/model-function/ModelFunctionOptions.d.ts +1 -1
- package/model-function/SuccessfulModelCall.cjs +4 -2
- package/model-function/SuccessfulModelCall.d.ts +1 -1
- package/model-function/SuccessfulModelCall.js +4 -2
- package/model-function/embed-text/TextEmbeddingEvent.d.ts +12 -12
- package/model-function/embed-text/TextEmbeddingModel.d.ts +1 -1
- package/model-function/embed-text/embedText.cjs +6 -61
- package/model-function/embed-text/embedText.d.ts +3 -3
- package/model-function/embed-text/embedText.js +6 -61
- package/model-function/executeCall.cjs +50 -30
- package/model-function/executeCall.d.ts +16 -22
- package/model-function/executeCall.js +48 -28
- package/model-function/generate-image/ImageGenerationEvent.d.ts +9 -11
- package/model-function/generate-image/generateImage.cjs +2 -27
- package/model-function/generate-image/generateImage.d.ts +1 -1
- package/model-function/generate-image/generateImage.js +2 -27
- package/model-function/generate-json/JsonGenerationEvent.d.ts +14 -11
- package/model-function/generate-json/JsonGenerationModel.d.ts +13 -0
- package/model-function/generate-json/JsonOrTextGenerationModel.d.ts +23 -0
- package/model-function/generate-json/JsonTextGenerationModel.cjs +3 -0
- package/model-function/generate-json/JsonTextGenerationModel.d.ts +6 -5
- package/model-function/generate-json/JsonTextGenerationModel.js +3 -0
- package/model-function/generate-json/generateJson.cjs +3 -27
- package/model-function/generate-json/generateJson.d.ts +2 -2
- package/model-function/generate-json/generateJson.js +3 -27
- package/model-function/generate-json/generateJsonOrText.cjs +3 -27
- package/model-function/generate-json/generateJsonOrText.d.ts +2 -2
- package/model-function/generate-json/generateJsonOrText.js +3 -27
- package/model-function/generate-text/TextGenerationEvent.d.ts +14 -11
- package/model-function/generate-text/TextGenerationModel.d.ts +5 -0
- package/model-function/generate-text/TextStreamingEvent.d.ts +4 -17
- package/model-function/generate-text/generateText.cjs +3 -27
- package/model-function/generate-text/generateText.d.ts +1 -1
- package/model-function/generate-text/generateText.js +3 -27
- package/model-function/generate-text/streamText.cjs +31 -33
- package/model-function/generate-text/streamText.d.ts +5 -5
- package/model-function/generate-text/streamText.js +31 -33
- package/model-function/index.cjs +3 -2
- package/model-function/index.d.ts +3 -2
- package/model-function/index.js +3 -2
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +12 -11
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +1 -1
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +2 -28
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +1 -1
- package/model-function/synthesize-speech/synthesizeSpeech.js +2 -28
- package/model-function/transcribe-speech/TranscriptionEvent.d.ts +9 -11
- package/model-function/transcribe-speech/TranscriptionModel.d.ts +1 -1
- package/model-function/transcribe-speech/transcribe.cjs +2 -27
- package/model-function/transcribe-speech/transcribe.d.ts +1 -1
- package/model-function/transcribe-speech/transcribe.js +2 -27
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +9 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +1 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +9 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +6 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +4 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.js +6 -0
- package/model-provider/cohere/CohereTextGenerationModel.cjs +20 -0
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +2 -1
- package/model-provider/cohere/CohereTextGenerationModel.js +20 -0
- package/model-provider/cohere/CohereTokenizer.d.ts +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.cjs +8 -0
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +1 -0
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.js +8 -0
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +7 -0
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +1 -0
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +7 -0
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +16 -0
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +1 -0
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +16 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +6 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +1 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +6 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +31 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +6 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +31 -0
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +1 -1
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +8 -0
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +1 -0
- package/model-provider/openai/OpenAIImageGenerationModel.js +8 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +5 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -11
- package/model-provider/openai/OpenAITextEmbeddingModel.js +5 -0
- package/model-provider/openai/OpenAITextGenerationModel.cjs +24 -0
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +14 -8
- package/model-provider/openai/OpenAITextGenerationModel.js +24 -0
- package/model-provider/openai/OpenAITranscriptionModel.cjs +7 -0
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +2 -0
- package/model-provider/openai/OpenAITranscriptionModel.js +7 -0
- package/model-provider/openai/chat/OpenAIChatModel.cjs +20 -0
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +17 -11
- package/model-provider/openai/chat/OpenAIChatModel.js +20 -0
- package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +2 -2
- package/model-provider/stability/StabilityImageGenerationModel.cjs +15 -0
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +1 -0
- package/model-provider/stability/StabilityImageGenerationModel.js +15 -0
- package/package.json +1 -1
- package/prompt/PromptFormatTextGenerationModel.cjs +3 -0
- package/prompt/PromptFormatTextGenerationModel.d.ts +1 -0
- package/prompt/PromptFormatTextGenerationModel.js +3 -0
- package/text-chunk/split/SplitFunction.d.ts +1 -1
- package/tool/ExecuteToolEvent.d.ts +7 -16
- package/tool/Tool.d.ts +1 -1
- package/tool/WebSearchTool.cjs +25 -0
- package/tool/WebSearchTool.d.ts +56 -1
- package/tool/WebSearchTool.js +25 -0
- package/tool/executeTool.cjs +17 -8
- package/tool/executeTool.d.ts +1 -1
- package/tool/executeTool.js +17 -8
- package/tool/useTool.d.ts +2 -2
- package/tool/useToolOrGenerateText.d.ts +2 -2
- package/vector-index/VectorIndex.d.ts +1 -1
- package/vector-index/memory/MemoryVectorIndex.d.ts +1 -1
- package/vector-index/pinecone/PineconeVectorIndex.d.ts +1 -1
- package/model-function/generate-json/GenerateJsonModel.d.ts +0 -8
- package/model-function/generate-json/GenerateJsonOrTextModel.d.ts +0 -18
- package/run/ConsoleLogger.cjs +0 -9
- package/run/ConsoleLogger.d.ts +0 -5
- package/run/ConsoleLogger.js +0 -5
- package/run/FunctionOptions.d.ts +0 -19
- /package/{run → core}/DefaultRun.d.ts +0 -0
- /package/{run → core}/FunctionEvent.cjs +0 -0
- /package/{run → core}/FunctionEvent.js +0 -0
- /package/{run → core}/FunctionEventSource.cjs +0 -0
- /package/{run → core}/FunctionEventSource.d.ts +0 -0
- /package/{run → core}/FunctionEventSource.js +0 -0
- /package/{run → core}/FunctionObserver.cjs +0 -0
- /package/{run → core}/FunctionObserver.d.ts +0 -0
- /package/{run → core}/FunctionObserver.js +0 -0
- /package/{run → core}/FunctionOptions.cjs +0 -0
- /package/{run → core}/FunctionOptions.js +0 -0
- /package/{run → core}/GlobalFunctionObservers.cjs +0 -0
- /package/{run → core}/GlobalFunctionObservers.d.ts +0 -0
- /package/{run → core}/GlobalFunctionObservers.js +0 -0
- /package/{run → core}/Run.cjs +0 -0
- /package/{run → core}/Run.d.ts +0 -0
- /package/{run → core}/Run.js +0 -0
- /package/{run → core}/Vector.cjs +0 -0
- /package/{run → core}/Vector.d.ts +0 -0
- /package/{run → core}/Vector.js +0 -0
- /package/model-function/generate-json/{GenerateJsonModel.cjs → JsonGenerationModel.cjs} +0 -0
- /package/model-function/generate-json/{GenerateJsonModel.js → JsonGenerationModel.js} +0 -0
- /package/model-function/generate-json/{GenerateJsonOrTextModel.cjs → JsonOrTextGenerationModel.cjs} +0 -0
- /package/model-function/generate-json/{GenerateJsonOrTextModel.js → JsonOrTextGenerationModel.js} +0 -0
@@ -1,20 +1,23 @@
|
|
1
1
|
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
-
export
|
2
|
+
export interface JsonGenerationStartedEvent extends BaseModelCallStartedEvent {
|
3
3
|
functionType: "json-generation" | "json-or-text-generation";
|
4
|
-
|
5
|
-
|
6
|
-
};
|
7
|
-
export type JsonGenerationFinishedEvent = BaseModelCallFinishedEvent & {
|
8
|
-
functionType: "json-generation" | "json-or-text-generation";
|
9
|
-
settings: unknown;
|
10
|
-
prompt: unknown;
|
11
|
-
} & ({
|
4
|
+
}
|
5
|
+
export type JsonGenerationFinishedEventResult = {
|
12
6
|
status: "success";
|
13
7
|
response: unknown;
|
14
|
-
|
8
|
+
output: unknown;
|
9
|
+
usage?: {
|
10
|
+
promptTokens: number;
|
11
|
+
completionTokens: number;
|
12
|
+
totalTokens: number;
|
13
|
+
};
|
15
14
|
} | {
|
16
15
|
status: "error";
|
17
16
|
error: unknown;
|
18
17
|
} | {
|
19
18
|
status: "abort";
|
20
|
-
}
|
19
|
+
};
|
20
|
+
export type JsonGenerationFinishedEvent = BaseModelCallFinishedEvent & {
|
21
|
+
functionType: "json-generation" | "json-or-text-generation";
|
22
|
+
result: JsonGenerationFinishedEventResult;
|
23
|
+
};
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
|
+
import { Model, ModelSettings } from "../Model.js";
|
3
|
+
export interface JsonGenerationModelSettings extends ModelSettings {
|
4
|
+
}
|
5
|
+
export interface JsonGenerationModel<PROMPT, RESPONSE, SETTINGS extends JsonGenerationModelSettings> extends Model<SETTINGS> {
|
6
|
+
generateJsonResponse(prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
7
|
+
extractJson(response: RESPONSE): unknown;
|
8
|
+
extractUsage?(response: RESPONSE): {
|
9
|
+
promptTokens: number;
|
10
|
+
completionTokens: number;
|
11
|
+
totalTokens: number;
|
12
|
+
};
|
13
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
|
+
import { Model, ModelSettings } from "../Model.js";
|
3
|
+
export interface JsonOrTextGenerationModelSettings extends ModelSettings {
|
4
|
+
}
|
5
|
+
export interface JsonOrTextGenerationPrompt<RESPONSE> {
|
6
|
+
extractJsonAndText(response: RESPONSE): {
|
7
|
+
schema: null;
|
8
|
+
value: null;
|
9
|
+
text: string;
|
10
|
+
} | {
|
11
|
+
schema: string;
|
12
|
+
value: unknown;
|
13
|
+
text: string | null;
|
14
|
+
};
|
15
|
+
}
|
16
|
+
export interface JsonOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS extends JsonOrTextGenerationModelSettings> extends Model<SETTINGS> {
|
17
|
+
generateJsonResponse(prompt: PROMPT & JsonOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
18
|
+
extractUsage?(response: RESPONSE): {
|
19
|
+
promptTokens: number;
|
20
|
+
completionTokens: number;
|
21
|
+
totalTokens: number;
|
22
|
+
};
|
23
|
+
}
|
@@ -25,6 +25,9 @@ class JsonTextGenerationModel {
|
|
25
25
|
get settings() {
|
26
26
|
return this.model.settings;
|
27
27
|
}
|
28
|
+
get settingsForEvent() {
|
29
|
+
return this.model.settingsForEvent;
|
30
|
+
}
|
28
31
|
async generateJsonResponse(prompt, options) {
|
29
32
|
return await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt), options);
|
30
33
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
|
2
2
|
import { SchemaDefinition } from "./SchemaDefinition.js";
|
3
3
|
import { InstructionWithSchema } from "./InstructionWithSchemaPrompt.js";
|
4
|
-
import {
|
4
|
+
import { JsonGenerationModel } from "./JsonGenerationModel.js";
|
5
5
|
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
6
6
|
export type JsonTextPromptFormat = {
|
7
7
|
createPrompt: (prompt: {
|
@@ -10,7 +10,7 @@ export type JsonTextPromptFormat = {
|
|
10
10
|
}) => string;
|
11
11
|
extractJson: (response: string) => unknown;
|
12
12
|
};
|
13
|
-
export declare class JsonTextGenerationModel<
|
13
|
+
export declare class JsonTextGenerationModel<MODEL extends TextGenerationModel<string, any, any, TextGenerationModelSettings>> implements JsonGenerationModel<InstructionWithSchema<string, unknown>, string, MODEL["settings"]> {
|
14
14
|
private readonly model;
|
15
15
|
private readonly format;
|
16
16
|
constructor({ model, format, }: {
|
@@ -18,8 +18,9 @@ export declare class JsonTextGenerationModel<SETTINGS extends TextGenerationMode
|
|
18
18
|
format: JsonTextPromptFormat;
|
19
19
|
});
|
20
20
|
get modelInformation(): import("../ModelInformation.js").ModelInformation;
|
21
|
-
get settings():
|
22
|
-
|
21
|
+
get settings(): TextGenerationModelSettings;
|
22
|
+
get settingsForEvent(): Partial<MODEL["settings"]>;
|
23
|
+
generateJsonResponse(prompt: InstructionWithSchema<string, unknown>, options?: ModelFunctionOptions<MODEL["settings"]> | undefined): Promise<string>;
|
23
24
|
extractJson(response: string): unknown;
|
24
|
-
withSettings(additionalSettings: Partial<
|
25
|
+
withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
|
25
26
|
}
|
@@ -22,6 +22,9 @@ export class JsonTextGenerationModel {
|
|
22
22
|
get settings() {
|
23
23
|
return this.model.settings;
|
24
24
|
}
|
25
|
+
get settingsForEvent() {
|
26
|
+
return this.model.settingsForEvent;
|
27
|
+
}
|
25
28
|
async generateJsonResponse(prompt, options) {
|
26
29
|
return await generateText(this.model, this.format.createPrompt(prompt), options);
|
27
30
|
}
|
@@ -6,6 +6,8 @@ const SchemaValidationError_js_1 = require("./SchemaValidationError.cjs");
|
|
6
6
|
function generateJson(model, schemaDefinition, prompt, options) {
|
7
7
|
const expandedPrompt = prompt(schemaDefinition);
|
8
8
|
return (0, executeCall_js_1.executeCall)({
|
9
|
+
functionType: "json-generation",
|
10
|
+
input: expandedPrompt,
|
9
11
|
model,
|
10
12
|
options,
|
11
13
|
generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
|
@@ -21,33 +23,7 @@ function generateJson(model, schemaDefinition, prompt, options) {
|
|
21
23
|
}
|
22
24
|
return parseResult.data;
|
23
25
|
},
|
24
|
-
|
25
|
-
...metadata,
|
26
|
-
functionType: "json-generation",
|
27
|
-
settings,
|
28
|
-
prompt,
|
29
|
-
}),
|
30
|
-
getAbortEvent: (metadata, settings) => ({
|
31
|
-
...metadata,
|
32
|
-
functionType: "json-generation",
|
33
|
-
settings,
|
34
|
-
prompt,
|
35
|
-
}),
|
36
|
-
getFailureEvent: (metadata, settings, error) => ({
|
37
|
-
...metadata,
|
38
|
-
functionType: "json-generation",
|
39
|
-
settings,
|
40
|
-
prompt,
|
41
|
-
error,
|
42
|
-
}),
|
43
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
44
|
-
...metadata,
|
45
|
-
functionType: "json-generation",
|
46
|
-
settings,
|
47
|
-
prompt,
|
48
|
-
response,
|
49
|
-
generatedJson: output,
|
50
|
-
}),
|
26
|
+
extractUsage: (result) => model.extractUsage?.(result),
|
51
27
|
});
|
52
28
|
}
|
53
29
|
exports.generateJson = generateJson;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
2
|
import { ModelFunctionPromise } from "../executeCall.js";
|
3
|
-
import {
|
3
|
+
import { JsonGenerationModel, JsonGenerationModelSettings } from "./JsonGenerationModel.js";
|
4
4
|
import { SchemaDefinition } from "./SchemaDefinition.js";
|
5
|
-
export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends
|
5
|
+
export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends JsonGenerationModelSettings>(model: JsonGenerationModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<STRUCTURE, RESPONSE>;
|
@@ -3,6 +3,8 @@ import { SchemaValidationError } from "./SchemaValidationError.js";
|
|
3
3
|
export function generateJson(model, schemaDefinition, prompt, options) {
|
4
4
|
const expandedPrompt = prompt(schemaDefinition);
|
5
5
|
return executeCall({
|
6
|
+
functionType: "json-generation",
|
7
|
+
input: expandedPrompt,
|
6
8
|
model,
|
7
9
|
options,
|
8
10
|
generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
|
@@ -18,32 +20,6 @@ export function generateJson(model, schemaDefinition, prompt, options) {
|
|
18
20
|
}
|
19
21
|
return parseResult.data;
|
20
22
|
},
|
21
|
-
|
22
|
-
...metadata,
|
23
|
-
functionType: "json-generation",
|
24
|
-
settings,
|
25
|
-
prompt,
|
26
|
-
}),
|
27
|
-
getAbortEvent: (metadata, settings) => ({
|
28
|
-
...metadata,
|
29
|
-
functionType: "json-generation",
|
30
|
-
settings,
|
31
|
-
prompt,
|
32
|
-
}),
|
33
|
-
getFailureEvent: (metadata, settings, error) => ({
|
34
|
-
...metadata,
|
35
|
-
functionType: "json-generation",
|
36
|
-
settings,
|
37
|
-
prompt,
|
38
|
-
error,
|
39
|
-
}),
|
40
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
41
|
-
...metadata,
|
42
|
-
functionType: "json-generation",
|
43
|
-
settings,
|
44
|
-
prompt,
|
45
|
-
response,
|
46
|
-
generatedJson: output,
|
47
|
-
}),
|
23
|
+
extractUsage: (result) => model.extractUsage?.(result),
|
48
24
|
});
|
49
25
|
}
|
@@ -7,6 +7,8 @@ const SchemaValidationError_js_1 = require("./SchemaValidationError.cjs");
|
|
7
7
|
function generateJsonOrText(model, schemaDefinitions, prompt, options) {
|
8
8
|
const expandedPrompt = prompt(schemaDefinitions);
|
9
9
|
return (0, executeCall_js_1.executeCall)({
|
10
|
+
functionType: "json-or-text-generation",
|
11
|
+
input: expandedPrompt,
|
10
12
|
model,
|
11
13
|
options,
|
12
14
|
generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
|
@@ -34,33 +36,7 @@ function generateJsonOrText(model, schemaDefinitions, prompt, options) {
|
|
34
36
|
text: text, // text is string | null, which is part of the response for schema values
|
35
37
|
};
|
36
38
|
},
|
37
|
-
|
38
|
-
...metadata,
|
39
|
-
functionType: "json-or-text-generation",
|
40
|
-
settings,
|
41
|
-
prompt,
|
42
|
-
}),
|
43
|
-
getAbortEvent: (metadata, settings) => ({
|
44
|
-
...metadata,
|
45
|
-
functionType: "json-or-text-generation",
|
46
|
-
settings,
|
47
|
-
prompt,
|
48
|
-
}),
|
49
|
-
getFailureEvent: (metadata, settings, error) => ({
|
50
|
-
...metadata,
|
51
|
-
functionType: "json-or-text-generation",
|
52
|
-
settings,
|
53
|
-
prompt,
|
54
|
-
error,
|
55
|
-
}),
|
56
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
57
|
-
...metadata,
|
58
|
-
functionType: "json-or-text-generation",
|
59
|
-
settings,
|
60
|
-
prompt,
|
61
|
-
response,
|
62
|
-
generatedJson: output,
|
63
|
-
}),
|
39
|
+
extractUsage: (result) => model.extractUsage?.(result),
|
64
40
|
});
|
65
41
|
}
|
66
42
|
exports.generateJsonOrText = generateJsonOrText;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
2
|
import { ModelFunctionPromise } from "../executeCall.js";
|
3
|
-
import {
|
3
|
+
import { JsonOrTextGenerationModel, JsonOrTextGenerationModelSettings, JsonOrTextGenerationPrompt } from "./JsonOrTextGenerationModel.js";
|
4
4
|
import { SchemaDefinition } from "./SchemaDefinition.js";
|
5
5
|
type SchemaDefinitionArray<T extends SchemaDefinition<any, any>[]> = T;
|
6
6
|
type ToSchemaDefinitionsMap<T extends SchemaDefinitionArray<SchemaDefinition<any, any>[]>> = {
|
@@ -14,7 +14,7 @@ type ToSchemaUnion<T> = {
|
|
14
14
|
} : never;
|
15
15
|
}[keyof T];
|
16
16
|
type ToOutputValue<SCHEMAS extends SchemaDefinitionArray<SchemaDefinition<any, any>[]>> = ToSchemaUnion<ToSchemaDefinitionsMap<SCHEMAS>>;
|
17
|
-
export declare function generateJsonOrText<SCHEMAS extends SchemaDefinition<any, any>[], PROMPT, RESPONSE, SETTINGS extends
|
17
|
+
export declare function generateJsonOrText<SCHEMAS extends SchemaDefinition<any, any>[], PROMPT, RESPONSE, SETTINGS extends JsonOrTextGenerationModelSettings>(model: JsonOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinitions: SCHEMAS, prompt: (schemaDefinitions: SCHEMAS) => PROMPT & JsonOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<{
|
18
18
|
schema: null;
|
19
19
|
value: null;
|
20
20
|
text: string;
|
@@ -4,6 +4,8 @@ import { SchemaValidationError } from "./SchemaValidationError.js";
|
|
4
4
|
export function generateJsonOrText(model, schemaDefinitions, prompt, options) {
|
5
5
|
const expandedPrompt = prompt(schemaDefinitions);
|
6
6
|
return executeCall({
|
7
|
+
functionType: "json-or-text-generation",
|
8
|
+
input: expandedPrompt,
|
7
9
|
model,
|
8
10
|
options,
|
9
11
|
generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
|
@@ -31,32 +33,6 @@ export function generateJsonOrText(model, schemaDefinitions, prompt, options) {
|
|
31
33
|
text: text, // text is string | null, which is part of the response for schema values
|
32
34
|
};
|
33
35
|
},
|
34
|
-
|
35
|
-
...metadata,
|
36
|
-
functionType: "json-or-text-generation",
|
37
|
-
settings,
|
38
|
-
prompt,
|
39
|
-
}),
|
40
|
-
getAbortEvent: (metadata, settings) => ({
|
41
|
-
...metadata,
|
42
|
-
functionType: "json-or-text-generation",
|
43
|
-
settings,
|
44
|
-
prompt,
|
45
|
-
}),
|
46
|
-
getFailureEvent: (metadata, settings, error) => ({
|
47
|
-
...metadata,
|
48
|
-
functionType: "json-or-text-generation",
|
49
|
-
settings,
|
50
|
-
prompt,
|
51
|
-
error,
|
52
|
-
}),
|
53
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
54
|
-
...metadata,
|
55
|
-
functionType: "json-or-text-generation",
|
56
|
-
settings,
|
57
|
-
prompt,
|
58
|
-
response,
|
59
|
-
generatedJson: output,
|
60
|
-
}),
|
36
|
+
extractUsage: (result) => model.extractUsage?.(result),
|
61
37
|
});
|
62
38
|
}
|
@@ -1,20 +1,23 @@
|
|
1
1
|
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
-
export
|
2
|
+
export interface TextGenerationStartedEvent extends BaseModelCallStartedEvent {
|
3
3
|
functionType: "text-generation";
|
4
|
-
|
5
|
-
|
6
|
-
};
|
7
|
-
export type TextGenerationFinishedEvent = BaseModelCallFinishedEvent & {
|
8
|
-
functionType: "text-generation";
|
9
|
-
settings: unknown;
|
10
|
-
prompt: unknown;
|
11
|
-
} & ({
|
4
|
+
}
|
5
|
+
export type TextGenerationFinishedEventResult = {
|
12
6
|
status: "success";
|
13
7
|
response: unknown;
|
14
|
-
|
8
|
+
output: string;
|
9
|
+
usage?: {
|
10
|
+
promptTokens: number;
|
11
|
+
completionTokens: number;
|
12
|
+
totalTokens: number;
|
13
|
+
};
|
15
14
|
} | {
|
16
15
|
status: "error";
|
17
16
|
error: unknown;
|
18
17
|
} | {
|
19
18
|
status: "abort";
|
20
|
-
}
|
19
|
+
};
|
20
|
+
export interface TextGenerationFinishedEvent extends BaseModelCallFinishedEvent {
|
21
|
+
functionType: "text-generation";
|
22
|
+
result: TextGenerationFinishedEventResult;
|
23
|
+
}
|
@@ -38,5 +38,10 @@ export interface TextGenerationModel<PROMPT, RESPONSE, FULL_DELTA, SETTINGS exte
|
|
38
38
|
* Optional. Implement for streaming support.
|
39
39
|
*/
|
40
40
|
readonly extractTextDelta: ((fullDelta: FULL_DELTA) => string | undefined) | undefined;
|
41
|
+
extractUsage?(response: RESPONSE): {
|
42
|
+
promptTokens: number;
|
43
|
+
completionTokens: number;
|
44
|
+
totalTokens: number;
|
45
|
+
};
|
41
46
|
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, RESPONSE, FULL_DELTA, SETTINGS, this>;
|
42
47
|
}
|
@@ -1,20 +1,7 @@
|
|
1
1
|
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
-
export
|
2
|
+
export interface TextStreamingStartedEvent extends BaseModelCallStartedEvent {
|
3
3
|
functionType: "text-streaming";
|
4
|
-
|
5
|
-
|
6
|
-
};
|
7
|
-
export type TextStreamingFinishedEvent = BaseModelCallFinishedEvent & {
|
4
|
+
}
|
5
|
+
export interface TextStreamingFinishedEvent extends BaseModelCallFinishedEvent {
|
8
6
|
functionType: "text-streaming";
|
9
|
-
|
10
|
-
prompt: unknown;
|
11
|
-
} & ({
|
12
|
-
status: "success";
|
13
|
-
response: unknown;
|
14
|
-
generatedText: string;
|
15
|
-
} | {
|
16
|
-
status: "error";
|
17
|
-
error: unknown;
|
18
|
-
} | {
|
19
|
-
status: "abort";
|
20
|
-
});
|
7
|
+
}
|
@@ -18,6 +18,8 @@ function generateText(
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
19
19
|
model, prompt, options) {
|
20
20
|
return (0, executeCall_js_1.executeCall)({
|
21
|
+
functionType: "text-generation",
|
22
|
+
input: prompt,
|
21
23
|
model,
|
22
24
|
options,
|
23
25
|
generateResponse: (options) => model.generateTextResponse(prompt, options),
|
@@ -27,33 +29,7 @@ model, prompt, options) {
|
|
27
29
|
? model.extractText(result).trim()
|
28
30
|
: model.extractText(result);
|
29
31
|
},
|
30
|
-
|
31
|
-
...metadata,
|
32
|
-
functionType: "text-generation",
|
33
|
-
settings,
|
34
|
-
prompt,
|
35
|
-
}),
|
36
|
-
getAbortEvent: (metadata, settings) => ({
|
37
|
-
...metadata,
|
38
|
-
functionType: "text-generation",
|
39
|
-
settings,
|
40
|
-
prompt,
|
41
|
-
}),
|
42
|
-
getFailureEvent: (metadata, settings, error) => ({
|
43
|
-
...metadata,
|
44
|
-
functionType: "text-generation",
|
45
|
-
settings,
|
46
|
-
prompt,
|
47
|
-
error,
|
48
|
-
}),
|
49
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
50
|
-
...metadata,
|
51
|
-
functionType: "text-generation",
|
52
|
-
settings,
|
53
|
-
prompt,
|
54
|
-
response,
|
55
|
-
generatedText: output,
|
56
|
-
}),
|
32
|
+
extractUsage: (result) => model.extractUsage?.(result),
|
57
33
|
});
|
58
34
|
}
|
59
35
|
exports.generateText = generateText;
|
@@ -13,4 +13,4 @@ import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerati
|
|
13
13
|
* "Write a short story about a robot learning to love:\n\n"
|
14
14
|
* );
|
15
15
|
*/
|
16
|
-
export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<
|
16
|
+
export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<string, RESPONSE>;
|
@@ -15,6 +15,8 @@ export function generateText(
|
|
15
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
16
16
|
model, prompt, options) {
|
17
17
|
return executeCall({
|
18
|
+
functionType: "text-generation",
|
19
|
+
input: prompt,
|
18
20
|
model,
|
19
21
|
options,
|
20
22
|
generateResponse: (options) => model.generateTextResponse(prompt, options),
|
@@ -24,32 +26,6 @@ model, prompt, options) {
|
|
24
26
|
? model.extractText(result).trim()
|
25
27
|
: model.extractText(result);
|
26
28
|
},
|
27
|
-
|
28
|
-
...metadata,
|
29
|
-
functionType: "text-generation",
|
30
|
-
settings,
|
31
|
-
prompt,
|
32
|
-
}),
|
33
|
-
getAbortEvent: (metadata, settings) => ({
|
34
|
-
...metadata,
|
35
|
-
functionType: "text-generation",
|
36
|
-
settings,
|
37
|
-
prompt,
|
38
|
-
}),
|
39
|
-
getFailureEvent: (metadata, settings, error) => ({
|
40
|
-
...metadata,
|
41
|
-
functionType: "text-generation",
|
42
|
-
settings,
|
43
|
-
prompt,
|
44
|
-
error,
|
45
|
-
}),
|
46
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
47
|
-
...metadata,
|
48
|
-
functionType: "text-generation",
|
49
|
-
settings,
|
50
|
-
prompt,
|
51
|
-
response,
|
52
|
-
generatedText: output,
|
53
|
-
}),
|
29
|
+
extractUsage: (result) => model.extractUsage?.(result),
|
54
30
|
});
|
55
31
|
}
|
@@ -2,8 +2,10 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.streamText = exports.StreamTextPromise = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
|
-
const FunctionEventSource_js_1 = require("../../
|
6
|
-
const
|
5
|
+
const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
|
6
|
+
const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
|
7
|
+
const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
|
8
|
+
const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
|
7
9
|
const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
|
8
10
|
const AbortError_js_1 = require("../../util/api/AbortError.cjs");
|
9
11
|
const runSafe_js_1 = require("../../util/runSafe.cjs");
|
@@ -59,6 +61,7 @@ async function doStreamText(model, prompt, options) {
|
|
59
61
|
const settings = model.settings;
|
60
62
|
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
61
63
|
observers: [
|
64
|
+
...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
|
62
65
|
...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
|
63
66
|
...(settings.observers ?? []),
|
64
67
|
...(run?.observers ?? []),
|
@@ -68,21 +71,21 @@ async function doStreamText(model, prompt, options) {
|
|
68
71
|
});
|
69
72
|
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
70
73
|
const startMetadata = {
|
74
|
+
functionType: "text-streaming",
|
71
75
|
callId: `call-${(0, nanoid_1.nanoid)()}`,
|
72
76
|
runId: run?.runId,
|
73
77
|
sessionId: run?.sessionId,
|
74
78
|
userId: run?.userId,
|
75
79
|
functionId: options?.functionId,
|
76
80
|
model: model.modelInformation,
|
81
|
+
settings: model.settingsForEvent,
|
82
|
+
input: prompt,
|
77
83
|
timestamp: durationMeasurement.startDate,
|
78
84
|
startTimestamp: durationMeasurement.startDate,
|
79
85
|
};
|
80
86
|
eventSource.notify({
|
81
|
-
...startMetadata,
|
82
|
-
functionType: "text-streaming",
|
83
87
|
eventType: "started",
|
84
|
-
|
85
|
-
prompt,
|
88
|
+
...startMetadata,
|
86
89
|
});
|
87
90
|
const result = await (0, runSafe_js_1.runSafe)(async () => (0, extractTextDeltas_js_1.extractTextDeltas)({
|
88
91
|
deltaIterable: await model.generateDeltaStreamResponse(prompt, {
|
@@ -93,70 +96,65 @@ async function doStreamText(model, prompt, options) {
|
|
93
96
|
extractDelta: (fullDelta) => model.extractTextDelta(fullDelta),
|
94
97
|
onDone: (fullText, lastFullDelta) => {
|
95
98
|
const finishMetadata = {
|
96
|
-
...startMetadata,
|
97
99
|
eventType: "finished",
|
100
|
+
...startMetadata,
|
98
101
|
finishTimestamp: new Date(),
|
99
102
|
durationInMs: durationMeasurement.durationInMs,
|
100
103
|
};
|
101
104
|
eventSource.notify({
|
102
105
|
...finishMetadata,
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
generatedText: fullText,
|
106
|
+
result: {
|
107
|
+
status: "success",
|
108
|
+
response: lastFullDelta,
|
109
|
+
output: fullText,
|
110
|
+
},
|
109
111
|
});
|
110
112
|
},
|
111
113
|
onError: (error) => {
|
112
114
|
const finishMetadata = {
|
113
|
-
...startMetadata,
|
114
115
|
eventType: "finished",
|
116
|
+
...startMetadata,
|
115
117
|
finishTimestamp: new Date(),
|
116
118
|
durationInMs: durationMeasurement.durationInMs,
|
117
119
|
};
|
118
120
|
eventSource.notify(error instanceof AbortError_js_1.AbortError
|
119
121
|
? {
|
120
122
|
...finishMetadata,
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
prompt,
|
123
|
+
result: {
|
124
|
+
status: "abort",
|
125
|
+
},
|
125
126
|
}
|
126
127
|
: {
|
127
128
|
...finishMetadata,
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
error,
|
129
|
+
result: {
|
130
|
+
status: "error",
|
131
|
+
error,
|
132
|
+
},
|
133
133
|
});
|
134
134
|
},
|
135
135
|
}));
|
136
136
|
if (!result.ok) {
|
137
137
|
const finishMetadata = {
|
138
|
-
...startMetadata,
|
139
138
|
eventType: "finished",
|
139
|
+
...startMetadata,
|
140
140
|
finishTimestamp: new Date(),
|
141
141
|
durationInMs: durationMeasurement.durationInMs,
|
142
142
|
};
|
143
143
|
if (result.isAborted) {
|
144
144
|
eventSource.notify({
|
145
145
|
...finishMetadata,
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
prompt,
|
146
|
+
result: {
|
147
|
+
status: "abort",
|
148
|
+
},
|
150
149
|
});
|
151
150
|
throw new AbortError_js_1.AbortError();
|
152
151
|
}
|
153
152
|
eventSource.notify({
|
154
153
|
...finishMetadata,
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
error: result.error,
|
154
|
+
result: {
|
155
|
+
status: "error",
|
156
|
+
error: result.error,
|
157
|
+
},
|
160
158
|
});
|
161
159
|
throw result.error;
|
162
160
|
}
|