modelfusion 0.98.0 → 0.99.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 +4 -4
- 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/PromptTemplateTextGenerationModel.cjs +2 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +2 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.js +2 -2
- package/model-function/generate-text/TextGenerationModel.d.ts +31 -5
- package/model-function/generate-text/generateText.cjs +10 -4
- package/model-function/generate-text/generateText.d.ts +1 -0
- package/model-function/generate-text/generateText.js +10 -4
- package/model-function/generate-text/prompt-template/trimChatPrompt.cjs +1 -1
- package/model-function/generate-text/prompt-template/trimChatPrompt.js +1 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +27 -31
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +2 -2
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +27 -31
- package/model-provider/cohere/CohereFacade.cjs +1 -1
- package/model-provider/cohere/CohereFacade.d.ts +1 -1
- package/model-provider/cohere/CohereFacade.js +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.cjs +34 -43
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +3 -4
- package/model-provider/cohere/CohereTextGenerationModel.js +34 -43
- package/model-provider/huggingface/HuggingFaceFacade.cjs +1 -1
- package/model-provider/huggingface/HuggingFaceFacade.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceFacade.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +31 -41
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +3 -4
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +31 -41
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +4 -4
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +2 -2
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +4 -4
- package/model-provider/mistral/MistralTextGenerationModel.cjs +5 -5
- package/model-provider/mistral/MistralTextGenerationModel.d.ts +2 -2
- package/model-provider/mistral/MistralTextGenerationModel.js +5 -5
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +4 -4
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +2 -2
- package/model-provider/ollama/OllamaTextGenerationModel.js +4 -4
- package/model-provider/openai/OpenAICompletionModel.cjs +48 -53
- package/model-provider/openai/OpenAICompletionModel.d.ts +3 -6
- package/model-provider/openai/OpenAICompletionModel.js +48 -53
- package/model-provider/openai/OpenAIFacade.cjs +2 -2
- package/model-provider/openai/OpenAIFacade.d.ts +2 -2
- package/model-provider/openai/OpenAIFacade.js +2 -2
- package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +50 -54
- package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +6 -27
- package/model-provider/openai/chat/AbstractOpenAIChatModel.js +50 -54
- package/model-provider/openai/chat/OpenAIChatModel.cjs +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.js +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.test.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.test.js +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleFacade.js +1 -1
- package/package.json +1 -1
@@ -66,7 +66,7 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
66
66
|
num_ctx: this.settings.contextWindowSize,
|
67
67
|
num_gpu: this.settings.numGpu,
|
68
68
|
num_gqa: this.settings.numGqa,
|
69
|
-
num_predict: this.settings.
|
69
|
+
num_predict: this.settings.maxGenerationTokens,
|
70
70
|
num_threads: this.settings.numThreads,
|
71
71
|
repeat_last_n: this.settings.repeatLastN,
|
72
72
|
repeat_penalty: this.settings.repeatPenalty,
|
@@ -90,7 +90,7 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
90
90
|
}
|
91
91
|
get settingsForEvent() {
|
92
92
|
const eventSettingProperties = [
|
93
|
-
"
|
93
|
+
"maxGenerationTokens",
|
94
94
|
"stopSequences",
|
95
95
|
"contextWindowSize",
|
96
96
|
"temperature",
|
@@ -114,14 +114,14 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
114
114
|
];
|
115
115
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
116
116
|
}
|
117
|
-
async
|
117
|
+
async doGenerateTexts(prompt, options) {
|
118
118
|
const response = await this.callAPI(prompt, {
|
119
119
|
...options,
|
120
120
|
responseFormat: exports.OllamaTextGenerationResponseFormat.json,
|
121
121
|
});
|
122
122
|
return {
|
123
123
|
response,
|
124
|
-
|
124
|
+
texts: [response.response],
|
125
125
|
};
|
126
126
|
}
|
127
127
|
doStreamText(prompt, options) {
|
@@ -132,7 +132,7 @@ export declare class OllamaTextGenerationModel<CONTEXT_WINDOW_SIZE extends numbe
|
|
132
132
|
responseFormat: OllamaTextGenerationResponseFormatType<RESPONSE>;
|
133
133
|
} & FunctionOptions): Promise<RESPONSE>;
|
134
134
|
get settingsForEvent(): Partial<OllamaTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>>;
|
135
|
-
|
135
|
+
doGenerateTexts(prompt: OllamaTextGenerationPrompt, options?: FunctionOptions): Promise<{
|
136
136
|
response: {
|
137
137
|
response: string;
|
138
138
|
model: string;
|
@@ -144,7 +144,7 @@ export declare class OllamaTextGenerationModel<CONTEXT_WINDOW_SIZE extends numbe
|
|
144
144
|
load_duration?: number | undefined;
|
145
145
|
context?: number[] | undefined;
|
146
146
|
};
|
147
|
-
|
147
|
+
texts: string[];
|
148
148
|
}>;
|
149
149
|
doStreamText(prompt: OllamaTextGenerationPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
|
150
150
|
asToolCallGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallPromptTemplate<INPUT_PROMPT, OllamaTextGenerationPrompt>): TextGenerationToolCallModel<INPUT_PROMPT, OllamaTextGenerationPrompt, this>;
|
@@ -63,7 +63,7 @@ export class OllamaTextGenerationModel extends AbstractModel {
|
|
63
63
|
num_ctx: this.settings.contextWindowSize,
|
64
64
|
num_gpu: this.settings.numGpu,
|
65
65
|
num_gqa: this.settings.numGqa,
|
66
|
-
num_predict: this.settings.
|
66
|
+
num_predict: this.settings.maxGenerationTokens,
|
67
67
|
num_threads: this.settings.numThreads,
|
68
68
|
repeat_last_n: this.settings.repeatLastN,
|
69
69
|
repeat_penalty: this.settings.repeatPenalty,
|
@@ -87,7 +87,7 @@ export class OllamaTextGenerationModel extends AbstractModel {
|
|
87
87
|
}
|
88
88
|
get settingsForEvent() {
|
89
89
|
const eventSettingProperties = [
|
90
|
-
"
|
90
|
+
"maxGenerationTokens",
|
91
91
|
"stopSequences",
|
92
92
|
"contextWindowSize",
|
93
93
|
"temperature",
|
@@ -111,14 +111,14 @@ export class OllamaTextGenerationModel extends AbstractModel {
|
|
111
111
|
];
|
112
112
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
113
113
|
}
|
114
|
-
async
|
114
|
+
async doGenerateTexts(prompt, options) {
|
115
115
|
const response = await this.callAPI(prompt, {
|
116
116
|
...options,
|
117
117
|
responseFormat: OllamaTextGenerationResponseFormat.json,
|
118
118
|
});
|
119
119
|
return {
|
120
120
|
response,
|
121
|
-
|
121
|
+
texts: [response.response],
|
122
122
|
};
|
123
123
|
}
|
124
124
|
doStreamText(prompt, options) {
|
@@ -137,7 +137,7 @@ exports.calculateOpenAICompletionCostInMillicents = calculateOpenAICompletionCos
|
|
137
137
|
* const model = new OpenAICompletionModel({
|
138
138
|
* model: "gpt-3.5-turbo-instruct",
|
139
139
|
* temperature: 0.7,
|
140
|
-
*
|
140
|
+
* maxGenerationTokens: 500,
|
141
141
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
142
142
|
* });
|
143
143
|
*
|
@@ -180,33 +180,60 @@ class OpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
|
|
180
180
|
return (0, countTokens_js_1.countTokens)(this.tokenizer, input);
|
181
181
|
}
|
182
182
|
async callAPI(prompt, options) {
|
183
|
-
const
|
184
|
-
const
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
maxTokens: this.settings.maxCompletionTokens,
|
191
|
-
// other settings:
|
192
|
-
abortSignal: run?.abortSignal,
|
193
|
-
prompt,
|
194
|
-
responseFormat,
|
195
|
-
};
|
183
|
+
const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
|
184
|
+
const user = this.settings.isUserIdForwardingEnabled
|
185
|
+
? options.run?.userId
|
186
|
+
: undefined;
|
187
|
+
const abortSignal = options.run?.abortSignal;
|
188
|
+
let { stopSequences } = this.settings;
|
189
|
+
const openaiResponseFormat = options.responseFormat;
|
196
190
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
197
|
-
retry:
|
198
|
-
throttle:
|
199
|
-
call: async () =>
|
191
|
+
retry: api.retry,
|
192
|
+
throttle: api.throttle,
|
193
|
+
call: async () => {
|
194
|
+
// empty arrays are not allowed for stop:
|
195
|
+
if (stopSequences != null &&
|
196
|
+
Array.isArray(stopSequences) &&
|
197
|
+
stopSequences.length === 0) {
|
198
|
+
stopSequences = undefined;
|
199
|
+
}
|
200
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
201
|
+
url: api.assembleUrl("/completions"),
|
202
|
+
headers: api.headers,
|
203
|
+
body: {
|
204
|
+
stream: openaiResponseFormat.stream,
|
205
|
+
model: this.settings.model,
|
206
|
+
prompt,
|
207
|
+
suffix: this.settings.suffix,
|
208
|
+
max_tokens: this.settings.maxGenerationTokens,
|
209
|
+
temperature: this.settings.temperature,
|
210
|
+
top_p: this.settings.topP,
|
211
|
+
n: this.settings.numberOfGenerations,
|
212
|
+
logprobs: this.settings.logprobs,
|
213
|
+
echo: this.settings.echo,
|
214
|
+
stop: this.settings.stopSequences,
|
215
|
+
seed: this.settings.seed,
|
216
|
+
presence_penalty: this.settings.presencePenalty,
|
217
|
+
frequency_penalty: this.settings.frequencyPenalty,
|
218
|
+
best_of: this.settings.bestOf,
|
219
|
+
logit_bias: this.settings.logitBias,
|
220
|
+
user,
|
221
|
+
},
|
222
|
+
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
223
|
+
successfulResponseHandler: openaiResponseFormat.handler,
|
224
|
+
abortSignal,
|
225
|
+
});
|
226
|
+
},
|
200
227
|
});
|
201
228
|
}
|
202
229
|
get settingsForEvent() {
|
203
230
|
const eventSettingProperties = [
|
204
|
-
"
|
231
|
+
"maxGenerationTokens",
|
205
232
|
"stopSequences",
|
233
|
+
"numberOfGenerations",
|
206
234
|
"suffix",
|
207
235
|
"temperature",
|
208
236
|
"topP",
|
209
|
-
"n",
|
210
237
|
"logprobs",
|
211
238
|
"echo",
|
212
239
|
"presencePenalty",
|
@@ -217,14 +244,14 @@ class OpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
|
|
217
244
|
];
|
218
245
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
219
246
|
}
|
220
|
-
async
|
247
|
+
async doGenerateTexts(prompt, options) {
|
221
248
|
const response = await this.callAPI(prompt, {
|
222
249
|
...options,
|
223
250
|
responseFormat: exports.OpenAITextResponseFormat.json,
|
224
251
|
});
|
225
252
|
return {
|
226
253
|
response,
|
227
|
-
|
254
|
+
texts: response.choices.map((choice) => choice.text),
|
228
255
|
usage: {
|
229
256
|
promptTokens: response.usage.prompt_tokens,
|
230
257
|
completionTokens: response.usage.completion_tokens,
|
@@ -287,38 +314,6 @@ const OpenAICompletionResponseSchema = zod_1.z.object({
|
|
287
314
|
total_tokens: zod_1.z.number(),
|
288
315
|
}),
|
289
316
|
});
|
290
|
-
async function callOpenAICompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, seed, user, }) {
|
291
|
-
// empty arrays are not allowed for stop:
|
292
|
-
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
293
|
-
stop = undefined;
|
294
|
-
}
|
295
|
-
return (0, postToApi_js_1.postJsonToApi)({
|
296
|
-
url: api.assembleUrl("/completions"),
|
297
|
-
headers: api.headers,
|
298
|
-
body: {
|
299
|
-
stream: responseFormat.stream,
|
300
|
-
model,
|
301
|
-
prompt,
|
302
|
-
suffix,
|
303
|
-
max_tokens: maxTokens,
|
304
|
-
temperature,
|
305
|
-
top_p: topP,
|
306
|
-
n,
|
307
|
-
logprobs,
|
308
|
-
echo,
|
309
|
-
stop,
|
310
|
-
seed,
|
311
|
-
presence_penalty: presencePenalty,
|
312
|
-
frequency_penalty: frequencyPenalty,
|
313
|
-
best_of: bestOf,
|
314
|
-
logit_bias: logitBias,
|
315
|
-
user,
|
316
|
-
},
|
317
|
-
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
318
|
-
successfulResponseHandler: responseFormat.handler,
|
319
|
-
abortSignal,
|
320
|
-
});
|
321
|
-
}
|
322
317
|
exports.OpenAITextResponseFormat = {
|
323
318
|
/**
|
324
319
|
* Returns the response as a JSON object.
|
@@ -101,13 +101,10 @@ export interface OpenAICompletionCallSettings {
|
|
101
101
|
api?: ApiConfiguration;
|
102
102
|
model: OpenAICompletionModelType;
|
103
103
|
suffix?: string;
|
104
|
-
maxTokens?: number;
|
105
104
|
temperature?: number;
|
106
105
|
topP?: number;
|
107
|
-
n?: number;
|
108
106
|
logprobs?: number;
|
109
107
|
echo?: boolean;
|
110
|
-
stop?: string | string[];
|
111
108
|
presencePenalty?: number;
|
112
109
|
frequencyPenalty?: number;
|
113
110
|
bestOf?: number;
|
@@ -126,7 +123,7 @@ export interface OpenAICompletionModelSettings extends TextGenerationModelSettin
|
|
126
123
|
* const model = new OpenAICompletionModel({
|
127
124
|
* model: "gpt-3.5-turbo-instruct",
|
128
125
|
* temperature: 0.7,
|
129
|
-
*
|
126
|
+
* maxGenerationTokens: 500,
|
130
127
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
131
128
|
* });
|
132
129
|
*
|
@@ -146,7 +143,7 @@ export declare class OpenAICompletionModel extends AbstractModel<OpenAICompletio
|
|
146
143
|
responseFormat: OpenAITextResponseFormatType<RESULT>;
|
147
144
|
} & FunctionOptions): Promise<RESULT>;
|
148
145
|
get settingsForEvent(): Partial<OpenAICompletionModelSettings>;
|
149
|
-
|
146
|
+
doGenerateTexts(prompt: string, options?: FunctionOptions): Promise<{
|
150
147
|
response: {
|
151
148
|
object: "text_completion";
|
152
149
|
usage: {
|
@@ -165,7 +162,7 @@ export declare class OpenAICompletionModel extends AbstractModel<OpenAICompletio
|
|
165
162
|
}[];
|
166
163
|
system_fingerprint?: string | undefined;
|
167
164
|
};
|
168
|
-
|
165
|
+
texts: string[];
|
169
166
|
usage: {
|
170
167
|
promptTokens: number;
|
171
168
|
completionTokens: number;
|
@@ -131,7 +131,7 @@ export const calculateOpenAICompletionCostInMillicents = ({ model, response, })
|
|
131
131
|
* const model = new OpenAICompletionModel({
|
132
132
|
* model: "gpt-3.5-turbo-instruct",
|
133
133
|
* temperature: 0.7,
|
134
|
-
*
|
134
|
+
* maxGenerationTokens: 500,
|
135
135
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
136
136
|
* });
|
137
137
|
*
|
@@ -174,33 +174,60 @@ export class OpenAICompletionModel extends AbstractModel {
|
|
174
174
|
return countTokens(this.tokenizer, input);
|
175
175
|
}
|
176
176
|
async callAPI(prompt, options) {
|
177
|
-
const
|
178
|
-
const
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
maxTokens: this.settings.maxCompletionTokens,
|
185
|
-
// other settings:
|
186
|
-
abortSignal: run?.abortSignal,
|
187
|
-
prompt,
|
188
|
-
responseFormat,
|
189
|
-
};
|
177
|
+
const api = this.settings.api ?? new OpenAIApiConfiguration();
|
178
|
+
const user = this.settings.isUserIdForwardingEnabled
|
179
|
+
? options.run?.userId
|
180
|
+
: undefined;
|
181
|
+
const abortSignal = options.run?.abortSignal;
|
182
|
+
let { stopSequences } = this.settings;
|
183
|
+
const openaiResponseFormat = options.responseFormat;
|
190
184
|
return callWithRetryAndThrottle({
|
191
|
-
retry:
|
192
|
-
throttle:
|
193
|
-
call: async () =>
|
185
|
+
retry: api.retry,
|
186
|
+
throttle: api.throttle,
|
187
|
+
call: async () => {
|
188
|
+
// empty arrays are not allowed for stop:
|
189
|
+
if (stopSequences != null &&
|
190
|
+
Array.isArray(stopSequences) &&
|
191
|
+
stopSequences.length === 0) {
|
192
|
+
stopSequences = undefined;
|
193
|
+
}
|
194
|
+
return postJsonToApi({
|
195
|
+
url: api.assembleUrl("/completions"),
|
196
|
+
headers: api.headers,
|
197
|
+
body: {
|
198
|
+
stream: openaiResponseFormat.stream,
|
199
|
+
model: this.settings.model,
|
200
|
+
prompt,
|
201
|
+
suffix: this.settings.suffix,
|
202
|
+
max_tokens: this.settings.maxGenerationTokens,
|
203
|
+
temperature: this.settings.temperature,
|
204
|
+
top_p: this.settings.topP,
|
205
|
+
n: this.settings.numberOfGenerations,
|
206
|
+
logprobs: this.settings.logprobs,
|
207
|
+
echo: this.settings.echo,
|
208
|
+
stop: this.settings.stopSequences,
|
209
|
+
seed: this.settings.seed,
|
210
|
+
presence_penalty: this.settings.presencePenalty,
|
211
|
+
frequency_penalty: this.settings.frequencyPenalty,
|
212
|
+
best_of: this.settings.bestOf,
|
213
|
+
logit_bias: this.settings.logitBias,
|
214
|
+
user,
|
215
|
+
},
|
216
|
+
failedResponseHandler: failedOpenAICallResponseHandler,
|
217
|
+
successfulResponseHandler: openaiResponseFormat.handler,
|
218
|
+
abortSignal,
|
219
|
+
});
|
220
|
+
},
|
194
221
|
});
|
195
222
|
}
|
196
223
|
get settingsForEvent() {
|
197
224
|
const eventSettingProperties = [
|
198
|
-
"
|
225
|
+
"maxGenerationTokens",
|
199
226
|
"stopSequences",
|
227
|
+
"numberOfGenerations",
|
200
228
|
"suffix",
|
201
229
|
"temperature",
|
202
230
|
"topP",
|
203
|
-
"n",
|
204
231
|
"logprobs",
|
205
232
|
"echo",
|
206
233
|
"presencePenalty",
|
@@ -211,14 +238,14 @@ export class OpenAICompletionModel extends AbstractModel {
|
|
211
238
|
];
|
212
239
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
213
240
|
}
|
214
|
-
async
|
241
|
+
async doGenerateTexts(prompt, options) {
|
215
242
|
const response = await this.callAPI(prompt, {
|
216
243
|
...options,
|
217
244
|
responseFormat: OpenAITextResponseFormat.json,
|
218
245
|
});
|
219
246
|
return {
|
220
247
|
response,
|
221
|
-
|
248
|
+
texts: response.choices.map((choice) => choice.text),
|
222
249
|
usage: {
|
223
250
|
promptTokens: response.usage.prompt_tokens,
|
224
251
|
completionTokens: response.usage.completion_tokens,
|
@@ -280,38 +307,6 @@ const OpenAICompletionResponseSchema = z.object({
|
|
280
307
|
total_tokens: z.number(),
|
281
308
|
}),
|
282
309
|
});
|
283
|
-
async function callOpenAICompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, seed, user, }) {
|
284
|
-
// empty arrays are not allowed for stop:
|
285
|
-
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
286
|
-
stop = undefined;
|
287
|
-
}
|
288
|
-
return postJsonToApi({
|
289
|
-
url: api.assembleUrl("/completions"),
|
290
|
-
headers: api.headers,
|
291
|
-
body: {
|
292
|
-
stream: responseFormat.stream,
|
293
|
-
model,
|
294
|
-
prompt,
|
295
|
-
suffix,
|
296
|
-
max_tokens: maxTokens,
|
297
|
-
temperature,
|
298
|
-
top_p: topP,
|
299
|
-
n,
|
300
|
-
logprobs,
|
301
|
-
echo,
|
302
|
-
stop,
|
303
|
-
seed,
|
304
|
-
presence_penalty: presencePenalty,
|
305
|
-
frequency_penalty: frequencyPenalty,
|
306
|
-
best_of: bestOf,
|
307
|
-
logit_bias: logitBias,
|
308
|
-
user,
|
309
|
-
},
|
310
|
-
failedResponseHandler: failedOpenAICallResponseHandler,
|
311
|
-
successfulResponseHandler: responseFormat.handler,
|
312
|
-
abortSignal,
|
313
|
-
});
|
314
|
-
}
|
315
310
|
export const OpenAITextResponseFormat = {
|
316
311
|
/**
|
317
312
|
* Returns the response as a JSON object.
|
@@ -17,7 +17,7 @@ const OpenAIChatModel_js_1 = require("./chat/OpenAIChatModel.cjs");
|
|
17
17
|
* const model = openai.CompletionTextGenerator({
|
18
18
|
* model: "gpt-3.5-turbo-instruct",
|
19
19
|
* temperature: 0.7,
|
20
|
-
*
|
20
|
+
* maxGenerationTokens: 500,
|
21
21
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
22
22
|
* });
|
23
23
|
*
|
@@ -41,7 +41,7 @@ exports.CompletionTextGenerator = CompletionTextGenerator;
|
|
41
41
|
* const model = openai.ChatTextGenerator({
|
42
42
|
* model: "gpt-3.5-turbo",
|
43
43
|
* temperature: 0.7,
|
44
|
-
*
|
44
|
+
* maxGenerationTokens: 500,
|
45
45
|
* });
|
46
46
|
*
|
47
47
|
* const text = await generateText(
|
@@ -14,7 +14,7 @@ import { OpenAIChatModel, OpenAIChatSettings } from "./chat/OpenAIChatModel.js";
|
|
14
14
|
* const model = openai.CompletionTextGenerator({
|
15
15
|
* model: "gpt-3.5-turbo-instruct",
|
16
16
|
* temperature: 0.7,
|
17
|
-
*
|
17
|
+
* maxGenerationTokens: 500,
|
18
18
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
19
19
|
* });
|
20
20
|
*
|
@@ -35,7 +35,7 @@ export declare function CompletionTextGenerator(settings: OpenAICompletionModelS
|
|
35
35
|
* const model = openai.ChatTextGenerator({
|
36
36
|
* model: "gpt-3.5-turbo",
|
37
37
|
* temperature: 0.7,
|
38
|
-
*
|
38
|
+
* maxGenerationTokens: 500,
|
39
39
|
* });
|
40
40
|
*
|
41
41
|
* const text = await generateText(
|
@@ -14,7 +14,7 @@ import { OpenAIChatModel } from "./chat/OpenAIChatModel.js";
|
|
14
14
|
* const model = openai.CompletionTextGenerator({
|
15
15
|
* model: "gpt-3.5-turbo-instruct",
|
16
16
|
* temperature: 0.7,
|
17
|
-
*
|
17
|
+
* maxGenerationTokens: 500,
|
18
18
|
* retry: retryWithExponentialBackoff({ maxTries: 5 }),
|
19
19
|
* });
|
20
20
|
*
|
@@ -37,7 +37,7 @@ export function CompletionTextGenerator(settings) {
|
|
37
37
|
* const model = openai.ChatTextGenerator({
|
38
38
|
* model: "gpt-3.5-turbo",
|
39
39
|
* temperature: 0.7,
|
40
|
-
*
|
40
|
+
* maxGenerationTokens: 500,
|
41
41
|
* });
|
42
42
|
*
|
43
43
|
* const text = await generateText(
|
@@ -20,38 +20,67 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
20
20
|
super({ settings });
|
21
21
|
}
|
22
22
|
async callAPI(messages, options) {
|
23
|
+
const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
|
24
|
+
const responseFormat = options.responseFormat;
|
25
|
+
const abortSignal = options.run?.abortSignal;
|
26
|
+
const user = this.settings.isUserIdForwardingEnabled
|
27
|
+
? options.run?.userId
|
28
|
+
: undefined;
|
29
|
+
const openAIResponseFormat = this.settings.responseFormat;
|
30
|
+
// function & tool calling:
|
31
|
+
const functions = options.functions ?? this.settings.functions;
|
32
|
+
const functionCall = options.functionCall ?? this.settings.functionCall;
|
33
|
+
const tools = options.tools ?? this.settings.tools;
|
34
|
+
const toolChoice = options.toolChoice ?? this.settings.toolChoice;
|
35
|
+
let { stopSequences } = this.settings;
|
23
36
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
24
37
|
retry: this.settings.api?.retry,
|
25
38
|
throttle: this.settings.api?.throttle,
|
26
|
-
call: async () =>
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
call: async () => {
|
40
|
+
// empty arrays are not allowed for stopSequences:
|
41
|
+
if (stopSequences != null &&
|
42
|
+
Array.isArray(stopSequences) &&
|
43
|
+
stopSequences.length === 0) {
|
44
|
+
stopSequences = undefined;
|
45
|
+
}
|
46
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
47
|
+
url: api.assembleUrl("/chat/completions"),
|
48
|
+
headers: api.headers,
|
49
|
+
body: {
|
50
|
+
stream: responseFormat.stream,
|
51
|
+
model: this.settings.model,
|
52
|
+
messages,
|
53
|
+
functions,
|
54
|
+
function_call: functionCall,
|
55
|
+
tools,
|
56
|
+
tool_choice: toolChoice,
|
57
|
+
temperature: this.settings.temperature,
|
58
|
+
top_p: this.settings.topP,
|
59
|
+
n: this.settings.numberOfGenerations,
|
60
|
+
stop: this.settings.stopSequences,
|
61
|
+
max_tokens: this.settings.maxGenerationTokens,
|
62
|
+
presence_penalty: this.settings.presencePenalty,
|
63
|
+
frequency_penalty: this.settings.frequencyPenalty,
|
64
|
+
logit_bias: this.settings.logitBias,
|
65
|
+
seed: this.settings.seed,
|
66
|
+
response_format: openAIResponseFormat,
|
67
|
+
user,
|
68
|
+
},
|
69
|
+
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
70
|
+
successfulResponseHandler: responseFormat.handler,
|
71
|
+
abortSignal,
|
72
|
+
});
|
73
|
+
},
|
45
74
|
});
|
46
75
|
}
|
47
|
-
async
|
76
|
+
async doGenerateTexts(prompt, options) {
|
48
77
|
const response = await this.callAPI(prompt, {
|
49
78
|
...options,
|
50
79
|
responseFormat: exports.OpenAIChatResponseFormat.json,
|
51
80
|
});
|
52
81
|
return {
|
53
82
|
response,
|
54
|
-
|
83
|
+
texts: response.choices.map((choice) => choice.message.content ?? ""),
|
55
84
|
usage: this.extractUsage(response),
|
56
85
|
};
|
57
86
|
}
|
@@ -173,39 +202,6 @@ const openAIChatResponseSchema = zod_1.z.object({
|
|
173
202
|
total_tokens: zod_1.z.number(),
|
174
203
|
}),
|
175
204
|
});
|
176
|
-
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, tools, toolChoice, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
177
|
-
// empty arrays are not allowed for stop:
|
178
|
-
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
179
|
-
stop = undefined;
|
180
|
-
}
|
181
|
-
return (0, postToApi_js_1.postJsonToApi)({
|
182
|
-
url: api.assembleUrl("/chat/completions"),
|
183
|
-
headers: api.headers,
|
184
|
-
body: {
|
185
|
-
stream: responseFormat.stream,
|
186
|
-
model,
|
187
|
-
messages,
|
188
|
-
functions,
|
189
|
-
function_call: functionCall,
|
190
|
-
tools,
|
191
|
-
tool_choice: toolChoice,
|
192
|
-
temperature,
|
193
|
-
top_p: topP,
|
194
|
-
n,
|
195
|
-
stop,
|
196
|
-
max_tokens: maxTokens,
|
197
|
-
presence_penalty: presencePenalty,
|
198
|
-
frequency_penalty: frequencyPenalty,
|
199
|
-
logit_bias: logitBias,
|
200
|
-
seed,
|
201
|
-
response_format: openAIResponseFormat,
|
202
|
-
user,
|
203
|
-
},
|
204
|
-
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
205
|
-
successfulResponseHandler: responseFormat.handler,
|
206
|
-
abortSignal,
|
207
|
-
});
|
208
|
-
}
|
209
205
|
exports.OpenAIChatResponseFormat = {
|
210
206
|
/**
|
211
207
|
* Returns the response as a JSON object.
|