modelfusion 0.83.0 → 0.84.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.
Files changed (33) hide show
  1. package/README.md +3 -3
  2. package/model-function/AbstractModel.d.ts +1 -1
  3. package/model-provider/index.cjs +1 -0
  4. package/model-provider/index.d.ts +1 -0
  5. package/model-provider/index.js +1 -0
  6. package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +1 -1
  7. package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +1 -1
  8. package/model-provider/openai/AzureOpenAIApiConfiguration.js +1 -1
  9. package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +228 -0
  10. package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +467 -0
  11. package/model-provider/openai/chat/AbstractOpenAIChatModel.js +224 -0
  12. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.cjs +3 -3
  13. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +6 -6
  14. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.js +1 -1
  15. package/model-provider/openai/chat/OpenAIChatModel.cjs +5 -218
  16. package/model-provider/openai/chat/OpenAIChatModel.d.ts +11 -460
  17. package/model-provider/openai/chat/OpenAIChatModel.js +4 -217
  18. package/model-provider/openai/index.cjs +1 -0
  19. package/model-provider/openai/index.d.ts +1 -0
  20. package/model-provider/openai/index.js +1 -0
  21. package/model-provider/openai-compatible/FireworksAIApiConfiguration.cjs +29 -0
  22. package/model-provider/openai-compatible/FireworksAIApiConfiguration.d.ts +18 -0
  23. package/model-provider/openai-compatible/FireworksAIApiConfiguration.js +25 -0
  24. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +100 -0
  25. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +45 -0
  26. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +96 -0
  27. package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +30 -0
  28. package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +24 -0
  29. package/model-provider/openai-compatible/OpenAICompatibleFacade.js +26 -0
  30. package/model-provider/openai-compatible/index.cjs +32 -0
  31. package/model-provider/openai-compatible/index.d.ts +3 -0
  32. package/model-provider/openai-compatible/index.js +3 -0
  33. package/package.json +1 -1
@@ -0,0 +1,467 @@
1
+ import { z } from "zod";
2
+ import { FunctionOptions } from "../../../core/FunctionOptions.js";
3
+ import { ApiConfiguration } from "../../../core/api/ApiConfiguration.js";
4
+ import { ResponseHandler } from "../../../core/api/postToApi.js";
5
+ import { AbstractModel } from "../../../model-function/AbstractModel.js";
6
+ import { Delta } from "../../../model-function/Delta.js";
7
+ import { TextGenerationModelSettings } from "../../../model-function/generate-text/TextGenerationModel.js";
8
+ import { ToolDefinition } from "../../../tool/ToolDefinition.js";
9
+ import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
10
+ export interface AbstractOpenAIChatCallSettings {
11
+ api?: ApiConfiguration;
12
+ model: string;
13
+ functions?: Array<{
14
+ name: string;
15
+ description?: string;
16
+ parameters: unknown;
17
+ }>;
18
+ functionCall?: "none" | "auto" | {
19
+ name: string;
20
+ };
21
+ tools?: Array<{
22
+ type: "function";
23
+ function: {
24
+ name: string;
25
+ description?: string;
26
+ parameters: unknown;
27
+ };
28
+ }>;
29
+ toolChoice?: "none" | "auto" | {
30
+ type: "function";
31
+ function: {
32
+ name: string;
33
+ };
34
+ };
35
+ stop?: string | string[];
36
+ maxTokens?: number;
37
+ temperature?: number;
38
+ topP?: number;
39
+ seed?: number | null;
40
+ responseFormat?: {
41
+ type?: "text" | "json_object";
42
+ };
43
+ n?: number;
44
+ presencePenalty?: number;
45
+ frequencyPenalty?: number;
46
+ logitBias?: Record<number, number>;
47
+ }
48
+ export interface AbstractOpenAIChatSettings extends TextGenerationModelSettings, Omit<AbstractOpenAIChatCallSettings, "stop" | "maxTokens"> {
49
+ isUserIdForwardingEnabled?: boolean;
50
+ }
51
+ export type OpenAIChatPrompt = OpenAIChatMessage[];
52
+ /**
53
+ * Abstract text generation model that calls an API that is compatible with the OpenAI chat API.
54
+ *
55
+ * @see https://platform.openai.com/docs/api-reference/chat/create
56
+ */
57
+ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractOpenAIChatSettings> extends AbstractModel<SETTINGS> {
58
+ constructor(settings: SETTINGS);
59
+ callAPI<RESULT>(messages: Array<OpenAIChatMessage>, options: {
60
+ responseFormat: OpenAIChatResponseFormatType<RESULT>;
61
+ } & FunctionOptions & {
62
+ functions?: AbstractOpenAIChatCallSettings["functions"];
63
+ functionCall?: AbstractOpenAIChatCallSettings["functionCall"];
64
+ tools?: AbstractOpenAIChatCallSettings["tools"];
65
+ toolChoice?: AbstractOpenAIChatCallSettings["toolChoice"];
66
+ }): Promise<RESULT>;
67
+ doGenerateText(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
68
+ response: {
69
+ object: "chat.completion";
70
+ usage: {
71
+ prompt_tokens: number;
72
+ total_tokens: number;
73
+ completion_tokens: number;
74
+ };
75
+ model: string;
76
+ id: string;
77
+ choices: {
78
+ message: {
79
+ role: "assistant";
80
+ content: string | null;
81
+ function_call?: {
82
+ name: string;
83
+ arguments: string;
84
+ } | undefined;
85
+ tool_calls?: {
86
+ function: {
87
+ name: string;
88
+ arguments: string;
89
+ };
90
+ type: "function";
91
+ id: string;
92
+ }[] | undefined;
93
+ };
94
+ index: number;
95
+ logprobs?: any;
96
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
97
+ }[];
98
+ created: number;
99
+ system_fingerprint?: string | undefined;
100
+ };
101
+ text: string;
102
+ usage: {
103
+ promptTokens: number;
104
+ completionTokens: number;
105
+ totalTokens: number;
106
+ };
107
+ }>;
108
+ doStreamText(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
109
+ doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
110
+ response: {
111
+ object: "chat.completion";
112
+ usage: {
113
+ prompt_tokens: number;
114
+ total_tokens: number;
115
+ completion_tokens: number;
116
+ };
117
+ model: string;
118
+ id: string;
119
+ choices: {
120
+ message: {
121
+ role: "assistant";
122
+ content: string | null;
123
+ function_call?: {
124
+ name: string;
125
+ arguments: string;
126
+ } | undefined;
127
+ tool_calls?: {
128
+ function: {
129
+ name: string;
130
+ arguments: string;
131
+ };
132
+ type: "function";
133
+ id: string;
134
+ }[] | undefined;
135
+ };
136
+ index: number;
137
+ logprobs?: any;
138
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
139
+ }[];
140
+ created: number;
141
+ system_fingerprint?: string | undefined;
142
+ };
143
+ toolCall: {
144
+ id: string;
145
+ args: unknown;
146
+ } | null;
147
+ usage: {
148
+ promptTokens: number;
149
+ completionTokens: number;
150
+ totalTokens: number;
151
+ };
152
+ }>;
153
+ doGenerateToolCallsOrText(tools: Array<ToolDefinition<string, unknown>>, prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
154
+ response: {
155
+ object: "chat.completion";
156
+ usage: {
157
+ prompt_tokens: number;
158
+ total_tokens: number;
159
+ completion_tokens: number;
160
+ };
161
+ model: string;
162
+ id: string;
163
+ choices: {
164
+ message: {
165
+ role: "assistant";
166
+ content: string | null;
167
+ function_call?: {
168
+ name: string;
169
+ arguments: string;
170
+ } | undefined;
171
+ tool_calls?: {
172
+ function: {
173
+ name: string;
174
+ arguments: string;
175
+ };
176
+ type: "function";
177
+ id: string;
178
+ }[] | undefined;
179
+ };
180
+ index: number;
181
+ logprobs?: any;
182
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
183
+ }[];
184
+ created: number;
185
+ system_fingerprint?: string | undefined;
186
+ };
187
+ text: string | null;
188
+ toolCalls: {
189
+ id: string;
190
+ name: string;
191
+ args: unknown;
192
+ }[] | null;
193
+ usage: {
194
+ promptTokens: number;
195
+ completionTokens: number;
196
+ totalTokens: number;
197
+ };
198
+ }>;
199
+ extractUsage(response: OpenAIChatResponse): {
200
+ promptTokens: number;
201
+ completionTokens: number;
202
+ totalTokens: number;
203
+ };
204
+ }
205
+ declare const openAIChatResponseSchema: z.ZodObject<{
206
+ id: z.ZodString;
207
+ choices: z.ZodArray<z.ZodObject<{
208
+ message: z.ZodObject<{
209
+ role: z.ZodLiteral<"assistant">;
210
+ content: z.ZodNullable<z.ZodString>;
211
+ function_call: z.ZodOptional<z.ZodObject<{
212
+ name: z.ZodString;
213
+ arguments: z.ZodString;
214
+ }, "strip", z.ZodTypeAny, {
215
+ name: string;
216
+ arguments: string;
217
+ }, {
218
+ name: string;
219
+ arguments: string;
220
+ }>>;
221
+ tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
222
+ id: z.ZodString;
223
+ type: z.ZodLiteral<"function">;
224
+ function: z.ZodObject<{
225
+ name: z.ZodString;
226
+ arguments: z.ZodString;
227
+ }, "strip", z.ZodTypeAny, {
228
+ name: string;
229
+ arguments: string;
230
+ }, {
231
+ name: string;
232
+ arguments: string;
233
+ }>;
234
+ }, "strip", z.ZodTypeAny, {
235
+ function: {
236
+ name: string;
237
+ arguments: string;
238
+ };
239
+ type: "function";
240
+ id: string;
241
+ }, {
242
+ function: {
243
+ name: string;
244
+ arguments: string;
245
+ };
246
+ type: "function";
247
+ id: string;
248
+ }>, "many">>;
249
+ }, "strip", z.ZodTypeAny, {
250
+ role: "assistant";
251
+ content: string | null;
252
+ function_call?: {
253
+ name: string;
254
+ arguments: string;
255
+ } | undefined;
256
+ tool_calls?: {
257
+ function: {
258
+ name: string;
259
+ arguments: string;
260
+ };
261
+ type: "function";
262
+ id: string;
263
+ }[] | undefined;
264
+ }, {
265
+ role: "assistant";
266
+ content: string | null;
267
+ function_call?: {
268
+ name: string;
269
+ arguments: string;
270
+ } | undefined;
271
+ tool_calls?: {
272
+ function: {
273
+ name: string;
274
+ arguments: string;
275
+ };
276
+ type: "function";
277
+ id: string;
278
+ }[] | undefined;
279
+ }>;
280
+ index: z.ZodNumber;
281
+ logprobs: z.ZodNullable<z.ZodAny>;
282
+ finish_reason: z.ZodNullable<z.ZodOptional<z.ZodEnum<["stop", "length", "tool_calls", "content_filter", "function_call"]>>>;
283
+ }, "strip", z.ZodTypeAny, {
284
+ message: {
285
+ role: "assistant";
286
+ content: string | null;
287
+ function_call?: {
288
+ name: string;
289
+ arguments: string;
290
+ } | undefined;
291
+ tool_calls?: {
292
+ function: {
293
+ name: string;
294
+ arguments: string;
295
+ };
296
+ type: "function";
297
+ id: string;
298
+ }[] | undefined;
299
+ };
300
+ index: number;
301
+ logprobs?: any;
302
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
303
+ }, {
304
+ message: {
305
+ role: "assistant";
306
+ content: string | null;
307
+ function_call?: {
308
+ name: string;
309
+ arguments: string;
310
+ } | undefined;
311
+ tool_calls?: {
312
+ function: {
313
+ name: string;
314
+ arguments: string;
315
+ };
316
+ type: "function";
317
+ id: string;
318
+ }[] | undefined;
319
+ };
320
+ index: number;
321
+ logprobs?: any;
322
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
323
+ }>, "many">;
324
+ created: z.ZodNumber;
325
+ model: z.ZodString;
326
+ system_fingerprint: z.ZodOptional<z.ZodString>;
327
+ object: z.ZodLiteral<"chat.completion">;
328
+ usage: z.ZodObject<{
329
+ prompt_tokens: z.ZodNumber;
330
+ completion_tokens: z.ZodNumber;
331
+ total_tokens: z.ZodNumber;
332
+ }, "strip", z.ZodTypeAny, {
333
+ prompt_tokens: number;
334
+ total_tokens: number;
335
+ completion_tokens: number;
336
+ }, {
337
+ prompt_tokens: number;
338
+ total_tokens: number;
339
+ completion_tokens: number;
340
+ }>;
341
+ }, "strip", z.ZodTypeAny, {
342
+ object: "chat.completion";
343
+ usage: {
344
+ prompt_tokens: number;
345
+ total_tokens: number;
346
+ completion_tokens: number;
347
+ };
348
+ model: string;
349
+ id: string;
350
+ choices: {
351
+ message: {
352
+ role: "assistant";
353
+ content: string | null;
354
+ function_call?: {
355
+ name: string;
356
+ arguments: string;
357
+ } | undefined;
358
+ tool_calls?: {
359
+ function: {
360
+ name: string;
361
+ arguments: string;
362
+ };
363
+ type: "function";
364
+ id: string;
365
+ }[] | undefined;
366
+ };
367
+ index: number;
368
+ logprobs?: any;
369
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
370
+ }[];
371
+ created: number;
372
+ system_fingerprint?: string | undefined;
373
+ }, {
374
+ object: "chat.completion";
375
+ usage: {
376
+ prompt_tokens: number;
377
+ total_tokens: number;
378
+ completion_tokens: number;
379
+ };
380
+ model: string;
381
+ id: string;
382
+ choices: {
383
+ message: {
384
+ role: "assistant";
385
+ content: string | null;
386
+ function_call?: {
387
+ name: string;
388
+ arguments: string;
389
+ } | undefined;
390
+ tool_calls?: {
391
+ function: {
392
+ name: string;
393
+ arguments: string;
394
+ };
395
+ type: "function";
396
+ id: string;
397
+ }[] | undefined;
398
+ };
399
+ index: number;
400
+ logprobs?: any;
401
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
402
+ }[];
403
+ created: number;
404
+ system_fingerprint?: string | undefined;
405
+ }>;
406
+ export type OpenAIChatResponse = z.infer<typeof openAIChatResponseSchema>;
407
+ export type OpenAIChatResponseFormatType<T> = {
408
+ stream: boolean;
409
+ handler: ResponseHandler<T>;
410
+ };
411
+ export declare const OpenAIChatResponseFormat: {
412
+ /**
413
+ * Returns the response as a JSON object.
414
+ */
415
+ json: {
416
+ stream: false;
417
+ handler: ResponseHandler<{
418
+ object: "chat.completion";
419
+ usage: {
420
+ prompt_tokens: number;
421
+ total_tokens: number;
422
+ completion_tokens: number;
423
+ };
424
+ model: string;
425
+ id: string;
426
+ choices: {
427
+ message: {
428
+ role: "assistant";
429
+ content: string | null;
430
+ function_call?: {
431
+ name: string;
432
+ arguments: string;
433
+ } | undefined;
434
+ tool_calls?: {
435
+ function: {
436
+ name: string;
437
+ arguments: string;
438
+ };
439
+ type: "function";
440
+ id: string;
441
+ }[] | undefined;
442
+ };
443
+ index: number;
444
+ logprobs?: any;
445
+ finish_reason?: "length" | "stop" | "tool_calls" | "function_call" | "content_filter" | null | undefined;
446
+ }[];
447
+ created: number;
448
+ system_fingerprint?: string | undefined;
449
+ }>;
450
+ };
451
+ /**
452
+ * Returns an async iterable over the text deltas (only the tex different of the first choice).
453
+ */
454
+ textDeltaIterable: {
455
+ stream: true;
456
+ handler: ({ response }: {
457
+ response: Response;
458
+ }) => Promise<AsyncIterable<Delta<string>>>;
459
+ };
460
+ structureDeltaIterable: {
461
+ stream: true;
462
+ handler: ({ response }: {
463
+ response: Response;
464
+ }) => Promise<AsyncIterable<Delta<unknown>>>;
465
+ };
466
+ };
467
+ export {};
@@ -0,0 +1,224 @@
1
+ import { z } from "zod";
2
+ import { callWithRetryAndThrottle } from "../../../core/api/callWithRetryAndThrottle.js";
3
+ import { createJsonResponseHandler, postJsonToApi, } from "../../../core/api/postToApi.js";
4
+ import { parseJSON } from "../../../core/schema/parseJSON.js";
5
+ import { AbstractModel } from "../../../model-function/AbstractModel.js";
6
+ import { parsePartialJson } from "../../../model-function/generate-structure/parsePartialJson.js";
7
+ import { OpenAIApiConfiguration } from "../OpenAIApiConfiguration.js";
8
+ import { failedOpenAICallResponseHandler } from "../OpenAIError.js";
9
+ import { createOpenAIChatDeltaIterableQueue } from "./OpenAIChatStreamIterable.js";
10
+ /**
11
+ * Abstract text generation model that calls an API that is compatible with the OpenAI chat API.
12
+ *
13
+ * @see https://platform.openai.com/docs/api-reference/chat/create
14
+ */
15
+ export class AbstractOpenAIChatModel extends AbstractModel {
16
+ constructor(settings) {
17
+ super({ settings });
18
+ }
19
+ async callAPI(messages, options) {
20
+ return callWithRetryAndThrottle({
21
+ retry: this.settings.api?.retry,
22
+ throttle: this.settings.api?.throttle,
23
+ call: async () => callOpenAIChatCompletionAPI({
24
+ ...this.settings,
25
+ // function & tool calling:
26
+ functions: options.functions ?? this.settings.functions,
27
+ functionCall: options.functionCall ?? this.settings.functionCall,
28
+ tools: options.tools ?? this.settings.tools,
29
+ toolChoice: options.toolChoice ?? this.settings.toolChoice,
30
+ // map to OpenAI API names:
31
+ stop: this.settings.stopSequences,
32
+ maxTokens: this.settings.maxCompletionTokens,
33
+ openAIResponseFormat: this.settings.responseFormat,
34
+ // other settings:
35
+ user: this.settings.isUserIdForwardingEnabled
36
+ ? options.run?.userId
37
+ : undefined,
38
+ abortSignal: options.run?.abortSignal,
39
+ responseFormat: options.responseFormat,
40
+ messages,
41
+ }),
42
+ });
43
+ }
44
+ async doGenerateText(prompt, options) {
45
+ const response = await this.callAPI(prompt, {
46
+ ...options,
47
+ responseFormat: OpenAIChatResponseFormat.json,
48
+ });
49
+ return {
50
+ response,
51
+ text: response.choices[0].message.content,
52
+ usage: this.extractUsage(response),
53
+ };
54
+ }
55
+ doStreamText(prompt, options) {
56
+ return this.callAPI(prompt, {
57
+ ...options,
58
+ responseFormat: OpenAIChatResponseFormat.textDeltaIterable,
59
+ });
60
+ }
61
+ async doGenerateToolCall(tool, prompt, options) {
62
+ const response = await this.callAPI(prompt, {
63
+ ...options,
64
+ responseFormat: OpenAIChatResponseFormat.json,
65
+ toolChoice: {
66
+ type: "function",
67
+ function: { name: tool.name },
68
+ },
69
+ tools: [
70
+ {
71
+ type: "function",
72
+ function: {
73
+ name: tool.name,
74
+ description: tool.description,
75
+ parameters: tool.parameters.getJsonSchema(),
76
+ },
77
+ },
78
+ ],
79
+ });
80
+ const toolCalls = response.choices[0]?.message.tool_calls;
81
+ return {
82
+ response,
83
+ toolCall: toolCalls == null || toolCalls.length === 0
84
+ ? null
85
+ : {
86
+ id: toolCalls[0].id,
87
+ args: parseJSON({ text: toolCalls[0].function.arguments }),
88
+ },
89
+ usage: this.extractUsage(response),
90
+ };
91
+ }
92
+ async doGenerateToolCallsOrText(tools, prompt, options) {
93
+ const response = await this.callAPI(prompt, {
94
+ ...options,
95
+ responseFormat: OpenAIChatResponseFormat.json,
96
+ toolChoice: "auto",
97
+ tools: tools.map((tool) => ({
98
+ type: "function",
99
+ function: {
100
+ name: tool.name,
101
+ description: tool.description,
102
+ parameters: tool.parameters.getJsonSchema(),
103
+ },
104
+ })),
105
+ });
106
+ const message = response.choices[0]?.message;
107
+ return {
108
+ response,
109
+ text: message.content ?? null,
110
+ toolCalls: message.tool_calls?.map((toolCall) => ({
111
+ id: toolCall.id,
112
+ name: toolCall.function.name,
113
+ args: parseJSON({ text: toolCall.function.arguments }),
114
+ })) ?? null,
115
+ usage: this.extractUsage(response),
116
+ };
117
+ }
118
+ extractUsage(response) {
119
+ return {
120
+ promptTokens: response.usage.prompt_tokens,
121
+ completionTokens: response.usage.completion_tokens,
122
+ totalTokens: response.usage.total_tokens,
123
+ };
124
+ }
125
+ }
126
+ const openAIChatResponseSchema = z.object({
127
+ id: z.string(),
128
+ choices: z.array(z.object({
129
+ message: z.object({
130
+ role: z.literal("assistant"),
131
+ content: z.string().nullable(),
132
+ function_call: z
133
+ .object({
134
+ name: z.string(),
135
+ arguments: z.string(),
136
+ })
137
+ .optional(),
138
+ tool_calls: z
139
+ .array(z.object({
140
+ id: z.string(),
141
+ type: z.literal("function"),
142
+ function: z.object({
143
+ name: z.string(),
144
+ arguments: z.string(),
145
+ }),
146
+ }))
147
+ .optional(),
148
+ }),
149
+ index: z.number(),
150
+ logprobs: z.nullable(z.any()),
151
+ finish_reason: z
152
+ .enum([
153
+ "stop",
154
+ "length",
155
+ "tool_calls",
156
+ "content_filter",
157
+ "function_call",
158
+ ])
159
+ .optional()
160
+ .nullable(),
161
+ })),
162
+ created: z.number(),
163
+ model: z.string(),
164
+ system_fingerprint: z.string().optional(),
165
+ object: z.literal("chat.completion"),
166
+ usage: z.object({
167
+ prompt_tokens: z.number(),
168
+ completion_tokens: z.number(),
169
+ total_tokens: z.number(),
170
+ }),
171
+ });
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
+ export const OpenAIChatResponseFormat = {
206
+ /**
207
+ * Returns the response as a JSON object.
208
+ */
209
+ json: {
210
+ stream: false,
211
+ handler: createJsonResponseHandler(openAIChatResponseSchema),
212
+ },
213
+ /**
214
+ * Returns an async iterable over the text deltas (only the tex different of the first choice).
215
+ */
216
+ textDeltaIterable: {
217
+ stream: true,
218
+ handler: async ({ response }) => createOpenAIChatDeltaIterableQueue(response.body, (delta) => delta[0]?.delta.content ?? ""),
219
+ },
220
+ structureDeltaIterable: {
221
+ stream: true,
222
+ handler: async ({ response }) => createOpenAIChatDeltaIterableQueue(response.body, (delta) => parsePartialJson(delta[0]?.function_call?.arguments)),
223
+ },
224
+ };