modelfusion 0.103.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 (181) hide show
  1. package/CHANGELOG.md +56 -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 -1
  26. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.d.ts +2 -2
  27. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.js +2 -1
  28. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.cjs +9 -5
  29. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.d.ts +4 -4
  30. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.js +9 -5
  31. package/model-function/generate-text/prompt-template/ChatPrompt.cjs +38 -20
  32. package/model-function/generate-text/prompt-template/ChatPrompt.d.ts +33 -34
  33. package/model-function/generate-text/prompt-template/ChatPrompt.js +37 -18
  34. package/model-function/generate-text/prompt-template/ContentPart.cjs +11 -0
  35. package/model-function/generate-text/prompt-template/ContentPart.d.ts +30 -0
  36. package/model-function/generate-text/prompt-template/ContentPart.js +7 -0
  37. package/model-function/generate-text/prompt-template/InstructionPrompt.d.ts +7 -22
  38. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +40 -6
  39. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.d.ts +16 -4
  40. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +38 -5
  41. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.cjs +10 -5
  42. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.d.ts +4 -4
  43. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.js +10 -5
  44. package/model-function/generate-text/prompt-template/TextPromptTemplate.cjs +8 -5
  45. package/model-function/generate-text/prompt-template/TextPromptTemplate.d.ts +4 -4
  46. package/model-function/generate-text/prompt-template/TextPromptTemplate.js +8 -5
  47. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.cjs +8 -4
  48. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.d.ts +2 -2
  49. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.js +8 -4
  50. package/model-function/generate-text/prompt-template/index.cjs +1 -1
  51. package/model-function/generate-text/prompt-template/index.d.ts +1 -1
  52. package/model-function/generate-text/prompt-template/index.js +1 -1
  53. package/model-function/generate-text/prompt-template/trimChatPrompt.cjs +0 -2
  54. package/model-function/generate-text/prompt-template/trimChatPrompt.d.ts +4 -4
  55. package/model-function/generate-text/prompt-template/trimChatPrompt.js +0 -2
  56. package/model-function/generate-text/streamText.cjs +27 -28
  57. package/model-function/generate-text/streamText.d.ts +1 -0
  58. package/model-function/generate-text/streamText.js +27 -28
  59. package/model-provider/anthropic/AnthropicPromptTemplate.cjs +9 -4
  60. package/model-provider/anthropic/AnthropicPromptTemplate.d.ts +4 -4
  61. package/model-provider/anthropic/AnthropicPromptTemplate.js +9 -4
  62. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +8 -14
  63. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +13 -4
  64. package/model-provider/anthropic/AnthropicTextGenerationModel.js +8 -14
  65. package/model-provider/anthropic/AnthropicTextGenerationModel.test.cjs +44 -0
  66. package/model-provider/anthropic/AnthropicTextGenerationModel.test.js +42 -0
  67. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +1 -1
  68. package/model-provider/cohere/CohereTextGenerationModel.cjs +6 -44
  69. package/model-provider/cohere/CohereTextGenerationModel.d.ts +47 -13
  70. package/model-provider/cohere/CohereTextGenerationModel.js +7 -45
  71. package/model-provider/cohere/CohereTextGenerationModel.test.cjs +33 -0
  72. package/model-provider/cohere/CohereTextGenerationModel.test.js +31 -0
  73. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +1 -2
  74. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +1 -2
  75. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.cjs +29 -17
  76. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.d.ts +4 -4
  77. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.js +29 -17
  78. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +7 -14
  79. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +157 -6
  80. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +8 -15
  81. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.cjs +37 -0
  82. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.d.ts +1 -0
  83. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.js +35 -0
  84. package/model-provider/mistral/MistralChatModel.cjs +30 -104
  85. package/model-provider/mistral/MistralChatModel.d.ts +49 -16
  86. package/model-provider/mistral/MistralChatModel.js +30 -104
  87. package/model-provider/mistral/MistralChatModel.test.cjs +51 -0
  88. package/model-provider/mistral/MistralChatModel.test.d.ts +1 -0
  89. package/model-provider/mistral/MistralChatModel.test.js +49 -0
  90. package/model-provider/mistral/MistralPromptTemplate.cjs +13 -5
  91. package/model-provider/mistral/MistralPromptTemplate.d.ts +4 -4
  92. package/model-provider/mistral/MistralPromptTemplate.js +13 -5
  93. package/model-provider/ollama/OllamaChatModel.cjs +7 -43
  94. package/model-provider/ollama/OllamaChatModel.d.ts +63 -11
  95. package/model-provider/ollama/OllamaChatModel.js +7 -43
  96. package/model-provider/ollama/OllamaChatModel.test.cjs +27 -0
  97. package/model-provider/ollama/OllamaChatModel.test.d.ts +1 -0
  98. package/model-provider/ollama/OllamaChatModel.test.js +25 -0
  99. package/model-provider/ollama/OllamaChatPromptTemplate.cjs +43 -17
  100. package/model-provider/ollama/OllamaChatPromptTemplate.d.ts +4 -4
  101. package/model-provider/ollama/OllamaChatPromptTemplate.js +43 -17
  102. package/model-provider/ollama/OllamaCompletionModel.cjs +22 -43
  103. package/model-provider/ollama/OllamaCompletionModel.d.ts +65 -9
  104. package/model-provider/ollama/OllamaCompletionModel.js +23 -44
  105. package/model-provider/ollama/OllamaCompletionModel.test.cjs +101 -13
  106. package/model-provider/ollama/OllamaCompletionModel.test.js +78 -13
  107. package/model-provider/openai/{chat/AbstractOpenAIChatModel.cjs → AbstractOpenAIChatModel.cjs} +71 -15
  108. package/model-provider/openai/{chat/AbstractOpenAIChatModel.d.ts → AbstractOpenAIChatModel.d.ts} +273 -19
  109. package/model-provider/openai/{chat/AbstractOpenAIChatModel.js → AbstractOpenAIChatModel.js} +71 -15
  110. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.cjs → OpenAIChatFunctionCallStructureGenerationModel.cjs} +18 -2
  111. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts → OpenAIChatFunctionCallStructureGenerationModel.d.ts} +41 -11
  112. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.js → OpenAIChatFunctionCallStructureGenerationModel.js} +18 -2
  113. package/model-provider/openai/{chat/OpenAIChatMessage.d.ts → OpenAIChatMessage.d.ts} +3 -3
  114. package/model-provider/openai/{chat/OpenAIChatModel.cjs → OpenAIChatModel.cjs} +5 -5
  115. package/model-provider/openai/{chat/OpenAIChatModel.d.ts → OpenAIChatModel.d.ts} +12 -12
  116. package/model-provider/openai/{chat/OpenAIChatModel.js → OpenAIChatModel.js} +5 -5
  117. package/model-provider/openai/OpenAIChatModel.test.cjs +94 -0
  118. package/model-provider/openai/OpenAIChatModel.test.d.ts +1 -0
  119. package/model-provider/openai/OpenAIChatModel.test.js +92 -0
  120. package/model-provider/openai/OpenAIChatPromptTemplate.cjs +114 -0
  121. package/model-provider/openai/OpenAIChatPromptTemplate.d.ts +20 -0
  122. package/model-provider/openai/OpenAIChatPromptTemplate.js +107 -0
  123. package/model-provider/openai/OpenAICompletionModel.cjs +32 -84
  124. package/model-provider/openai/OpenAICompletionModel.d.ts +29 -12
  125. package/model-provider/openai/OpenAICompletionModel.js +33 -85
  126. package/model-provider/openai/OpenAICompletionModel.test.cjs +53 -0
  127. package/model-provider/openai/OpenAICompletionModel.test.d.ts +1 -0
  128. package/model-provider/openai/OpenAICompletionModel.test.js +51 -0
  129. package/model-provider/openai/OpenAICostCalculator.cjs +1 -1
  130. package/model-provider/openai/OpenAICostCalculator.js +1 -1
  131. package/model-provider/openai/OpenAIFacade.cjs +2 -2
  132. package/model-provider/openai/OpenAIFacade.d.ts +3 -3
  133. package/model-provider/openai/OpenAIFacade.js +2 -2
  134. package/model-provider/openai/OpenAITranscriptionModel.d.ts +6 -6
  135. package/model-provider/openai/TikTokenTokenizer.d.ts +1 -1
  136. package/model-provider/openai/{chat/countOpenAIChatMessageTokens.cjs → countOpenAIChatMessageTokens.cjs} +2 -2
  137. package/model-provider/openai/{chat/countOpenAIChatMessageTokens.js → countOpenAIChatMessageTokens.js} +2 -2
  138. package/model-provider/openai/index.cjs +6 -6
  139. package/model-provider/openai/index.d.ts +5 -6
  140. package/model-provider/openai/index.js +5 -5
  141. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +4 -4
  142. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +6 -6
  143. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +4 -4
  144. package/model-provider/stability/StabilityImageGenerationModel.d.ts +1 -1
  145. package/package.json +5 -5
  146. package/test/JsonTestServer.cjs +33 -0
  147. package/test/JsonTestServer.d.ts +7 -0
  148. package/test/JsonTestServer.js +29 -0
  149. package/test/StreamingTestServer.cjs +55 -0
  150. package/test/StreamingTestServer.d.ts +7 -0
  151. package/test/StreamingTestServer.js +51 -0
  152. package/test/arrayFromAsync.cjs +13 -0
  153. package/test/arrayFromAsync.d.ts +1 -0
  154. package/test/arrayFromAsync.js +9 -0
  155. package/util/streaming/createEventSourceResponseHandler.cjs +9 -0
  156. package/util/streaming/createEventSourceResponseHandler.d.ts +4 -0
  157. package/util/streaming/createEventSourceResponseHandler.js +5 -0
  158. package/util/streaming/createJsonStreamResponseHandler.cjs +9 -0
  159. package/util/streaming/createJsonStreamResponseHandler.d.ts +4 -0
  160. package/util/streaming/createJsonStreamResponseHandler.js +5 -0
  161. package/util/streaming/parseEventSourceStreamAsAsyncIterable.cjs +52 -0
  162. package/util/streaming/parseEventSourceStreamAsAsyncIterable.d.ts +6 -0
  163. package/util/streaming/parseEventSourceStreamAsAsyncIterable.js +48 -0
  164. package/util/streaming/parseJsonStreamAsAsyncIterable.cjs +21 -0
  165. package/util/streaming/parseJsonStreamAsAsyncIterable.d.ts +6 -0
  166. package/util/streaming/parseJsonStreamAsAsyncIterable.js +17 -0
  167. package/model-function/generate-text/prompt-template/Content.cjs +0 -2
  168. package/model-function/generate-text/prompt-template/Content.d.ts +0 -20
  169. package/model-provider/openai/chat/OpenAIChatModel.test.cjs +0 -61
  170. package/model-provider/openai/chat/OpenAIChatModel.test.js +0 -59
  171. package/model-provider/openai/chat/OpenAIChatPromptTemplate.cjs +0 -72
  172. package/model-provider/openai/chat/OpenAIChatPromptTemplate.d.ts +0 -20
  173. package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +0 -65
  174. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +0 -156
  175. package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +0 -19
  176. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +0 -152
  177. /package/{model-function/generate-text/prompt-template/Content.js → model-provider/anthropic/AnthropicTextGenerationModel.test.d.ts} +0 -0
  178. /package/model-provider/{openai/chat/OpenAIChatModel.test.d.ts → cohere/CohereTextGenerationModel.test.d.ts} +0 -0
  179. /package/model-provider/openai/{chat/OpenAIChatMessage.cjs → OpenAIChatMessage.cjs} +0 -0
  180. /package/model-provider/openai/{chat/OpenAIChatMessage.js → OpenAIChatMessage.js} +0 -0
  181. /package/model-provider/openai/{chat/countOpenAIChatMessageTokens.d.ts → countOpenAIChatMessageTokens.d.ts} +0 -0
@@ -0,0 +1,33 @@
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 CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
7
+ const CohereTextGenerationModel_js_1 = require("./CohereTextGenerationModel.cjs");
8
+ describe("streamText", () => {
9
+ const server = new StreamingTestServer_js_1.StreamingTestServer("https://api.cohere.ai/v1/generate");
10
+ server.setupTestEnvironment();
11
+ it("should return a text stream", async () => {
12
+ server.responseChunks = [
13
+ `{"text":"Hello","is_finished":false}\n`,
14
+ `{"text":", ","is_finished":false}\n`,
15
+ `{"text":"world!","is_finished":false}\n`,
16
+ `{"is_finished":true,"finish_reason":"COMPLETE",` +
17
+ `"response":{"id":"40141e4f-2202-4e09-9188-c6ece324b7ba",` +
18
+ `"generations":[{"id":"c598f9d2-4a6d-46d6-beed-47d55b996540",` +
19
+ `"text":"Hello, world!","finish_reason":"COMPLETE"}],` +
20
+ `"prompt":"hello"}}\n`,
21
+ ];
22
+ const stream = await (0, streamText_js_1.streamText)(new CohereTextGenerationModel_js_1.CohereTextGenerationModel({
23
+ api: new CohereApiConfiguration_js_1.CohereApiConfiguration({ apiKey: "test-key" }),
24
+ model: "command-light",
25
+ }), "hello");
26
+ // note: space moved to last chunk bc of trimming
27
+ expect(await (0, arrayFromAsync_js_1.arrayFromAsync)(stream)).toStrictEqual([
28
+ "Hello",
29
+ ",",
30
+ " world!",
31
+ ]);
32
+ });
33
+ });
@@ -0,0 +1,31 @@
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 { CohereApiConfiguration } from "./CohereApiConfiguration.js";
5
+ import { CohereTextGenerationModel } from "./CohereTextGenerationModel.js";
6
+ describe("streamText", () => {
7
+ const server = new StreamingTestServer("https://api.cohere.ai/v1/generate");
8
+ server.setupTestEnvironment();
9
+ it("should return a text stream", async () => {
10
+ server.responseChunks = [
11
+ `{"text":"Hello","is_finished":false}\n`,
12
+ `{"text":", ","is_finished":false}\n`,
13
+ `{"text":"world!","is_finished":false}\n`,
14
+ `{"is_finished":true,"finish_reason":"COMPLETE",` +
15
+ `"response":{"id":"40141e4f-2202-4e09-9188-c6ece324b7ba",` +
16
+ `"generations":[{"id":"c598f9d2-4a6d-46d6-beed-47d55b996540",` +
17
+ `"text":"Hello, world!","finish_reason":"COMPLETE"}],` +
18
+ `"prompt":"hello"}}\n`,
19
+ ];
20
+ const stream = await streamText(new CohereTextGenerationModel({
21
+ api: new CohereApiConfiguration({ apiKey: "test-key" }),
22
+ model: "command-light",
23
+ }), "hello");
24
+ // note: space moved to last chunk bc of trimming
25
+ expect(await arrayFromAsync(stream)).toStrictEqual([
26
+ "Hello",
27
+ ",",
28
+ " world!",
29
+ ]);
30
+ });
31
+ });
@@ -149,8 +149,7 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
149
149
  if (!response.isFinal) {
150
150
  queue.push({
151
151
  type: "delta",
152
- fullDelta: event,
153
- valueDelta: Buffer.from(response.audio, "base64"),
152
+ deltaValue: Buffer.from(response.audio, "base64"),
154
153
  });
155
154
  }
156
155
  };
@@ -146,8 +146,7 @@ export class ElevenLabsSpeechModel extends AbstractModel {
146
146
  if (!response.isFinal) {
147
147
  queue.push({
148
148
  type: "delta",
149
- fullDelta: event,
150
- valueDelta: Buffer.from(response.audio, "base64"),
149
+ deltaValue: Buffer.from(response.audio, "base64"),
151
150
  });
152
151
  }
153
152
  };
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.chat = exports.instruction = void 0;
4
- const ChatPrompt_js_1 = require("../../model-function/generate-text/prompt-template/ChatPrompt.cjs");
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");
5
6
  // default Vicuna 1 system message
6
7
  const DEFAULT_SYSTEM_MESSAGE = "A chat between a curious user and an artificial intelligence assistant. " +
7
8
  "The assistant gives helpful, detailed, and polite answers to the user's questions.";
@@ -16,23 +17,28 @@ function instruction() {
16
17
  let text = "";
17
18
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
18
19
  text += `USER: `;
19
- // construct text and image mapping:
20
- let imageCounter = 1;
21
20
  const images = {};
22
- for (const content of prompt.instruction) {
23
- switch (content.type) {
24
- case "text": {
25
- text += content.text;
26
- break;
27
- }
28
- case "image": {
29
- text += `[img-${imageCounter}]`;
30
- images[imageCounter.toString()] = content.base64Image;
31
- imageCounter++;
32
- break;
21
+ if (typeof prompt.instruction === "string") {
22
+ text += `${prompt.instruction}\n`;
23
+ }
24
+ else {
25
+ // construct text and image mapping:
26
+ let imageCounter = 1;
27
+ for (const content of prompt.instruction) {
28
+ switch (content.type) {
29
+ case "text": {
30
+ text += content.text;
31
+ break;
32
+ }
33
+ case "image": {
34
+ text += `[img-${imageCounter}]`;
35
+ images[imageCounter.toString()] = content.base64Image;
36
+ imageCounter++;
37
+ break;
38
+ }
33
39
  }
40
+ text += `${content}\n`;
34
41
  }
35
- text += `${content}\n`;
36
42
  }
37
43
  text += `\nASSISTANT: `;
38
44
  return { text, images };
@@ -44,7 +50,6 @@ exports.instruction = instruction;
44
50
  function chat() {
45
51
  return {
46
52
  format(prompt) {
47
- (0, ChatPrompt_js_1.validateChatPrompt)(prompt);
48
53
  let text = "";
49
54
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
50
55
  // construct text and image mapping:
@@ -54,6 +59,10 @@ function chat() {
54
59
  switch (role) {
55
60
  case "user": {
56
61
  text += `USER: `;
62
+ if (typeof content === "string") {
63
+ text += content;
64
+ break;
65
+ }
57
66
  for (const part of content) {
58
67
  switch (part.type) {
59
68
  case "text": {
@@ -71,9 +80,12 @@ function chat() {
71
80
  break;
72
81
  }
73
82
  case "assistant": {
74
- text += `ASSISTANT: ${content}`;
83
+ text += `ASSISTANT: ${(0, ContentPart_js_1.validateContentIsString)(content, prompt)}`;
75
84
  break;
76
85
  }
86
+ case "tool": {
87
+ throw new InvalidPromptError_js_1.InvalidPromptError("Tool messages are not supported.", prompt);
88
+ }
77
89
  default: {
78
90
  const _exhaustiveCheck = role;
79
91
  throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
@@ -1,11 +1,11 @@
1
1
  import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
2
- import { MultiModalChatPrompt } from "../../model-function/generate-text/prompt-template/ChatPrompt.js";
3
- import { MultiModalInstructionPrompt } from "../../model-function/generate-text/prompt-template/InstructionPrompt.js";
2
+ import { ChatPrompt } from "../../model-function/generate-text/prompt-template/ChatPrompt.js";
3
+ import { InstructionPrompt } from "../../model-function/generate-text/prompt-template/InstructionPrompt.js";
4
4
  import { LlamaCppTextGenerationPrompt } from "./LlamaCppTextGenerationModel.js";
5
5
  /**
6
6
  * BakLLaVA 1 uses a Vicuna 1 prompt. This mapping combines it with the LlamaCpp prompt structure.
7
7
  *
8
8
  * @see https://github.com/SkunkworksAI/BakLLaVA
9
9
  */
10
- export declare function instruction(): TextGenerationPromptTemplate<MultiModalInstructionPrompt, LlamaCppTextGenerationPrompt>;
11
- export declare function chat(): TextGenerationPromptTemplate<MultiModalChatPrompt, LlamaCppTextGenerationPrompt>;
10
+ export declare function instruction(): TextGenerationPromptTemplate<InstructionPrompt, LlamaCppTextGenerationPrompt>;
11
+ export declare function chat(): TextGenerationPromptTemplate<ChatPrompt, LlamaCppTextGenerationPrompt>;
@@ -1,4 +1,5 @@
1
- import { validateChatPrompt, } from "../../model-function/generate-text/prompt-template/ChatPrompt.js";
1
+ import { validateContentIsString } from "../../model-function/generate-text/prompt-template/ContentPart.js";
2
+ import { InvalidPromptError } from "../../model-function/generate-text/prompt-template/InvalidPromptError.js";
2
3
  // default Vicuna 1 system message
3
4
  const DEFAULT_SYSTEM_MESSAGE = "A chat between a curious user and an artificial intelligence assistant. " +
4
5
  "The assistant gives helpful, detailed, and polite answers to the user's questions.";
@@ -13,23 +14,28 @@ export function instruction() {
13
14
  let text = "";
14
15
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
15
16
  text += `USER: `;
16
- // construct text and image mapping:
17
- let imageCounter = 1;
18
17
  const images = {};
19
- for (const content of prompt.instruction) {
20
- switch (content.type) {
21
- case "text": {
22
- text += content.text;
23
- break;
24
- }
25
- case "image": {
26
- text += `[img-${imageCounter}]`;
27
- images[imageCounter.toString()] = content.base64Image;
28
- imageCounter++;
29
- break;
18
+ if (typeof prompt.instruction === "string") {
19
+ text += `${prompt.instruction}\n`;
20
+ }
21
+ else {
22
+ // construct text and image mapping:
23
+ let imageCounter = 1;
24
+ for (const content of prompt.instruction) {
25
+ switch (content.type) {
26
+ case "text": {
27
+ text += content.text;
28
+ break;
29
+ }
30
+ case "image": {
31
+ text += `[img-${imageCounter}]`;
32
+ images[imageCounter.toString()] = content.base64Image;
33
+ imageCounter++;
34
+ break;
35
+ }
30
36
  }
37
+ text += `${content}\n`;
31
38
  }
32
- text += `${content}\n`;
33
39
  }
34
40
  text += `\nASSISTANT: `;
35
41
  return { text, images };
@@ -40,7 +46,6 @@ export function instruction() {
40
46
  export function chat() {
41
47
  return {
42
48
  format(prompt) {
43
- validateChatPrompt(prompt);
44
49
  let text = "";
45
50
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
46
51
  // construct text and image mapping:
@@ -50,6 +55,10 @@ export function chat() {
50
55
  switch (role) {
51
56
  case "user": {
52
57
  text += `USER: `;
58
+ if (typeof content === "string") {
59
+ text += content;
60
+ break;
61
+ }
53
62
  for (const part of content) {
54
63
  switch (part.type) {
55
64
  case "text": {
@@ -67,9 +76,12 @@ export function chat() {
67
76
  break;
68
77
  }
69
78
  case "assistant": {
70
- text += `ASSISTANT: ${content}`;
79
+ text += `ASSISTANT: ${validateContentIsString(content, prompt)}`;
71
80
  break;
72
81
  }
82
+ case "tool": {
83
+ throw new InvalidPromptError("Tool messages are not supported.", prompt);
84
+ }
73
85
  default: {
74
86
  const _exhaustiveCheck = role;
75
87
  throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
@@ -5,11 +5,11 @@ const zod_1 = require("zod");
5
5
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
7
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
8
9
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
10
  const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
10
11
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
11
12
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
12
- const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
13
13
  const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
14
14
  const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
15
15
  const LlamaCppError_js_1 = require("./LlamaCppError.cjs");
@@ -138,6 +138,9 @@ class LlamaCppTextGenerationModel extends AbstractModel_js_1.AbstractModel {
138
138
  responseFormat: exports.LlamaCppTextGenerationResponseFormat.deltaIterable,
139
139
  });
140
140
  }
141
+ extractTextDelta(delta) {
142
+ return delta.content;
143
+ }
141
144
  withTextPrompt() {
142
145
  return this.withPromptTemplate({
143
146
  format(prompt) {
@@ -228,7 +231,7 @@ const llamaCppTextGenerationResponseSchema = zod_1.z.object({
228
231
  tokens_predicted: zod_1.z.number(),
229
232
  truncated: zod_1.z.boolean(),
230
233
  });
231
- const llamaCppTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("stop", [
234
+ const llamaCppTextStreamChunkSchema = (0, ZodSchema_js_1.zodSchema)(zod_1.z.discriminatedUnion("stop", [
232
235
  zod_1.z.object({
233
236
  content: zod_1.z.string(),
234
237
  stop: zod_1.z.literal(false),
@@ -237,7 +240,6 @@ const llamaCppTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z
237
240
  ]));
238
241
  async function createLlamaCppFullDeltaIterableQueue(stream) {
239
242
  const queue = new AsyncQueue_js_1.AsyncQueue();
240
- let content = "";
241
243
  // process the stream asynchonously (no 'await' on purpose):
242
244
  (0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
243
245
  .then(async (events) => {
@@ -246,18 +248,9 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
246
248
  const data = event.data;
247
249
  const eventData = (0, parseJSON_js_1.parseJSON)({
248
250
  text: data,
249
- schema: llamaCppTextStreamingResponseSchema,
250
- });
251
- content += eventData.content;
252
- queue.push({
253
- type: "delta",
254
- fullDelta: {
255
- content,
256
- isComplete: eventData.stop,
257
- delta: eventData.content,
258
- },
259
- valueDelta: eventData.content,
251
+ schema: llamaCppTextStreamChunkSchema,
260
252
  });
253
+ queue.push({ type: "delta", deltaValue: eventData });
261
254
  if (eventData.stop) {
262
255
  queue.close();
263
256
  }
@@ -116,7 +116,59 @@ export declare class LlamaCppTextGenerationModel<CONTEXT_WINDOW_SIZE extends num
116
116
  totalTokens: number;
117
117
  };
118
118
  }>;
119
- doStreamText(prompt: LlamaCppTextGenerationPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
119
+ doStreamText(prompt: LlamaCppTextGenerationPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<{
120
+ stop: true;
121
+ model: string;
122
+ prompt: string;
123
+ content: string;
124
+ generation_settings: {
125
+ stop: string[];
126
+ model: string;
127
+ stream: boolean;
128
+ seed: number;
129
+ mirostat: number;
130
+ frequency_penalty: number;
131
+ ignore_eos: boolean;
132
+ logit_bias: number[];
133
+ mirostat_eta: number;
134
+ mirostat_tau: number;
135
+ n_ctx: number;
136
+ n_keep: number;
137
+ n_predict: number;
138
+ n_probs: number;
139
+ penalize_nl: boolean;
140
+ presence_penalty: number;
141
+ repeat_last_n: number;
142
+ repeat_penalty: number;
143
+ temp: number;
144
+ tfs_z: number;
145
+ top_k: number;
146
+ top_p: number;
147
+ typical_p: number;
148
+ };
149
+ stopped_eos: boolean;
150
+ stopped_limit: boolean;
151
+ stopped_word: boolean;
152
+ stopping_word: string;
153
+ timings: {
154
+ predicted_ms: number;
155
+ predicted_n: number;
156
+ predicted_per_second: number | null;
157
+ predicted_per_token_ms: number | null;
158
+ prompt_ms: number | null;
159
+ prompt_n: number;
160
+ prompt_per_second: number | null;
161
+ prompt_per_token_ms: number | null;
162
+ };
163
+ tokens_cached: number;
164
+ tokens_evaluated: number;
165
+ tokens_predicted: number;
166
+ truncated: boolean;
167
+ } | {
168
+ stop: false;
169
+ content: string;
170
+ }>>>;
171
+ extractTextDelta(delta: unknown): string;
120
172
  withTextPrompt(): PromptTemplateTextStreamingModel<string, LlamaCppTextGenerationPrompt, LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>, this>;
121
173
  /**
122
174
  * Maps the prompt for a text version of the Llama.cpp prompt template (without image support).
@@ -340,11 +392,59 @@ declare const llamaCppTextGenerationResponseSchema: z.ZodObject<{
340
392
  truncated: boolean;
341
393
  }>;
342
394
  export type LlamaCppTextGenerationResponse = z.infer<typeof llamaCppTextGenerationResponseSchema>;
343
- export type LlamaCppTextGenerationDelta = {
395
+ declare const llamaCppTextStreamChunkSchema: import("../../core/schema/ZodSchema.js").ZodSchema<{
396
+ stop: true;
397
+ model: string;
398
+ prompt: string;
344
399
  content: string;
345
- isComplete: boolean;
346
- delta: string;
347
- };
400
+ generation_settings: {
401
+ stop: string[];
402
+ model: string;
403
+ stream: boolean;
404
+ seed: number;
405
+ mirostat: number;
406
+ frequency_penalty: number;
407
+ ignore_eos: boolean;
408
+ logit_bias: number[];
409
+ mirostat_eta: number;
410
+ mirostat_tau: number;
411
+ n_ctx: number;
412
+ n_keep: number;
413
+ n_predict: number;
414
+ n_probs: number;
415
+ penalize_nl: boolean;
416
+ presence_penalty: number;
417
+ repeat_last_n: number;
418
+ repeat_penalty: number;
419
+ temp: number;
420
+ tfs_z: number;
421
+ top_k: number;
422
+ top_p: number;
423
+ typical_p: number;
424
+ };
425
+ stopped_eos: boolean;
426
+ stopped_limit: boolean;
427
+ stopped_word: boolean;
428
+ stopping_word: string;
429
+ timings: {
430
+ predicted_ms: number;
431
+ predicted_n: number;
432
+ predicted_per_second: number | null;
433
+ predicted_per_token_ms: number | null;
434
+ prompt_ms: number | null;
435
+ prompt_n: number;
436
+ prompt_per_second: number | null;
437
+ prompt_per_token_ms: number | null;
438
+ };
439
+ tokens_cached: number;
440
+ tokens_evaluated: number;
441
+ tokens_predicted: number;
442
+ truncated: boolean;
443
+ } | {
444
+ stop: false;
445
+ content: string;
446
+ }>;
447
+ export type LlamaCppTextStreamChunk = (typeof llamaCppTextStreamChunkSchema)["_type"];
348
448
  export type LlamaCppTextGenerationResponseFormatType<T> = {
349
449
  stream: boolean;
350
450
  handler: ResponseHandler<T>;
@@ -413,7 +513,58 @@ export declare const LlamaCppTextGenerationResponseFormat: {
413
513
  stream: true;
414
514
  handler: ({ response }: {
415
515
  response: Response;
416
- }) => Promise<AsyncIterable<Delta<string>>>;
516
+ }) => Promise<AsyncIterable<Delta<{
517
+ stop: true;
518
+ model: string;
519
+ prompt: string;
520
+ content: string;
521
+ generation_settings: {
522
+ stop: string[];
523
+ model: string;
524
+ stream: boolean;
525
+ seed: number;
526
+ mirostat: number;
527
+ frequency_penalty: number;
528
+ ignore_eos: boolean;
529
+ logit_bias: number[];
530
+ mirostat_eta: number;
531
+ mirostat_tau: number;
532
+ n_ctx: number;
533
+ n_keep: number;
534
+ n_predict: number;
535
+ n_probs: number;
536
+ penalize_nl: boolean;
537
+ presence_penalty: number;
538
+ repeat_last_n: number;
539
+ repeat_penalty: number;
540
+ temp: number;
541
+ tfs_z: number;
542
+ top_k: number;
543
+ top_p: number;
544
+ typical_p: number;
545
+ };
546
+ stopped_eos: boolean;
547
+ stopped_limit: boolean;
548
+ stopped_word: boolean;
549
+ stopping_word: string;
550
+ timings: {
551
+ predicted_ms: number;
552
+ predicted_n: number;
553
+ predicted_per_second: number | null;
554
+ predicted_per_token_ms: number | null;
555
+ prompt_ms: number | null;
556
+ prompt_n: number;
557
+ prompt_per_second: number | null;
558
+ prompt_per_token_ms: number | null;
559
+ };
560
+ tokens_cached: number;
561
+ tokens_evaluated: number;
562
+ tokens_predicted: number;
563
+ truncated: boolean;
564
+ } | {
565
+ stop: false;
566
+ content: string;
567
+ }>>>;
417
568
  };
418
569
  };
419
570
  export {};
@@ -1,12 +1,12 @@
1
1
  import { z } from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
- import { ZodSchema } from "../../core/schema/ZodSchema.js";
4
+ import { zodSchema } from "../../core/schema/ZodSchema.js";
5
+ import { parseJSON } from "../../core/schema/parseJSON.js";
5
6
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
7
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
7
8
  import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
8
9
  import { AsyncQueue } from "../../util/AsyncQueue.js";
9
- import { parseJSON } from "../../core/schema/parseJSON.js";
10
10
  import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
11
11
  import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
12
12
  import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
@@ -135,6 +135,9 @@ export class LlamaCppTextGenerationModel extends AbstractModel {
135
135
  responseFormat: LlamaCppTextGenerationResponseFormat.deltaIterable,
136
136
  });
137
137
  }
138
+ extractTextDelta(delta) {
139
+ return delta.content;
140
+ }
138
141
  withTextPrompt() {
139
142
  return this.withPromptTemplate({
140
143
  format(prompt) {
@@ -224,7 +227,7 @@ const llamaCppTextGenerationResponseSchema = z.object({
224
227
  tokens_predicted: z.number(),
225
228
  truncated: z.boolean(),
226
229
  });
227
- const llamaCppTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("stop", [
230
+ const llamaCppTextStreamChunkSchema = zodSchema(z.discriminatedUnion("stop", [
228
231
  z.object({
229
232
  content: z.string(),
230
233
  stop: z.literal(false),
@@ -233,7 +236,6 @@ const llamaCppTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("
233
236
  ]));
234
237
  async function createLlamaCppFullDeltaIterableQueue(stream) {
235
238
  const queue = new AsyncQueue();
236
- let content = "";
237
239
  // process the stream asynchonously (no 'await' on purpose):
238
240
  parseEventSourceStream({ stream })
239
241
  .then(async (events) => {
@@ -242,18 +244,9 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
242
244
  const data = event.data;
243
245
  const eventData = parseJSON({
244
246
  text: data,
245
- schema: llamaCppTextStreamingResponseSchema,
246
- });
247
- content += eventData.content;
248
- queue.push({
249
- type: "delta",
250
- fullDelta: {
251
- content,
252
- isComplete: eventData.stop,
253
- delta: eventData.content,
254
- },
255
- valueDelta: eventData.content,
247
+ schema: llamaCppTextStreamChunkSchema,
256
248
  });
249
+ queue.push({ type: "delta", deltaValue: eventData });
257
250
  if (eventData.stop) {
258
251
  queue.close();
259
252
  }
@@ -0,0 +1,37 @@
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 LlamaCppTextGenerationModel_js_1 = require("./LlamaCppTextGenerationModel.cjs");
7
+ describe("streamText", () => {
8
+ const server = new StreamingTestServer_js_1.StreamingTestServer("http://127.0.0.1:8080/completion");
9
+ server.setupTestEnvironment();
10
+ it("should return a text stream", async () => {
11
+ server.responseChunks = [
12
+ `data: {"content":"Hello","multimodal":false,"slot_id":0,"stop":false}\n\n`,
13
+ `data: {"content":", ","multimodal":false,"slot_id":0,"stop":false}\n\n`,
14
+ `data: {"content":"world!","multimodal":false,"slot_id":0,"stop":false}\n\n`,
15
+ `data: {"content":"","generation_settings":{"frequency_penalty":0.0,"grammar":"",` +
16
+ `"ignore_eos":false,"logit_bias":[],"min_p":0.05000000074505806,"mirostat":0,` +
17
+ `"mirostat_eta":0.10000000149011612,"mirostat_tau":5.0,"model":"models/llama-2-7b-chat.Q4_K_M.gguf",` +
18
+ `"n_ctx":4096,"n_keep":0,"n_predict":-1,"n_probs":0,"penalize_nl":true,"penalty_prompt_tokens":[],` +
19
+ `"presence_penalty":0.0,"repeat_last_n":64,"repeat_penalty":1.100000023841858,"seed":4294967295,` +
20
+ `"stop":[],"stream":true,"temp":0.800000011920929,"tfs_z":1.0,"top_k":40,"top_p":0.949999988079071,` +
21
+ `"typical_p":1.0,"use_penalty_prompt_tokens":false},"model":"models/llama-2-7b-chat.Q4_K_M.gguf",` +
22
+ `"prompt":"hello","slot_id":0,"stop":true,"stopped_eos":true,"stopped_limit":false,` +
23
+ `"stopped_word":false,"stopping_word":"","timings":{"predicted_ms":1054.704,"predicted_n":69,` +
24
+ `"predicted_per_second":65.421198743913,"predicted_per_token_ms":15.285565217391303,` +
25
+ `"prompt_ms":244.228,"prompt_n":5,"prompt_per_second":20.472673075978186,` +
26
+ `"prompt_per_token_ms":48.845600000000005},"tokens_cached":74,"tokens_evaluated":5,` +
27
+ `"tokens_predicted":69,"truncated":false}\n\n`,
28
+ ];
29
+ const stream = await (0, streamText_js_1.streamText)(new LlamaCppTextGenerationModel_js_1.LlamaCppTextGenerationModel().withTextPrompt(), "hello");
30
+ // note: space moved to last chunk bc of trimming
31
+ expect(await (0, arrayFromAsync_js_1.arrayFromAsync)(stream)).toStrictEqual([
32
+ "Hello",
33
+ ",",
34
+ " world!",
35
+ ]);
36
+ });
37
+ });