modelfusion 0.59.0 → 0.60.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.
package/README.md CHANGED
@@ -39,8 +39,7 @@ You can provide API keys for the different [integrations](https://modelfusion.de
39
39
 
40
40
  ### [Generate Text](https://modelfusion.dev/guide/function/generate-text)
41
41
 
42
- Generate text using a language model and a prompt.
43
- You can stream the text if it is supported by the model.
42
+ Generate text using a language model and a prompt. You can stream the text if it is supported by the model. You can use images for multi-modal prompting if the model supports it (e.g. with [llama.cpp](https://modelfusion.dev/guide/)).
44
43
  You can use [prompt formats](https://modelfusion.dev/guide/function/generate-text#prompt-format) to change the prompt format of a model.
45
44
 
46
45
  #### generateText
@@ -75,7 +75,7 @@ class LlamaCppTextGenerationModel extends AbstractModel_js_1.AbstractModel {
75
75
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
76
76
  }
77
77
  async countPromptTokens(prompt) {
78
- const tokens = await this.tokenizer.tokenize(prompt);
78
+ const tokens = await this.tokenizer.tokenize(prompt.text);
79
79
  return tokens.length;
80
80
  }
81
81
  async doGenerateText(prompt, options) {
@@ -99,6 +99,14 @@ class LlamaCppTextGenerationModel extends AbstractModel_js_1.AbstractModel {
99
99
  responseFormat: exports.LlamaCppTextGenerationResponseFormat.deltaIterable,
100
100
  });
101
101
  }
102
+ withTextPrompt() {
103
+ return this.withPromptFormat({
104
+ format(prompt) {
105
+ return { text: prompt };
106
+ },
107
+ stopSequences: [],
108
+ });
109
+ }
102
110
  withPromptFormat(promptFormat) {
103
111
  return new PromptFormatTextStreamingModel_js_1.PromptFormatTextStreamingModel({
104
112
  model: this.withSettings({
@@ -177,7 +185,7 @@ async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguratio
177
185
  headers: api.headers,
178
186
  body: {
179
187
  stream: responseFormat.stream,
180
- prompt,
188
+ prompt: prompt.text,
181
189
  temperature,
182
190
  top_k: topK,
183
191
  top_p: topP,
@@ -195,6 +203,12 @@ async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguratio
195
203
  seed,
196
204
  ignore_eos: ignoreEos,
197
205
  logit_bias: logitBias,
206
+ image_data: prompt.images != null
207
+ ? Object.entries(prompt.images).map(([id, data]) => ({
208
+ id: +id,
209
+ data,
210
+ }))
211
+ : undefined,
198
212
  },
199
213
  failedResponseHandler: LlamaCppError_js_1.failedLlamaCppCallResponseHandler,
200
214
  successfulResponseHandler: responseFormat.handler,
@@ -31,18 +31,28 @@ export interface LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE extends
31
31
  ignoreEos?: boolean;
32
32
  logitBias?: Array<[number, number | false]>;
33
33
  }
34
- export declare class LlamaCppTextGenerationModel<CONTEXT_WINDOW_SIZE extends number | undefined> extends AbstractModel<LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>> implements TextStreamingModel<string, LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>> {
34
+ export interface LlamaCppTextGenerationPrompt {
35
+ /**
36
+ * Text prompt. Images can be included through references such as `[img-ID]`, e.g. `[img-1]`.
37
+ */
38
+ text: string;
39
+ /**
40
+ * Maps image id to image base data.
41
+ */
42
+ images?: Record<number, string>;
43
+ }
44
+ export declare class LlamaCppTextGenerationModel<CONTEXT_WINDOW_SIZE extends number | undefined> extends AbstractModel<LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>> implements TextStreamingModel<LlamaCppTextGenerationPrompt, LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>> {
35
45
  constructor(settings?: LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>);
36
46
  readonly provider = "llamacpp";
37
47
  get modelName(): null;
38
48
  get contextWindowSize(): CONTEXT_WINDOW_SIZE;
39
49
  readonly tokenizer: LlamaCppTokenizer;
40
- callAPI<RESPONSE>(prompt: string, options: {
50
+ callAPI<RESPONSE>(prompt: LlamaCppTextGenerationPrompt, options: {
41
51
  responseFormat: LlamaCppTextGenerationResponseFormatType<RESPONSE>;
42
52
  } & FunctionOptions): Promise<RESPONSE>;
43
53
  get settingsForEvent(): Partial<LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>>;
44
- countPromptTokens(prompt: string): Promise<number>;
45
- doGenerateText(prompt: string, options?: FunctionOptions): Promise<{
54
+ countPromptTokens(prompt: LlamaCppTextGenerationPrompt): Promise<number>;
55
+ doGenerateText(prompt: LlamaCppTextGenerationPrompt, options?: FunctionOptions): Promise<{
46
56
  response: {
47
57
  model: string;
48
58
  prompt: string;
@@ -99,8 +109,9 @@ export declare class LlamaCppTextGenerationModel<CONTEXT_WINDOW_SIZE extends num
99
109
  totalTokens: number;
100
110
  };
101
111
  }>;
102
- doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
103
- withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, string>): PromptFormatTextStreamingModel<INPUT_PROMPT, string, LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>, this>;
112
+ doStreamText(prompt: LlamaCppTextGenerationPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
113
+ withTextPrompt(): PromptFormatTextStreamingModel<string, LlamaCppTextGenerationPrompt, LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>, this>;
114
+ withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, LlamaCppTextGenerationPrompt>): PromptFormatTextStreamingModel<INPUT_PROMPT, LlamaCppTextGenerationPrompt, LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>, this>;
104
115
  withSettings(additionalSettings: Partial<LlamaCppTextGenerationModelSettings<CONTEXT_WINDOW_SIZE>>): this;
105
116
  }
106
117
  declare const llamaCppTextGenerationResponseSchema: z.ZodObject<{
@@ -72,7 +72,7 @@ export class LlamaCppTextGenerationModel extends AbstractModel {
72
72
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
73
73
  }
74
74
  async countPromptTokens(prompt) {
75
- const tokens = await this.tokenizer.tokenize(prompt);
75
+ const tokens = await this.tokenizer.tokenize(prompt.text);
76
76
  return tokens.length;
77
77
  }
78
78
  async doGenerateText(prompt, options) {
@@ -96,6 +96,14 @@ export class LlamaCppTextGenerationModel extends AbstractModel {
96
96
  responseFormat: LlamaCppTextGenerationResponseFormat.deltaIterable,
97
97
  });
98
98
  }
99
+ withTextPrompt() {
100
+ return this.withPromptFormat({
101
+ format(prompt) {
102
+ return { text: prompt };
103
+ },
104
+ stopSequences: [],
105
+ });
106
+ }
99
107
  withPromptFormat(promptFormat) {
100
108
  return new PromptFormatTextStreamingModel({
101
109
  model: this.withSettings({
@@ -173,7 +181,7 @@ async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguratio
173
181
  headers: api.headers,
174
182
  body: {
175
183
  stream: responseFormat.stream,
176
- prompt,
184
+ prompt: prompt.text,
177
185
  temperature,
178
186
  top_k: topK,
179
187
  top_p: topP,
@@ -191,6 +199,12 @@ async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguratio
191
199
  seed,
192
200
  ignore_eos: ignoreEos,
193
201
  logit_bias: logitBias,
202
+ image_data: prompt.images != null
203
+ ? Object.entries(prompt.images).map(([id, data]) => ({
204
+ id: +id,
205
+ data,
206
+ }))
207
+ : undefined,
194
208
  },
195
209
  failedResponseHandler: failedLlamaCppCallResponseHandler,
196
210
  successfulResponseHandler: responseFormat.handler,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.59.0",
4
+ "version": "0.60.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [