modelfusion 0.106.0 → 0.108.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 (82) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +19 -59
  3. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.test.cjs +11 -0
  4. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.test.js +11 -0
  5. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +9 -7
  6. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +9 -7
  7. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.test.cjs +11 -0
  8. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.test.js +11 -0
  9. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.cjs +150 -0
  10. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.d.ts +62 -0
  11. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.js +143 -0
  12. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.test.cjs +60 -0
  13. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.test.js +58 -0
  14. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.test.cjs +11 -0
  15. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.test.js +11 -0
  16. package/model-function/generate-text/prompt-template/TextPromptTemplate.test.cjs +11 -0
  17. package/model-function/generate-text/prompt-template/TextPromptTemplate.test.js +11 -0
  18. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.test.cjs +11 -0
  19. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.test.js +11 -0
  20. package/model-function/generate-text/prompt-template/index.cjs +2 -1
  21. package/model-function/generate-text/prompt-template/index.d.ts +1 -0
  22. package/model-function/generate-text/prompt-template/index.js +1 -0
  23. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.d.ts +3 -3
  24. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.cjs → LlamaCppCompletionModel.cjs} +25 -11
  25. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.d.ts → LlamaCppCompletionModel.d.ts} +125 -38
  26. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.js → LlamaCppCompletionModel.js} +23 -9
  27. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.test.cjs → LlamaCppCompletionModel.test.cjs} +3 -3
  28. package/model-provider/llamacpp/LlamaCppCompletionModel.test.d.ts +1 -0
  29. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.test.js → LlamaCppCompletionModel.test.js} +3 -3
  30. package/model-provider/llamacpp/LlamaCppFacade.cjs +2 -2
  31. package/model-provider/llamacpp/LlamaCppFacade.d.ts +2 -2
  32. package/model-provider/llamacpp/LlamaCppFacade.js +2 -2
  33. package/model-provider/llamacpp/index.cjs +1 -1
  34. package/model-provider/llamacpp/index.d.ts +1 -1
  35. package/model-provider/llamacpp/index.js +1 -1
  36. package/model-provider/mistral/MistralChatModel.cjs +4 -4
  37. package/model-provider/mistral/MistralChatModel.d.ts +6 -6
  38. package/model-provider/mistral/MistralChatModel.js +1 -1
  39. package/model-provider/mistral/index.cjs +3 -3
  40. package/model-provider/mistral/index.d.ts +2 -2
  41. package/model-provider/mistral/index.js +2 -2
  42. package/model-provider/openai/AbstractOpenAIChatModel.cjs +2 -10
  43. package/model-provider/openai/AbstractOpenAIChatModel.d.ts +13 -195
  44. package/model-provider/openai/AbstractOpenAIChatModel.js +2 -10
  45. package/model-provider/openai/AbstractOpenAICompletionModel.cjs +167 -0
  46. package/model-provider/openai/AbstractOpenAICompletionModel.d.ts +199 -0
  47. package/model-provider/openai/AbstractOpenAICompletionModel.js +163 -0
  48. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +1 -3
  49. package/model-provider/openai/OpenAIChatModel.d.ts +3 -6
  50. package/model-provider/openai/OpenAICompletionModel.cjs +4 -156
  51. package/model-provider/openai/OpenAICompletionModel.d.ts +4 -191
  52. package/model-provider/openai/OpenAICompletionModel.js +3 -155
  53. package/model-provider/openai/index.cjs +1 -0
  54. package/model-provider/openai/index.d.ts +1 -0
  55. package/model-provider/openai/index.js +1 -0
  56. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +4 -5
  57. package/model-provider/openai-compatible/OpenAICompatibleCompletionModel.cjs +74 -0
  58. package/model-provider/openai-compatible/OpenAICompatibleCompletionModel.d.ts +27 -0
  59. package/model-provider/openai-compatible/OpenAICompatibleCompletionModel.js +70 -0
  60. package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +37 -6
  61. package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +33 -5
  62. package/model-provider/openai-compatible/OpenAICompatibleFacade.js +35 -5
  63. package/model-provider/openai-compatible/OpenAICompatibleProviderName.cjs +2 -0
  64. package/model-provider/openai-compatible/OpenAICompatibleProviderName.d.ts +1 -0
  65. package/model-provider/openai-compatible/OpenAICompatibleProviderName.js +1 -0
  66. package/model-provider/openai-compatible/TogetherAIApiConfiguration.cjs +29 -0
  67. package/model-provider/openai-compatible/TogetherAIApiConfiguration.d.ts +18 -0
  68. package/model-provider/openai-compatible/TogetherAIApiConfiguration.js +25 -0
  69. package/model-provider/openai-compatible/index.cjs +4 -1
  70. package/model-provider/openai-compatible/index.d.ts +4 -1
  71. package/model-provider/openai-compatible/index.js +4 -1
  72. package/package.json +16 -16
  73. package/tool/generate-tool-call/index.cjs +1 -0
  74. package/tool/generate-tool-call/index.d.ts +1 -0
  75. package/tool/generate-tool-call/index.js +1 -0
  76. package/tool/generate-tool-call/jsonToolCallPrompt.cjs +30 -0
  77. package/tool/generate-tool-call/jsonToolCallPrompt.d.ts +5 -0
  78. package/tool/generate-tool-call/jsonToolCallPrompt.js +27 -0
  79. /package/{model-provider/llamacpp/LlamaCppTextGenerationModel.test.d.ts → model-function/generate-text/prompt-template/MistralInstructPromptTemplate.test.d.ts} +0 -0
  80. /package/model-provider/mistral/{MistralPromptTemplate.cjs → MistralChatPromptTemplate.cjs} +0 -0
  81. /package/model-provider/mistral/{MistralPromptTemplate.d.ts → MistralChatPromptTemplate.d.ts} +0 -0
  82. /package/model-provider/mistral/{MistralPromptTemplate.js → MistralChatPromptTemplate.js} +0 -0
@@ -3,5 +3,5 @@ export * as LlamaCppBakLLaVA1Prompt from "./LlamaCppBakLLaVA1PromptTemplate.js";
3
3
  export { LlamaCppError, LlamaCppErrorData } from "./LlamaCppError.js";
4
4
  export * as llamacpp from "./LlamaCppFacade.js";
5
5
  export * from "./LlamaCppTextEmbeddingModel.js";
6
- export * from "./LlamaCppTextGenerationModel.js";
6
+ export * from "./LlamaCppCompletionModel.js";
7
7
  export * from "./LlamaCppTokenizer.js";
@@ -3,5 +3,5 @@ export * as LlamaCppBakLLaVA1Prompt from "./LlamaCppBakLLaVA1PromptTemplate.js";
3
3
  export { LlamaCppError } from "./LlamaCppError.js";
4
4
  export * as llamacpp from "./LlamaCppFacade.js";
5
5
  export * from "./LlamaCppTextEmbeddingModel.js";
6
- export * from "./LlamaCppTextGenerationModel.js";
6
+ export * from "./LlamaCppCompletionModel.js";
7
7
  export * from "./LlamaCppTokenizer.js";
@@ -10,8 +10,8 @@ const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/gene
10
10
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
11
11
  const createEventSourceResponseHandler_js_1 = require("../../util/streaming/createEventSourceResponseHandler.cjs");
12
12
  const MistralApiConfiguration_js_1 = require("./MistralApiConfiguration.cjs");
13
+ const MistralChatPromptTemplate_js_1 = require("./MistralChatPromptTemplate.cjs");
13
14
  const MistralError_js_1 = require("./MistralError.cjs");
14
- const MistralPromptTemplate_js_1 = require("./MistralPromptTemplate.cjs");
15
15
  class MistralChatModel extends AbstractModel_js_1.AbstractModel {
16
16
  constructor(settings) {
17
17
  super({ settings });
@@ -118,19 +118,19 @@ class MistralChatModel extends AbstractModel_js_1.AbstractModel {
118
118
  * Returns this model with a text prompt template.
119
119
  */
120
120
  withTextPrompt() {
121
- return this.withPromptTemplate((0, MistralPromptTemplate_js_1.text)());
121
+ return this.withPromptTemplate((0, MistralChatPromptTemplate_js_1.text)());
122
122
  }
123
123
  /**
124
124
  * Returns this model with an instruction prompt template.
125
125
  */
126
126
  withInstructionPrompt() {
127
- return this.withPromptTemplate((0, MistralPromptTemplate_js_1.instruction)());
127
+ return this.withPromptTemplate((0, MistralChatPromptTemplate_js_1.instruction)());
128
128
  }
129
129
  /**
130
130
  * Returns this model with a chat prompt template.
131
131
  */
132
132
  withChatPrompt() {
133
- return this.withPromptTemplate((0, MistralPromptTemplate_js_1.chat)());
133
+ return this.withPromptTemplate((0, MistralChatPromptTemplate_js_1.chat)());
134
134
  }
135
135
  withPromptTemplate(promptTemplate) {
136
136
  return new PromptTemplateTextStreamingModel_js_1.PromptTemplateTextStreamingModel({
@@ -64,8 +64,8 @@ export declare class MistralChatModel extends AbstractModel<MistralChatModelSett
64
64
  object: string;
65
65
  usage: {
66
66
  prompt_tokens: number;
67
- total_tokens: number;
68
67
  completion_tokens: number;
68
+ total_tokens: number;
69
69
  };
70
70
  model: string;
71
71
  id: string;
@@ -154,19 +154,19 @@ declare const mistralChatResponseSchema: z.ZodObject<{
154
154
  total_tokens: z.ZodNumber;
155
155
  }, "strip", z.ZodTypeAny, {
156
156
  prompt_tokens: number;
157
- total_tokens: number;
158
157
  completion_tokens: number;
158
+ total_tokens: number;
159
159
  }, {
160
160
  prompt_tokens: number;
161
- total_tokens: number;
162
161
  completion_tokens: number;
162
+ total_tokens: number;
163
163
  }>;
164
164
  }, "strip", z.ZodTypeAny, {
165
165
  object: string;
166
166
  usage: {
167
167
  prompt_tokens: number;
168
- total_tokens: number;
169
168
  completion_tokens: number;
169
+ total_tokens: number;
170
170
  };
171
171
  model: string;
172
172
  id: string;
@@ -183,8 +183,8 @@ declare const mistralChatResponseSchema: z.ZodObject<{
183
183
  object: string;
184
184
  usage: {
185
185
  prompt_tokens: number;
186
- total_tokens: number;
187
186
  completion_tokens: number;
187
+ total_tokens: number;
188
188
  };
189
189
  model: string;
190
190
  id: string;
@@ -228,8 +228,8 @@ export declare const MistralChatResponseFormat: {
228
228
  object: string;
229
229
  usage: {
230
230
  prompt_tokens: number;
231
- total_tokens: number;
232
231
  completion_tokens: number;
232
+ total_tokens: number;
233
233
  };
234
234
  model: string;
235
235
  id: string;
@@ -7,8 +7,8 @@ import { PromptTemplateTextStreamingModel } from "../../model-function/generate-
7
7
  import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
8
8
  import { createEventSourceResponseHandler } from "../../util/streaming/createEventSourceResponseHandler.js";
9
9
  import { MistralApiConfiguration } from "./MistralApiConfiguration.js";
10
+ import { chat, instruction, text } from "./MistralChatPromptTemplate.js";
10
11
  import { failedMistralCallResponseHandler } from "./MistralError.js";
11
- import { chat, instruction, text } from "./MistralPromptTemplate.js";
12
12
  export class MistralChatModel extends AbstractModel {
13
13
  constructor(settings) {
14
14
  super({ settings });
@@ -26,9 +26,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.MistralPrompt = exports.mistral = void 0;
29
+ exports.mistral = exports.MistralChatPrompt = void 0;
30
30
  __exportStar(require("./MistralApiConfiguration.cjs"), exports);
31
+ __exportStar(require("./MistralChatModel.cjs"), exports);
32
+ exports.MistralChatPrompt = __importStar(require("./MistralChatPromptTemplate.cjs"));
31
33
  exports.mistral = __importStar(require("./MistralFacade.cjs"));
32
- exports.MistralPrompt = __importStar(require("./MistralPromptTemplate.cjs"));
33
34
  __exportStar(require("./MistralTextEmbeddingModel.cjs"), exports);
34
- __exportStar(require("./MistralChatModel.cjs"), exports);
@@ -1,6 +1,6 @@
1
1
  export * from "./MistralApiConfiguration.js";
2
+ export * from "./MistralChatModel.js";
3
+ export * as MistralChatPrompt from "./MistralChatPromptTemplate.js";
2
4
  export { MistralErrorData } from "./MistralError.js";
3
5
  export * as mistral from "./MistralFacade.js";
4
- export * as MistralPrompt from "./MistralPromptTemplate.js";
5
6
  export * from "./MistralTextEmbeddingModel.js";
6
- export * from "./MistralChatModel.js";
@@ -1,5 +1,5 @@
1
1
  export * from "./MistralApiConfiguration.js";
2
+ export * from "./MistralChatModel.js";
3
+ export * as MistralChatPrompt from "./MistralChatPromptTemplate.js";
2
4
  export * as mistral from "./MistralFacade.js";
3
- export * as MistralPrompt from "./MistralPromptTemplate.js";
4
5
  export * from "./MistralTextEmbeddingModel.js";
5
- export * from "./MistralChatModel.js";
@@ -232,7 +232,7 @@ const openAIChatResponseSchema = zod_1.z.object({
232
232
  total_tokens: zod_1.z.number(),
233
233
  }),
234
234
  });
235
- const chatCompletionChunkSchema = zod_1.z.object({
235
+ const openaiChatChunkSchema = (0, ZodSchema_js_1.zodSchema)(zod_1.z.object({
236
236
  object: zod_1.z.literal("chat.completion.chunk"),
237
237
  id: zod_1.z.string(),
238
238
  choices: zod_1.z.array(zod_1.z.object({
@@ -271,15 +271,7 @@ const chatCompletionChunkSchema = zod_1.z.object({
271
271
  created: zod_1.z.number(),
272
272
  model: zod_1.z.string(),
273
273
  system_fingerprint: zod_1.z.string().optional().nullable(),
274
- });
275
- const openaiChatChunkSchema = (0, ZodSchema_js_1.zodSchema)(zod_1.z.union([
276
- chatCompletionChunkSchema,
277
- zod_1.z.object({
278
- object: zod_1.z.string().refine((obj) => obj !== "chat.completion.chunk", {
279
- message: "Object must be 'chat.completion.chunk'",
280
- }),
281
- }),
282
- ]));
274
+ }));
283
275
  exports.OpenAIChatResponseFormat = {
284
276
  /**
285
277
  * Returns the response as a JSON object.
@@ -7,7 +7,7 @@ import { TextGenerationModelSettings } from "../../model-function/generate-text/
7
7
  import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
8
8
  import { ToolDefinition } from "../../tool/ToolDefinition.js";
9
9
  import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
10
- export interface AbstractOpenAIChatCallSettings {
10
+ export interface AbstractOpenAIChatSettings extends TextGenerationModelSettings {
11
11
  api?: ApiConfiguration;
12
12
  model: string;
13
13
  functions?: Array<{
@@ -72,8 +72,6 @@ export interface AbstractOpenAIChatCallSettings {
72
72
  type?: "text" | "json_object";
73
73
  };
74
74
  logitBias?: Record<number, number>;
75
- }
76
- export interface AbstractOpenAIChatSettings extends TextGenerationModelSettings, AbstractOpenAIChatCallSettings {
77
75
  isUserIdForwardingEnabled?: boolean;
78
76
  }
79
77
  export type OpenAIChatPrompt = OpenAIChatMessage[];
@@ -87,18 +85,18 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
87
85
  callAPI<RESULT>(messages: OpenAIChatPrompt, options: {
88
86
  responseFormat: OpenAIChatResponseFormatType<RESULT>;
89
87
  } & FunctionOptions & {
90
- functions?: AbstractOpenAIChatCallSettings["functions"];
91
- functionCall?: AbstractOpenAIChatCallSettings["functionCall"];
92
- tools?: AbstractOpenAIChatCallSettings["tools"];
93
- toolChoice?: AbstractOpenAIChatCallSettings["toolChoice"];
88
+ functions?: AbstractOpenAIChatSettings["functions"];
89
+ functionCall?: AbstractOpenAIChatSettings["functionCall"];
90
+ tools?: AbstractOpenAIChatSettings["tools"];
91
+ toolChoice?: AbstractOpenAIChatSettings["toolChoice"];
94
92
  }): Promise<RESULT>;
95
93
  doGenerateTexts(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
96
94
  response: {
97
95
  object: "chat.completion";
98
96
  usage: {
99
97
  prompt_tokens: number;
100
- total_tokens: number;
101
98
  completion_tokens: number;
99
+ total_tokens: number;
102
100
  };
103
101
  model: string;
104
102
  id: string;
@@ -163,8 +161,6 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
163
161
  finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
164
162
  }[];
165
163
  system_fingerprint?: string | null | undefined;
166
- } | {
167
- object: string;
168
164
  }>>>;
169
165
  extractTextDelta(delta: unknown): string | undefined;
170
166
  doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
@@ -172,8 +168,8 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
172
168
  object: "chat.completion";
173
169
  usage: {
174
170
  prompt_tokens: number;
175
- total_tokens: number;
176
171
  completion_tokens: number;
172
+ total_tokens: number;
177
173
  };
178
174
  model: string;
179
175
  id: string;
@@ -216,8 +212,8 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
216
212
  object: "chat.completion";
217
213
  usage: {
218
214
  prompt_tokens: number;
219
- total_tokens: number;
220
215
  completion_tokens: number;
216
+ total_tokens: number;
221
217
  };
222
218
  model: string;
223
219
  id: string;
@@ -392,19 +388,19 @@ declare const openAIChatResponseSchema: z.ZodObject<{
392
388
  total_tokens: z.ZodNumber;
393
389
  }, "strip", z.ZodTypeAny, {
394
390
  prompt_tokens: number;
395
- total_tokens: number;
396
391
  completion_tokens: number;
392
+ total_tokens: number;
397
393
  }, {
398
394
  prompt_tokens: number;
399
- total_tokens: number;
400
395
  completion_tokens: number;
396
+ total_tokens: number;
401
397
  }>;
402
398
  }, "strip", z.ZodTypeAny, {
403
399
  object: "chat.completion";
404
400
  usage: {
405
401
  prompt_tokens: number;
406
- total_tokens: number;
407
402
  completion_tokens: number;
403
+ total_tokens: number;
408
404
  };
409
405
  model: string;
410
406
  id: string;
@@ -435,8 +431,8 @@ declare const openAIChatResponseSchema: z.ZodObject<{
435
431
  object: "chat.completion";
436
432
  usage: {
437
433
  prompt_tokens: number;
438
- total_tokens: number;
439
434
  completion_tokens: number;
435
+ total_tokens: number;
440
436
  };
441
437
  model: string;
442
438
  id: string;
@@ -465,180 +461,6 @@ declare const openAIChatResponseSchema: z.ZodObject<{
465
461
  system_fingerprint?: string | null | undefined;
466
462
  }>;
467
463
  export type OpenAIChatResponse = z.infer<typeof openAIChatResponseSchema>;
468
- declare const chatCompletionChunkSchema: z.ZodObject<{
469
- object: z.ZodLiteral<"chat.completion.chunk">;
470
- id: z.ZodString;
471
- choices: z.ZodArray<z.ZodObject<{
472
- delta: z.ZodObject<{
473
- role: z.ZodOptional<z.ZodEnum<["assistant", "user"]>>;
474
- content: z.ZodOptional<z.ZodNullable<z.ZodString>>;
475
- function_call: z.ZodOptional<z.ZodObject<{
476
- name: z.ZodOptional<z.ZodString>;
477
- arguments: z.ZodOptional<z.ZodString>;
478
- }, "strip", z.ZodTypeAny, {
479
- name?: string | undefined;
480
- arguments?: string | undefined;
481
- }, {
482
- name?: string | undefined;
483
- arguments?: string | undefined;
484
- }>>;
485
- tool_calls: z.ZodOptional<z.ZodArray<z.ZodObject<{
486
- id: z.ZodString;
487
- type: z.ZodLiteral<"function">;
488
- function: z.ZodObject<{
489
- name: z.ZodString;
490
- arguments: z.ZodString;
491
- }, "strip", z.ZodTypeAny, {
492
- name: string;
493
- arguments: string;
494
- }, {
495
- name: string;
496
- arguments: string;
497
- }>;
498
- }, "strip", z.ZodTypeAny, {
499
- function: {
500
- name: string;
501
- arguments: string;
502
- };
503
- type: "function";
504
- id: string;
505
- }, {
506
- function: {
507
- name: string;
508
- arguments: string;
509
- };
510
- type: "function";
511
- id: string;
512
- }>, "many">>;
513
- }, "strip", z.ZodTypeAny, {
514
- role?: "user" | "assistant" | undefined;
515
- content?: string | null | undefined;
516
- function_call?: {
517
- name?: string | undefined;
518
- arguments?: string | undefined;
519
- } | undefined;
520
- tool_calls?: {
521
- function: {
522
- name: string;
523
- arguments: string;
524
- };
525
- type: "function";
526
- id: string;
527
- }[] | undefined;
528
- }, {
529
- role?: "user" | "assistant" | undefined;
530
- content?: string | null | undefined;
531
- function_call?: {
532
- name?: string | undefined;
533
- arguments?: string | undefined;
534
- } | undefined;
535
- tool_calls?: {
536
- function: {
537
- name: string;
538
- arguments: string;
539
- };
540
- type: "function";
541
- id: string;
542
- }[] | undefined;
543
- }>;
544
- finish_reason: z.ZodOptional<z.ZodNullable<z.ZodEnum<["stop", "length", "tool_calls", "content_filter", "function_call"]>>>;
545
- index: z.ZodNumber;
546
- }, "strip", z.ZodTypeAny, {
547
- delta: {
548
- role?: "user" | "assistant" | undefined;
549
- content?: string | null | undefined;
550
- function_call?: {
551
- name?: string | undefined;
552
- arguments?: string | undefined;
553
- } | undefined;
554
- tool_calls?: {
555
- function: {
556
- name: string;
557
- arguments: string;
558
- };
559
- type: "function";
560
- id: string;
561
- }[] | undefined;
562
- };
563
- index: number;
564
- finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
565
- }, {
566
- delta: {
567
- role?: "user" | "assistant" | undefined;
568
- content?: string | null | undefined;
569
- function_call?: {
570
- name?: string | undefined;
571
- arguments?: string | undefined;
572
- } | undefined;
573
- tool_calls?: {
574
- function: {
575
- name: string;
576
- arguments: string;
577
- };
578
- type: "function";
579
- id: string;
580
- }[] | undefined;
581
- };
582
- index: number;
583
- finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
584
- }>, "many">;
585
- created: z.ZodNumber;
586
- model: z.ZodString;
587
- system_fingerprint: z.ZodNullable<z.ZodOptional<z.ZodString>>;
588
- }, "strip", z.ZodTypeAny, {
589
- object: "chat.completion.chunk";
590
- model: string;
591
- id: string;
592
- created: number;
593
- choices: {
594
- delta: {
595
- role?: "user" | "assistant" | undefined;
596
- content?: string | null | undefined;
597
- function_call?: {
598
- name?: string | undefined;
599
- arguments?: string | undefined;
600
- } | undefined;
601
- tool_calls?: {
602
- function: {
603
- name: string;
604
- arguments: string;
605
- };
606
- type: "function";
607
- id: string;
608
- }[] | undefined;
609
- };
610
- index: number;
611
- finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
612
- }[];
613
- system_fingerprint?: string | null | undefined;
614
- }, {
615
- object: "chat.completion.chunk";
616
- model: string;
617
- id: string;
618
- created: number;
619
- choices: {
620
- delta: {
621
- role?: "user" | "assistant" | undefined;
622
- content?: string | null | undefined;
623
- function_call?: {
624
- name?: string | undefined;
625
- arguments?: string | undefined;
626
- } | undefined;
627
- tool_calls?: {
628
- function: {
629
- name: string;
630
- arguments: string;
631
- };
632
- type: "function";
633
- id: string;
634
- }[] | undefined;
635
- };
636
- index: number;
637
- finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
638
- }[];
639
- system_fingerprint?: string | null | undefined;
640
- }>;
641
- export type OpenAIChatCompletionChunk = z.infer<typeof chatCompletionChunkSchema>;
642
464
  declare const openaiChatChunkSchema: import("../../core/schema/ZodSchema.js").ZodSchema<{
643
465
  object: "chat.completion.chunk";
644
466
  model: string;
@@ -665,8 +487,6 @@ declare const openaiChatChunkSchema: import("../../core/schema/ZodSchema.js").Zo
665
487
  finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
666
488
  }[];
667
489
  system_fingerprint?: string | null | undefined;
668
- } | {
669
- object: string;
670
490
  }>;
671
491
  export type OpenAIChatChunk = (typeof openaiChatChunkSchema)["_type"];
672
492
  export type OpenAIChatResponseFormatType<T> = {
@@ -683,8 +503,8 @@ export declare const OpenAIChatResponseFormat: {
683
503
  object: "chat.completion";
684
504
  usage: {
685
505
  prompt_tokens: number;
686
- total_tokens: number;
687
506
  completion_tokens: number;
507
+ total_tokens: number;
688
508
  };
689
509
  model: string;
690
510
  id: string;
@@ -746,8 +566,6 @@ export declare const OpenAIChatResponseFormat: {
746
566
  finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
747
567
  }[];
748
568
  system_fingerprint?: string | null | undefined;
749
- } | {
750
- object: string;
751
569
  }>>>;
752
570
  };
753
571
  };
@@ -228,7 +228,7 @@ const openAIChatResponseSchema = z.object({
228
228
  total_tokens: z.number(),
229
229
  }),
230
230
  });
231
- const chatCompletionChunkSchema = z.object({
231
+ const openaiChatChunkSchema = zodSchema(z.object({
232
232
  object: z.literal("chat.completion.chunk"),
233
233
  id: z.string(),
234
234
  choices: z.array(z.object({
@@ -267,15 +267,7 @@ const chatCompletionChunkSchema = z.object({
267
267
  created: z.number(),
268
268
  model: z.string(),
269
269
  system_fingerprint: z.string().optional().nullable(),
270
- });
271
- const openaiChatChunkSchema = zodSchema(z.union([
272
- chatCompletionChunkSchema,
273
- z.object({
274
- object: z.string().refine((obj) => obj !== "chat.completion.chunk", {
275
- message: "Object must be 'chat.completion.chunk'",
276
- }),
277
- }),
278
- ]));
270
+ }));
279
271
  export const OpenAIChatResponseFormat = {
280
272
  /**
281
273
  * Returns the response as a JSON object.
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpenAITextResponseFormat = exports.AbstractOpenAICompletionModel = void 0;
4
+ const zod_1 = require("zod");
5
+ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
+ const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
+ const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
+ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
+ const createEventSourceResponseHandler_js_1 = require("../../util/streaming/createEventSourceResponseHandler.cjs");
10
+ const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
11
+ const OpenAIError_js_1 = require("./OpenAIError.cjs");
12
+ /**
13
+ * Abstract completion model that calls an API that is compatible with the OpenAI completions API.
14
+ *
15
+ * @see https://platform.openai.com/docs/api-reference/completions/create
16
+ */
17
+ class AbstractOpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
18
+ constructor(settings) {
19
+ super({ settings });
20
+ }
21
+ async callAPI(prompt, options) {
22
+ const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
23
+ const user = this.settings.isUserIdForwardingEnabled
24
+ ? options.run?.userId
25
+ : undefined;
26
+ const abortSignal = options.run?.abortSignal;
27
+ const openaiResponseFormat = options.responseFormat;
28
+ // empty arrays are not allowed for stop:
29
+ const stopSequences = this.settings.stopSequences != null &&
30
+ Array.isArray(this.settings.stopSequences) &&
31
+ this.settings.stopSequences.length === 0
32
+ ? undefined
33
+ : this.settings.stopSequences;
34
+ return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
35
+ retry: api.retry,
36
+ throttle: api.throttle,
37
+ call: async () => {
38
+ return (0, postToApi_js_1.postJsonToApi)({
39
+ url: api.assembleUrl("/completions"),
40
+ headers: api.headers,
41
+ body: {
42
+ stream: openaiResponseFormat.stream,
43
+ model: this.settings.model,
44
+ prompt,
45
+ suffix: this.settings.suffix,
46
+ max_tokens: this.settings.maxGenerationTokens,
47
+ temperature: this.settings.temperature,
48
+ top_p: this.settings.topP,
49
+ n: this.settings.numberOfGenerations,
50
+ logprobs: this.settings.logprobs,
51
+ echo: this.settings.echo,
52
+ stop: stopSequences,
53
+ seed: this.settings.seed,
54
+ presence_penalty: this.settings.presencePenalty,
55
+ frequency_penalty: this.settings.frequencyPenalty,
56
+ best_of: this.settings.bestOf,
57
+ logit_bias: this.settings.logitBias,
58
+ user,
59
+ },
60
+ failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
61
+ successfulResponseHandler: openaiResponseFormat.handler,
62
+ abortSignal,
63
+ });
64
+ },
65
+ });
66
+ }
67
+ async doGenerateTexts(prompt, options) {
68
+ const response = await this.callAPI(prompt, {
69
+ ...options,
70
+ responseFormat: exports.OpenAITextResponseFormat.json,
71
+ });
72
+ return {
73
+ response,
74
+ textGenerationResults: response.choices.map((choice) => {
75
+ return {
76
+ finishReason: this.translateFinishReason(choice.finish_reason),
77
+ text: choice.text,
78
+ };
79
+ }),
80
+ usage: {
81
+ promptTokens: response.usage.prompt_tokens,
82
+ completionTokens: response.usage.completion_tokens,
83
+ totalTokens: response.usage.total_tokens,
84
+ },
85
+ };
86
+ }
87
+ translateFinishReason(finishReason) {
88
+ switch (finishReason) {
89
+ case "stop":
90
+ return "stop";
91
+ case "length":
92
+ return "length";
93
+ case "content_filter":
94
+ return "content-filter";
95
+ default:
96
+ return "unknown";
97
+ }
98
+ }
99
+ doStreamText(prompt, options) {
100
+ return this.callAPI(prompt, {
101
+ ...options,
102
+ responseFormat: exports.OpenAITextResponseFormat.deltaIterable,
103
+ });
104
+ }
105
+ extractTextDelta(delta) {
106
+ const chunk = delta;
107
+ const firstChoice = chunk.choices[0];
108
+ if (firstChoice.index > 0) {
109
+ return undefined;
110
+ }
111
+ return chunk.choices[0].text;
112
+ }
113
+ }
114
+ exports.AbstractOpenAICompletionModel = AbstractOpenAICompletionModel;
115
+ const OpenAICompletionResponseSchema = zod_1.z.object({
116
+ id: zod_1.z.string(),
117
+ choices: zod_1.z.array(zod_1.z.object({
118
+ finish_reason: zod_1.z
119
+ .enum(["stop", "length", "content_filter"])
120
+ .optional()
121
+ .nullable(),
122
+ index: zod_1.z.number(),
123
+ logprobs: zod_1.z.nullable(zod_1.z.any()),
124
+ text: zod_1.z.string(),
125
+ })),
126
+ created: zod_1.z.number(),
127
+ model: zod_1.z.string(),
128
+ system_fingerprint: zod_1.z.string().optional(),
129
+ object: zod_1.z.literal("text_completion"),
130
+ usage: zod_1.z.object({
131
+ prompt_tokens: zod_1.z.number(),
132
+ completion_tokens: zod_1.z.number(),
133
+ total_tokens: zod_1.z.number(),
134
+ }),
135
+ });
136
+ const openaiCompletionStreamChunkSchema = (0, ZodSchema_js_1.zodSchema)(zod_1.z.object({
137
+ choices: zod_1.z.array(zod_1.z.object({
138
+ text: zod_1.z.string(),
139
+ finish_reason: zod_1.z
140
+ .enum(["stop", "length", "content_filter"])
141
+ .optional()
142
+ .nullable(),
143
+ index: zod_1.z.number(),
144
+ })),
145
+ created: zod_1.z.number(),
146
+ id: zod_1.z.string(),
147
+ model: zod_1.z.string(),
148
+ system_fingerprint: zod_1.z.string().optional(),
149
+ object: zod_1.z.literal("text_completion"),
150
+ }));
151
+ exports.OpenAITextResponseFormat = {
152
+ /**
153
+ * Returns the response as a JSON object.
154
+ */
155
+ json: {
156
+ stream: false,
157
+ handler: (0, postToApi_js_1.createJsonResponseHandler)(OpenAICompletionResponseSchema),
158
+ },
159
+ /**
160
+ * Returns an async iterable over the full deltas (all choices, including full current state at time of event)
161
+ * of the response stream.
162
+ */
163
+ deltaIterable: {
164
+ stream: true,
165
+ handler: (0, createEventSourceResponseHandler_js_1.createEventSourceResponseHandler)(openaiCompletionStreamChunkSchema),
166
+ },
167
+ };