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
@@ -12,8 +12,7 @@ const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/gene
12
12
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
13
13
  const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
14
14
  const TextGenerationToolCallsOrGenerateTextModel_js_1 = require("../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs");
15
- const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
16
- const parseJsonStream_js_1 = require("../../util/streaming/parseJsonStream.cjs");
15
+ const createJsonStreamResponseHandler_js_1 = require("../../util/streaming/createJsonStreamResponseHandler.cjs");
17
16
  const OllamaApiConfiguration_js_1 = require("./OllamaApiConfiguration.cjs");
18
17
  const OllamaChatPromptTemplate_js_1 = require("./OllamaChatPromptTemplate.cjs");
19
18
  const OllamaError_js_1 = require("./OllamaError.cjs");
@@ -133,6 +132,10 @@ class OllamaChatModel extends AbstractModel_js_1.AbstractModel {
133
132
  responseFormat: exports.OllamaChatResponseFormat.deltaIterable,
134
133
  });
135
134
  }
135
+ extractTextDelta(delta) {
136
+ const chunk = delta;
137
+ return chunk.done === true ? undefined : chunk.message.content;
138
+ }
136
139
  asToolCallGenerationModel(promptTemplate) {
137
140
  return new TextGenerationToolCallModel_js_1.TextGenerationToolCallModel({
138
141
  model: this,
@@ -194,7 +197,7 @@ const ollamaChatResponseSchema = zod_1.z.object({
194
197
  eval_count: zod_1.z.number(),
195
198
  eval_duration: zod_1.z.number(),
196
199
  });
197
- const ollamaChatStreamSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("done", [
200
+ const ollamaChatStreamChunkSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("done", [
198
201
  zod_1.z.object({
199
202
  done: zod_1.z.literal(false),
200
203
  model: zod_1.z.string(),
@@ -216,45 +219,6 @@ const ollamaChatStreamSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminate
216
219
  eval_duration: zod_1.z.number(),
217
220
  }),
218
221
  ]));
219
- async function createOllamaFullDeltaIterableQueue(stream) {
220
- const queue = new AsyncQueue_js_1.AsyncQueue();
221
- let accumulatedText = "";
222
- // process the stream asynchonously (no 'await' on purpose):
223
- (0, parseJsonStream_js_1.parseJsonStream)({
224
- stream,
225
- schema: ollamaChatStreamSchema,
226
- process(event) {
227
- if (event.done === true) {
228
- queue.push({
229
- type: "delta",
230
- fullDelta: {
231
- content: accumulatedText,
232
- isComplete: true,
233
- delta: "",
234
- },
235
- valueDelta: "",
236
- });
237
- }
238
- else {
239
- const deltaText = event.message.content;
240
- accumulatedText += deltaText;
241
- queue.push({
242
- type: "delta",
243
- fullDelta: {
244
- content: accumulatedText,
245
- isComplete: false,
246
- delta: deltaText,
247
- },
248
- valueDelta: deltaText,
249
- });
250
- }
251
- },
252
- onDone() {
253
- queue.close();
254
- },
255
- });
256
- return queue;
257
- }
258
222
  exports.OllamaChatResponseFormat = {
259
223
  /**
260
224
  * Returns the response as a JSON object.
@@ -303,6 +267,6 @@ exports.OllamaChatResponseFormat = {
303
267
  */
304
268
  deltaIterable: {
305
269
  stream: true,
306
- handler: async ({ response }) => createOllamaFullDeltaIterableQueue(response.body),
270
+ handler: (0, createJsonStreamResponseHandler_js_1.createJsonStreamResponseHandler)(ollamaChatStreamChunkSchema),
307
271
  },
308
272
  };
@@ -2,8 +2,8 @@ import { z } from "zod";
2
2
  import { FunctionOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { ResponseHandler } from "../../core/api/postToApi.js";
5
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
5
6
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
- import { Delta } from "../../model-function/Delta.js";
7
7
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
8
8
  import { TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
9
9
  import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
@@ -57,7 +57,26 @@ export declare class OllamaChatModel extends AbstractModel<OllamaChatModelSettin
57
57
  finishReason: "unknown";
58
58
  }[];
59
59
  }>;
60
- doStreamText(prompt: OllamaChatPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
60
+ doStreamText(prompt: OllamaChatPrompt, options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
61
+ model: string;
62
+ message: {
63
+ role: string;
64
+ content: string;
65
+ };
66
+ done: false;
67
+ created_at: string;
68
+ } | {
69
+ model: string;
70
+ done: true;
71
+ created_at: string;
72
+ total_duration: number;
73
+ prompt_eval_count: number;
74
+ eval_count: number;
75
+ eval_duration: number;
76
+ load_duration?: number | undefined;
77
+ prompt_eval_duration?: number | undefined;
78
+ }>>>;
79
+ extractTextDelta(delta: unknown): string | undefined;
61
80
  asToolCallGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallPromptTemplate<INPUT_PROMPT, OllamaChatPrompt>): TextGenerationToolCallModel<INPUT_PROMPT, OllamaChatPrompt, this>;
62
81
  asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallsOrGenerateTextPromptTemplate<INPUT_PROMPT, OllamaChatPrompt>): TextGenerationToolCallsOrGenerateTextModel<INPUT_PROMPT, OllamaChatPrompt, this>;
63
82
  /**
@@ -125,11 +144,26 @@ declare const ollamaChatResponseSchema: z.ZodObject<{
125
144
  prompt_eval_duration?: number | undefined;
126
145
  }>;
127
146
  export type OllamaChatResponse = z.infer<typeof ollamaChatResponseSchema>;
128
- export type OllamaChatDelta = {
129
- content: string;
130
- isComplete: boolean;
131
- delta: string;
132
- };
147
+ declare const ollamaChatStreamChunkSchema: ZodSchema<{
148
+ model: string;
149
+ message: {
150
+ role: string;
151
+ content: string;
152
+ };
153
+ done: false;
154
+ created_at: string;
155
+ } | {
156
+ model: string;
157
+ done: true;
158
+ created_at: string;
159
+ total_duration: number;
160
+ prompt_eval_count: number;
161
+ eval_count: number;
162
+ eval_duration: number;
163
+ load_duration?: number | undefined;
164
+ prompt_eval_duration?: number | undefined;
165
+ }>;
166
+ export type OllamaChatStreamChunk = (typeof ollamaChatStreamChunkSchema)["_type"];
133
167
  export type OllamaChatResponseFormatType<T> = {
134
168
  stream: boolean;
135
169
  handler: ResponseHandler<T>;
@@ -165,10 +199,28 @@ export declare const OllamaChatResponseFormat: {
165
199
  * of the response stream.
166
200
  */
167
201
  deltaIterable: {
168
- stream: true;
202
+ stream: boolean;
169
203
  handler: ({ response }: {
170
204
  response: Response;
171
- }) => Promise<AsyncIterable<Delta<string>>>;
205
+ }) => Promise<AsyncIterable<import("../../index.js").Delta<{
206
+ model: string;
207
+ message: {
208
+ role: string;
209
+ content: string;
210
+ };
211
+ done: false;
212
+ created_at: string;
213
+ } | {
214
+ model: string;
215
+ done: true;
216
+ created_at: string;
217
+ total_duration: number;
218
+ prompt_eval_count: number;
219
+ eval_count: number;
220
+ eval_duration: number;
221
+ load_duration?: number | undefined;
222
+ prompt_eval_duration?: number | undefined;
223
+ }>>>;
172
224
  };
173
225
  };
174
226
  export {};
@@ -9,8 +9,7 @@ import { PromptTemplateTextStreamingModel } from "../../model-function/generate-
9
9
  import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
10
10
  import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
11
11
  import { TextGenerationToolCallsOrGenerateTextModel, } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
12
- import { AsyncQueue } from "../../util/AsyncQueue.js";
13
- import { parseJsonStream } from "../../util/streaming/parseJsonStream.js";
12
+ import { createJsonStreamResponseHandler } from "../../util/streaming/createJsonStreamResponseHandler.js";
14
13
  import { OllamaApiConfiguration } from "./OllamaApiConfiguration.js";
15
14
  import { chat, instruction, text } from "./OllamaChatPromptTemplate.js";
16
15
  import { failedOllamaCallResponseHandler } from "./OllamaError.js";
@@ -130,6 +129,10 @@ export class OllamaChatModel extends AbstractModel {
130
129
  responseFormat: OllamaChatResponseFormat.deltaIterable,
131
130
  });
132
131
  }
132
+ extractTextDelta(delta) {
133
+ const chunk = delta;
134
+ return chunk.done === true ? undefined : chunk.message.content;
135
+ }
133
136
  asToolCallGenerationModel(promptTemplate) {
134
137
  return new TextGenerationToolCallModel({
135
138
  model: this,
@@ -190,7 +193,7 @@ const ollamaChatResponseSchema = z.object({
190
193
  eval_count: z.number(),
191
194
  eval_duration: z.number(),
192
195
  });
193
- const ollamaChatStreamSchema = new ZodSchema(z.discriminatedUnion("done", [
196
+ const ollamaChatStreamChunkSchema = new ZodSchema(z.discriminatedUnion("done", [
194
197
  z.object({
195
198
  done: z.literal(false),
196
199
  model: z.string(),
@@ -212,45 +215,6 @@ const ollamaChatStreamSchema = new ZodSchema(z.discriminatedUnion("done", [
212
215
  eval_duration: z.number(),
213
216
  }),
214
217
  ]));
215
- async function createOllamaFullDeltaIterableQueue(stream) {
216
- const queue = new AsyncQueue();
217
- let accumulatedText = "";
218
- // process the stream asynchonously (no 'await' on purpose):
219
- parseJsonStream({
220
- stream,
221
- schema: ollamaChatStreamSchema,
222
- process(event) {
223
- if (event.done === true) {
224
- queue.push({
225
- type: "delta",
226
- fullDelta: {
227
- content: accumulatedText,
228
- isComplete: true,
229
- delta: "",
230
- },
231
- valueDelta: "",
232
- });
233
- }
234
- else {
235
- const deltaText = event.message.content;
236
- accumulatedText += deltaText;
237
- queue.push({
238
- type: "delta",
239
- fullDelta: {
240
- content: accumulatedText,
241
- isComplete: false,
242
- delta: deltaText,
243
- },
244
- valueDelta: deltaText,
245
- });
246
- }
247
- },
248
- onDone() {
249
- queue.close();
250
- },
251
- });
252
- return queue;
253
- }
254
218
  export const OllamaChatResponseFormat = {
255
219
  /**
256
220
  * Returns the response as a JSON object.
@@ -299,6 +263,6 @@ export const OllamaChatResponseFormat = {
299
263
  */
300
264
  deltaIterable: {
301
265
  stream: true,
302
- handler: async ({ response }) => createOllamaFullDeltaIterableQueue(response.body),
266
+ handler: createJsonStreamResponseHandler(ollamaChatStreamChunkSchema),
303
267
  },
304
268
  };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const streamText_js_1 = require("../../model-function/generate-text/streamText.cjs");
4
+ const StreamingTestServer_js_1 = require("../../test/StreamingTestServer.cjs");
5
+ const arrayFromAsync_js_1 = require("../../test/arrayFromAsync.cjs");
6
+ const OllamaChatModel_js_1 = require("./OllamaChatModel.cjs");
7
+ describe("streamText", () => {
8
+ const server = new StreamingTestServer_js_1.StreamingTestServer("http://127.0.0.1:11434/api/chat");
9
+ server.setupTestEnvironment();
10
+ it("should return a text stream", async () => {
11
+ server.responseChunks = [
12
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:17.948267Z","message":{"role":"assistant","content":"Hello"},"done":false}\n`,
13
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:17.948267Z","message":{"role":"assistant","content":", "},"done":false}\n`,
14
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:17.948267Z","message":{"role":"assistant","content":"world!"},"done":false}\n`,
15
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:19.927399Z","message":{"role":"assistant","content":""},` +
16
+ `"done":true,"total_duration":4843619375,"load_duration":1101458,"prompt_eval_count":5,"prompt_eval_duration":199339000,` +
17
+ `"eval_count":317,"eval_duration":4639772000}\n`,
18
+ ];
19
+ const stream = await (0, streamText_js_1.streamText)(new OllamaChatModel_js_1.OllamaChatModel({ model: "mistral:text" }).withTextPrompt(), "hello");
20
+ // note: space moved to last chunk bc of trimming
21
+ expect(await (0, arrayFromAsync_js_1.arrayFromAsync)(stream)).toStrictEqual([
22
+ "Hello",
23
+ ",",
24
+ " world!",
25
+ ]);
26
+ });
27
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ import { streamText } from "../../model-function/generate-text/streamText.js";
2
+ import { StreamingTestServer } from "../../test/StreamingTestServer.js";
3
+ import { arrayFromAsync } from "../../test/arrayFromAsync.js";
4
+ import { OllamaChatModel } from "./OllamaChatModel.js";
5
+ describe("streamText", () => {
6
+ const server = new StreamingTestServer("http://127.0.0.1:11434/api/chat");
7
+ server.setupTestEnvironment();
8
+ it("should return a text stream", async () => {
9
+ server.responseChunks = [
10
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:17.948267Z","message":{"role":"assistant","content":"Hello"},"done":false}\n`,
11
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:17.948267Z","message":{"role":"assistant","content":", "},"done":false}\n`,
12
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:17.948267Z","message":{"role":"assistant","content":"world!"},"done":false}\n`,
13
+ `{"model":"mistral:text","created_at":"2023-12-24T16:49:19.927399Z","message":{"role":"assistant","content":""},` +
14
+ `"done":true,"total_duration":4843619375,"load_duration":1101458,"prompt_eval_count":5,"prompt_eval_duration":199339000,` +
15
+ `"eval_count":317,"eval_duration":4639772000}\n`,
16
+ ];
17
+ const stream = await streamText(new OllamaChatModel({ model: "mistral:text" }).withTextPrompt(), "hello");
18
+ // note: space moved to last chunk bc of trimming
19
+ expect(await arrayFromAsync(stream)).toStrictEqual([
20
+ "Hello",
21
+ ",",
22
+ " world!",
23
+ ]);
24
+ });
25
+ });
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.chat = exports.instruction = exports.text = exports.identity = void 0;
4
+ const ContentPart_js_1 = require("../../model-function/generate-text/prompt-template/ContentPart.cjs");
5
+ const InvalidPromptError_js_1 = require("../../model-function/generate-text/prompt-template/InvalidPromptError.cjs");
4
6
  /**
5
7
  * OllamaChatPrompt identity chat format.
6
8
  */
@@ -26,9 +28,15 @@ function instruction() {
26
28
  format(prompt) {
27
29
  const messages = [];
28
30
  if (prompt.system != null) {
29
- messages.push({ role: "system", content: prompt.system });
31
+ messages.push({
32
+ role: "system",
33
+ content: prompt.system,
34
+ });
30
35
  }
31
- messages.push({ role: "user", ...extractContent(prompt.instruction) });
36
+ messages.push({
37
+ role: "user",
38
+ ...extractUserContent(prompt.instruction),
39
+ });
32
40
  return messages;
33
41
  },
34
42
  stopSequences: [],
@@ -46,7 +54,29 @@ function chat() {
46
54
  messages.push({ role: "system", content: prompt.system });
47
55
  }
48
56
  for (const { role, content } of prompt.messages) {
49
- messages.push({ role, ...extractContent(content) });
57
+ switch (role) {
58
+ case "user": {
59
+ messages.push({
60
+ role: "user",
61
+ ...extractUserContent(content),
62
+ });
63
+ break;
64
+ }
65
+ case "assistant": {
66
+ messages.push({
67
+ role: "assistant",
68
+ content: (0, ContentPart_js_1.validateContentIsString)(content, prompt),
69
+ });
70
+ break;
71
+ }
72
+ case "tool": {
73
+ throw new InvalidPromptError_js_1.InvalidPromptError("Tool messages are not supported.", prompt);
74
+ }
75
+ default: {
76
+ const _exhaustiveCheck = role;
77
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
78
+ }
79
+ }
50
80
  }
51
81
  return messages;
52
82
  },
@@ -54,7 +84,7 @@ function chat() {
54
84
  };
55
85
  }
56
86
  exports.chat = chat;
57
- function extractContent(input) {
87
+ function extractUserContent(input) {
58
88
  if (typeof input === "string") {
59
89
  return { content: input, images: undefined };
60
90
  }
@@ -1,3 +1,5 @@
1
+ import { validateContentIsString } from "../../model-function/generate-text/prompt-template/ContentPart.js";
2
+ import { InvalidPromptError } from "../../model-function/generate-text/prompt-template/InvalidPromptError.js";
1
3
  /**
2
4
  * OllamaChatPrompt identity chat format.
3
5
  */
@@ -21,9 +23,15 @@ export function instruction() {
21
23
  format(prompt) {
22
24
  const messages = [];
23
25
  if (prompt.system != null) {
24
- messages.push({ role: "system", content: prompt.system });
26
+ messages.push({
27
+ role: "system",
28
+ content: prompt.system,
29
+ });
25
30
  }
26
- messages.push({ role: "user", ...extractContent(prompt.instruction) });
31
+ messages.push({
32
+ role: "user",
33
+ ...extractUserContent(prompt.instruction),
34
+ });
27
35
  return messages;
28
36
  },
29
37
  stopSequences: [],
@@ -40,14 +48,36 @@ export function chat() {
40
48
  messages.push({ role: "system", content: prompt.system });
41
49
  }
42
50
  for (const { role, content } of prompt.messages) {
43
- messages.push({ role, ...extractContent(content) });
51
+ switch (role) {
52
+ case "user": {
53
+ messages.push({
54
+ role: "user",
55
+ ...extractUserContent(content),
56
+ });
57
+ break;
58
+ }
59
+ case "assistant": {
60
+ messages.push({
61
+ role: "assistant",
62
+ content: validateContentIsString(content, prompt),
63
+ });
64
+ break;
65
+ }
66
+ case "tool": {
67
+ throw new InvalidPromptError("Tool messages are not supported.", prompt);
68
+ }
69
+ default: {
70
+ const _exhaustiveCheck = role;
71
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
72
+ }
73
+ }
44
74
  }
45
75
  return messages;
46
76
  },
47
77
  stopSequences: [],
48
78
  };
49
79
  }
50
- function extractContent(input) {
80
+ function extractUserContent(input) {
51
81
  if (typeof input === "string") {
52
82
  return { content: input, images: undefined };
53
83
  }
@@ -12,8 +12,7 @@ const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/gene
12
12
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
13
13
  const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
14
14
  const TextGenerationToolCallsOrGenerateTextModel_js_1 = require("../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs");
15
- const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
16
- const parseJsonStream_js_1 = require("../../util/streaming/parseJsonStream.cjs");
15
+ const createJsonStreamResponseHandler_js_1 = require("../../util/streaming/createJsonStreamResponseHandler.cjs");
17
16
  const OllamaApiConfiguration_js_1 = require("./OllamaApiConfiguration.cjs");
18
17
  const OllamaError_js_1 = require("./OllamaError.cjs");
19
18
  class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
@@ -135,6 +134,10 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
135
134
  responseFormat: exports.OllamaCompletionResponseFormat.deltaIterable,
136
135
  });
137
136
  }
137
+ extractTextDelta(delta) {
138
+ const chunk = delta;
139
+ return chunk.done === true ? undefined : chunk.response;
140
+ }
138
141
  asToolCallGenerationModel(promptTemplate) {
139
142
  return new TextGenerationToolCallModel_js_1.TextGenerationToolCallModel({
140
143
  model: this,
@@ -150,11 +153,25 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
150
153
  withTextPrompt() {
151
154
  return this.withPromptTemplate({
152
155
  format(prompt) {
153
- return { prompt: prompt };
156
+ return { prompt };
154
157
  },
155
158
  stopSequences: [],
156
159
  });
157
160
  }
161
+ /**
162
+ * Maps the prompt for a text version of the Ollama completion prompt template (without image support).
163
+ */
164
+ withTextPromptTemplate(promptTemplate) {
165
+ return new PromptTemplateTextStreamingModel_js_1.PromptTemplateTextStreamingModel({
166
+ model: this.withTextPrompt().withSettings({
167
+ stopSequences: [
168
+ ...(this.settings.stopSequences ?? []),
169
+ ...promptTemplate.stopSequences,
170
+ ],
171
+ }),
172
+ promptTemplate,
173
+ });
174
+ }
158
175
  withPromptTemplate(promptTemplate) {
159
176
  return new PromptTemplateTextStreamingModel_js_1.PromptTemplateTextStreamingModel({
160
177
  model: this.withSettings({
@@ -184,7 +201,7 @@ const ollamaCompletionResponseSchema = zod_1.z.object({
184
201
  eval_duration: zod_1.z.number(),
185
202
  context: zod_1.z.array(zod_1.z.number()).optional(),
186
203
  });
187
- const ollamaCompletionStreamSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("done", [
204
+ const ollamaCompletionStreamChunkSchema = (0, ZodSchema_js_1.zodSchema)(zod_1.z.discriminatedUnion("done", [
188
205
  zod_1.z.object({
189
206
  done: zod_1.z.literal(false),
190
207
  model: zod_1.z.string(),
@@ -206,44 +223,6 @@ const ollamaCompletionStreamSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discri
206
223
  context: zod_1.z.array(zod_1.z.number()).optional(),
207
224
  }),
208
225
  ]));
209
- async function createOllamaFullDeltaIterableQueue(stream) {
210
- const queue = new AsyncQueue_js_1.AsyncQueue();
211
- let accumulatedText = "";
212
- // process the stream asynchonously (no 'await' on purpose):
213
- (0, parseJsonStream_js_1.parseJsonStream)({
214
- stream,
215
- schema: ollamaCompletionStreamSchema,
216
- process(event) {
217
- if (event.done === true) {
218
- queue.push({
219
- type: "delta",
220
- fullDelta: {
221
- content: accumulatedText,
222
- isComplete: true,
223
- delta: "",
224
- },
225
- valueDelta: "",
226
- });
227
- }
228
- else {
229
- accumulatedText += event.response;
230
- queue.push({
231
- type: "delta",
232
- fullDelta: {
233
- content: accumulatedText,
234
- isComplete: false,
235
- delta: event.response,
236
- },
237
- valueDelta: event.response,
238
- });
239
- }
240
- },
241
- onDone() {
242
- queue.close();
243
- },
244
- });
245
- return queue;
246
- }
247
226
  exports.OllamaCompletionResponseFormat = {
248
227
  /**
249
228
  * Returns the response as a JSON object.
@@ -293,6 +272,6 @@ exports.OllamaCompletionResponseFormat = {
293
272
  */
294
273
  deltaIterable: {
295
274
  stream: true,
296
- handler: async ({ response }) => createOllamaFullDeltaIterableQueue(response.body),
275
+ handler: (0, createJsonStreamResponseHandler_js_1.createJsonStreamResponseHandler)(ollamaCompletionStreamChunkSchema),
297
276
  },
298
277
  };
@@ -2,8 +2,8 @@ import { z } from "zod";
2
2
  import { FunctionOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { ResponseHandler } from "../../core/api/postToApi.js";
5
+ import { ZodSchema } from "../../core/schema/ZodSchema.js";
5
6
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
- import { Delta } from "../../model-function/Delta.js";
7
7
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
8
8
  import { TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
9
9
  import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
@@ -70,10 +70,33 @@ export declare class OllamaCompletionModel<CONTEXT_WINDOW_SIZE extends number |
70
70
  finishReason: "unknown";
71
71
  }[];
72
72
  }>;
73
- doStreamText(prompt: OllamaCompletionPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
73
+ doStreamText(prompt: OllamaCompletionPrompt, options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
74
+ response: string;
75
+ model: string;
76
+ done: false;
77
+ created_at: string;
78
+ } | {
79
+ model: string;
80
+ done: true;
81
+ created_at: string;
82
+ total_duration: number;
83
+ prompt_eval_count: number;
84
+ eval_count: number;
85
+ eval_duration: number;
86
+ load_duration?: number | undefined;
87
+ sample_count?: number | undefined;
88
+ sample_duration?: number | undefined;
89
+ prompt_eval_duration?: number | undefined;
90
+ context?: number[] | undefined;
91
+ }>>>;
92
+ extractTextDelta(delta: unknown): string | undefined;
74
93
  asToolCallGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallPromptTemplate<INPUT_PROMPT, OllamaCompletionPrompt>): TextGenerationToolCallModel<INPUT_PROMPT, OllamaCompletionPrompt, this>;
75
94
  asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallsOrGenerateTextPromptTemplate<INPUT_PROMPT, OllamaCompletionPrompt>): TextGenerationToolCallsOrGenerateTextModel<INPUT_PROMPT, OllamaCompletionPrompt, this>;
76
95
  withTextPrompt(): PromptTemplateTextStreamingModel<string, OllamaCompletionPrompt, OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>, this>;
96
+ /**
97
+ * Maps the prompt for a text version of the Ollama completion prompt template (without image support).
98
+ */
99
+ withTextPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextStreamingModel<INPUT_PROMPT, string, OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>, PromptTemplateTextStreamingModel<string, OllamaCompletionPrompt, OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>, this>>;
77
100
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, OllamaCompletionPrompt>): PromptTemplateTextStreamingModel<INPUT_PROMPT, OllamaCompletionPrompt, OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>, this>;
78
101
  withSettings(additionalSettings: Partial<OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>>): this;
79
102
  }
@@ -115,11 +138,26 @@ declare const ollamaCompletionResponseSchema: z.ZodObject<{
115
138
  context?: number[] | undefined;
116
139
  }>;
117
140
  export type OllamaCompletionResponse = z.infer<typeof ollamaCompletionResponseSchema>;
118
- export type OllamaCompletionDelta = {
119
- content: string;
120
- isComplete: boolean;
121
- delta: string;
122
- };
141
+ declare const ollamaCompletionStreamChunkSchema: ZodSchema<{
142
+ response: string;
143
+ model: string;
144
+ done: false;
145
+ created_at: string;
146
+ } | {
147
+ model: string;
148
+ done: true;
149
+ created_at: string;
150
+ total_duration: number;
151
+ prompt_eval_count: number;
152
+ eval_count: number;
153
+ eval_duration: number;
154
+ load_duration?: number | undefined;
155
+ sample_count?: number | undefined;
156
+ sample_duration?: number | undefined;
157
+ prompt_eval_duration?: number | undefined;
158
+ context?: number[] | undefined;
159
+ }>;
160
+ export type OllamaCompletionStreamChunk = (typeof ollamaCompletionStreamChunkSchema)["_type"];
123
161
  export type OllamaCompletionResponseFormatType<T> = {
124
162
  stream: boolean;
125
163
  handler: ResponseHandler<T>;
@@ -153,10 +191,28 @@ export declare const OllamaCompletionResponseFormat: {
153
191
  * of the response stream.
154
192
  */
155
193
  deltaIterable: {
156
- stream: true;
194
+ stream: boolean;
157
195
  handler: ({ response }: {
158
196
  response: Response;
159
- }) => Promise<AsyncIterable<Delta<string>>>;
197
+ }) => Promise<AsyncIterable<import("../../index.js").Delta<{
198
+ response: string;
199
+ model: string;
200
+ done: false;
201
+ created_at: string;
202
+ } | {
203
+ model: string;
204
+ done: true;
205
+ created_at: string;
206
+ total_duration: number;
207
+ prompt_eval_count: number;
208
+ eval_count: number;
209
+ eval_duration: number;
210
+ load_duration?: number | undefined;
211
+ sample_count?: number | undefined;
212
+ sample_duration?: number | undefined;
213
+ prompt_eval_duration?: number | undefined;
214
+ context?: number[] | undefined;
215
+ }>>>;
160
216
  };
161
217
  };
162
218
  export {};