modelfusion 0.98.0 → 0.100.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 +13 -19
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +1 -1
- package/guard/fixStructure.cjs +3 -3
- package/guard/fixStructure.d.ts +3 -3
- package/guard/fixStructure.js +3 -3
- package/model-function/Model.d.ts +2 -2
- package/model-function/generate-structure/generateStructure.d.ts +2 -2
- package/model-function/generate-structure/streamStructure.d.ts +1 -1
- 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 → MistralChatModel.cjs} +18 -18
- package/model-provider/mistral/{MistralTextGenerationModel.d.ts → MistralChatModel.d.ts} +22 -21
- package/model-provider/mistral/{MistralTextGenerationModel.js → MistralChatModel.js} +16 -16
- package/model-provider/mistral/MistralFacade.cjs +5 -5
- package/model-provider/mistral/MistralFacade.d.ts +3 -2
- package/model-provider/mistral/MistralFacade.js +3 -3
- package/model-provider/mistral/MistralPromptTemplate.d.ts +4 -4
- package/model-provider/mistral/index.cjs +1 -1
- package/model-provider/mistral/index.d.ts +1 -1
- package/model-provider/mistral/index.js +1 -1
- package/model-provider/ollama/OllamaApiConfiguration.d.ts +6 -5
- package/model-provider/ollama/OllamaChatModel.cjs +303 -0
- package/model-provider/ollama/OllamaChatModel.d.ts +171 -0
- package/model-provider/ollama/OllamaChatModel.js +299 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.cjs +76 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.d.ts +20 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.js +69 -0
- package/model-provider/ollama/{OllamaTextGenerationModel.cjs → OllamaCompletionModel.cjs} +17 -15
- package/model-provider/ollama/OllamaCompletionModel.d.ts +159 -0
- package/model-provider/ollama/{OllamaTextGenerationModel.js → OllamaCompletionModel.js} +15 -13
- package/model-provider/ollama/{OllamaTextGenerationModel.test.cjs → OllamaCompletionModel.test.cjs} +3 -3
- package/model-provider/ollama/{OllamaTextGenerationModel.test.js → OllamaCompletionModel.test.js} +3 -3
- package/model-provider/ollama/OllamaFacade.cjs +15 -5
- package/model-provider/ollama/OllamaFacade.d.ts +7 -2
- package/model-provider/ollama/OllamaFacade.js +11 -3
- package/model-provider/ollama/OllamaTextGenerationSettings.cjs +2 -0
- package/model-provider/ollama/OllamaTextGenerationSettings.d.ts +87 -0
- package/model-provider/ollama/OllamaTextGenerationSettings.js +1 -0
- package/model-provider/ollama/index.cjs +4 -1
- package/model-provider/ollama/index.d.ts +4 -1
- package/model-provider/ollama/index.js +4 -1
- 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 +6 -4
- package/model-provider/openai/OpenAIFacade.d.ts +5 -3
- package/model-provider/openai/OpenAIFacade.js +4 -3
- package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +50 -54
- package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +7 -28
- package/model-provider/openai/chat/AbstractOpenAIChatModel.js +50 -54
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +4 -4
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.js +4 -4
- package/model-provider/openai/chat/OpenAIChatModel.test.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.test.js +1 -1
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.d.ts +5 -5
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.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 +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleFacade.js +2 -2
- package/package.json +1 -1
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +0 -230
- /package/model-provider/ollama/{OllamaTextGenerationModel.test.d.ts → OllamaCompletionModel.test.d.ts} +0 -0
@@ -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.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.Tokenizer = exports.ImageGenerator = exports.Transcriber = exports.SpeechGenerator = exports.TextEmbedder = exports.ChatTextGenerator = exports.CompletionTextGenerator = void 0;
|
3
|
+
exports.ChatMessage = exports.Tokenizer = exports.ImageGenerator = exports.Transcriber = exports.SpeechGenerator = exports.TextEmbedder = exports.ChatTextGenerator = exports.CompletionTextGenerator = void 0;
|
4
4
|
const OpenAICompletionModel_js_1 = require("./OpenAICompletionModel.cjs");
|
5
5
|
const OpenAIImageGenerationModel_js_1 = require("./OpenAIImageGenerationModel.cjs");
|
6
6
|
const OpenAISpeechModel_js_1 = require("./OpenAISpeechModel.cjs");
|
@@ -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,13 +41,13 @@ 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(
|
48
48
|
* model,
|
49
49
|
* [
|
50
|
-
*
|
50
|
+
* openai.ChatMessage.system(
|
51
51
|
* "Write a short story about a robot learning to love:"
|
52
52
|
* ),
|
53
53
|
* ]
|
@@ -148,3 +148,5 @@ function Tokenizer(settings) {
|
|
148
148
|
return new TikTokenTokenizer_js_1.TikTokenTokenizer(settings);
|
149
149
|
}
|
150
150
|
exports.Tokenizer = Tokenizer;
|
151
|
+
var OpenAIChatMessage_js_1 = require("./chat/OpenAIChatMessage.cjs");
|
152
|
+
Object.defineProperty(exports, "ChatMessage", { enumerable: true, get: function () { return OpenAIChatMessage_js_1.OpenAIChatMessage; } });
|
@@ -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,13 +35,13 @@ 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(
|
42
42
|
* model,
|
43
43
|
* [
|
44
|
-
*
|
44
|
+
* openai.ChatMessage.system(
|
45
45
|
* "Write a short story about a robot learning to love:"
|
46
46
|
* ),
|
47
47
|
* ]
|
@@ -124,3 +124,5 @@ export declare function ImageGenerator(settings: OpenAIImageGenerationSettings):
|
|
124
124
|
* @returns A new instance of {@link TikTokenTokenizer}.
|
125
125
|
*/
|
126
126
|
export declare function Tokenizer(settings: TikTokenTokenizerSettings): TikTokenTokenizer;
|
127
|
+
export { OpenAIChatMessage as ChatMessage } from "./chat/OpenAIChatMessage.js";
|
128
|
+
export { OpenAIChatPrompt as ChatPrompt } from "./chat/AbstractOpenAIChatModel.js";
|
@@ -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,13 +37,13 @@ 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(
|
44
44
|
* model,
|
45
45
|
* [
|
46
|
-
*
|
46
|
+
* openai.ChatMessage.system(
|
47
47
|
* "Write a short story about a robot learning to love:"
|
48
48
|
* ),
|
49
49
|
* ]
|
@@ -138,3 +138,4 @@ export function ImageGenerator(settings) {
|
|
138
138
|
export function Tokenizer(settings) {
|
139
139
|
return new TikTokenTokenizer(settings);
|
140
140
|
}
|
141
|
+
export { OpenAIChatMessage as ChatMessage } from "./chat/OpenAIChatMessage.js";
|
@@ -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.
|
@@ -32,19 +32,6 @@ export interface AbstractOpenAIChatCallSettings {
|
|
32
32
|
name: string;
|
33
33
|
};
|
34
34
|
};
|
35
|
-
/**
|
36
|
-
* An array of strings or a single string that the model will recognize as end-of-text indicators.
|
37
|
-
* The model stops generating more content when it encounters any of these strings.
|
38
|
-
* This is particularly useful in scripted or formatted text generation, where a specific end point is required.
|
39
|
-
* Example: stop: ['\n', 'END']
|
40
|
-
*/
|
41
|
-
stop?: string | string[];
|
42
|
-
/**
|
43
|
-
* Specifies the maximum number of tokens (words, punctuation, parts of words) that the model can generate in a single response.
|
44
|
-
* It helps to control the length of the output, this can help prevent wasted time and tokens when tweaker topP or temperature.
|
45
|
-
* Example: maxTokens: 1000
|
46
|
-
*/
|
47
|
-
maxTokens?: number;
|
48
35
|
/**
|
49
36
|
* `temperature`: Controls the randomness and creativity in the model's responses.
|
50
37
|
* A lower temperature (close to 0) results in more predictable, conservative text, while a higher temperature (close to 1) produces more varied and creative output.
|
@@ -67,17 +54,6 @@ export interface AbstractOpenAIChatCallSettings {
|
|
67
54
|
* Example: seed: 89 (or) seed: null
|
68
55
|
*/
|
69
56
|
seed?: number | null;
|
70
|
-
responseFormat?: {
|
71
|
-
type?: "text" | "json_object";
|
72
|
-
};
|
73
|
-
/**
|
74
|
-
* Specifies the number of responses or completions the model should generate for a given prompt.
|
75
|
-
* This is useful when you need multiple different outputs or ideas for a single prompt.
|
76
|
-
* The model will generate 'n' distinct responses, each based on the same initial prompt.
|
77
|
-
* In a streaming model this will result in both responses streamed back in real time.
|
78
|
-
* Example: n: 3 // The model will produce 3 different responses.
|
79
|
-
*/
|
80
|
-
n?: number;
|
81
57
|
/**
|
82
58
|
* Discourages the model from repeating the same information or context already mentioned in the conversation or prompt.
|
83
59
|
* Increasing this value encourages the model to introduce new topics or ideas, rather than reiterating what has been said.
|
@@ -92,9 +68,12 @@ export interface AbstractOpenAIChatCallSettings {
|
|
92
68
|
* Example: frequencyPenalty: 0.5 // Moderately discourages repetitive language.
|
93
69
|
*/
|
94
70
|
frequencyPenalty?: number;
|
71
|
+
responseFormat?: {
|
72
|
+
type?: "text" | "json_object";
|
73
|
+
};
|
95
74
|
logitBias?: Record<number, number>;
|
96
75
|
}
|
97
|
-
export interface AbstractOpenAIChatSettings extends TextGenerationModelSettings,
|
76
|
+
export interface AbstractOpenAIChatSettings extends TextGenerationModelSettings, AbstractOpenAIChatCallSettings {
|
98
77
|
isUserIdForwardingEnabled?: boolean;
|
99
78
|
}
|
100
79
|
export type OpenAIChatPrompt = OpenAIChatMessage[];
|
@@ -105,7 +84,7 @@ export type OpenAIChatPrompt = OpenAIChatMessage[];
|
|
105
84
|
*/
|
106
85
|
export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractOpenAIChatSettings> extends AbstractModel<SETTINGS> {
|
107
86
|
constructor(settings: SETTINGS);
|
108
|
-
callAPI<RESULT>(messages:
|
87
|
+
callAPI<RESULT>(messages: OpenAIChatPrompt, options: {
|
109
88
|
responseFormat: OpenAIChatResponseFormatType<RESULT>;
|
110
89
|
} & FunctionOptions & {
|
111
90
|
functions?: AbstractOpenAIChatCallSettings["functions"];
|
@@ -113,7 +92,7 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
|
|
113
92
|
tools?: AbstractOpenAIChatCallSettings["tools"];
|
114
93
|
toolChoice?: AbstractOpenAIChatCallSettings["toolChoice"];
|
115
94
|
}): Promise<RESULT>;
|
116
|
-
|
95
|
+
doGenerateTexts(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
|
117
96
|
response: {
|
118
97
|
object: "chat.completion";
|
119
98
|
usage: {
|
@@ -147,7 +126,7 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
|
|
147
126
|
}[];
|
148
127
|
system_fingerprint?: string | null | undefined;
|
149
128
|
};
|
150
|
-
|
129
|
+
texts: string[];
|
151
130
|
usage: {
|
152
131
|
promptTokens: number;
|
153
132
|
completionTokens: number;
|
@@ -17,38 +17,67 @@ export class AbstractOpenAIChatModel extends AbstractModel {
|
|
17
17
|
super({ settings });
|
18
18
|
}
|
19
19
|
async callAPI(messages, options) {
|
20
|
+
const api = this.settings.api ?? new OpenAIApiConfiguration();
|
21
|
+
const responseFormat = options.responseFormat;
|
22
|
+
const abortSignal = options.run?.abortSignal;
|
23
|
+
const user = this.settings.isUserIdForwardingEnabled
|
24
|
+
? options.run?.userId
|
25
|
+
: undefined;
|
26
|
+
const openAIResponseFormat = this.settings.responseFormat;
|
27
|
+
// function & tool calling:
|
28
|
+
const functions = options.functions ?? this.settings.functions;
|
29
|
+
const functionCall = options.functionCall ?? this.settings.functionCall;
|
30
|
+
const tools = options.tools ?? this.settings.tools;
|
31
|
+
const toolChoice = options.toolChoice ?? this.settings.toolChoice;
|
32
|
+
let { stopSequences } = this.settings;
|
20
33
|
return callWithRetryAndThrottle({
|
21
34
|
retry: this.settings.api?.retry,
|
22
35
|
throttle: this.settings.api?.throttle,
|
23
|
-
call: async () =>
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
call: async () => {
|
37
|
+
// empty arrays are not allowed for stopSequences:
|
38
|
+
if (stopSequences != null &&
|
39
|
+
Array.isArray(stopSequences) &&
|
40
|
+
stopSequences.length === 0) {
|
41
|
+
stopSequences = undefined;
|
42
|
+
}
|
43
|
+
return postJsonToApi({
|
44
|
+
url: api.assembleUrl("/chat/completions"),
|
45
|
+
headers: api.headers,
|
46
|
+
body: {
|
47
|
+
stream: responseFormat.stream,
|
48
|
+
model: this.settings.model,
|
49
|
+
messages,
|
50
|
+
functions,
|
51
|
+
function_call: functionCall,
|
52
|
+
tools,
|
53
|
+
tool_choice: toolChoice,
|
54
|
+
temperature: this.settings.temperature,
|
55
|
+
top_p: this.settings.topP,
|
56
|
+
n: this.settings.numberOfGenerations,
|
57
|
+
stop: this.settings.stopSequences,
|
58
|
+
max_tokens: this.settings.maxGenerationTokens,
|
59
|
+
presence_penalty: this.settings.presencePenalty,
|
60
|
+
frequency_penalty: this.settings.frequencyPenalty,
|
61
|
+
logit_bias: this.settings.logitBias,
|
62
|
+
seed: this.settings.seed,
|
63
|
+
response_format: openAIResponseFormat,
|
64
|
+
user,
|
65
|
+
},
|
66
|
+
failedResponseHandler: failedOpenAICallResponseHandler,
|
67
|
+
successfulResponseHandler: responseFormat.handler,
|
68
|
+
abortSignal,
|
69
|
+
});
|
70
|
+
},
|
42
71
|
});
|
43
72
|
}
|
44
|
-
async
|
73
|
+
async doGenerateTexts(prompt, options) {
|
45
74
|
const response = await this.callAPI(prompt, {
|
46
75
|
...options,
|
47
76
|
responseFormat: OpenAIChatResponseFormat.json,
|
48
77
|
});
|
49
78
|
return {
|
50
79
|
response,
|
51
|
-
|
80
|
+
texts: response.choices.map((choice) => choice.message.content ?? ""),
|
52
81
|
usage: this.extractUsage(response),
|
53
82
|
};
|
54
83
|
}
|
@@ -169,39 +198,6 @@ const openAIChatResponseSchema = z.object({
|
|
169
198
|
total_tokens: z.number(),
|
170
199
|
}),
|
171
200
|
});
|
172
|
-
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, tools, toolChoice, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
173
|
-
// empty arrays are not allowed for stop:
|
174
|
-
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
175
|
-
stop = undefined;
|
176
|
-
}
|
177
|
-
return postJsonToApi({
|
178
|
-
url: api.assembleUrl("/chat/completions"),
|
179
|
-
headers: api.headers,
|
180
|
-
body: {
|
181
|
-
stream: responseFormat.stream,
|
182
|
-
model,
|
183
|
-
messages,
|
184
|
-
functions,
|
185
|
-
function_call: functionCall,
|
186
|
-
tools,
|
187
|
-
tool_choice: toolChoice,
|
188
|
-
temperature,
|
189
|
-
top_p: topP,
|
190
|
-
n,
|
191
|
-
stop,
|
192
|
-
max_tokens: maxTokens,
|
193
|
-
presence_penalty: presencePenalty,
|
194
|
-
frequency_penalty: frequencyPenalty,
|
195
|
-
logit_bias: logitBias,
|
196
|
-
seed,
|
197
|
-
response_format: openAIResponseFormat,
|
198
|
-
user,
|
199
|
-
},
|
200
|
-
failedResponseHandler: failedOpenAICallResponseHandler,
|
201
|
-
successfulResponseHandler: responseFormat.handler,
|
202
|
-
abortSignal,
|
203
|
-
});
|
204
|
-
}
|
205
201
|
export const OpenAIChatResponseFormat = {
|
206
202
|
/**
|
207
203
|
* Returns the response as a JSON object.
|
@@ -23,15 +23,15 @@ OpenAIChatSettings> {
|
|
23
23
|
/**
|
24
24
|
* Returns this model with a text prompt template.
|
25
25
|
*/
|
26
|
-
withTextPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<string,
|
26
|
+
withTextPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<string, OpenAIChatPrompt>>;
|
27
27
|
/**
|
28
28
|
* Returns this model with an instruction prompt template.
|
29
29
|
*/
|
30
|
-
withInstructionPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../../index.js").MultiModalInstructionPrompt | import("../../../index.js").TextInstructionPrompt,
|
30
|
+
withInstructionPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../../index.js").MultiModalInstructionPrompt | import("../../../index.js").TextInstructionPrompt, OpenAIChatPrompt>>;
|
31
31
|
/**
|
32
32
|
* Returns this model with a chat prompt template.
|
33
33
|
*/
|
34
|
-
withChatPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../../index.js").TextChatPrompt | import("../../../index.js").MultiModalChatPrompt,
|
34
|
+
withChatPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../../index.js").TextChatPrompt | import("../../../index.js").MultiModalChatPrompt, OpenAIChatPrompt>>;
|
35
35
|
withPromptTemplate<TARGET_PROMPT_FORMAT extends TextGenerationPromptTemplate<unknown, OpenAIChatPrompt>>(promptTemplate: TARGET_PROMPT_FORMAT): OpenAIChatFunctionCallStructureGenerationModel<TARGET_PROMPT_FORMAT>;
|
36
36
|
withSettings(additionalSettings: Partial<OpenAIChatSettings>): this;
|
37
37
|
/**
|