modelfusion 0.104.0 → 0.105.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 (164) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/model-function/Delta.d.ts +1 -2
  3. package/model-function/executeStreamCall.cjs +6 -4
  4. package/model-function/executeStreamCall.d.ts +2 -2
  5. package/model-function/executeStreamCall.js +6 -4
  6. package/model-function/generate-speech/streamSpeech.cjs +1 -2
  7. package/model-function/generate-speech/streamSpeech.js +1 -2
  8. package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +25 -29
  9. package/model-function/generate-structure/StructureFromTextStreamingModel.d.ts +3 -1
  10. package/model-function/generate-structure/StructureFromTextStreamingModel.js +25 -29
  11. package/model-function/generate-structure/StructureGenerationModel.d.ts +2 -0
  12. package/model-function/generate-structure/streamStructure.cjs +7 -8
  13. package/model-function/generate-structure/streamStructure.d.ts +1 -1
  14. package/model-function/generate-structure/streamStructure.js +7 -8
  15. package/model-function/generate-text/PromptTemplateFullTextModel.cjs +35 -0
  16. package/model-function/generate-text/PromptTemplateFullTextModel.d.ts +41 -0
  17. package/model-function/generate-text/PromptTemplateFullTextModel.js +31 -0
  18. package/model-function/generate-text/PromptTemplateTextStreamingModel.cjs +3 -0
  19. package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +2 -1
  20. package/model-function/generate-text/PromptTemplateTextStreamingModel.js +3 -0
  21. package/model-function/generate-text/TextGenerationModel.d.ts +2 -1
  22. package/model-function/generate-text/index.cjs +1 -0
  23. package/model-function/generate-text/index.d.ts +1 -0
  24. package/model-function/generate-text/index.js +1 -0
  25. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.cjs +2 -2
  26. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.js +1 -1
  27. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.cjs +8 -5
  28. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.js +7 -4
  29. package/model-function/generate-text/prompt-template/ChatPrompt.cjs +42 -0
  30. package/model-function/generate-text/prompt-template/ChatPrompt.d.ts +27 -5
  31. package/model-function/generate-text/prompt-template/ChatPrompt.js +41 -1
  32. package/model-function/generate-text/prompt-template/{Content.cjs → ContentPart.cjs} +1 -1
  33. package/model-function/generate-text/prompt-template/ContentPart.d.ts +30 -0
  34. package/model-function/generate-text/prompt-template/{Content.js → ContentPart.js} +1 -1
  35. package/model-function/generate-text/prompt-template/InstructionPrompt.d.ts +3 -2
  36. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +7 -4
  37. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +5 -2
  38. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.cjs +8 -4
  39. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.js +6 -2
  40. package/model-function/generate-text/prompt-template/TextPromptTemplate.cjs +8 -4
  41. package/model-function/generate-text/prompt-template/TextPromptTemplate.js +6 -2
  42. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.cjs +7 -3
  43. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.js +6 -2
  44. package/model-function/generate-text/prompt-template/index.cjs +1 -1
  45. package/model-function/generate-text/prompt-template/index.d.ts +1 -1
  46. package/model-function/generate-text/prompt-template/index.js +1 -1
  47. package/model-function/generate-text/streamText.cjs +27 -28
  48. package/model-function/generate-text/streamText.d.ts +1 -0
  49. package/model-function/generate-text/streamText.js +27 -28
  50. package/model-provider/anthropic/AnthropicPromptTemplate.cjs +7 -3
  51. package/model-provider/anthropic/AnthropicPromptTemplate.js +5 -1
  52. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +8 -14
  53. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +11 -2
  54. package/model-provider/anthropic/AnthropicTextGenerationModel.js +8 -14
  55. package/model-provider/anthropic/AnthropicTextGenerationModel.test.cjs +44 -0
  56. package/model-provider/anthropic/AnthropicTextGenerationModel.test.js +42 -0
  57. package/model-provider/cohere/CohereTextGenerationModel.cjs +6 -44
  58. package/model-provider/cohere/CohereTextGenerationModel.d.ts +45 -11
  59. package/model-provider/cohere/CohereTextGenerationModel.js +7 -45
  60. package/model-provider/cohere/CohereTextGenerationModel.test.cjs +33 -0
  61. package/model-provider/cohere/CohereTextGenerationModel.test.d.ts +1 -0
  62. package/model-provider/cohere/CohereTextGenerationModel.test.js +31 -0
  63. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +1 -2
  64. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +1 -2
  65. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.cjs +6 -1
  66. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.js +6 -1
  67. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +7 -14
  68. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +157 -6
  69. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +8 -15
  70. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.cjs +37 -0
  71. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.d.ts +1 -0
  72. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.js +35 -0
  73. package/model-provider/mistral/MistralChatModel.cjs +30 -104
  74. package/model-provider/mistral/MistralChatModel.d.ts +47 -14
  75. package/model-provider/mistral/MistralChatModel.js +30 -104
  76. package/model-provider/mistral/MistralChatModel.test.cjs +51 -0
  77. package/model-provider/mistral/MistralChatModel.test.d.ts +1 -0
  78. package/model-provider/mistral/MistralChatModel.test.js +49 -0
  79. package/model-provider/mistral/MistralPromptTemplate.cjs +11 -4
  80. package/model-provider/mistral/MistralPromptTemplate.js +9 -2
  81. package/model-provider/ollama/OllamaChatModel.cjs +7 -43
  82. package/model-provider/ollama/OllamaChatModel.d.ts +61 -9
  83. package/model-provider/ollama/OllamaChatModel.js +7 -43
  84. package/model-provider/ollama/OllamaChatModel.test.cjs +27 -0
  85. package/model-provider/ollama/OllamaChatModel.test.d.ts +1 -0
  86. package/model-provider/ollama/OllamaChatModel.test.js +25 -0
  87. package/model-provider/ollama/OllamaChatPromptTemplate.cjs +34 -4
  88. package/model-provider/ollama/OllamaChatPromptTemplate.js +34 -4
  89. package/model-provider/ollama/OllamaCompletionModel.cjs +22 -43
  90. package/model-provider/ollama/OllamaCompletionModel.d.ts +65 -9
  91. package/model-provider/ollama/OllamaCompletionModel.js +23 -44
  92. package/model-provider/ollama/OllamaCompletionModel.test.cjs +101 -13
  93. package/model-provider/ollama/OllamaCompletionModel.test.js +78 -13
  94. package/model-provider/openai/{chat/AbstractOpenAIChatModel.cjs → AbstractOpenAIChatModel.cjs} +71 -15
  95. package/model-provider/openai/{chat/AbstractOpenAIChatModel.d.ts → AbstractOpenAIChatModel.d.ts} +273 -19
  96. package/model-provider/openai/{chat/AbstractOpenAIChatModel.js → AbstractOpenAIChatModel.js} +71 -15
  97. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.cjs → OpenAIChatFunctionCallStructureGenerationModel.cjs} +18 -2
  98. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts → OpenAIChatFunctionCallStructureGenerationModel.d.ts} +41 -11
  99. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.js → OpenAIChatFunctionCallStructureGenerationModel.js} +18 -2
  100. package/model-provider/openai/{chat/OpenAIChatMessage.d.ts → OpenAIChatMessage.d.ts} +3 -3
  101. package/model-provider/openai/{chat/OpenAIChatModel.cjs → OpenAIChatModel.cjs} +5 -5
  102. package/model-provider/openai/{chat/OpenAIChatModel.d.ts → OpenAIChatModel.d.ts} +12 -12
  103. package/model-provider/openai/{chat/OpenAIChatModel.js → OpenAIChatModel.js} +5 -5
  104. package/model-provider/openai/OpenAIChatModel.test.cjs +94 -0
  105. package/model-provider/openai/OpenAIChatModel.test.d.ts +1 -0
  106. package/model-provider/openai/OpenAIChatModel.test.js +92 -0
  107. package/model-provider/openai/OpenAIChatPromptTemplate.cjs +114 -0
  108. package/model-provider/openai/{chat/OpenAIChatPromptTemplate.d.ts → OpenAIChatPromptTemplate.d.ts} +3 -3
  109. package/model-provider/openai/OpenAIChatPromptTemplate.js +107 -0
  110. package/model-provider/openai/OpenAICompletionModel.cjs +32 -84
  111. package/model-provider/openai/OpenAICompletionModel.d.ts +27 -10
  112. package/model-provider/openai/OpenAICompletionModel.js +33 -85
  113. package/model-provider/openai/OpenAICompletionModel.test.cjs +53 -0
  114. package/model-provider/openai/OpenAICompletionModel.test.d.ts +1 -0
  115. package/model-provider/openai/OpenAICompletionModel.test.js +51 -0
  116. package/model-provider/openai/OpenAICostCalculator.cjs +1 -1
  117. package/model-provider/openai/OpenAICostCalculator.js +1 -1
  118. package/model-provider/openai/OpenAIFacade.cjs +2 -2
  119. package/model-provider/openai/OpenAIFacade.d.ts +3 -3
  120. package/model-provider/openai/OpenAIFacade.js +2 -2
  121. package/model-provider/openai/OpenAITranscriptionModel.d.ts +6 -6
  122. package/model-provider/openai/TikTokenTokenizer.d.ts +1 -1
  123. package/model-provider/openai/{chat/countOpenAIChatMessageTokens.cjs → countOpenAIChatMessageTokens.cjs} +2 -2
  124. package/model-provider/openai/{chat/countOpenAIChatMessageTokens.js → countOpenAIChatMessageTokens.js} +2 -2
  125. package/model-provider/openai/index.cjs +6 -6
  126. package/model-provider/openai/index.d.ts +5 -6
  127. package/model-provider/openai/index.js +5 -5
  128. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +4 -4
  129. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +6 -6
  130. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +4 -4
  131. package/package.json +5 -5
  132. package/test/JsonTestServer.cjs +33 -0
  133. package/test/JsonTestServer.d.ts +7 -0
  134. package/test/JsonTestServer.js +29 -0
  135. package/test/StreamingTestServer.cjs +55 -0
  136. package/test/StreamingTestServer.d.ts +7 -0
  137. package/test/StreamingTestServer.js +51 -0
  138. package/test/arrayFromAsync.cjs +13 -0
  139. package/test/arrayFromAsync.d.ts +1 -0
  140. package/test/arrayFromAsync.js +9 -0
  141. package/util/streaming/createEventSourceResponseHandler.cjs +9 -0
  142. package/util/streaming/createEventSourceResponseHandler.d.ts +4 -0
  143. package/util/streaming/createEventSourceResponseHandler.js +5 -0
  144. package/util/streaming/createJsonStreamResponseHandler.cjs +9 -0
  145. package/util/streaming/createJsonStreamResponseHandler.d.ts +4 -0
  146. package/util/streaming/createJsonStreamResponseHandler.js +5 -0
  147. package/util/streaming/parseEventSourceStreamAsAsyncIterable.cjs +52 -0
  148. package/util/streaming/parseEventSourceStreamAsAsyncIterable.d.ts +6 -0
  149. package/util/streaming/parseEventSourceStreamAsAsyncIterable.js +48 -0
  150. package/util/streaming/parseJsonStreamAsAsyncIterable.cjs +21 -0
  151. package/util/streaming/parseJsonStreamAsAsyncIterable.d.ts +6 -0
  152. package/util/streaming/parseJsonStreamAsAsyncIterable.js +17 -0
  153. package/model-function/generate-text/prompt-template/Content.d.ts +0 -25
  154. package/model-provider/openai/chat/OpenAIChatModel.test.cjs +0 -61
  155. package/model-provider/openai/chat/OpenAIChatModel.test.js +0 -59
  156. package/model-provider/openai/chat/OpenAIChatPromptTemplate.cjs +0 -70
  157. package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +0 -63
  158. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +0 -156
  159. package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +0 -19
  160. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +0 -152
  161. /package/model-provider/{openai/chat/OpenAIChatModel.test.d.ts → anthropic/AnthropicTextGenerationModel.test.d.ts} +0 -0
  162. /package/model-provider/openai/{chat/OpenAIChatMessage.cjs → OpenAIChatMessage.cjs} +0 -0
  163. /package/model-provider/openai/{chat/OpenAIChatMessage.js → OpenAIChatMessage.js} +0 -0
  164. /package/model-provider/openai/{chat/countOpenAIChatMessageTokens.d.ts → countOpenAIChatMessageTokens.d.ts} +0 -0
@@ -1,12 +1,11 @@
1
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 { TextGenerationFinishReason } from "../../../model-function/generate-text/TextGenerationResult.js";
9
- import { ToolDefinition } from "../../../tool/ToolDefinition.js";
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 { TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
7
+ import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
8
+ import { ToolDefinition } from "../../tool/ToolDefinition.js";
10
9
  import { OpenAIChatMessage } from "./OpenAIChatMessage.js";
11
10
  export interface AbstractOpenAIChatCallSettings {
12
11
  api?: ApiConfiguration;
@@ -138,7 +137,36 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
138
137
  };
139
138
  }>;
140
139
  private translateFinishReason;
141
- doStreamText(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
140
+ doStreamText(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
141
+ object: "chat.completion.chunk";
142
+ model: string;
143
+ id: string;
144
+ created: number;
145
+ choices: {
146
+ delta: {
147
+ role?: "user" | "assistant" | undefined;
148
+ content?: string | null | undefined;
149
+ function_call?: {
150
+ name?: string | undefined;
151
+ arguments?: string | undefined;
152
+ } | undefined;
153
+ tool_calls?: {
154
+ function: {
155
+ name: string;
156
+ arguments: string;
157
+ };
158
+ type: "function";
159
+ id: string;
160
+ }[] | undefined;
161
+ };
162
+ index: number;
163
+ finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
164
+ }[];
165
+ system_fingerprint?: string | null | undefined;
166
+ } | {
167
+ object: string;
168
+ }>>>;
169
+ extractTextDelta(delta: unknown): string | undefined;
142
170
  doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
143
171
  response: {
144
172
  object: "chat.completion";
@@ -437,6 +465,210 @@ declare const openAIChatResponseSchema: z.ZodObject<{
437
465
  system_fingerprint?: string | null | undefined;
438
466
  }>;
439
467
  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
+ declare const openaiChatChunkSchema: import("../../core/schema/ZodSchema.js").ZodSchema<{
643
+ object: "chat.completion.chunk";
644
+ model: string;
645
+ id: string;
646
+ created: number;
647
+ choices: {
648
+ delta: {
649
+ role?: "user" | "assistant" | undefined;
650
+ content?: string | null | undefined;
651
+ function_call?: {
652
+ name?: string | undefined;
653
+ arguments?: string | undefined;
654
+ } | undefined;
655
+ tool_calls?: {
656
+ function: {
657
+ name: string;
658
+ arguments: string;
659
+ };
660
+ type: "function";
661
+ id: string;
662
+ }[] | undefined;
663
+ };
664
+ index: number;
665
+ finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
666
+ }[];
667
+ system_fingerprint?: string | null | undefined;
668
+ } | {
669
+ object: string;
670
+ }>;
671
+ export type OpenAIChatChunk = (typeof openaiChatChunkSchema)["_type"];
440
672
  export type OpenAIChatResponseFormatType<T> = {
441
673
  stream: boolean;
442
674
  handler: ResponseHandler<T>;
@@ -446,7 +678,7 @@ export declare const OpenAIChatResponseFormat: {
446
678
  * Returns the response as a JSON object.
447
679
  */
448
680
  json: {
449
- stream: false;
681
+ stream: boolean;
450
682
  handler: ResponseHandler<{
451
683
  object: "chat.completion";
452
684
  usage: {
@@ -484,17 +716,39 @@ export declare const OpenAIChatResponseFormat: {
484
716
  /**
485
717
  * Returns an async iterable over the text deltas (only the tex different of the first choice).
486
718
  */
487
- textDeltaIterable: {
488
- stream: true;
719
+ deltaIterable: {
720
+ stream: boolean;
489
721
  handler: ({ response }: {
490
722
  response: Response;
491
- }) => Promise<AsyncIterable<Delta<string>>>;
492
- };
493
- structureDeltaIterable: {
494
- stream: true;
495
- handler: ({ response }: {
496
- response: Response;
497
- }) => Promise<AsyncIterable<Delta<unknown>>>;
723
+ }) => Promise<AsyncIterable<import("../../index.js").Delta<{
724
+ object: "chat.completion.chunk";
725
+ model: string;
726
+ id: string;
727
+ created: number;
728
+ choices: {
729
+ delta: {
730
+ role?: "user" | "assistant" | undefined;
731
+ content?: string | null | undefined;
732
+ function_call?: {
733
+ name?: string | undefined;
734
+ arguments?: string | undefined;
735
+ } | undefined;
736
+ tool_calls?: {
737
+ function: {
738
+ name: string;
739
+ arguments: string;
740
+ };
741
+ type: "function";
742
+ id: string;
743
+ }[] | undefined;
744
+ };
745
+ index: number;
746
+ finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
747
+ }[];
748
+ system_fingerprint?: string | null | undefined;
749
+ } | {
750
+ object: string;
751
+ }>>>;
498
752
  };
499
753
  };
500
754
  export {};
@@ -1,12 +1,12 @@
1
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";
2
+ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
+ import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
+ import { zodSchema } from "../../core/schema/ZodSchema.js";
5
+ import { parseJSON } from "../../core/schema/parseJSON.js";
6
+ import { AbstractModel } from "../../model-function/AbstractModel.js";
7
+ import { createEventSourceResponseHandler } from "../../util/streaming/createEventSourceResponseHandler.js";
8
+ import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
9
+ import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
10
10
  /**
11
11
  * Abstract text generation model that calls an API that is compatible with the OpenAI chat API.
12
12
  *
@@ -102,9 +102,21 @@ export class AbstractOpenAIChatModel extends AbstractModel {
102
102
  doStreamText(prompt, options) {
103
103
  return this.callAPI(prompt, {
104
104
  ...options,
105
- responseFormat: OpenAIChatResponseFormat.textDeltaIterable,
105
+ responseFormat: OpenAIChatResponseFormat.deltaIterable,
106
106
  });
107
107
  }
108
+ extractTextDelta(delta) {
109
+ const chunk = delta;
110
+ if (chunk.object !== "chat.completion.chunk") {
111
+ return undefined;
112
+ }
113
+ const chatChunk = chunk;
114
+ const firstChoice = chatChunk.choices[0];
115
+ if (firstChoice.index > 0) {
116
+ return undefined;
117
+ }
118
+ return firstChoice.delta.content ?? undefined;
119
+ }
108
120
  async doGenerateToolCall(tool, prompt, options) {
109
121
  const response = await this.callAPI(prompt, {
110
122
  ...options,
@@ -216,6 +228,54 @@ const openAIChatResponseSchema = z.object({
216
228
  total_tokens: z.number(),
217
229
  }),
218
230
  });
231
+ const chatCompletionChunkSchema = z.object({
232
+ object: z.literal("chat.completion.chunk"),
233
+ id: z.string(),
234
+ choices: z.array(z.object({
235
+ delta: z.object({
236
+ role: z.enum(["assistant", "user"]).optional(),
237
+ content: z.string().nullable().optional(),
238
+ function_call: z
239
+ .object({
240
+ name: z.string().optional(),
241
+ arguments: z.string().optional(),
242
+ })
243
+ .optional(),
244
+ tool_calls: z
245
+ .array(z.object({
246
+ id: z.string(),
247
+ type: z.literal("function"),
248
+ function: z.object({
249
+ name: z.string(),
250
+ arguments: z.string(),
251
+ }),
252
+ }))
253
+ .optional(),
254
+ }),
255
+ finish_reason: z
256
+ .enum([
257
+ "stop",
258
+ "length",
259
+ "tool_calls",
260
+ "content_filter",
261
+ "function_call",
262
+ ])
263
+ .nullable()
264
+ .optional(),
265
+ index: z.number(),
266
+ })),
267
+ created: z.number(),
268
+ model: z.string(),
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
+ ]));
219
279
  export const OpenAIChatResponseFormat = {
220
280
  /**
221
281
  * Returns the response as a JSON object.
@@ -227,12 +287,8 @@ export const OpenAIChatResponseFormat = {
227
287
  /**
228
288
  * Returns an async iterable over the text deltas (only the tex different of the first choice).
229
289
  */
230
- textDeltaIterable: {
231
- stream: true,
232
- handler: async ({ response }) => createOpenAIChatDeltaIterableQueue(response.body, (delta) => delta[0]?.delta?.content ?? ""),
233
- },
234
- structureDeltaIterable: {
290
+ deltaIterable: {
235
291
  stream: true,
236
- handler: async ({ response }) => createOpenAIChatDeltaIterableQueue(response.body, (delta) => parsePartialJson(delta[0]?.function_call?.arguments)),
292
+ handler: createEventSourceResponseHandler(openaiChatChunkSchema),
237
293
  },
238
294
  };
@@ -5,7 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.OpenAIChatFunctionCallStructureGenerationModel = void 0;
7
7
  const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
- const StructureParseError_js_1 = require("../../../model-function/generate-structure/StructureParseError.cjs");
8
+ const StructureParseError_js_1 = require("../../model-function/generate-structure/StructureParseError.cjs");
9
+ const parsePartialJson_js_1 = require("../../model-function/generate-structure/parsePartialJson.cjs");
9
10
  const AbstractOpenAIChatModel_js_1 = require("./AbstractOpenAIChatModel.cjs");
10
11
  const OpenAIChatPromptTemplate_js_1 = require("./OpenAIChatPromptTemplate.cjs");
11
12
  class OpenAIChatFunctionCallStructureGenerationModel {
@@ -132,7 +133,7 @@ class OpenAIChatFunctionCallStructureGenerationModel {
132
133
  const expandedPrompt = this.promptTemplate.format(prompt);
133
134
  return this.model.callAPI(expandedPrompt, {
134
135
  ...options,
135
- responseFormat: AbstractOpenAIChatModel_js_1.OpenAIChatResponseFormat.structureDeltaIterable,
136
+ responseFormat: AbstractOpenAIChatModel_js_1.OpenAIChatResponseFormat.deltaIterable,
136
137
  functionCall: { name: this.fnName },
137
138
  functions: [
138
139
  {
@@ -143,5 +144,20 @@ class OpenAIChatFunctionCallStructureGenerationModel {
143
144
  ],
144
145
  });
145
146
  }
147
+ extractStructureTextDelta(delta) {
148
+ const chunk = delta;
149
+ if (chunk.object !== "chat.completion.chunk") {
150
+ return undefined;
151
+ }
152
+ const chatChunk = chunk;
153
+ const firstChoice = chatChunk.choices[0];
154
+ if (firstChoice.index > 0) {
155
+ return undefined;
156
+ }
157
+ return firstChoice.delta.function_call?.arguments;
158
+ }
159
+ parseAccumulatedStructureText(accumulatedText) {
160
+ return (0, parsePartialJson_js_1.parsePartialJson)(accumulatedText);
161
+ }
146
162
  }
147
163
  exports.OpenAIChatFunctionCallStructureGenerationModel = OpenAIChatFunctionCallStructureGenerationModel;
@@ -1,11 +1,11 @@
1
- import { FunctionOptions } from "../../../core/FunctionOptions.js";
2
- import { JsonSchemaProducer } from "../../../core/schema/JsonSchemaProducer.js";
3
- import { Schema } from "../../../core/schema/Schema.js";
4
- import { StructureGenerationModel } from "../../../model-function/generate-structure/StructureGenerationModel.js";
5
- import { TextGenerationPromptTemplate } from "../../../model-function/generate-text/TextGenerationPromptTemplate.js";
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
3
+ import { Schema } from "../../core/schema/Schema.js";
4
+ import { StructureStreamingModel } from "../../model-function/generate-structure/StructureGenerationModel.js";
5
+ import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
6
6
  import { OpenAIChatPrompt } from "./AbstractOpenAIChatModel.js";
7
- import { OpenAIChatModel, OpenAIChatSettings } from "./OpenAIChatModel";
8
- export declare class OpenAIChatFunctionCallStructureGenerationModel<PROMPT_TEMPLATE extends TextGenerationPromptTemplate<unknown, OpenAIChatPrompt>> implements StructureGenerationModel<Parameters<PROMPT_TEMPLATE["format"]>[0], // first argument of the function
7
+ import { OpenAIChatModel, OpenAIChatSettings } from "./OpenAIChatModel.js";
8
+ export declare class OpenAIChatFunctionCallStructureGenerationModel<PROMPT_TEMPLATE extends TextGenerationPromptTemplate<unknown, OpenAIChatPrompt>> implements StructureStreamingModel<Parameters<PROMPT_TEMPLATE["format"]>[0], // first argument of the function
9
9
  OpenAIChatSettings> {
10
10
  readonly model: OpenAIChatModel;
11
11
  readonly fnName: string;
@@ -17,7 +17,7 @@ OpenAIChatSettings> {
17
17
  fnDescription?: string;
18
18
  promptTemplate: PROMPT_TEMPLATE;
19
19
  });
20
- get modelInformation(): import("../../../index.js").ModelInformation;
20
+ get modelInformation(): import("../../index.js").ModelInformation;
21
21
  get settings(): OpenAIChatSettings;
22
22
  get settingsForEvent(): Partial<OpenAIChatSettings>;
23
23
  /**
@@ -27,11 +27,11 @@ OpenAIChatSettings> {
27
27
  /**
28
28
  * Returns this model with an instruction prompt template.
29
29
  */
30
- withInstructionPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../../index.js").InstructionPrompt, OpenAIChatPrompt>>;
30
+ withInstructionPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../index.js").InstructionPrompt, OpenAIChatPrompt>>;
31
31
  /**
32
32
  * Returns this model with a chat prompt template.
33
33
  */
34
- withChatPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../../index.js").ChatPrompt, OpenAIChatPrompt>>;
34
+ withChatPrompt(): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<import("../../index.js").ChatPrompt, 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
  /**
@@ -85,5 +85,35 @@ OpenAIChatSettings> {
85
85
  };
86
86
  }>;
87
87
  doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: Parameters<PROMPT_TEMPLATE["format"]>[0], // first argument of the function
88
- options?: FunctionOptions): Promise<AsyncIterable<import("../../../index.js").Delta<unknown>>>;
88
+ options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
89
+ object: "chat.completion.chunk";
90
+ model: string;
91
+ id: string;
92
+ created: number;
93
+ choices: {
94
+ delta: {
95
+ role?: "user" | "assistant" | undefined;
96
+ content?: string | null | undefined;
97
+ function_call?: {
98
+ name?: string | undefined;
99
+ arguments?: string | undefined;
100
+ } | undefined;
101
+ tool_calls?: {
102
+ function: {
103
+ name: string;
104
+ arguments: string;
105
+ };
106
+ type: "function";
107
+ id: string;
108
+ }[] | undefined;
109
+ };
110
+ index: number;
111
+ finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
112
+ }[];
113
+ system_fingerprint?: string | null | undefined;
114
+ } | {
115
+ object: string;
116
+ }>>>;
117
+ extractStructureTextDelta(delta: unknown): string | undefined;
118
+ parseAccumulatedStructureText(accumulatedText: string): unknown;
89
119
  }
@@ -1,5 +1,6 @@
1
1
  import SecureJSON from "secure-json-parse";
2
- import { StructureParseError } from "../../../model-function/generate-structure/StructureParseError.js";
2
+ import { StructureParseError } from "../../model-function/generate-structure/StructureParseError.js";
3
+ import { parsePartialJson } from "../../model-function/generate-structure/parsePartialJson.js";
3
4
  import { OpenAIChatResponseFormat, } from "./AbstractOpenAIChatModel.js";
4
5
  import { chat, instruction, text } from "./OpenAIChatPromptTemplate.js";
5
6
  export class OpenAIChatFunctionCallStructureGenerationModel {
@@ -126,7 +127,7 @@ export class OpenAIChatFunctionCallStructureGenerationModel {
126
127
  const expandedPrompt = this.promptTemplate.format(prompt);
127
128
  return this.model.callAPI(expandedPrompt, {
128
129
  ...options,
129
- responseFormat: OpenAIChatResponseFormat.structureDeltaIterable,
130
+ responseFormat: OpenAIChatResponseFormat.deltaIterable,
130
131
  functionCall: { name: this.fnName },
131
132
  functions: [
132
133
  {
@@ -137,4 +138,19 @@ export class OpenAIChatFunctionCallStructureGenerationModel {
137
138
  ],
138
139
  });
139
140
  }
141
+ extractStructureTextDelta(delta) {
142
+ const chunk = delta;
143
+ if (chunk.object !== "chat.completion.chunk") {
144
+ return undefined;
145
+ }
146
+ const chatChunk = chunk;
147
+ const firstChoice = chatChunk.choices[0];
148
+ if (firstChoice.index > 0) {
149
+ return undefined;
150
+ }
151
+ return firstChoice.delta.function_call?.arguments;
152
+ }
153
+ parseAccumulatedStructureText(accumulatedText) {
154
+ return parsePartialJson(accumulatedText);
155
+ }
140
156
  }
@@ -1,5 +1,5 @@
1
- import { Content } from "../../../model-function/generate-text/prompt-template/Content.js";
2
- import { ToolCall } from "../../../tool/ToolCall.js";
1
+ import { ImagePart, TextPart } from "../../model-function/generate-text/prompt-template/ContentPart.js";
2
+ import { ToolCall } from "../../tool/ToolCall.js";
3
3
  export type OpenAIChatMessage = {
4
4
  role: "system";
5
5
  content: string;
@@ -50,7 +50,7 @@ export declare const OpenAIChatMessage: {
50
50
  /**
51
51
  * Creates a user chat message. The message can be a string or a multi-modal input.
52
52
  */
53
- user(content: Content, options?: {
53
+ user(content: string | Array<TextPart | ImagePart>, options?: {
54
54
  name?: string;
55
55
  }): OpenAIChatMessage;
56
56
  /**
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpenAIChatModel = exports.calculateOpenAIChatCostInMillicents = exports.isOpenAIChatModel = exports.getOpenAIChatModelInformation = exports.OPENAI_CHAT_MODELS = void 0;
4
- const StructureFromTextStreamingModel_js_1 = require("../../../model-function/generate-structure/StructureFromTextStreamingModel.cjs");
5
- const PromptTemplateTextStreamingModel_js_1 = require("../../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
6
- const TextGenerationModel_js_1 = require("../../../model-function/generate-text/TextGenerationModel.cjs");
7
- const TikTokenTokenizer_js_1 = require("../TikTokenTokenizer.cjs");
4
+ const StructureFromTextStreamingModel_js_1 = require("../../model-function/generate-structure/StructureFromTextStreamingModel.cjs");
5
+ const PromptTemplateFullTextModel_js_1 = require("../../model-function/generate-text/PromptTemplateFullTextModel.cjs");
6
+ const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
8
7
  const AbstractOpenAIChatModel_js_1 = require("./AbstractOpenAIChatModel.cjs");
9
8
  const OpenAIChatFunctionCallStructureGenerationModel_js_1 = require("./OpenAIChatFunctionCallStructureGenerationModel.cjs");
10
9
  const OpenAIChatPromptTemplate_js_1 = require("./OpenAIChatPromptTemplate.cjs");
10
+ const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs");
11
11
  const countOpenAIChatMessageTokens_js_1 = require("./countOpenAIChatMessageTokens.cjs");
12
12
  /*
13
13
  * Available OpenAI chat models, their token limits, and pricing.
@@ -243,7 +243,7 @@ class OpenAIChatModel extends AbstractOpenAIChatModel_js_1.AbstractOpenAIChatMod
243
243
  return this.withPromptTemplate((0, OpenAIChatPromptTemplate_js_1.chat)());
244
244
  }
245
245
  withPromptTemplate(promptTemplate) {
246
- return new PromptTemplateTextStreamingModel_js_1.PromptTemplateTextStreamingModel({
246
+ return new PromptTemplateFullTextModel_js_1.PromptTemplateFullTextModel({
247
247
  model: this.withSettings({
248
248
  stopSequences: [
249
249
  ...(this.settings.stopSequences ?? []),