modelfusion 0.102.0 → 0.104.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 (97) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +1 -1
  3. package/model-function/generate-text/TextGenerationModel.cjs +7 -0
  4. package/model-function/generate-text/TextGenerationModel.d.ts +3 -1
  5. package/model-function/generate-text/TextGenerationModel.js +6 -1
  6. package/model-function/generate-text/TextGenerationResult.cjs +2 -0
  7. package/model-function/generate-text/TextGenerationResult.d.ts +11 -0
  8. package/model-function/generate-text/TextGenerationResult.js +1 -0
  9. package/model-function/generate-text/generateText.cjs +14 -9
  10. package/model-function/generate-text/generateText.d.ts +3 -0
  11. package/model-function/generate-text/generateText.js +14 -9
  12. package/model-function/generate-text/index.cjs +1 -0
  13. package/model-function/generate-text/index.d.ts +1 -0
  14. package/model-function/generate-text/index.js +1 -0
  15. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.cjs +2 -1
  16. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.d.ts +2 -2
  17. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.js +2 -1
  18. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.cjs +5 -4
  19. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.d.ts +4 -4
  20. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.js +5 -4
  21. package/model-function/generate-text/prompt-template/ChatPrompt.cjs +0 -24
  22. package/model-function/generate-text/prompt-template/ChatPrompt.d.ts +11 -34
  23. package/model-function/generate-text/prompt-template/ChatPrompt.js +1 -22
  24. package/model-function/generate-text/prompt-template/Content.cjs +9 -0
  25. package/model-function/generate-text/prompt-template/Content.d.ts +9 -4
  26. package/model-function/generate-text/prompt-template/Content.js +7 -1
  27. package/model-function/generate-text/prompt-template/InstructionPrompt.d.ts +6 -22
  28. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +36 -5
  29. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.d.ts +16 -4
  30. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +34 -4
  31. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.cjs +5 -4
  32. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.d.ts +4 -4
  33. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.js +5 -4
  34. package/model-function/generate-text/prompt-template/TextPromptTemplate.cjs +3 -4
  35. package/model-function/generate-text/prompt-template/TextPromptTemplate.d.ts +4 -4
  36. package/model-function/generate-text/prompt-template/TextPromptTemplate.js +3 -4
  37. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.cjs +3 -3
  38. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.d.ts +2 -2
  39. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.js +3 -3
  40. package/model-function/generate-text/prompt-template/trimChatPrompt.cjs +0 -2
  41. package/model-function/generate-text/prompt-template/trimChatPrompt.d.ts +4 -4
  42. package/model-function/generate-text/prompt-template/trimChatPrompt.js +0 -2
  43. package/model-provider/anthropic/AnthropicPromptTemplate.cjs +5 -4
  44. package/model-provider/anthropic/AnthropicPromptTemplate.d.ts +4 -4
  45. package/model-provider/anthropic/AnthropicPromptTemplate.js +5 -4
  46. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +23 -8
  47. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +8 -3
  48. package/model-provider/anthropic/AnthropicTextGenerationModel.js +24 -9
  49. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +1 -1
  50. package/model-provider/cohere/CohereTextGenerationModel.cjs +22 -6
  51. package/model-provider/cohere/CohereTextGenerationModel.d.ts +8 -3
  52. package/model-provider/cohere/CohereTextGenerationModel.js +22 -6
  53. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +2 -2
  54. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +2 -2
  55. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +9 -8
  56. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +4 -5
  57. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +9 -8
  58. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.cjs +23 -16
  59. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.d.ts +4 -4
  60. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.js +23 -16
  61. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +51 -51
  62. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +14 -11
  63. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +51 -51
  64. package/model-provider/mistral/MistralChatModel.cjs +19 -2
  65. package/model-provider/mistral/MistralChatModel.d.ts +8 -3
  66. package/model-provider/mistral/MistralChatModel.js +19 -2
  67. package/model-provider/mistral/MistralPromptTemplate.cjs +5 -4
  68. package/model-provider/mistral/MistralPromptTemplate.d.ts +4 -4
  69. package/model-provider/mistral/MistralPromptTemplate.js +5 -4
  70. package/model-provider/ollama/OllamaChatModel.cjs +8 -3
  71. package/model-provider/ollama/OllamaChatModel.d.ts +6 -3
  72. package/model-provider/ollama/OllamaChatModel.js +8 -3
  73. package/model-provider/ollama/OllamaChatPromptTemplate.cjs +9 -13
  74. package/model-provider/ollama/OllamaChatPromptTemplate.d.ts +4 -4
  75. package/model-provider/ollama/OllamaChatPromptTemplate.js +9 -13
  76. package/model-provider/ollama/OllamaCompletionModel.cjs +8 -3
  77. package/model-provider/ollama/OllamaCompletionModel.d.ts +4 -1
  78. package/model-provider/ollama/OllamaCompletionModel.js +8 -3
  79. package/model-provider/openai/OpenAICompletionModel.cjs +20 -4
  80. package/model-provider/openai/OpenAICompletionModel.d.ts +8 -3
  81. package/model-provider/openai/OpenAICompletionModel.js +20 -4
  82. package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +19 -1
  83. package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +6 -1
  84. package/model-provider/openai/chat/AbstractOpenAIChatModel.js +19 -1
  85. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +2 -2
  86. package/model-provider/openai/chat/OpenAIChatMessage.d.ts +2 -2
  87. package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -3
  88. package/model-provider/openai/chat/OpenAIChatModel.d.ts +2 -2
  89. package/model-provider/openai/chat/OpenAIChatModel.js +2 -3
  90. package/model-provider/openai/chat/OpenAIChatPromptTemplate.cjs +0 -2
  91. package/model-provider/openai/chat/OpenAIChatPromptTemplate.d.ts +4 -4
  92. package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +0 -2
  93. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +2 -3
  94. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +2 -2
  95. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +2 -3
  96. package/model-provider/stability/StabilityImageGenerationModel.d.ts +6 -6
  97. package/package.json +2 -2
@@ -7,6 +7,7 @@ import { Delta } from "../../model-function/Delta.js";
7
7
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
8
8
  import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
9
9
  import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
10
+ import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
10
11
  export declare const ANTHROPIC_TEXT_GENERATION_MODELS: {
11
12
  "claude-instant-1": {
12
13
  contextWindowSize: number;
@@ -55,8 +56,12 @@ export declare class AnthropicTextGenerationModel extends AbstractModel<Anthropi
55
56
  completion: string;
56
57
  stop_reason: string;
57
58
  };
58
- texts: string[];
59
+ textGenerationResults: {
60
+ text: string;
61
+ finishReason: TextGenerationFinishReason;
62
+ }[];
59
63
  }>;
64
+ private translateFinishReason;
60
65
  doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
61
66
  /**
62
67
  * Returns this model with a text prompt template.
@@ -65,11 +70,11 @@ export declare class AnthropicTextGenerationModel extends AbstractModel<Anthropi
65
70
  /**
66
71
  * Returns this model with an instruction prompt template.
67
72
  */
68
- withInstructionPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").TextInstructionPrompt, string, AnthropicTextGenerationModelSettings, this>;
73
+ withInstructionPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").InstructionPrompt, string, AnthropicTextGenerationModelSettings, this>;
69
74
  /**
70
75
  * Returns this model with a chat prompt template.
71
76
  */
72
- withChatPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").TextChatPrompt, string, AnthropicTextGenerationModelSettings, this>;
77
+ withChatPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").ChatPrompt, string, AnthropicTextGenerationModelSettings, this>;
73
78
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextStreamingModel<INPUT_PROMPT, string, AnthropicTextGenerationModelSettings, this>;
74
79
  withSettings(additionalSettings: Partial<AnthropicTextGenerationModelSettings>): this;
75
80
  }
@@ -1,15 +1,16 @@
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 { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.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";
8
+ import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
7
9
  import { AsyncQueue } from "../../util/AsyncQueue.js";
8
- import { ZodSchema } from "../../core/schema/ZodSchema.js";
9
- import { parseJSON } from "../../core/schema/parseJSON.js";
10
+ import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
10
11
  import { AnthropicApiConfiguration } from "./AnthropicApiConfiguration.js";
11
12
  import { failedAnthropicCallResponseHandler } from "./AnthropicError.js";
12
- import { instruction, chat, text } from "./AnthropicPromptTemplate.js";
13
+ import { chat, instruction, text } from "./AnthropicPromptTemplate.js";
13
14
  export const ANTHROPIC_TEXT_GENERATION_MODELS = {
14
15
  "claude-instant-1": {
15
16
  contextWindowSize: 100000,
@@ -71,8 +72,8 @@ export class AnthropicTextGenerationModel extends AbstractModel {
71
72
  const abortSignal = options.run?.abortSignal;
72
73
  const userId = this.settings.userId;
73
74
  return callWithRetryAndThrottle({
74
- retry: this.settings.api?.retry,
75
- throttle: this.settings.api?.throttle,
75
+ retry: api.retry,
76
+ throttle: api.throttle,
76
77
  call: async () => {
77
78
  return postJsonToApi({
78
79
  url: api.assembleUrl(`/complete`),
@@ -97,8 +98,7 @@ export class AnthropicTextGenerationModel extends AbstractModel {
97
98
  }
98
99
  get settingsForEvent() {
99
100
  const eventSettingProperties = [
100
- "maxGenerationTokens",
101
- "stopSequences",
101
+ ...textGenerationModelProperties,
102
102
  "temperature",
103
103
  "topK",
104
104
  "topP",
@@ -113,9 +113,24 @@ export class AnthropicTextGenerationModel extends AbstractModel {
113
113
  });
114
114
  return {
115
115
  response,
116
- texts: [response.completion],
116
+ textGenerationResults: [
117
+ {
118
+ text: response.completion,
119
+ finishReason: this.translateFinishReason(response.stop_reason),
120
+ },
121
+ ],
117
122
  };
118
123
  }
124
+ translateFinishReason(finishReason) {
125
+ switch (finishReason) {
126
+ case "stop_sequence":
127
+ return "stop";
128
+ case "max_tokens":
129
+ return "length";
130
+ default:
131
+ return "unknown";
132
+ }
133
+ }
119
134
  doStreamText(prompt, options) {
120
135
  return this.callAPI(prompt, {
121
136
  ...options,
@@ -49,7 +49,7 @@ export declare class Automatic1111ImageGenerationModel extends AbstractModel<Aut
49
49
  }>;
50
50
  withTextPrompt(): PromptTemplateImageGenerationModel<string, Automatic1111ImageGenerationPrompt, Automatic1111ImageGenerationSettings, this>;
51
51
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: PromptTemplate<INPUT_PROMPT, Automatic1111ImageGenerationPrompt>): PromptTemplateImageGenerationModel<INPUT_PROMPT, Automatic1111ImageGenerationPrompt, Automatic1111ImageGenerationSettings, this>;
52
- withSettings(additionalSettings: Automatic1111ImageGenerationSettings): this;
52
+ withSettings(additionalSettings: Partial<Automatic1111ImageGenerationSettings>): this;
53
53
  }
54
54
  declare const Automatic1111ImageGenerationResponseSchema: z.ZodObject<{
55
55
  images: z.ZodArray<z.ZodString, "many">;
@@ -7,6 +7,7 @@ const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
7
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
8
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
9
  const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
10
+ const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
10
11
  const TextPromptTemplate_js_1 = require("../../model-function/generate-text/prompt-template/TextPromptTemplate.cjs");
11
12
  const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
12
13
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
@@ -84,8 +85,8 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
84
85
  const responseFormat = options.responseFormat;
85
86
  const abortSignal = options.run?.abortSignal;
86
87
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
87
- retry: this.settings.api?.retry,
88
- throttle: this.settings.api?.throttle,
88
+ retry: api.retry,
89
+ throttle: api.throttle,
89
90
  call: async () => {
90
91
  return (0, postToApi_js_1.postJsonToApi)({
91
92
  url: api.assembleUrl(`/generate`),
@@ -116,9 +117,7 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
116
117
  }
117
118
  get settingsForEvent() {
118
119
  const eventSettingProperties = [
119
- "maxGenerationTokens",
120
- "stopSequences",
121
- "numberOfGenerations",
120
+ ...TextGenerationModel_js_1.textGenerationModelProperties,
122
121
  "temperature",
123
122
  "k",
124
123
  "p",
@@ -138,9 +137,26 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
138
137
  });
139
138
  return {
140
139
  response,
141
- texts: response.generations.map((generation) => generation.text),
140
+ textGenerationResults: response.generations.map((generation) => ({
141
+ text: generation.text,
142
+ finishReason: this.translateFinishReason(generation.finish_reason),
143
+ })),
142
144
  };
143
145
  }
146
+ translateFinishReason(finishReason) {
147
+ switch (finishReason) {
148
+ case "COMPLETE":
149
+ return "stop";
150
+ case "MAX_TOKENS":
151
+ return "length";
152
+ case "ERROR_TOXIC":
153
+ return "content-filter";
154
+ case "ERROR":
155
+ return "error";
156
+ default:
157
+ return "unknown";
158
+ }
159
+ }
144
160
  doStreamText(prompt, options) {
145
161
  return this.callAPI(prompt, {
146
162
  ...options,
@@ -7,6 +7,7 @@ import { Delta } from "../../model-function/Delta.js";
7
7
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
8
8
  import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
9
9
  import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
10
+ import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
10
11
  import { CohereTokenizer } from "./CohereTokenizer.js";
11
12
  export declare const COHERE_TEXT_GENERATION_MODELS: {
12
13
  command: {
@@ -79,21 +80,25 @@ export declare class CohereTextGenerationModel extends AbstractModel<CohereTextG
79
80
  };
80
81
  } | undefined;
81
82
  };
82
- texts: string[];
83
+ textGenerationResults: {
84
+ text: string;
85
+ finishReason: TextGenerationFinishReason;
86
+ }[];
83
87
  }>;
88
+ private translateFinishReason;
84
89
  doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
85
90
  extractTextDelta(fullDelta: CohereTextGenerationDelta): string | undefined;
86
91
  /**
87
92
  * Returns this model with an instruction prompt template.
88
93
  */
89
- withInstructionPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").TextInstructionPrompt, string, CohereTextGenerationModelSettings, this>;
94
+ withInstructionPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").InstructionPrompt, string, CohereTextGenerationModelSettings, this>;
90
95
  /**
91
96
  * Returns this model with a chat prompt template.
92
97
  */
93
98
  withChatPrompt(options?: {
94
99
  user?: string;
95
100
  assistant?: string;
96
- }): PromptTemplateTextStreamingModel<import("../../index.js").TextChatPrompt, string, CohereTextGenerationModelSettings, this>;
101
+ }): PromptTemplateTextStreamingModel<import("../../index.js").ChatPrompt, string, CohereTextGenerationModelSettings, this>;
97
102
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextStreamingModel<INPUT_PROMPT, string, CohereTextGenerationModelSettings, this>;
98
103
  withSettings(additionalSettings: Partial<CohereTextGenerationModelSettings>): this;
99
104
  }
@@ -4,6 +4,7 @@ import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postTo
4
4
  import { ZodSchema } from "../../core/schema/ZodSchema.js";
5
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
6
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
7
+ import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
7
8
  import { chat, instruction, } from "../../model-function/generate-text/prompt-template/TextPromptTemplate.js";
8
9
  import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
9
10
  import { AsyncQueue } from "../../util/AsyncQueue.js";
@@ -81,8 +82,8 @@ export class CohereTextGenerationModel extends AbstractModel {
81
82
  const responseFormat = options.responseFormat;
82
83
  const abortSignal = options.run?.abortSignal;
83
84
  return callWithRetryAndThrottle({
84
- retry: this.settings.api?.retry,
85
- throttle: this.settings.api?.throttle,
85
+ retry: api.retry,
86
+ throttle: api.throttle,
86
87
  call: async () => {
87
88
  return postJsonToApi({
88
89
  url: api.assembleUrl(`/generate`),
@@ -113,9 +114,7 @@ export class CohereTextGenerationModel extends AbstractModel {
113
114
  }
114
115
  get settingsForEvent() {
115
116
  const eventSettingProperties = [
116
- "maxGenerationTokens",
117
- "stopSequences",
118
- "numberOfGenerations",
117
+ ...textGenerationModelProperties,
119
118
  "temperature",
120
119
  "k",
121
120
  "p",
@@ -135,9 +134,26 @@ export class CohereTextGenerationModel extends AbstractModel {
135
134
  });
136
135
  return {
137
136
  response,
138
- texts: response.generations.map((generation) => generation.text),
137
+ textGenerationResults: response.generations.map((generation) => ({
138
+ text: generation.text,
139
+ finishReason: this.translateFinishReason(generation.finish_reason),
140
+ })),
139
141
  };
140
142
  }
143
+ translateFinishReason(finishReason) {
144
+ switch (finishReason) {
145
+ case "COMPLETE":
146
+ return "stop";
147
+ case "MAX_TOKENS":
148
+ return "length";
149
+ case "ERROR_TOXIC":
150
+ return "content-filter";
151
+ case "ERROR":
152
+ return "error";
153
+ default:
154
+ return "unknown";
155
+ }
156
+ }
141
157
  doStreamText(prompt, options) {
142
158
  return this.callAPI(prompt, {
143
159
  ...options,
@@ -101,7 +101,7 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
101
101
  // The JS WebSocket API does not support authorization headers, so we send the API key in the BOS message.
102
102
  // See https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api
103
103
  xi_api_key: api.apiKey,
104
- text: " ",
104
+ text: " ", // first message
105
105
  voice_settings: toApiVoiceSettings(this.settings.voiceSettings),
106
106
  generation_config: toGenerationConfig(this.settings.generationConfig),
107
107
  }));
@@ -125,7 +125,7 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
125
125
  // send remaining text:
126
126
  if (textBuffer.length > 0) {
127
127
  socket.send(JSON.stringify({
128
- text: `${textBuffer} `,
128
+ text: `${textBuffer} `, // append space
129
129
  try_trigger_generation: true,
130
130
  }));
131
131
  }
@@ -98,7 +98,7 @@ export class ElevenLabsSpeechModel extends AbstractModel {
98
98
  // The JS WebSocket API does not support authorization headers, so we send the API key in the BOS message.
99
99
  // See https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api
100
100
  xi_api_key: api.apiKey,
101
- text: " ",
101
+ text: " ", // first message
102
102
  voice_settings: toApiVoiceSettings(this.settings.voiceSettings),
103
103
  generation_config: toGenerationConfig(this.settings.generationConfig),
104
104
  }));
@@ -122,7 +122,7 @@ export class ElevenLabsSpeechModel extends AbstractModel {
122
122
  // send remaining text:
123
123
  if (textBuffer.length > 0) {
124
124
  socket.send(JSON.stringify({
125
- text: `${textBuffer} `,
125
+ text: `${textBuffer} `, // append space
126
126
  try_trigger_generation: true,
127
127
  }));
128
128
  }
@@ -6,6 +6,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
7
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
8
8
  const PromptTemplateTextGenerationModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextGenerationModel.cjs");
9
+ const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
9
10
  const HuggingFaceApiConfiguration_js_1 = require("./HuggingFaceApiConfiguration.cjs");
10
11
  const HuggingFaceError_js_1 = require("./HuggingFaceError.cjs");
11
12
  /**
@@ -61,8 +62,8 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
61
62
  const api = this.settings.api ?? new HuggingFaceApiConfiguration_js_1.HuggingFaceApiConfiguration();
62
63
  const abortSignal = options?.run?.abortSignal;
63
64
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
64
- retry: this.settings.api?.retry,
65
- throttle: this.settings.api?.throttle,
65
+ retry: api.retry,
66
+ throttle: api.throttle,
66
67
  call: async () => {
67
68
  return (0, postToApi_js_1.postJsonToApi)({
68
69
  url: api.assembleUrl(`/${this.settings.model}`),
@@ -91,16 +92,13 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
91
92
  }
92
93
  get settingsForEvent() {
93
94
  const eventSettingProperties = [
94
- "stopSequences",
95
- "maxGenerationTokens",
96
- "numberOfGenerations",
95
+ ...TextGenerationModel_js_1.textGenerationModelProperties,
97
96
  "topK",
98
97
  "topP",
99
98
  "temperature",
100
99
  "repetitionPenalty",
101
100
  "maxTime",
102
101
  "doSample",
103
- "options",
104
102
  ];
105
103
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
106
104
  }
@@ -108,12 +106,15 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
108
106
  const response = await this.callAPI(prompt, options);
109
107
  return {
110
108
  response,
111
- texts: response.map((response) => response.generated_text),
109
+ textGenerationResults: response.map((response) => ({
110
+ text: response.generated_text,
111
+ finishReason: "unknown",
112
+ })),
112
113
  };
113
114
  }
114
115
  withPromptTemplate(promptTemplate) {
115
116
  return new PromptTemplateTextGenerationModel_js_1.PromptTemplateTextGenerationModel({
116
- model: this,
117
+ model: this, // stop tokens are not supported by this model
117
118
  promptTemplate,
118
119
  });
119
120
  }
@@ -14,10 +14,6 @@ export interface HuggingFaceTextGenerationModelSettings extends TextGenerationMo
14
14
  repetitionPenalty?: number;
15
15
  maxTime?: number;
16
16
  doSample?: boolean;
17
- options?: {
18
- useCache?: boolean;
19
- waitForModel?: boolean;
20
- };
21
17
  }
22
18
  /**
23
19
  * Create a text generation model that calls a Hugging Face Inference API Text Generation Task.
@@ -50,7 +46,10 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
50
46
  response: {
51
47
  generated_text: string;
52
48
  }[];
53
- texts: string[];
49
+ textGenerationResults: {
50
+ text: string;
51
+ finishReason: "unknown";
52
+ }[];
54
53
  }>;
55
54
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextGenerationModel<INPUT_PROMPT, string, HuggingFaceTextGenerationModelSettings, this>;
56
55
  withSettings(additionalSettings: Partial<HuggingFaceTextGenerationModelSettings>): this;
@@ -3,6 +3,7 @@ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottl
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
4
  import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { PromptTemplateTextGenerationModel } from "../../model-function/generate-text/PromptTemplateTextGenerationModel.js";
6
+ import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
6
7
  import { HuggingFaceApiConfiguration } from "./HuggingFaceApiConfiguration.js";
7
8
  import { failedHuggingFaceCallResponseHandler } from "./HuggingFaceError.js";
8
9
  /**
@@ -58,8 +59,8 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
58
59
  const api = this.settings.api ?? new HuggingFaceApiConfiguration();
59
60
  const abortSignal = options?.run?.abortSignal;
60
61
  return callWithRetryAndThrottle({
61
- retry: this.settings.api?.retry,
62
- throttle: this.settings.api?.throttle,
62
+ retry: api.retry,
63
+ throttle: api.throttle,
63
64
  call: async () => {
64
65
  return postJsonToApi({
65
66
  url: api.assembleUrl(`/${this.settings.model}`),
@@ -88,16 +89,13 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
88
89
  }
89
90
  get settingsForEvent() {
90
91
  const eventSettingProperties = [
91
- "stopSequences",
92
- "maxGenerationTokens",
93
- "numberOfGenerations",
92
+ ...textGenerationModelProperties,
94
93
  "topK",
95
94
  "topP",
96
95
  "temperature",
97
96
  "repetitionPenalty",
98
97
  "maxTime",
99
98
  "doSample",
100
- "options",
101
99
  ];
102
100
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
103
101
  }
@@ -105,12 +103,15 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
105
103
  const response = await this.callAPI(prompt, options);
106
104
  return {
107
105
  response,
108
- texts: response.map((response) => response.generated_text),
106
+ textGenerationResults: response.map((response) => ({
107
+ text: response.generated_text,
108
+ finishReason: "unknown",
109
+ })),
109
110
  };
110
111
  }
111
112
  withPromptTemplate(promptTemplate) {
112
113
  return new PromptTemplateTextGenerationModel({
113
- model: this,
114
+ model: this, // stop tokens are not supported by this model
114
115
  promptTemplate,
115
116
  });
116
117
  }
@@ -1,7 +1,6 @@
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");
5
4
  // default Vicuna 1 system message
6
5
  const DEFAULT_SYSTEM_MESSAGE = "A chat between a curious user and an artificial intelligence assistant. " +
7
6
  "The assistant gives helpful, detailed, and polite answers to the user's questions.";
@@ -16,23 +15,28 @@ function instruction() {
16
15
  let text = "";
17
16
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
18
17
  text += `USER: `;
19
- // construct text and image mapping:
20
- let imageCounter = 1;
21
18
  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;
19
+ if (typeof prompt.instruction === "string") {
20
+ text += `${prompt.instruction}\n`;
21
+ }
22
+ else {
23
+ // construct text and image mapping:
24
+ let imageCounter = 1;
25
+ for (const content of prompt.instruction) {
26
+ switch (content.type) {
27
+ case "text": {
28
+ text += content.text;
29
+ break;
30
+ }
31
+ case "image": {
32
+ text += `[img-${imageCounter}]`;
33
+ images[imageCounter.toString()] = content.base64Image;
34
+ imageCounter++;
35
+ break;
36
+ }
33
37
  }
38
+ text += `${content}\n`;
34
39
  }
35
- text += `${content}\n`;
36
40
  }
37
41
  text += `\nASSISTANT: `;
38
42
  return { text, images };
@@ -44,7 +48,6 @@ exports.instruction = instruction;
44
48
  function chat() {
45
49
  return {
46
50
  format(prompt) {
47
- (0, ChatPrompt_js_1.validateChatPrompt)(prompt);
48
51
  let text = "";
49
52
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
50
53
  // construct text and image mapping:
@@ -54,6 +57,10 @@ function chat() {
54
57
  switch (role) {
55
58
  case "user": {
56
59
  text += `USER: `;
60
+ if (typeof content === "string") {
61
+ text += content;
62
+ break;
63
+ }
57
64
  for (const part of content) {
58
65
  switch (part.type) {
59
66
  case "text": {
@@ -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,3 @@
1
- import { validateChatPrompt, } from "../../model-function/generate-text/prompt-template/ChatPrompt.js";
2
1
  // default Vicuna 1 system message
3
2
  const DEFAULT_SYSTEM_MESSAGE = "A chat between a curious user and an artificial intelligence assistant. " +
4
3
  "The assistant gives helpful, detailed, and polite answers to the user's questions.";
@@ -13,23 +12,28 @@ export function instruction() {
13
12
  let text = "";
14
13
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
15
14
  text += `USER: `;
16
- // construct text and image mapping:
17
- let imageCounter = 1;
18
15
  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;
16
+ if (typeof prompt.instruction === "string") {
17
+ text += `${prompt.instruction}\n`;
18
+ }
19
+ else {
20
+ // construct text and image mapping:
21
+ let imageCounter = 1;
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;
33
+ }
30
34
  }
35
+ text += `${content}\n`;
31
36
  }
32
- text += `${content}\n`;
33
37
  }
34
38
  text += `\nASSISTANT: `;
35
39
  return { text, images };
@@ -40,7 +44,6 @@ export function instruction() {
40
44
  export function chat() {
41
45
  return {
42
46
  format(prompt) {
43
- validateChatPrompt(prompt);
44
47
  let text = "";
45
48
  text += `${prompt.system ?? DEFAULT_SYSTEM_MESSAGE}\n\n`;
46
49
  // construct text and image mapping:
@@ -50,6 +53,10 @@ export function chat() {
50
53
  switch (role) {
51
54
  case "user": {
52
55
  text += `USER: `;
56
+ if (typeof content === "string") {
57
+ text += content;
58
+ break;
59
+ }
53
60
  for (const part of content) {
54
61
  switch (part.type) {
55
62
  case "text": {