modelfusion 0.101.0 → 0.103.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 (77) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/model-function/generate-image/ImageGenerationModel.d.ts +12 -2
  3. package/model-function/generate-image/PromptTemplateImageGenerationModel.cjs +3 -3
  4. package/model-function/generate-image/PromptTemplateImageGenerationModel.d.ts +2 -2
  5. package/model-function/generate-image/PromptTemplateImageGenerationModel.js +3 -3
  6. package/model-function/generate-image/generateImage.cjs +9 -7
  7. package/model-function/generate-image/generateImage.d.ts +2 -0
  8. package/model-function/generate-image/generateImage.js +9 -7
  9. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +1 -1
  10. package/model-function/generate-text/TextGenerationModel.cjs +7 -0
  11. package/model-function/generate-text/TextGenerationModel.d.ts +3 -1
  12. package/model-function/generate-text/TextGenerationModel.js +6 -1
  13. package/model-function/generate-text/TextGenerationResult.cjs +2 -0
  14. package/model-function/generate-text/TextGenerationResult.d.ts +11 -0
  15. package/model-function/generate-text/TextGenerationResult.js +1 -0
  16. package/model-function/generate-text/generateText.cjs +14 -9
  17. package/model-function/generate-text/generateText.d.ts +3 -0
  18. package/model-function/generate-text/generateText.js +14 -9
  19. package/model-function/generate-text/index.cjs +1 -0
  20. package/model-function/generate-text/index.d.ts +1 -0
  21. package/model-function/generate-text/index.js +1 -0
  22. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +23 -8
  23. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +6 -1
  24. package/model-provider/anthropic/AnthropicTextGenerationModel.js +24 -9
  25. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +7 -5
  26. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +16 -2
  27. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +7 -5
  28. package/model-provider/automatic1111/Automatic1111ImageGenerationPrompt.d.ts +0 -1
  29. package/model-provider/cohere/CohereTextGenerationModel.cjs +22 -6
  30. package/model-provider/cohere/CohereTextGenerationModel.d.ts +6 -1
  31. package/model-provider/cohere/CohereTextGenerationModel.js +22 -6
  32. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +2 -2
  33. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +2 -2
  34. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +9 -8
  35. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +4 -5
  36. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +9 -8
  37. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +51 -51
  38. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +20 -17
  39. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +51 -51
  40. package/model-provider/mistral/MistralChatModel.cjs +19 -2
  41. package/model-provider/mistral/MistralChatModel.d.ts +6 -1
  42. package/model-provider/mistral/MistralChatModel.js +19 -2
  43. package/model-provider/ollama/OllamaChatModel.cjs +8 -3
  44. package/model-provider/ollama/OllamaChatModel.d.ts +4 -1
  45. package/model-provider/ollama/OllamaChatModel.js +8 -3
  46. package/model-provider/ollama/OllamaCompletionModel.cjs +8 -3
  47. package/model-provider/ollama/OllamaCompletionModel.d.ts +4 -1
  48. package/model-provider/ollama/OllamaCompletionModel.js +8 -3
  49. package/model-provider/openai/OpenAICompletionModel.cjs +20 -4
  50. package/model-provider/openai/OpenAICompletionModel.d.ts +6 -1
  51. package/model-provider/openai/OpenAICompletionModel.js +20 -4
  52. package/model-provider/openai/OpenAIImageGenerationModel.cjs +25 -31
  53. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +2 -3
  54. package/model-provider/openai/OpenAIImageGenerationModel.js +25 -31
  55. package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +19 -1
  56. package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +6 -1
  57. package/model-provider/openai/chat/AbstractOpenAIChatModel.js +19 -1
  58. package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -3
  59. package/model-provider/openai/chat/OpenAIChatModel.js +2 -3
  60. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +2 -3
  61. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +2 -3
  62. package/model-provider/stability/StabilityApiConfiguration.cjs +12 -5
  63. package/model-provider/stability/StabilityApiConfiguration.d.ts +7 -8
  64. package/model-provider/stability/StabilityApiConfiguration.js +12 -5
  65. package/model-provider/stability/StabilityError.cjs +7 -31
  66. package/model-provider/stability/StabilityError.d.ts +2 -11
  67. package/model-provider/stability/StabilityError.js +6 -28
  68. package/model-provider/stability/StabilityFacade.cjs +11 -3
  69. package/model-provider/stability/StabilityFacade.d.ts +10 -2
  70. package/model-provider/stability/StabilityFacade.js +9 -2
  71. package/model-provider/stability/StabilityImageGenerationModel.cjs +39 -50
  72. package/model-provider/stability/StabilityImageGenerationModel.d.ts +42 -27
  73. package/model-provider/stability/StabilityImageGenerationModel.js +39 -50
  74. package/model-provider/stability/index.cjs +1 -3
  75. package/model-provider/stability/index.d.ts +1 -1
  76. package/model-provider/stability/index.js +0 -1
  77. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.103.0 - 2023-12-23
4
+
5
+ ### Added
6
+
7
+ - `finishReason` support for `generateText`.
8
+
9
+ The finish reason can be `stop` (the model stopped because it generated a stop sequence), `length` (the model stopped because it generated the maximum number of tokens), `content-filter` (the model stopped because the content filter detected a violation), `tool-calls` (the model stopped because it triggered a tool call), `error` (the model stopped because of an error), `other` (the model stopped for another reason), or `unknown` (the model stop reason is not know or the model does not support finish reasons).
10
+
11
+ You can extract it from the full response when using `fullResponse: true`:
12
+
13
+ ```ts
14
+ const { text, finishReason } = await generateText(
15
+ openai
16
+ .ChatTextGenerator({ model: "gpt-3.5-turbo", maxGenerationTokens: 200 })
17
+ .withTextPrompt(),
18
+ "Write a short story about a robot learning to love:",
19
+ { fullResponse: true }
20
+ );
21
+ ```
22
+
23
+ ## v0.102.0 - 2023-12-22
24
+
25
+ ### Added
26
+
27
+ - You can specify `numberOfGenerations` on image generation models and create multiple images by using the `fullResponse: true` option. Example:
28
+
29
+ ```ts
30
+ // generate 2 images:
31
+ const { images } = await generateImage(
32
+ openai.ImageGenerator({
33
+ model: "dall-e-3",
34
+ numberOfGenerations: 2,
35
+ size: "1024x1024",
36
+ }),
37
+ "the wicked witch of the west in the style of early 19th century painting",
38
+ { fullResponse: true }
39
+ );
40
+ ```
41
+
42
+ - **breaking change**: Image generation models use a generalized `numberOfGenerations` parameter (instead of model specific parameters) to specify the number of generations.
43
+
3
44
  ## v0.101.0 - 2023-12-22
4
45
 
5
46
  ### Changed
@@ -2,11 +2,21 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
2
  import { Model, ModelSettings } from "../Model.js";
3
3
  import { PromptTemplate } from "../PromptTemplate.js";
4
4
  export interface ImageGenerationModelSettings extends ModelSettings {
5
+ /**
6
+ * Number of images to generate.
7
+ *
8
+ * Specifies the number of images the model should generate for a given prompt.
9
+ *
10
+ * Does nothing if the model does not support this setting.
11
+ *
12
+ * Example: `numberOfGenerations: 2` // The model will produce 2 images
13
+ */
14
+ numberOfGenerations?: number;
5
15
  }
6
16
  export interface ImageGenerationModel<PROMPT, SETTINGS extends ImageGenerationModelSettings = ImageGenerationModelSettings> extends Model<SETTINGS> {
7
- doGenerateImage(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
17
+ doGenerateImages(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
8
18
  response: unknown;
9
- base64Image: string;
19
+ base64Images: string[];
10
20
  }>;
11
21
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: PromptTemplate<INPUT_PROMPT, PROMPT>): ImageGenerationModel<INPUT_PROMPT, SETTINGS>;
12
22
  }
@@ -24,15 +24,15 @@ class PromptTemplateImageGenerationModel {
24
24
  get settings() {
25
25
  return this.model.settings;
26
26
  }
27
- doGenerateImage(prompt, options) {
27
+ doGenerateImages(prompt, options) {
28
28
  const mappedPrompt = this.promptTemplate.format(prompt);
29
- return this.model.doGenerateImage(mappedPrompt, options);
29
+ return this.model.doGenerateImages(mappedPrompt, options);
30
30
  }
31
31
  get settingsForEvent() {
32
32
  return this.model.settingsForEvent;
33
33
  }
34
34
  withPromptTemplate(promptTemplate) {
35
- return new PromptTemplateImageGenerationModel({ model: this, promptTemplate: promptTemplate });
35
+ return new PromptTemplateImageGenerationModel({ model: this, promptTemplate });
36
36
  }
37
37
  withSettings(additionalSettings) {
38
38
  return new PromptTemplateImageGenerationModel({
@@ -10,9 +10,9 @@ export declare class PromptTemplateImageGenerationModel<PROMPT, MODEL_PROMPT, SE
10
10
  });
11
11
  get modelInformation(): import("../ModelInformation.js").ModelInformation;
12
12
  get settings(): SETTINGS;
13
- doGenerateImage(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
13
+ doGenerateImages(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
14
14
  response: unknown;
15
- base64Image: string;
15
+ base64Images: string[];
16
16
  }>;
17
17
  get settingsForEvent(): Partial<SETTINGS>;
18
18
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: PromptTemplate<INPUT_PROMPT, PROMPT>): PromptTemplateImageGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
@@ -21,15 +21,15 @@ export class PromptTemplateImageGenerationModel {
21
21
  get settings() {
22
22
  return this.model.settings;
23
23
  }
24
- doGenerateImage(prompt, options) {
24
+ doGenerateImages(prompt, options) {
25
25
  const mappedPrompt = this.promptTemplate.format(prompt);
26
- return this.model.doGenerateImage(mappedPrompt, options);
26
+ return this.model.doGenerateImages(mappedPrompt, options);
27
27
  }
28
28
  get settingsForEvent() {
29
29
  return this.model.settingsForEvent;
30
30
  }
31
31
  withPromptTemplate(promptTemplate) {
32
- return new PromptTemplateImageGenerationModel({ model: this, promptTemplate: promptTemplate });
32
+ return new PromptTemplateImageGenerationModel({ model: this, promptTemplate });
33
33
  }
34
34
  withSettings(additionalSettings) {
35
35
  return new PromptTemplateImageGenerationModel({
@@ -9,22 +9,24 @@ async function generateImage(model, prompt, options) {
9
9
  model,
10
10
  options,
11
11
  generateResponse: async (options) => {
12
- const result = await model.doGenerateImage(prompt, options);
12
+ const result = await model.doGenerateImages(prompt, options);
13
13
  return {
14
14
  response: result.response,
15
- extractedValue: result.base64Image,
15
+ extractedValue: result.base64Images,
16
16
  };
17
17
  },
18
18
  });
19
- const imageBase64 = fullResponse.value;
20
- const image = Buffer.from(imageBase64, "base64");
19
+ const imagesBase64 = fullResponse.value;
20
+ const images = imagesBase64.map((imageBase64) => Buffer.from(imageBase64, "base64"));
21
21
  return options?.fullResponse
22
22
  ? {
23
- image,
24
- imageBase64,
23
+ image: images[0],
24
+ imageBase64: imagesBase64[0],
25
+ images,
26
+ imagesBase64,
25
27
  response: fullResponse.response,
26
28
  metadata: fullResponse.metadata,
27
29
  }
28
- : image;
30
+ : images[0];
29
31
  }
30
32
  exports.generateImage = generateImage;
@@ -34,6 +34,8 @@ export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT
34
34
  }): Promise<{
35
35
  image: Buffer;
36
36
  imageBase64: string;
37
+ images: Buffer[];
38
+ imagesBase64: string[];
37
39
  response: unknown;
38
40
  metadata: ModelCallMetadata;
39
41
  }>;
@@ -6,21 +6,23 @@ export async function generateImage(model, prompt, options) {
6
6
  model,
7
7
  options,
8
8
  generateResponse: async (options) => {
9
- const result = await model.doGenerateImage(prompt, options);
9
+ const result = await model.doGenerateImages(prompt, options);
10
10
  return {
11
11
  response: result.response,
12
- extractedValue: result.base64Image,
12
+ extractedValue: result.base64Images,
13
13
  };
14
14
  },
15
15
  });
16
- const imageBase64 = fullResponse.value;
17
- const image = Buffer.from(imageBase64, "base64");
16
+ const imagesBase64 = fullResponse.value;
17
+ const images = imagesBase64.map((imageBase64) => Buffer.from(imageBase64, "base64"));
18
18
  return options?.fullResponse
19
19
  ? {
20
- image,
21
- imageBase64,
20
+ image: images[0],
21
+ imageBase64: imagesBase64[0],
22
+ images,
23
+ imagesBase64,
22
24
  response: fullResponse.response,
23
25
  metadata: fullResponse.metadata,
24
26
  }
25
- : image;
27
+ : images[0];
26
28
  }
@@ -19,7 +19,7 @@ export declare class PromptTemplateTextGenerationModel<PROMPT, MODEL_PROMPT, SET
19
19
  get countPromptTokens(): MODEL["countPromptTokens"] extends undefined ? undefined : (prompt: PROMPT) => PromiseLike<number>;
20
20
  doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
21
21
  response: unknown;
22
- texts: string[];
22
+ textGenerationResults: import("./TextGenerationResult.js").TextGenerationResult[];
23
23
  usage?: {
24
24
  promptTokens: number;
25
25
  completionTokens: number;
@@ -1,2 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.textGenerationModelProperties = void 0;
4
+ exports.textGenerationModelProperties = [
5
+ "maxGenerationTokens",
6
+ "stopSequences",
7
+ "numberOfGenerations",
8
+ "trimWhitespace",
9
+ ];
@@ -3,6 +3,8 @@ import { Delta } from "../Delta.js";
3
3
  import { Model, ModelSettings } from "../Model.js";
4
4
  import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
5
5
  import { TextGenerationPromptTemplate } from "./TextGenerationPromptTemplate.js";
6
+ import { TextGenerationResult } from "./TextGenerationResult.js";
7
+ export declare const textGenerationModelProperties: readonly ["maxGenerationTokens", "stopSequences", "numberOfGenerations", "trimWhitespace"];
6
8
  export interface TextGenerationModelSettings extends ModelSettings {
7
9
  /**
8
10
  * Specifies the maximum number of tokens (words, punctuation, parts of words) that the model can generate in a single response.
@@ -62,7 +64,7 @@ export interface TextGenerationModel<PROMPT, SETTINGS extends TextGenerationMode
62
64
  readonly countPromptTokens: ((prompt: PROMPT) => PromiseLike<number>) | undefined;
63
65
  doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
64
66
  response: unknown;
65
- texts: string[];
67
+ textGenerationResults: TextGenerationResult[];
66
68
  usage?: {
67
69
  promptTokens: number;
68
70
  completionTokens: number;
@@ -1 +1,6 @@
1
- export {};
1
+ export const textGenerationModelProperties = [
2
+ "maxGenerationTokens",
3
+ "stopSequences",
4
+ "numberOfGenerations",
5
+ "trimWhitespace",
6
+ ];
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ export type TextGenerationResult = {
2
+ /**
3
+ * The generated text.
4
+ */
5
+ text: string;
6
+ /**
7
+ * The reason why the generation stopped.
8
+ */
9
+ finishReason: TextGenerationFinishReason;
10
+ };
11
+ export type TextGenerationFinishReason = "stop" | "length" | "content-filter" | "tool-calls" | "error" | "other" | "unknown";
@@ -11,25 +11,30 @@ async function generateText(model, prompt, options) {
11
11
  generateResponse: async (options) => {
12
12
  const result = await model.doGenerateTexts(prompt, options);
13
13
  const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
14
- const texts = shouldTrimWhitespace
15
- ? result.texts.map((text) => text.trim())
16
- : result.texts;
14
+ const textGenerationResults = shouldTrimWhitespace
15
+ ? result.textGenerationResults.map((textGeneration) => ({
16
+ text: textGeneration.text.trim(),
17
+ finishReason: textGeneration.finishReason,
18
+ }))
19
+ : result.textGenerationResults;
17
20
  return {
18
21
  response: result.response,
19
- extractedValue: texts,
22
+ extractedValue: textGenerationResults,
20
23
  usage: result.usage,
21
24
  };
22
25
  },
23
26
  });
24
- const texts = fullResponse.value;
25
- const text = texts[0];
27
+ const textGenerationResults = fullResponse.value;
28
+ const firstResult = textGenerationResults[0];
26
29
  return options?.fullResponse
27
30
  ? {
28
- text,
29
- texts,
31
+ text: firstResult.text,
32
+ finishReason: firstResult.finishReason,
33
+ texts: textGenerationResults.map((textGeneration) => textGeneration.text),
34
+ textGenerationResults,
30
35
  response: fullResponse.response,
31
36
  metadata: fullResponse.metadata,
32
37
  }
33
- : text;
38
+ : firstResult.text;
34
39
  }
35
40
  exports.generateText = generateText;
@@ -1,6 +1,7 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
2
  import { ModelCallMetadata } from "../ModelCallMetadata.js";
3
3
  import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
4
+ import { TextGenerationFinishReason, TextGenerationResult } from "./TextGenerationResult.js";
4
5
  /**
5
6
  * Generate text for a prompt and return it as a string.
6
7
  *
@@ -29,7 +30,9 @@ export declare function generateText<PROMPT>(model: TextGenerationModel<PROMPT,
29
30
  fullResponse: true;
30
31
  }): Promise<{
31
32
  text: string;
33
+ finishReason: TextGenerationFinishReason;
32
34
  texts: string[];
35
+ textGenerationResults: TextGenerationResult[];
33
36
  response: unknown;
34
37
  metadata: ModelCallMetadata;
35
38
  }>;
@@ -8,24 +8,29 @@ export async function generateText(model, prompt, options) {
8
8
  generateResponse: async (options) => {
9
9
  const result = await model.doGenerateTexts(prompt, options);
10
10
  const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
11
- const texts = shouldTrimWhitespace
12
- ? result.texts.map((text) => text.trim())
13
- : result.texts;
11
+ const textGenerationResults = shouldTrimWhitespace
12
+ ? result.textGenerationResults.map((textGeneration) => ({
13
+ text: textGeneration.text.trim(),
14
+ finishReason: textGeneration.finishReason,
15
+ }))
16
+ : result.textGenerationResults;
14
17
  return {
15
18
  response: result.response,
16
- extractedValue: texts,
19
+ extractedValue: textGenerationResults,
17
20
  usage: result.usage,
18
21
  };
19
22
  },
20
23
  });
21
- const texts = fullResponse.value;
22
- const text = texts[0];
24
+ const textGenerationResults = fullResponse.value;
25
+ const firstResult = textGenerationResults[0];
23
26
  return options?.fullResponse
24
27
  ? {
25
- text,
26
- texts,
28
+ text: firstResult.text,
29
+ finishReason: firstResult.finishReason,
30
+ texts: textGenerationResults.map((textGeneration) => textGeneration.text),
31
+ textGenerationResults,
27
32
  response: fullResponse.response,
28
33
  metadata: fullResponse.metadata,
29
34
  }
30
- : text;
35
+ : firstResult.text;
31
36
  }
@@ -19,6 +19,7 @@ __exportStar(require("./PromptTemplateTextStreamingModel.cjs"), exports);
19
19
  __exportStar(require("./TextGenerationEvent.cjs"), exports);
20
20
  __exportStar(require("./TextGenerationModel.cjs"), exports);
21
21
  __exportStar(require("./TextGenerationPromptTemplate.cjs"), exports);
22
+ __exportStar(require("./TextGenerationResult.cjs"), exports);
22
23
  __exportStar(require("./generateText.cjs"), exports);
23
24
  __exportStar(require("./prompt-template/index.cjs"), exports);
24
25
  __exportStar(require("./streamText.cjs"), exports);
@@ -3,6 +3,7 @@ export * from "./PromptTemplateTextStreamingModel.js";
3
3
  export * from "./TextGenerationEvent.js";
4
4
  export * from "./TextGenerationModel.js";
5
5
  export * from "./TextGenerationPromptTemplate.js";
6
+ export * from "./TextGenerationResult.js";
6
7
  export * from "./generateText.js";
7
8
  export * from "./prompt-template/index.js";
8
9
  export * from "./streamText.js";
@@ -3,6 +3,7 @@ export * from "./PromptTemplateTextStreamingModel.js";
3
3
  export * from "./TextGenerationEvent.js";
4
4
  export * from "./TextGenerationModel.js";
5
5
  export * from "./TextGenerationPromptTemplate.js";
6
+ export * from "./TextGenerationResult.js";
6
7
  export * from "./generateText.js";
7
8
  export * from "./prompt-template/index.js";
8
9
  export * from "./streamText.js";
@@ -4,12 +4,13 @@ exports.AnthropicTextGenerationResponseFormat = exports.AnthropicTextGenerationM
4
4
  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
- const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
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");
11
+ const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
10
12
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
11
- const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
12
- const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
13
+ const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
13
14
  const AnthropicApiConfiguration_js_1 = require("./AnthropicApiConfiguration.cjs");
14
15
  const AnthropicError_js_1 = require("./AnthropicError.cjs");
15
16
  const AnthropicPromptTemplate_js_1 = require("./AnthropicPromptTemplate.cjs");
@@ -74,8 +75,8 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
74
75
  const abortSignal = options.run?.abortSignal;
75
76
  const userId = this.settings.userId;
76
77
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
77
- retry: this.settings.api?.retry,
78
- throttle: this.settings.api?.throttle,
78
+ retry: api.retry,
79
+ throttle: api.throttle,
79
80
  call: async () => {
80
81
  return (0, postToApi_js_1.postJsonToApi)({
81
82
  url: api.assembleUrl(`/complete`),
@@ -100,8 +101,7 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
100
101
  }
101
102
  get settingsForEvent() {
102
103
  const eventSettingProperties = [
103
- "maxGenerationTokens",
104
- "stopSequences",
104
+ ...TextGenerationModel_js_1.textGenerationModelProperties,
105
105
  "temperature",
106
106
  "topK",
107
107
  "topP",
@@ -116,9 +116,24 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
116
116
  });
117
117
  return {
118
118
  response,
119
- texts: [response.completion],
119
+ textGenerationResults: [
120
+ {
121
+ text: response.completion,
122
+ finishReason: this.translateFinishReason(response.stop_reason),
123
+ },
124
+ ],
120
125
  };
121
126
  }
127
+ translateFinishReason(finishReason) {
128
+ switch (finishReason) {
129
+ case "stop_sequence":
130
+ return "stop";
131
+ case "max_tokens":
132
+ return "length";
133
+ default:
134
+ return "unknown";
135
+ }
136
+ }
122
137
  doStreamText(prompt, options) {
123
138
  return this.callAPI(prompt, {
124
139
  ...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
  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.
@@ -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,
@@ -37,13 +37,15 @@ class Automatic1111ImageGenerationModel extends AbstractModel_js_1.AbstractModel
37
37
  url: api.assembleUrl(`/txt2img`),
38
38
  headers: api.headers,
39
39
  body: {
40
- height: this.settings.height,
41
- width: this.settings.width,
42
40
  prompt: input.prompt,
43
41
  negative_prompt: input.negativePrompt,
42
+ seed: this.settings.seed,
43
+ batch_size: this.settings.numberOfGenerations,
44
+ height: this.settings.height,
45
+ width: this.settings.width,
46
+ cfg_scale: this.settings.cfgScale,
44
47
  sampler_index: this.settings.sampler,
45
48
  steps: this.settings.steps,
46
- seed: input.seed,
47
49
  override_settings: {
48
50
  sd_model_checkpoint: this.settings.model,
49
51
  },
@@ -62,11 +64,11 @@ class Automatic1111ImageGenerationModel extends AbstractModel_js_1.AbstractModel
62
64
  steps: this.settings.steps,
63
65
  };
64
66
  }
65
- async doGenerateImage(prompt, options) {
67
+ async doGenerateImages(prompt, options) {
66
68
  const response = await this.callAPI(prompt, options);
67
69
  return {
68
70
  response,
69
- base64Image: response.images[0],
71
+ base64Images: response.images,
70
72
  };
71
73
  }
72
74
  withTextPrompt() {
@@ -8,11 +8,25 @@ import { PromptTemplateImageGenerationModel } from "../../model-function/generat
8
8
  import { Automatic1111ImageGenerationPrompt } from "./Automatic1111ImageGenerationPrompt.js";
9
9
  export interface Automatic1111ImageGenerationSettings extends ImageGenerationModelSettings {
10
10
  api?: ApiConfiguration;
11
+ /**
12
+ * Stable Diffusion checkpoint.
13
+ */
11
14
  model: string;
12
15
  height?: number;
13
16
  width?: number;
17
+ /**
18
+ * Sampling method.
19
+ */
14
20
  sampler?: string;
21
+ /**
22
+ * Sampling steps.
23
+ */
15
24
  steps?: number;
25
+ /**
26
+ * CFG Scale.
27
+ */
28
+ cfgScale?: number;
29
+ seed?: number;
16
30
  }
17
31
  /**
18
32
  * Create an image generation model that calls the AUTOMATIC1111 Stable Diffusion Web UI API.
@@ -25,13 +39,13 @@ export declare class Automatic1111ImageGenerationModel extends AbstractModel<Aut
25
39
  get modelName(): string;
26
40
  callAPI(input: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<Automatic1111ImageGenerationResponse>;
27
41
  get settingsForEvent(): Partial<Automatic1111ImageGenerationSettings>;
28
- doGenerateImage(prompt: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<{
42
+ doGenerateImages(prompt: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<{
29
43
  response: {
30
44
  images: string[];
31
45
  parameters: {};
32
46
  info: string;
33
47
  };
34
- base64Image: string;
48
+ base64Images: string[];
35
49
  }>;
36
50
  withTextPrompt(): PromptTemplateImageGenerationModel<string, Automatic1111ImageGenerationPrompt, Automatic1111ImageGenerationSettings, this>;
37
51
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: PromptTemplate<INPUT_PROMPT, Automatic1111ImageGenerationPrompt>): PromptTemplateImageGenerationModel<INPUT_PROMPT, Automatic1111ImageGenerationPrompt, Automatic1111ImageGenerationSettings, this>;