modelfusion 0.4.0 → 0.4.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/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +4 -5
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +2 -2
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +4 -5
- package/model-function/generate-text/streamText.d.ts +1 -1
- package/package.json +1 -1
- package/prompt/chat/trimChatPrompt.cjs +2 -1
- package/prompt/chat/trimChatPrompt.d.ts +1 -2
- package/prompt/chat/trimChatPrompt.js +2 -1
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs
CHANGED
@@ -9,17 +9,16 @@ const summarizeRecursively_js_1 = require("./summarizeRecursively.cjs");
|
|
9
9
|
* It automatically splits the text into optimal chunks that are small enough to be processed by the model,
|
10
10
|
* while leaving enough space for the model to generate text.
|
11
11
|
*/
|
12
|
-
async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt,
|
12
|
+
async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
|
13
|
+
(model.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
|
13
14
|
const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
|
14
15
|
return (0, summarizeRecursively_js_1.summarizeRecursively)({
|
15
16
|
split: (0, splitRecursively_js_1.splitRecursivelyAtTokenAsSplitFunction)({
|
16
17
|
tokenizer: model.tokenizer,
|
17
|
-
maxChunkSize:
|
18
|
-
reservedCompletionTokens -
|
19
|
-
emptyPromptTokens,
|
18
|
+
maxChunkSize: tokenLimit - emptyPromptTokens,
|
20
19
|
}),
|
21
20
|
summarize: async (input) => {
|
22
|
-
const { text } = await (0, generateText_js_1.generateText)(model
|
21
|
+
const { text } = await (0, generateText_js_1.generateText)(model, await prompt(input), options);
|
23
22
|
return text;
|
24
23
|
},
|
25
24
|
join,
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts
CHANGED
@@ -6,7 +6,7 @@ import { Run } from "../../run/Run.js";
|
|
6
6
|
* It automatically splits the text into optimal chunks that are small enough to be processed by the model,
|
7
7
|
* while leaving enough space for the model to generate text.
|
8
8
|
*/
|
9
|
-
export declare function summarizeRecursivelyWithTextGenerationAndTokenSplitting<PROMPT>({ text, model, prompt,
|
9
|
+
export declare function summarizeRecursivelyWithTextGenerationAndTokenSplitting<PROMPT>({ text, model, prompt, tokenLimit, join, }: {
|
10
10
|
text: string;
|
11
11
|
model: TextGenerationModel<PROMPT, any, any, TextGenerationModelSettings> & {
|
12
12
|
contextWindowSize: number;
|
@@ -16,7 +16,7 @@ export declare function summarizeRecursivelyWithTextGenerationAndTokenSplitting<
|
|
16
16
|
prompt: (input: {
|
17
17
|
text: string;
|
18
18
|
}) => Promise<PROMPT>;
|
19
|
-
|
19
|
+
tokenLimit?: number;
|
20
20
|
join?: (texts: Array<string>) => string;
|
21
21
|
}, options?: {
|
22
22
|
functionId?: string;
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js
CHANGED
@@ -6,17 +6,16 @@ import { summarizeRecursively } from "./summarizeRecursively.js";
|
|
6
6
|
* It automatically splits the text into optimal chunks that are small enough to be processed by the model,
|
7
7
|
* while leaving enough space for the model to generate text.
|
8
8
|
*/
|
9
|
-
export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt,
|
9
|
+
export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
|
10
|
+
(model.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
|
10
11
|
const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
|
11
12
|
return summarizeRecursively({
|
12
13
|
split: splitRecursivelyAtTokenAsSplitFunction({
|
13
14
|
tokenizer: model.tokenizer,
|
14
|
-
maxChunkSize:
|
15
|
-
reservedCompletionTokens -
|
16
|
-
emptyPromptTokens,
|
15
|
+
maxChunkSize: tokenLimit - emptyPromptTokens,
|
17
16
|
}),
|
18
17
|
summarize: async (input) => {
|
19
|
-
const { text } = await generateText(model
|
18
|
+
const { text } = await generateText(model, await prompt(input), options);
|
20
19
|
return text;
|
21
20
|
},
|
22
21
|
join,
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { FunctionOptions } from "../FunctionOptions.js";
|
2
|
+
import { CallMetadata } from "../executeCall.js";
|
2
3
|
import { DeltaEvent } from "./DeltaEvent.js";
|
3
4
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
4
|
-
import { CallMetadata } from "model-function/executeCall.js";
|
5
5
|
export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
|
6
6
|
generateDeltaStreamResponse: (prompt: PROMPT, options: FunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
|
7
7
|
extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
|
package/package.json
CHANGED
@@ -12,7 +12,8 @@ const validateChatPrompt_js_1 = require("./validateChatPrompt.cjs");
|
|
12
12
|
*
|
13
13
|
* @see https://modelfusion.dev/guide/function/generate-text/prompt-mapping#limiting-the-chat-length
|
14
14
|
*/
|
15
|
-
async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
|
15
|
+
async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
|
16
|
+
(model.maxCompletionTokens ?? model.contextWindowSize / 4), }) {
|
16
17
|
(0, validateChatPrompt_js_1.validateChatPrompt)(prompt);
|
17
18
|
const startsWithSystemMessage = "system" in prompt[0];
|
18
19
|
const systemMessage = startsWithSystemMessage ? [prompt[0]] : [];
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { TextGenerationModel } from "model-function/generate-text/TextGenerationModel.js";
|
1
|
+
import { TextGenerationModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
2
2
|
import { ChatPrompt } from "./ChatPrompt.js";
|
3
3
|
/**
|
4
4
|
* Keeps only the most recent messages in the prompt, while leaving enough space for the completion.
|
@@ -14,7 +14,6 @@ export declare function trimChatPrompt({ prompt, model, tokenLimit, }: {
|
|
14
14
|
prompt: ChatPrompt;
|
15
15
|
model: TextGenerationModel<ChatPrompt, any, any, any> & {
|
16
16
|
contextWindowSize: number;
|
17
|
-
maxCompletionTokens: number;
|
18
17
|
countPromptTokens: (prompt: ChatPrompt) => PromiseLike<number>;
|
19
18
|
};
|
20
19
|
tokenLimit?: number;
|
@@ -9,7 +9,8 @@ import { validateChatPrompt } from "./validateChatPrompt.js";
|
|
9
9
|
*
|
10
10
|
* @see https://modelfusion.dev/guide/function/generate-text/prompt-mapping#limiting-the-chat-length
|
11
11
|
*/
|
12
|
-
export async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
|
12
|
+
export async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
|
13
|
+
(model.maxCompletionTokens ?? model.contextWindowSize / 4), }) {
|
13
14
|
validateChatPrompt(prompt);
|
14
15
|
const startsWithSystemMessage = "system" in prompt[0];
|
15
16
|
const systemMessage = startsWithSystemMessage ? [prompt[0]] : [];
|