modelfusion 0.13.0 → 0.15.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 +16 -10
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +1 -1
- package/model-function/Model.d.ts +2 -2
- package/model-function/generate-text/TextGenerationModel.d.ts +18 -18
- package/model-function/generate-text/generateText.cjs +2 -2
- package/model-function/generate-text/generateText.js +2 -2
- package/model-provider/cohere/CohereTextGenerationModel.cjs +19 -20
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +4 -9
- package/model-provider/cohere/CohereTextGenerationModel.js +19 -20
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +13 -18
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +4 -8
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +13 -18
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +16 -16
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +9 -14
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +16 -16
- package/model-provider/openai/OpenAITextGenerationModel.cjs +20 -18
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -9
- package/model-provider/openai/OpenAITextGenerationModel.js +20 -18
- package/model-provider/openai/chat/OpenAIChatModel.cjs +15 -18
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +5 -8
- package/model-provider/openai/chat/OpenAIChatModel.js +15 -18
- package/package.json +3 -3
- package/prompt/{AlpacaPromptMapping.cjs → AlpacaPromptFormat.cjs} +6 -6
- package/prompt/{AlpacaPromptMapping.d.ts → AlpacaPromptFormat.d.ts} +3 -3
- package/prompt/{AlpacaPromptMapping.js → AlpacaPromptFormat.js} +4 -4
- package/prompt/{Llama2PromptMapping.cjs → Llama2PromptFormat.cjs} +13 -10
- package/prompt/Llama2PromptFormat.d.ts +13 -0
- package/prompt/{Llama2PromptMapping.js → Llama2PromptFormat.js} +10 -7
- package/prompt/{OpenAIChatPromptMapping.cjs → OpenAIChatPromptFormat.cjs} +15 -9
- package/prompt/OpenAIChatPromptFormat.d.ts +12 -0
- package/prompt/{OpenAIChatPromptMapping.js → OpenAIChatPromptFormat.js} +12 -6
- package/prompt/PromptFormat.d.ts +14 -0
- package/prompt/{PromptMappingTextGenerationModel.js → PromptFormatTextGenerationModel.cjs} +19 -28
- package/prompt/{PromptMappingTextGenerationModel.d.ts → PromptFormatTextGenerationModel.d.ts} +6 -9
- package/prompt/{PromptMappingTextGenerationModel.cjs → PromptFormatTextGenerationModel.js} +15 -32
- package/prompt/{TextPromptMapping.cjs → TextPromptFormat.cjs} +13 -10
- package/prompt/TextPromptFormat.d.ts +17 -0
- package/prompt/{TextPromptMapping.js → TextPromptFormat.js} +10 -7
- package/prompt/{VicunaPromptMapping.cjs → VicunaPromptFormat.cjs} +6 -6
- package/prompt/{VicunaPromptMapping.d.ts → VicunaPromptFormat.d.ts} +3 -3
- package/prompt/{VicunaPromptMapping.js → VicunaPromptFormat.js} +4 -4
- package/prompt/chat/trimChatPrompt.cjs +2 -2
- package/prompt/chat/trimChatPrompt.d.ts +1 -1
- package/prompt/chat/trimChatPrompt.js +2 -2
- package/prompt/index.cjs +7 -7
- package/prompt/index.d.ts +7 -7
- package/prompt/index.js +7 -7
- package/tool/WebSearchTool.cjs +7 -28
- package/tool/WebSearchTool.d.ts +6 -67
- package/tool/WebSearchTool.js +7 -28
- package/tool/executeTool.cjs +1 -0
- package/tool/executeTool.d.ts +5 -4
- package/tool/executeTool.js +1 -0
- package/prompt/Llama2PromptMapping.d.ts +0 -10
- package/prompt/OpenAIChatPromptMapping.d.ts +0 -6
- package/prompt/PromptMapping.d.ts +0 -7
- package/prompt/TextPromptMapping.d.ts +0 -14
- /package/prompt/{PromptMapping.cjs → PromptFormat.cjs} +0 -0
- /package/prompt/{PromptMapping.js → PromptFormat.js} +0 -0
package/README.md
CHANGED
@@ -43,7 +43,7 @@ You can provide API keys for the different [integrations](https://modelfusion.de
|
|
43
43
|
|
44
44
|
Generate text using a language model and a prompt.
|
45
45
|
You can stream the text if it is supported by the model.
|
46
|
-
You can use [prompt
|
46
|
+
You can use [prompt formats](https://modelfusion.dev/guide/function/generate-text/prompt-format) to change the prompt format of a model.
|
47
47
|
|
48
48
|
#### generateText
|
49
49
|
|
@@ -58,7 +58,10 @@ const text = await generateText(
|
|
58
58
|
|
59
59
|
```ts
|
60
60
|
const textStream = await streamText(
|
61
|
-
new OpenAIChatModel({
|
61
|
+
new OpenAIChatModel({
|
62
|
+
model: "gpt-3.5-turbo",
|
63
|
+
maxCompletionTokens: 1000,
|
64
|
+
}),
|
62
65
|
[
|
63
66
|
OpenAIChatMessage.system("You are a story writer."),
|
64
67
|
OpenAIChatMessage.user("Write a story about a robot learning to love"),
|
@@ -70,16 +73,16 @@ for await (const textFragment of textStream) {
|
|
70
73
|
}
|
71
74
|
```
|
72
75
|
|
73
|
-
#### Prompt
|
76
|
+
#### Prompt Format
|
74
77
|
|
75
|
-
[Prompt
|
78
|
+
[Prompt format](https://modelfusion.dev/guide/function/generate-text/prompt-format) lets you use higher level prompt structures (such as instruction or chat prompts) for different models.
|
76
79
|
|
77
80
|
```ts
|
78
81
|
const text = await generateText(
|
79
82
|
new LlamaCppTextGenerationModel({
|
80
83
|
contextWindowSize: 4096, // Llama 2 context window size
|
81
|
-
|
82
|
-
}).
|
84
|
+
maxCompletionTokens: 1000,
|
85
|
+
}).withPromptFormat(Llama2InstructionPromptFormat()),
|
83
86
|
{
|
84
87
|
system: "You are a story writer.",
|
85
88
|
instruction: "Write a short story about a robot learning to love.",
|
@@ -91,7 +94,7 @@ const text = await generateText(
|
|
91
94
|
const textStream = await streamText(
|
92
95
|
new OpenAIChatModel({
|
93
96
|
model: "gpt-3.5-turbo",
|
94
|
-
}).
|
97
|
+
}).withPromptFormat(OpenAIChatChatPromptFormat()),
|
95
98
|
[
|
96
99
|
{ system: "You are a celebrated poet." },
|
97
100
|
{ user: "Write a short story about a robot learning to love." },
|
@@ -111,7 +114,7 @@ ModelFusion model functions return rich results that include the original respon
|
|
111
114
|
const { response, metadata } = await generateText(
|
112
115
|
new OpenAITextGenerationModel({
|
113
116
|
model: "text-davinci-003",
|
114
|
-
|
117
|
+
maxCompletionTokens: 1000,
|
115
118
|
n: 2, // generate 2 completions
|
116
119
|
}),
|
117
120
|
"Write a short story about a robot learning to love:\n\n",
|
@@ -134,7 +137,7 @@ const value = await generateJson(
|
|
134
137
|
new OpenAIChatModel({
|
135
138
|
model: "gpt-3.5-turbo",
|
136
139
|
temperature: 0,
|
137
|
-
|
140
|
+
maxCompletionTokens: 50,
|
138
141
|
}),
|
139
142
|
{
|
140
143
|
name: "sentiment" as const,
|
@@ -165,7 +168,10 @@ It either matches one of the schemas or is text reponse.
|
|
165
168
|
|
166
169
|
```ts
|
167
170
|
const { schema, value, text } = await generateJsonOrText(
|
168
|
-
new OpenAIChatModel({
|
171
|
+
new OpenAIChatModel({
|
172
|
+
model: "gpt-3.5-turbo",
|
173
|
+
maxCompletionTokens: 1000,
|
174
|
+
}),
|
169
175
|
[
|
170
176
|
{
|
171
177
|
name: "getCurrentWeather" as const, // mark 'as const' for type inference
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs
CHANGED
@@ -10,7 +10,7 @@ const summarizeRecursively_js_1 = require("./summarizeRecursively.cjs");
|
|
10
10
|
* while leaving enough space for the model to generate text.
|
11
11
|
*/
|
12
12
|
async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
|
13
|
-
(model.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
|
13
|
+
(model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
|
14
14
|
const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
|
15
15
|
return (0, summarizeRecursively_js_1.summarizeRecursively)({
|
16
16
|
split: (0, splitRecursively_js_1.splitAtToken)({
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js
CHANGED
@@ -7,7 +7,7 @@ import { summarizeRecursively } from "./summarizeRecursively.js";
|
|
7
7
|
* while leaving enough space for the model to generate text.
|
8
8
|
*/
|
9
9
|
export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
|
10
|
-
(model.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
|
10
|
+
(model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
|
11
11
|
const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
|
12
12
|
return summarizeRecursively({
|
13
13
|
split: splitAtToken({
|
@@ -12,11 +12,11 @@ export interface Model<SETTINGS> {
|
|
12
12
|
* @example
|
13
13
|
* const model = new OpenAITextGenerationModel({
|
14
14
|
* model: "text-davinci-003",
|
15
|
-
*
|
15
|
+
* maxCompletionTokens: 500,
|
16
16
|
* });
|
17
17
|
*
|
18
18
|
* const modelWithMoreTokens = model.withSettings({
|
19
|
-
*
|
19
|
+
* maxCompletionTokens: 1000,
|
20
20
|
* });
|
21
21
|
*/
|
22
22
|
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
@@ -1,11 +1,25 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
2
|
+
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
3
3
|
import { FunctionOptions } from "../FunctionOptions.js";
|
4
4
|
import { Model, ModelSettings } from "../Model.js";
|
5
5
|
import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
|
6
6
|
import { DeltaEvent } from "./DeltaEvent.js";
|
7
7
|
export interface TextGenerationModelSettings extends ModelSettings {
|
8
|
-
|
8
|
+
/**
|
9
|
+
* Maximum number of tokens to generate.
|
10
|
+
* Does nothing if the model does not support this setting.
|
11
|
+
*/
|
12
|
+
maxCompletionTokens?: number | undefined;
|
13
|
+
/**
|
14
|
+
* Stop sequences to use. Stop sequences are not included in the generated text.
|
15
|
+
* Does nothing if the model does not support this setting.
|
16
|
+
*/
|
17
|
+
stopSequences?: string[] | undefined;
|
18
|
+
/**
|
19
|
+
* When true, the leading and trailing white space and line terminator characters
|
20
|
+
* are removed from the generated text.
|
21
|
+
*/
|
22
|
+
trimWhitespace?: boolean;
|
9
23
|
}
|
10
24
|
export interface TextGenerationModel<PROMPT, RESPONSE, FULL_DELTA, SETTINGS extends TextGenerationModelSettings> extends Model<SETTINGS> {
|
11
25
|
readonly contextWindowSize: number | undefined;
|
@@ -24,19 +38,5 @@ export interface TextGenerationModel<PROMPT, RESPONSE, FULL_DELTA, SETTINGS exte
|
|
24
38
|
* Optional. Implement for streaming support.
|
25
39
|
*/
|
26
40
|
readonly extractTextDelta: ((fullDelta: FULL_DELTA) => string | undefined) | undefined;
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Maximum number of tokens to generate.
|
30
|
-
*/
|
31
|
-
readonly maxCompletionTokens: number | undefined;
|
32
|
-
/**
|
33
|
-
* Sets the maximum number of tokens to generate.
|
34
|
-
* Does nothing if the model does not support this setting.
|
35
|
-
*/
|
36
|
-
withMaxCompletionTokens(maxCompletionTokens: number): this;
|
37
|
-
/**
|
38
|
-
* Sets the stop tokens to use. Stop tokens are not included in the generated text.
|
39
|
-
* Does nothing if the model does not support this setting.
|
40
|
-
*/
|
41
|
-
withStopTokens(stopTokens: string[]): this;
|
41
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, RESPONSE, FULL_DELTA, SETTINGS, this>;
|
42
42
|
}
|
@@ -10,8 +10,8 @@ model, prompt, options) {
|
|
10
10
|
options,
|
11
11
|
generateResponse: (options) => model.generateTextResponse(prompt, options),
|
12
12
|
extractOutputValue: (result) => {
|
13
|
-
const
|
14
|
-
return
|
13
|
+
const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
|
14
|
+
return shouldTrimWhitespace
|
15
15
|
? model.extractText(result).trim()
|
16
16
|
: model.extractText(result);
|
17
17
|
},
|
@@ -7,8 +7,8 @@ model, prompt, options) {
|
|
7
7
|
options,
|
8
8
|
generateResponse: (options) => model.generateTextResponse(prompt, options),
|
9
9
|
extractOutputValue: (result) => {
|
10
|
-
const
|
11
|
-
return
|
10
|
+
const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
|
11
|
+
return shouldTrimWhitespace
|
12
12
|
? model.extractText(result).trim()
|
13
13
|
: model.extractText(result);
|
14
14
|
},
|
@@ -9,7 +9,7 @@ const zod_1 = require("zod");
|
|
9
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
10
10
|
const AsyncQueue_js_1 = require("../../model-function/generate-text/AsyncQueue.cjs");
|
11
11
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
12
|
-
const
|
12
|
+
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
13
13
|
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
14
14
|
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
15
15
|
const CohereError_js_1 = require("./CohereError.cjs");
|
@@ -37,7 +37,7 @@ exports.COHERE_TEXT_GENERATION_MODELS = {
|
|
37
37
|
* const model = new CohereTextGenerationModel({
|
38
38
|
* model: "command-nightly",
|
39
39
|
* temperature: 0.7,
|
40
|
-
*
|
40
|
+
* maxCompletionTokens: 500,
|
41
41
|
* });
|
42
42
|
*
|
43
43
|
* const text = await generateText(
|
@@ -91,13 +91,21 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
91
91
|
}
|
92
92
|
async callAPI(prompt, options) {
|
93
93
|
const { run, settings, responseFormat } = options;
|
94
|
-
const
|
94
|
+
const combinedSettings = {
|
95
|
+
...this.settings,
|
96
|
+
settings,
|
97
|
+
};
|
98
|
+
const callSettings = {
|
95
99
|
apiKey: this.apiKey,
|
96
|
-
|
100
|
+
...combinedSettings,
|
101
|
+
// use endSequences instead of stopSequences
|
102
|
+
// to exclude stop tokens from the generated text
|
103
|
+
endSequences: combinedSettings.stopSequences,
|
104
|
+
maxTokens: combinedSettings.maxCompletionTokens,
|
97
105
|
abortSignal: run?.abortSignal,
|
98
106
|
prompt,
|
99
107
|
responseFormat,
|
100
|
-
}
|
108
|
+
};
|
101
109
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
102
110
|
retry: this.settings.retry,
|
103
111
|
throttle: this.settings.throttle,
|
@@ -122,26 +130,17 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
122
130
|
extractTextDelta(fullDelta) {
|
123
131
|
return fullDelta.delta;
|
124
132
|
}
|
125
|
-
|
126
|
-
return new
|
127
|
-
model: this.
|
128
|
-
|
133
|
+
withPromptFormat(promptFormat) {
|
134
|
+
return new PromptFormatTextGenerationModel_js_1.PromptFormatTextGenerationModel({
|
135
|
+
model: this.withSettings({
|
136
|
+
stopSequences: promptFormat.stopSequences,
|
137
|
+
}),
|
138
|
+
promptFormat,
|
129
139
|
});
|
130
140
|
}
|
131
141
|
withSettings(additionalSettings) {
|
132
142
|
return new CohereTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
133
143
|
}
|
134
|
-
get maxCompletionTokens() {
|
135
|
-
return this.settings.maxTokens;
|
136
|
-
}
|
137
|
-
withMaxCompletionTokens(maxCompletionTokens) {
|
138
|
-
return this.withSettings({ maxTokens: maxCompletionTokens });
|
139
|
-
}
|
140
|
-
withStopTokens(stopTokens) {
|
141
|
-
// use endSequences instead of stopSequences
|
142
|
-
// to exclude stop tokens from the generated text
|
143
|
-
return this.withSettings({ endSequences: stopTokens });
|
144
|
-
}
|
145
144
|
}
|
146
145
|
exports.CohereTextGenerationModel = CohereTextGenerationModel;
|
147
146
|
const cohereTextGenerationResponseSchema = zod_1.z.object({
|
@@ -3,8 +3,8 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
|
|
3
3
|
import { FunctionOptions } from "../../model-function/FunctionOptions.js";
|
4
4
|
import { DeltaEvent } from "../../model-function/generate-text/DeltaEvent.js";
|
5
5
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
|
6
|
-
import {
|
7
|
-
import {
|
6
|
+
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
7
|
+
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
8
8
|
import { RetryFunction } from "../../util/api/RetryFunction.js";
|
9
9
|
import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
|
10
10
|
import { ResponseHandler } from "../../util/api/postToApi.js";
|
@@ -35,13 +35,11 @@ export interface CohereTextGenerationModelSettings extends TextGenerationModelSe
|
|
35
35
|
throttle?: ThrottleFunction;
|
36
36
|
};
|
37
37
|
numGenerations?: number;
|
38
|
-
maxTokens?: number;
|
39
38
|
temperature?: number;
|
40
39
|
k?: number;
|
41
40
|
p?: number;
|
42
41
|
frequencyPenalty?: number;
|
43
42
|
presencePenalty?: number;
|
44
|
-
endSequences?: string[];
|
45
43
|
stopSequences?: string[];
|
46
44
|
returnLikelihoods?: "GENERATION" | "ALL" | "NONE";
|
47
45
|
logitBias?: Record<string, number>;
|
@@ -56,7 +54,7 @@ export interface CohereTextGenerationModelSettings extends TextGenerationModelSe
|
|
56
54
|
* const model = new CohereTextGenerationModel({
|
57
55
|
* model: "command-nightly",
|
58
56
|
* temperature: 0.7,
|
59
|
-
*
|
57
|
+
* maxCompletionTokens: 500,
|
60
58
|
* });
|
61
59
|
*
|
62
60
|
* const text = await generateText(
|
@@ -92,11 +90,8 @@ export declare class CohereTextGenerationModel extends AbstractModel<CohereTextG
|
|
92
90
|
extractText(response: CohereTextGenerationResponse): string;
|
93
91
|
generateDeltaStreamResponse(prompt: string, options?: FunctionOptions<CohereTextGenerationModelSettings>): Promise<AsyncIterable<DeltaEvent<CohereTextGenerationDelta>>>;
|
94
92
|
extractTextDelta(fullDelta: CohereTextGenerationDelta): string | undefined;
|
95
|
-
|
93
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, string>): PromptFormatTextGenerationModel<INPUT_PROMPT, string, CohereTextGenerationResponse, CohereTextGenerationDelta, CohereTextGenerationModelSettings, this>;
|
96
94
|
withSettings(additionalSettings: Partial<CohereTextGenerationModelSettings>): this;
|
97
|
-
get maxCompletionTokens(): number | undefined;
|
98
|
-
withMaxCompletionTokens(maxCompletionTokens: number): this;
|
99
|
-
withStopTokens(stopTokens: string[]): this;
|
100
95
|
}
|
101
96
|
declare const cohereTextGenerationResponseSchema: z.ZodObject<{
|
102
97
|
id: z.ZodString;
|
@@ -3,7 +3,7 @@ import { z } from "zod";
|
|
3
3
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
4
4
|
import { AsyncQueue } from "../../model-function/generate-text/AsyncQueue.js";
|
5
5
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
6
|
-
import {
|
6
|
+
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
7
7
|
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
8
8
|
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
9
9
|
import { failedCohereCallResponseHandler } from "./CohereError.js";
|
@@ -31,7 +31,7 @@ export const COHERE_TEXT_GENERATION_MODELS = {
|
|
31
31
|
* const model = new CohereTextGenerationModel({
|
32
32
|
* model: "command-nightly",
|
33
33
|
* temperature: 0.7,
|
34
|
-
*
|
34
|
+
* maxCompletionTokens: 500,
|
35
35
|
* });
|
36
36
|
*
|
37
37
|
* const text = await generateText(
|
@@ -85,13 +85,21 @@ export class CohereTextGenerationModel extends AbstractModel {
|
|
85
85
|
}
|
86
86
|
async callAPI(prompt, options) {
|
87
87
|
const { run, settings, responseFormat } = options;
|
88
|
-
const
|
88
|
+
const combinedSettings = {
|
89
|
+
...this.settings,
|
90
|
+
settings,
|
91
|
+
};
|
92
|
+
const callSettings = {
|
89
93
|
apiKey: this.apiKey,
|
90
|
-
|
94
|
+
...combinedSettings,
|
95
|
+
// use endSequences instead of stopSequences
|
96
|
+
// to exclude stop tokens from the generated text
|
97
|
+
endSequences: combinedSettings.stopSequences,
|
98
|
+
maxTokens: combinedSettings.maxCompletionTokens,
|
91
99
|
abortSignal: run?.abortSignal,
|
92
100
|
prompt,
|
93
101
|
responseFormat,
|
94
|
-
}
|
102
|
+
};
|
95
103
|
return callWithRetryAndThrottle({
|
96
104
|
retry: this.settings.retry,
|
97
105
|
throttle: this.settings.throttle,
|
@@ -116,26 +124,17 @@ export class CohereTextGenerationModel extends AbstractModel {
|
|
116
124
|
extractTextDelta(fullDelta) {
|
117
125
|
return fullDelta.delta;
|
118
126
|
}
|
119
|
-
|
120
|
-
return new
|
121
|
-
model: this.
|
122
|
-
|
127
|
+
withPromptFormat(promptFormat) {
|
128
|
+
return new PromptFormatTextGenerationModel({
|
129
|
+
model: this.withSettings({
|
130
|
+
stopSequences: promptFormat.stopSequences,
|
131
|
+
}),
|
132
|
+
promptFormat,
|
123
133
|
});
|
124
134
|
}
|
125
135
|
withSettings(additionalSettings) {
|
126
136
|
return new CohereTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
127
137
|
}
|
128
|
-
get maxCompletionTokens() {
|
129
|
-
return this.settings.maxTokens;
|
130
|
-
}
|
131
|
-
withMaxCompletionTokens(maxCompletionTokens) {
|
132
|
-
return this.withSettings({ maxTokens: maxCompletionTokens });
|
133
|
-
}
|
134
|
-
withStopTokens(stopTokens) {
|
135
|
-
// use endSequences instead of stopSequences
|
136
|
-
// to exclude stop tokens from the generated text
|
137
|
-
return this.withSettings({ endSequences: stopTokens });
|
138
|
-
}
|
139
138
|
}
|
140
139
|
const cohereTextGenerationResponseSchema = z.object({
|
141
140
|
id: z.string(),
|
@@ -9,7 +9,7 @@ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
|
9
9
|
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
10
10
|
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
11
11
|
const HuggingFaceError_js_1 = require("./HuggingFaceError.cjs");
|
12
|
-
const
|
12
|
+
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
13
13
|
/**
|
14
14
|
* Create a text generation model that calls a Hugging Face Inference API Text Generation Task.
|
15
15
|
*
|
@@ -19,7 +19,7 @@ const PromptMappingTextGenerationModel_js_1 = require("../../prompt/PromptMappin
|
|
19
19
|
* const model = new HuggingFaceTextGenerationModel({
|
20
20
|
* model: "tiiuae/falcon-7b",
|
21
21
|
* temperature: 0.7,
|
22
|
-
*
|
22
|
+
* maxCompletionTokens: 500,
|
23
23
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
24
24
|
* });
|
25
25
|
*
|
@@ -81,16 +81,21 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
81
81
|
async callAPI(prompt, options) {
|
82
82
|
const run = options?.run;
|
83
83
|
const settings = options?.settings;
|
84
|
-
const
|
84
|
+
const combinedSettings = {
|
85
|
+
...this.settings,
|
86
|
+
...settings,
|
87
|
+
};
|
88
|
+
const callSettings = {
|
85
89
|
apiKey: this.apiKey,
|
86
90
|
options: {
|
87
91
|
useCache: true,
|
88
92
|
waitForModel: true,
|
89
93
|
},
|
90
|
-
|
94
|
+
...combinedSettings,
|
95
|
+
maxNewTokens: combinedSettings.maxCompletionTokens,
|
91
96
|
abortSignal: run?.abortSignal,
|
92
97
|
inputs: prompt,
|
93
|
-
}
|
98
|
+
};
|
94
99
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
95
100
|
retry: this.settings.retry,
|
96
101
|
throttle: this.settings.throttle,
|
@@ -103,25 +108,15 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
103
108
|
extractText(response) {
|
104
109
|
return response[0].generated_text;
|
105
110
|
}
|
106
|
-
|
107
|
-
return new
|
111
|
+
withPromptFormat(promptFormat) {
|
112
|
+
return new PromptFormatTextGenerationModel_js_1.PromptFormatTextGenerationModel({
|
108
113
|
model: this,
|
109
|
-
|
114
|
+
promptFormat,
|
110
115
|
});
|
111
116
|
}
|
112
117
|
withSettings(additionalSettings) {
|
113
118
|
return new HuggingFaceTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
114
119
|
}
|
115
|
-
get maxCompletionTokens() {
|
116
|
-
return this.settings.maxNewTokens;
|
117
|
-
}
|
118
|
-
withMaxCompletionTokens(maxCompletionTokens) {
|
119
|
-
return this.withSettings({ maxNewTokens: maxCompletionTokens });
|
120
|
-
}
|
121
|
-
withStopTokens() {
|
122
|
-
// stop tokens are not supported by the HuggingFace API
|
123
|
-
return this;
|
124
|
-
}
|
125
120
|
}
|
126
121
|
exports.HuggingFaceTextGenerationModel = HuggingFaceTextGenerationModel;
|
127
122
|
const huggingFaceTextGenerationResponseSchema = zod_1.default.array(zod_1.default.object({
|
@@ -4,8 +4,8 @@ import { FunctionOptions } from "../../model-function/FunctionOptions.js";
|
|
4
4
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
|
5
5
|
import { RetryFunction } from "../../util/api/RetryFunction.js";
|
6
6
|
import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
|
7
|
-
import {
|
8
|
-
import {
|
7
|
+
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
8
|
+
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
9
9
|
export interface HuggingFaceTextGenerationModelSettings extends TextGenerationModelSettings {
|
10
10
|
model: string;
|
11
11
|
baseUrl?: string;
|
@@ -16,7 +16,6 @@ export interface HuggingFaceTextGenerationModelSettings extends TextGenerationMo
|
|
16
16
|
topP?: number;
|
17
17
|
temperature?: number;
|
18
18
|
repetitionPenalty?: number;
|
19
|
-
maxNewTokens?: number;
|
20
19
|
maxTime?: number;
|
21
20
|
numReturnSequences?: number;
|
22
21
|
doSample?: boolean;
|
@@ -34,7 +33,7 @@ export interface HuggingFaceTextGenerationModelSettings extends TextGenerationMo
|
|
34
33
|
* const model = new HuggingFaceTextGenerationModel({
|
35
34
|
* model: "tiiuae/falcon-7b",
|
36
35
|
* temperature: 0.7,
|
37
|
-
*
|
36
|
+
* maxCompletionTokens: 500,
|
38
37
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
39
38
|
* });
|
40
39
|
*
|
@@ -58,11 +57,8 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
|
|
58
57
|
extractText(response: HuggingFaceTextGenerationResponse): string;
|
59
58
|
generateDeltaStreamResponse: undefined;
|
60
59
|
extractTextDelta: undefined;
|
61
|
-
|
60
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, string>): PromptFormatTextGenerationModel<INPUT_PROMPT, string, HuggingFaceTextGenerationResponse, undefined, HuggingFaceTextGenerationModelSettings, this>;
|
62
61
|
withSettings(additionalSettings: Partial<HuggingFaceTextGenerationModelSettings>): this;
|
63
|
-
get maxCompletionTokens(): number | undefined;
|
64
|
-
withMaxCompletionTokens(maxCompletionTokens: number): this;
|
65
|
-
withStopTokens(): this;
|
66
62
|
}
|
67
63
|
declare const huggingFaceTextGenerationResponseSchema: z.ZodArray<z.ZodObject<{
|
68
64
|
generated_text: z.ZodString;
|
@@ -3,7 +3,7 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
|
|
3
3
|
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
4
4
|
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
5
5
|
import { failedHuggingFaceCallResponseHandler } from "./HuggingFaceError.js";
|
6
|
-
import {
|
6
|
+
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
7
7
|
/**
|
8
8
|
* Create a text generation model that calls a Hugging Face Inference API Text Generation Task.
|
9
9
|
*
|
@@ -13,7 +13,7 @@ import { PromptMappingTextGenerationModel } from "../../prompt/PromptMappingText
|
|
13
13
|
* const model = new HuggingFaceTextGenerationModel({
|
14
14
|
* model: "tiiuae/falcon-7b",
|
15
15
|
* temperature: 0.7,
|
16
|
-
*
|
16
|
+
* maxCompletionTokens: 500,
|
17
17
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
18
18
|
* });
|
19
19
|
*
|
@@ -75,16 +75,21 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
75
75
|
async callAPI(prompt, options) {
|
76
76
|
const run = options?.run;
|
77
77
|
const settings = options?.settings;
|
78
|
-
const
|
78
|
+
const combinedSettings = {
|
79
|
+
...this.settings,
|
80
|
+
...settings,
|
81
|
+
};
|
82
|
+
const callSettings = {
|
79
83
|
apiKey: this.apiKey,
|
80
84
|
options: {
|
81
85
|
useCache: true,
|
82
86
|
waitForModel: true,
|
83
87
|
},
|
84
|
-
|
88
|
+
...combinedSettings,
|
89
|
+
maxNewTokens: combinedSettings.maxCompletionTokens,
|
85
90
|
abortSignal: run?.abortSignal,
|
86
91
|
inputs: prompt,
|
87
|
-
}
|
92
|
+
};
|
88
93
|
return callWithRetryAndThrottle({
|
89
94
|
retry: this.settings.retry,
|
90
95
|
throttle: this.settings.throttle,
|
@@ -97,25 +102,15 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
97
102
|
extractText(response) {
|
98
103
|
return response[0].generated_text;
|
99
104
|
}
|
100
|
-
|
101
|
-
return new
|
105
|
+
withPromptFormat(promptFormat) {
|
106
|
+
return new PromptFormatTextGenerationModel({
|
102
107
|
model: this,
|
103
|
-
|
108
|
+
promptFormat,
|
104
109
|
});
|
105
110
|
}
|
106
111
|
withSettings(additionalSettings) {
|
107
112
|
return new HuggingFaceTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
108
113
|
}
|
109
|
-
get maxCompletionTokens() {
|
110
|
-
return this.settings.maxNewTokens;
|
111
|
-
}
|
112
|
-
withMaxCompletionTokens(maxCompletionTokens) {
|
113
|
-
return this.withSettings({ maxNewTokens: maxCompletionTokens });
|
114
|
-
}
|
115
|
-
withStopTokens() {
|
116
|
-
// stop tokens are not supported by the HuggingFace API
|
117
|
-
return this;
|
118
|
-
}
|
119
114
|
}
|
120
115
|
const huggingFaceTextGenerationResponseSchema = z.array(z.object({
|
121
116
|
generated_text: z.string(),
|
@@ -9,7 +9,7 @@ const zod_1 = __importDefault(require("zod"));
|
|
9
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
10
10
|
const AsyncQueue_js_1 = require("../../model-function/generate-text/AsyncQueue.cjs");
|
11
11
|
const parseEventSourceReadableStream_js_1 = require("../../model-function/generate-text/parseEventSourceReadableStream.cjs");
|
12
|
-
const
|
12
|
+
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
13
13
|
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
14
14
|
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
15
15
|
const LlamaCppError_js_1 = require("./LlamaCppError.cjs");
|
@@ -43,11 +43,18 @@ class LlamaCppTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
43
43
|
}
|
44
44
|
async callAPI(prompt, options) {
|
45
45
|
const { run, settings, responseFormat } = options;
|
46
|
-
const
|
46
|
+
const combinedSettings = {
|
47
|
+
...this.settings,
|
48
|
+
...settings,
|
49
|
+
};
|
50
|
+
const callSettings = {
|
51
|
+
...combinedSettings,
|
52
|
+
nPredict: combinedSettings.maxCompletionTokens,
|
53
|
+
stop: combinedSettings.stopSequences,
|
47
54
|
abortSignal: run?.abortSignal,
|
48
55
|
prompt,
|
49
56
|
responseFormat,
|
50
|
-
}
|
57
|
+
};
|
51
58
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
52
59
|
retry: this.settings.retry,
|
53
60
|
throttle: this.settings.throttle,
|
@@ -76,24 +83,17 @@ class LlamaCppTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
76
83
|
extractTextDelta(fullDelta) {
|
77
84
|
return fullDelta.delta;
|
78
85
|
}
|
79
|
-
|
80
|
-
return new
|
81
|
-
model: this.
|
82
|
-
|
86
|
+
withPromptFormat(promptFormat) {
|
87
|
+
return new PromptFormatTextGenerationModel_js_1.PromptFormatTextGenerationModel({
|
88
|
+
model: this.withSettings({
|
89
|
+
stopSequences: promptFormat.stopSequences,
|
90
|
+
}),
|
91
|
+
promptFormat,
|
83
92
|
});
|
84
93
|
}
|
85
94
|
withSettings(additionalSettings) {
|
86
95
|
return new LlamaCppTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
87
96
|
}
|
88
|
-
get maxCompletionTokens() {
|
89
|
-
return this.settings.nPredict;
|
90
|
-
}
|
91
|
-
withMaxCompletionTokens(maxCompletionTokens) {
|
92
|
-
return this.withSettings({ nPredict: maxCompletionTokens });
|
93
|
-
}
|
94
|
-
withStopTokens(stopTokens) {
|
95
|
-
return this.withSettings({ stop: stopTokens });
|
96
|
-
}
|
97
97
|
}
|
98
98
|
exports.LlamaCppTextGenerationModel = LlamaCppTextGenerationModel;
|
99
99
|
const llamaCppTextGenerationResponseSchema = zod_1.default.object({
|