modelfusion 0.19.0 → 0.20.1

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 (39) hide show
  1. package/README.md +5 -6
  2. package/model-function/embed-text/embedText.cjs +64 -18
  3. package/model-function/embed-text/embedText.d.ts +3 -19
  4. package/model-function/embed-text/embedText.js +64 -18
  5. package/model-function/executeCall.cjs +49 -3
  6. package/model-function/executeCall.d.ts +18 -5
  7. package/model-function/executeCall.js +46 -1
  8. package/model-function/generate-image/generateImage.cjs +17 -9
  9. package/model-function/generate-image/generateImage.d.ts +2 -11
  10. package/model-function/generate-image/generateImage.js +17 -9
  11. package/model-function/generate-json/generateJson.cjs +2 -9
  12. package/model-function/generate-json/generateJson.d.ts +2 -11
  13. package/model-function/generate-json/generateJson.js +2 -9
  14. package/model-function/generate-json/generateJsonOrText.cjs +2 -9
  15. package/model-function/generate-json/generateJsonOrText.d.ts +3 -15
  16. package/model-function/generate-json/generateJsonOrText.js +2 -9
  17. package/model-function/generate-text/generateText.cjs +14 -9
  18. package/model-function/generate-text/generateText.d.ts +2 -11
  19. package/model-function/generate-text/generateText.js +14 -9
  20. package/model-function/generate-text/streamText.cjs +44 -9
  21. package/model-function/generate-text/streamText.d.ts +16 -12
  22. package/model-function/generate-text/streamText.js +41 -7
  23. package/model-function/synthesize-speech/synthesizeSpeech.cjs +5 -8
  24. package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -10
  25. package/model-function/synthesize-speech/synthesizeSpeech.js +5 -8
  26. package/model-function/transcribe-speech/transcribe.cjs +16 -9
  27. package/model-function/transcribe-speech/transcribe.d.ts +2 -11
  28. package/model-function/transcribe-speech/transcribe.js +16 -9
  29. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  30. package/package.json +1 -1
  31. package/run/RunFunctionEvent.d.ts +1 -1
  32. package/tool/executeTool.cjs +48 -9
  33. package/tool/executeTool.d.ts +16 -9
  34. package/tool/executeTool.js +45 -7
  35. package/tool/index.cjs +1 -0
  36. package/tool/index.d.ts +1 -0
  37. package/tool/index.js +1 -0
  38. package/tool/useTool.cjs +2 -3
  39. package/tool/useTool.js +2 -3
package/README.md CHANGED
@@ -106,20 +106,19 @@ const textStream = await streamText(
106
106
 
107
107
  #### Metadata and original responses
108
108
 
109
- ModelFusion model functions return rich results that include the original response and metadata when you set the `fullResponse` option to `true`.
109
+ ModelFusion model functions return rich results that include the original response and metadata when you call `.asFullResponse()` before resolving the promise.
110
110
 
111
111
  ```ts
112
112
  // access the full response and the metadata:
113
113
  // the response type is specific to the model that's being used
114
- const { response, metadata } = await generateText(
114
+ const { output, response, metadata } = await generateText(
115
115
  new OpenAITextGenerationModel({
116
116
  model: "text-davinci-003",
117
117
  maxCompletionTokens: 1000,
118
118
  n: 2, // generate 2 completions
119
119
  }),
120
- "Write a short story about a robot learning to love:\n\n",
121
- { fullResponse: true }
122
- );
120
+ "Write a short story about a robot learning to love:\n\n"
121
+ ).asFullResponse();
123
122
 
124
123
  for (const choice of response.choices) {
125
124
  console.log(choice.text);
@@ -253,7 +252,7 @@ Text is generated as a fallback.
253
252
  ```ts
254
253
  const { tool, parameters, result, text } = await useToolOrGenerateText(
255
254
  new OpenAIChatModel({ model: "gpt-3.5-turbo" }),
256
- [calculator /* ... */],
255
+ [calculator /* and other tools... */],
257
256
  OpenAIChatFunctionPrompt.forToolsCurried([
258
257
  OpenAIChatMessage.user("What's fourteen times twelve?"),
259
258
  ])
@@ -2,8 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.embedText = exports.embedTexts = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
- async function embedTexts(model, texts, options) {
6
- const result = await (0, executeCall_js_1.executeCall)({
5
+ /**
6
+ * Generate embeddings for multiple texts.
7
+ *
8
+ * @example
9
+ * const embeddings = await embedTexts(
10
+ * new OpenAITextEmbeddingModel(...),
11
+ * [
12
+ * "At first, Nox didn't know what to do with the pup.",
13
+ * "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
14
+ * ]
15
+ * );
16
+ */
17
+ function embedTexts(model, texts, options) {
18
+ return (0, executeCall_js_1.executeCall)({
7
19
  model,
8
20
  options,
9
21
  generateResponse: (options) => {
@@ -53,24 +65,58 @@ async function embedTexts(model, texts, options) {
53
65
  generatedEmbeddings: output,
54
66
  }),
55
67
  });
56
- return options?.fullResponse === true
57
- ? {
58
- embeddings: result.output,
59
- metadata: result.metadata,
60
- }
61
- : result.output;
62
68
  }
63
69
  exports.embedTexts = embedTexts;
64
- async function embedText(model, text, options) {
65
- const result = await embedTexts(model, [text], {
66
- ...(options ?? {}),
67
- fullResponse: true,
70
+ /**
71
+ * Generate an embedding for a single text.
72
+ *
73
+ * @example
74
+ * const embedding = await embedText(
75
+ * new OpenAITextEmbeddingModel(...),
76
+ * "At first, Nox didn't know what to do with the pup."
77
+ * );
78
+ */
79
+ function embedText(model, text, options) {
80
+ const texts = [text];
81
+ return (0, executeCall_js_1.executeCall)({
82
+ model,
83
+ options,
84
+ generateResponse: (options) => {
85
+ return model.generateEmbeddingResponse(texts, options);
86
+ },
87
+ extractOutputValue: (result) => {
88
+ return model.extractEmbeddings(result)[0];
89
+ },
90
+ getStartEvent: (metadata, settings) => ({
91
+ type: "text-embedding-started",
92
+ metadata,
93
+ settings,
94
+ texts,
95
+ }),
96
+ getAbortEvent: (metadata, settings) => ({
97
+ type: "text-embedding-finished",
98
+ status: "abort",
99
+ metadata,
100
+ settings,
101
+ texts,
102
+ }),
103
+ getFailureEvent: (metadata, settings, error) => ({
104
+ type: "text-embedding-finished",
105
+ status: "failure",
106
+ metadata,
107
+ settings,
108
+ error,
109
+ texts,
110
+ }),
111
+ getSuccessEvent: (metadata, settings, response, output) => ({
112
+ type: "text-embedding-finished",
113
+ status: "success",
114
+ metadata,
115
+ settings,
116
+ texts,
117
+ response,
118
+ generatedEmbeddings: [output],
119
+ }),
68
120
  });
69
- return options?.fullResponse === true
70
- ? {
71
- embedding: result.embeddings[0],
72
- metadata: result.metadata,
73
- }
74
- : result.embeddings[0];
75
121
  }
76
122
  exports.embedText = embedText;
@@ -1,6 +1,6 @@
1
1
  import { Vector } from "../../run/Vector.js";
2
2
  import { FunctionOptions } from "../FunctionOptions.js";
3
- import { CallMetadata } from "../executeCall.js";
3
+ import { ModelFunctionPromise } from "../executeCall.js";
4
4
  import { TextEmbeddingModel, TextEmbeddingModelSettings } from "./TextEmbeddingModel.js";
5
5
  /**
6
6
  * Generate embeddings for multiple texts.
@@ -14,15 +14,7 @@ import { TextEmbeddingModel, TextEmbeddingModelSettings } from "./TextEmbeddingM
14
14
  * ]
15
15
  * );
16
16
  */
17
- export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, texts: string[], options: FunctionOptions<SETTINGS> & {
18
- fullResponse: true;
19
- }): Promise<{
20
- embeddings: Array<Vector>;
21
- metadata: CallMetadata<TextEmbeddingModel<RESPONSE, SETTINGS>>;
22
- }>;
23
- export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, texts: string[], options?: FunctionOptions<SETTINGS> & {
24
- fullResponse?: false;
25
- }): Promise<Array<Vector>>;
17
+ export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, texts: string[], options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<TextEmbeddingModel<RESPONSE, SETTINGS>, Vector[], RESPONSE[]>;
26
18
  /**
27
19
  * Generate an embedding for a single text.
28
20
  *
@@ -32,12 +24,4 @@ export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModel
32
24
  * "At first, Nox didn't know what to do with the pup."
33
25
  * );
34
26
  */
35
- export declare function embedText<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, text: string, options: FunctionOptions<SETTINGS> & {
36
- fullResponse: true;
37
- }): Promise<{
38
- embedding: Vector;
39
- metadata: CallMetadata<TextEmbeddingModel<RESPONSE, SETTINGS>>;
40
- }>;
41
- export declare function embedText<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, text: string, options?: FunctionOptions<SETTINGS> & {
42
- fullResponse?: false;
43
- }): Promise<Vector>;
27
+ export declare function embedText<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, text: string, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<TextEmbeddingModel<RESPONSE, SETTINGS>, Vector, RESPONSE>;
@@ -1,6 +1,18 @@
1
1
  import { executeCall } from "../executeCall.js";
2
- export async function embedTexts(model, texts, options) {
3
- const result = await executeCall({
2
+ /**
3
+ * Generate embeddings for multiple texts.
4
+ *
5
+ * @example
6
+ * const embeddings = await embedTexts(
7
+ * new OpenAITextEmbeddingModel(...),
8
+ * [
9
+ * "At first, Nox didn't know what to do with the pup.",
10
+ * "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
11
+ * ]
12
+ * );
13
+ */
14
+ export function embedTexts(model, texts, options) {
15
+ return executeCall({
4
16
  model,
5
17
  options,
6
18
  generateResponse: (options) => {
@@ -50,22 +62,56 @@ export async function embedTexts(model, texts, options) {
50
62
  generatedEmbeddings: output,
51
63
  }),
52
64
  });
53
- return options?.fullResponse === true
54
- ? {
55
- embeddings: result.output,
56
- metadata: result.metadata,
57
- }
58
- : result.output;
59
65
  }
60
- export async function embedText(model, text, options) {
61
- const result = await embedTexts(model, [text], {
62
- ...(options ?? {}),
63
- fullResponse: true,
66
+ /**
67
+ * Generate an embedding for a single text.
68
+ *
69
+ * @example
70
+ * const embedding = await embedText(
71
+ * new OpenAITextEmbeddingModel(...),
72
+ * "At first, Nox didn't know what to do with the pup."
73
+ * );
74
+ */
75
+ export function embedText(model, text, options) {
76
+ const texts = [text];
77
+ return executeCall({
78
+ model,
79
+ options,
80
+ generateResponse: (options) => {
81
+ return model.generateEmbeddingResponse(texts, options);
82
+ },
83
+ extractOutputValue: (result) => {
84
+ return model.extractEmbeddings(result)[0];
85
+ },
86
+ getStartEvent: (metadata, settings) => ({
87
+ type: "text-embedding-started",
88
+ metadata,
89
+ settings,
90
+ texts,
91
+ }),
92
+ getAbortEvent: (metadata, settings) => ({
93
+ type: "text-embedding-finished",
94
+ status: "abort",
95
+ metadata,
96
+ settings,
97
+ texts,
98
+ }),
99
+ getFailureEvent: (metadata, settings, error) => ({
100
+ type: "text-embedding-finished",
101
+ status: "failure",
102
+ metadata,
103
+ settings,
104
+ error,
105
+ texts,
106
+ }),
107
+ getSuccessEvent: (metadata, settings, response, output) => ({
108
+ type: "text-embedding-finished",
109
+ status: "success",
110
+ metadata,
111
+ settings,
112
+ texts,
113
+ response,
114
+ generatedEmbeddings: [output],
115
+ }),
64
116
  });
65
- return options?.fullResponse === true
66
- ? {
67
- embedding: result.embeddings[0],
68
- metadata: result.metadata,
69
- }
70
- : result.embeddings[0];
71
117
  }
@@ -1,12 +1,59 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.executeCall = void 0;
3
+ exports.ModelFunctionPromise = exports.executeCall = void 0;
4
4
  const nanoid_1 = require("nanoid");
5
5
  const RunFunctionEventSource_js_1 = require("../run/RunFunctionEventSource.cjs");
6
6
  const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
7
7
  const AbortError_js_1 = require("../util/api/AbortError.cjs");
8
8
  const runSafe_js_1 = require("../util/runSafe.cjs");
9
- async function executeCall({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }) {
9
+ function executeCall({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }) {
10
+ return new ModelFunctionPromise(doExecuteCall({
11
+ model,
12
+ options,
13
+ getStartEvent,
14
+ getAbortEvent,
15
+ getFailureEvent,
16
+ getSuccessEvent,
17
+ generateResponse,
18
+ extractOutputValue,
19
+ }));
20
+ }
21
+ exports.executeCall = executeCall;
22
+ class ModelFunctionPromise extends Promise {
23
+ constructor(fullPromise) {
24
+ super((resolve) => {
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ resolve(null); // we override the resolve function
27
+ });
28
+ Object.defineProperty(this, "fullPromise", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: fullPromise
33
+ });
34
+ Object.defineProperty(this, "outputPromise", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ this.outputPromise = fullPromise.then((result) => result.output);
41
+ }
42
+ asFullResponse() {
43
+ return this.fullPromise;
44
+ }
45
+ then(onfulfilled, onrejected) {
46
+ return this.outputPromise.then(onfulfilled, onrejected);
47
+ }
48
+ catch(onrejected) {
49
+ return this.outputPromise.catch(onrejected);
50
+ }
51
+ finally(onfinally) {
52
+ return this.outputPromise.finally(onfinally);
53
+ }
54
+ }
55
+ exports.ModelFunctionPromise = ModelFunctionPromise;
56
+ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }) {
10
57
  if (options?.settings != null) {
11
58
  model = model.withSettings(options.settings);
12
59
  options = {
@@ -57,4 +104,3 @@ async function executeCall({ model, options, getStartEvent, getAbortEvent, getFa
57
104
  metadata: finishMetadata,
58
105
  };
59
106
  }
60
- exports.executeCall = executeCall;
@@ -20,8 +20,21 @@ export declare function executeCall<SETTINGS extends ModelSettings, MODEL extend
20
20
  getSuccessEvent: (metadata: ModelCallFinishedEventMetadata, settings: SETTINGS, response: RESPONSE, output: OUTPUT) => ModelCallFinishedEvent;
21
21
  generateResponse: (options: FunctionOptions<SETTINGS>) => PromiseLike<RESPONSE>;
22
22
  extractOutputValue: (response: RESPONSE) => OUTPUT;
23
- }): Promise<{
24
- output: OUTPUT;
25
- response: RESPONSE;
26
- metadata: CallMetadata<MODEL>;
27
- }>;
23
+ }): ModelFunctionPromise<MODEL, OUTPUT, RESPONSE>;
24
+ export declare class ModelFunctionPromise<MODEL extends Model<any>, OUTPUT, RESPONSE> extends Promise<OUTPUT> {
25
+ private fullPromise;
26
+ private outputPromise;
27
+ constructor(fullPromise: Promise<{
28
+ output: OUTPUT;
29
+ response: RESPONSE;
30
+ metadata: CallMetadata<MODEL>;
31
+ }>);
32
+ asFullResponse(): Promise<{
33
+ output: OUTPUT;
34
+ response: RESPONSE;
35
+ metadata: CallMetadata<MODEL>;
36
+ }>;
37
+ then<TResult1 = OUTPUT, TResult2 = never>(onfulfilled?: ((value: OUTPUT) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
38
+ catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<OUTPUT | TResult>;
39
+ finally(onfinally?: (() => void) | undefined | null): Promise<OUTPUT>;
40
+ }
@@ -3,7 +3,52 @@ import { RunFunctionEventSource } from "../run/RunFunctionEventSource.js";
3
3
  import { startDurationMeasurement } from "../util/DurationMeasurement.js";
4
4
  import { AbortError } from "../util/api/AbortError.js";
5
5
  import { runSafe } from "../util/runSafe.js";
6
- export async function executeCall({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }) {
6
+ export function executeCall({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }) {
7
+ return new ModelFunctionPromise(doExecuteCall({
8
+ model,
9
+ options,
10
+ getStartEvent,
11
+ getAbortEvent,
12
+ getFailureEvent,
13
+ getSuccessEvent,
14
+ generateResponse,
15
+ extractOutputValue,
16
+ }));
17
+ }
18
+ export class ModelFunctionPromise extends Promise {
19
+ constructor(fullPromise) {
20
+ super((resolve) => {
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ resolve(null); // we override the resolve function
23
+ });
24
+ Object.defineProperty(this, "fullPromise", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: fullPromise
29
+ });
30
+ Object.defineProperty(this, "outputPromise", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ this.outputPromise = fullPromise.then((result) => result.output);
37
+ }
38
+ asFullResponse() {
39
+ return this.fullPromise;
40
+ }
41
+ then(onfulfilled, onrejected) {
42
+ return this.outputPromise.then(onfulfilled, onrejected);
43
+ }
44
+ catch(onrejected) {
45
+ return this.outputPromise.catch(onrejected);
46
+ }
47
+ finally(onfinally) {
48
+ return this.outputPromise.finally(onfinally);
49
+ }
50
+ }
51
+ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }) {
7
52
  if (options?.settings != null) {
8
53
  model = model.withSettings(options.settings);
9
54
  options = {
@@ -2,8 +2,23 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateImage = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
- async function generateImage(model, prompt, options) {
6
- const result = await (0, executeCall_js_1.executeCall)({
5
+ /**
6
+ * Generates a base64-encoded image using a prompt.
7
+ * The prompt format depends on the model.
8
+ * For example, OpenAI image models expect a string prompt,
9
+ * and Stability AI models expect an array of text prompts with optional weights.
10
+ *
11
+ * @example
12
+ * const image = await generateImage(
13
+ * new StabilityImageGenerationModel(...),
14
+ * [
15
+ * { text: "the wicked witch of the west" },
16
+ * { text: "style of early 19th century painting", weight: 0.5 },
17
+ * ]
18
+ * );
19
+ */
20
+ function generateImage(model, prompt, options) {
21
+ return (0, executeCall_js_1.executeCall)({
7
22
  model,
8
23
  options,
9
24
  generateResponse: (options) => model.generateImageResponse(prompt, options),
@@ -39,12 +54,5 @@ async function generateImage(model, prompt, options) {
39
54
  generatedImage: output,
40
55
  }),
41
56
  });
42
- return options?.fullResponse === true
43
- ? {
44
- image: result.output,
45
- response: result.response,
46
- metadata: result.metadata,
47
- }
48
- : result.output;
49
57
  }
50
58
  exports.generateImage = generateImage;
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../FunctionOptions.js";
2
- import { CallMetadata } from "../executeCall.js";
2
+ import { ModelFunctionPromise } from "../executeCall.js";
3
3
  import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGenerationModel.js";
4
4
  /**
5
5
  * Generates a base64-encoded image using a prompt.
@@ -16,13 +16,4 @@ import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGener
16
16
  * ]
17
17
  * );
18
18
  */
19
- export declare function generateImage<PROMPT, RESPONSE, SETTINGS extends ImageGenerationModelSettings>(model: ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, prompt: PROMPT, options: FunctionOptions<SETTINGS> & {
20
- fullResponse: true;
21
- }): Promise<{
22
- image: string;
23
- response: RESPONSE;
24
- metadata: CallMetadata<ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>>;
25
- }>;
26
- export declare function generateImage<PROMPT, RESPONSE, SETTINGS extends ImageGenerationModelSettings>(model: ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, prompt: PROMPT, options?: FunctionOptions<SETTINGS> & {
27
- fullResponse?: false;
28
- }): Promise<string>;
19
+ export declare function generateImage<PROMPT, RESPONSE, SETTINGS extends ImageGenerationModelSettings>(model: ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, prompt: PROMPT, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, string, RESPONSE>;
@@ -1,6 +1,21 @@
1
1
  import { executeCall } from "../executeCall.js";
2
- export async function generateImage(model, prompt, options) {
3
- const result = await executeCall({
2
+ /**
3
+ * Generates a base64-encoded image using a prompt.
4
+ * The prompt format depends on the model.
5
+ * For example, OpenAI image models expect a string prompt,
6
+ * and Stability AI models expect an array of text prompts with optional weights.
7
+ *
8
+ * @example
9
+ * const image = await generateImage(
10
+ * new StabilityImageGenerationModel(...),
11
+ * [
12
+ * { text: "the wicked witch of the west" },
13
+ * { text: "style of early 19th century painting", weight: 0.5 },
14
+ * ]
15
+ * );
16
+ */
17
+ export function generateImage(model, prompt, options) {
18
+ return executeCall({
4
19
  model,
5
20
  options,
6
21
  generateResponse: (options) => model.generateImageResponse(prompt, options),
@@ -36,11 +51,4 @@ export async function generateImage(model, prompt, options) {
36
51
  generatedImage: output,
37
52
  }),
38
53
  });
39
- return options?.fullResponse === true
40
- ? {
41
- image: result.output,
42
- response: result.response,
43
- metadata: result.metadata,
44
- }
45
- : result.output;
46
54
  }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateJson = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
5
  const SchemaValidationError_js_1 = require("./SchemaValidationError.cjs");
6
- async function generateJson(model, schemaDefinition, prompt, options) {
6
+ function generateJson(model, schemaDefinition, prompt, options) {
7
7
  const expandedPrompt = prompt(schemaDefinition);
8
- const result = await (0, executeCall_js_1.executeCall)({
8
+ return (0, executeCall_js_1.executeCall)({
9
9
  model,
10
10
  options,
11
11
  generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
@@ -52,12 +52,5 @@ async function generateJson(model, schemaDefinition, prompt, options) {
52
52
  generatedJson: output,
53
53
  }),
54
54
  });
55
- return options?.fullResponse === true
56
- ? {
57
- value: result.output,
58
- response: result.response,
59
- metadata: result.metadata,
60
- }
61
- : result.output;
62
55
  }
63
56
  exports.generateJson = generateJson;
@@ -1,14 +1,5 @@
1
1
  import { FunctionOptions } from "../FunctionOptions.js";
2
- import { CallMetadata } from "../executeCall.js";
2
+ import { ModelFunctionPromise } from "../executeCall.js";
3
3
  import { GenerateJsonModel, GenerateJsonModelSettings } from "./GenerateJsonModel.js";
4
4
  import { SchemaDefinition } from "./SchemaDefinition.js";
5
- export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options: FunctionOptions<SETTINGS> & {
6
- fullResponse: true;
7
- }): Promise<{
8
- value: STRUCTURE;
9
- response: RESPONSE;
10
- metadata: CallMetadata<GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>>;
11
- }>;
12
- export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options?: FunctionOptions<SETTINGS> & {
13
- fullResponse?: false;
14
- }): Promise<STRUCTURE>;
5
+ export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends GenerateJsonModelSettings>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, STRUCTURE, RESPONSE>;
@@ -1,8 +1,8 @@
1
1
  import { executeCall } from "../executeCall.js";
2
2
  import { SchemaValidationError } from "./SchemaValidationError.js";
3
- export async function generateJson(model, schemaDefinition, prompt, options) {
3
+ export function generateJson(model, schemaDefinition, prompt, options) {
4
4
  const expandedPrompt = prompt(schemaDefinition);
5
- const result = await executeCall({
5
+ return executeCall({
6
6
  model,
7
7
  options,
8
8
  generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
@@ -49,11 +49,4 @@ export async function generateJson(model, schemaDefinition, prompt, options) {
49
49
  generatedJson: output,
50
50
  }),
51
51
  });
52
- return options?.fullResponse === true
53
- ? {
54
- value: result.output,
55
- response: result.response,
56
- metadata: result.metadata,
57
- }
58
- : result.output;
59
52
  }
@@ -4,9 +4,9 @@ exports.generateJsonOrText = void 0;
4
4
  const executeCall_js_1 = require("../executeCall.cjs");
5
5
  const NoSuchSchemaError_js_1 = require("./NoSuchSchemaError.cjs");
6
6
  const SchemaValidationError_js_1 = require("./SchemaValidationError.cjs");
7
- async function generateJsonOrText(model, schemaDefinitions, prompt, options) {
7
+ function generateJsonOrText(model, schemaDefinitions, prompt, options) {
8
8
  const expandedPrompt = prompt(schemaDefinitions);
9
- const result = await (0, executeCall_js_1.executeCall)({
9
+ return (0, executeCall_js_1.executeCall)({
10
10
  model,
11
11
  options,
12
12
  generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
@@ -65,12 +65,5 @@ async function generateJsonOrText(model, schemaDefinitions, prompt, options) {
65
65
  generatedJson: output,
66
66
  }),
67
67
  });
68
- return options?.fullResponse === true
69
- ? {
70
- ...result.output,
71
- response: result.response,
72
- metadata: result.metadata,
73
- }
74
- : result.output;
75
68
  }
76
69
  exports.generateJsonOrText = generateJsonOrText;
@@ -1,5 +1,5 @@
1
1
  import { FunctionOptions } from "../FunctionOptions.js";
2
- import { CallMetadata } from "../executeCall.js";
2
+ import { ModelFunctionPromise } from "../executeCall.js";
3
3
  import { GenerateJsonOrTextModel, GenerateJsonOrTextModelSettings, GenerateJsonOrTextPrompt } from "./GenerateJsonOrTextModel.js";
4
4
  import { SchemaDefinition } from "./SchemaDefinition.js";
5
5
  type SchemaDefinitionArray<T extends SchemaDefinition<any, any>[]> = T;
@@ -14,21 +14,9 @@ type ToSchemaUnion<T> = {
14
14
  } : never;
15
15
  }[keyof T];
16
16
  type ToOutputValue<SCHEMAS extends SchemaDefinitionArray<SchemaDefinition<any, any>[]>> = ToSchemaUnion<ToSchemaDefinitionsMap<SCHEMAS>>;
17
- export declare function generateJsonOrText<SCHEMAS extends SchemaDefinition<any, any>[], PROMPT, RESPONSE, SETTINGS extends GenerateJsonOrTextModelSettings>(model: GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinitions: SCHEMAS, prompt: (schemaDefinitions: SCHEMAS) => PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options: FunctionOptions<SETTINGS> & {
18
- fullResponse: true;
19
- }): Promise<({
17
+ export declare function generateJsonOrText<SCHEMAS extends SchemaDefinition<any, any>[], PROMPT, RESPONSE, SETTINGS extends GenerateJsonOrTextModelSettings>(model: GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinitions: SCHEMAS, prompt: (schemaDefinitions: SCHEMAS) => PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>, {
20
18
  schema: null;
21
19
  value: null;
22
20
  text: string;
23
- } | ToOutputValue<SCHEMAS>) & {
24
- response: RESPONSE;
25
- metadata: CallMetadata<GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>>;
26
- }>;
27
- export declare function generateJsonOrText<SCHEMAS extends SchemaDefinition<any, any>[], PROMPT, RESPONSE, SETTINGS extends GenerateJsonOrTextModelSettings>(model: GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinitions: SCHEMAS, prompt: (schemaDefinitions: SCHEMAS) => PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS> & {
28
- fullResponse?: false;
29
- }): Promise<{
30
- schema: null;
31
- value: null;
32
- text: string;
33
- } | ToOutputValue<SCHEMAS>>;
21
+ } | ToOutputValue<SCHEMAS>, RESPONSE>;
34
22
  export {};
@@ -1,9 +1,9 @@
1
1
  import { executeCall } from "../executeCall.js";
2
2
  import { NoSuchSchemaError } from "./NoSuchSchemaError.js";
3
3
  import { SchemaValidationError } from "./SchemaValidationError.js";
4
- export async function generateJsonOrText(model, schemaDefinitions, prompt, options) {
4
+ export function generateJsonOrText(model, schemaDefinitions, prompt, options) {
5
5
  const expandedPrompt = prompt(schemaDefinitions);
6
- const result = await executeCall({
6
+ return executeCall({
7
7
  model,
8
8
  options,
9
9
  generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
@@ -62,11 +62,4 @@ export async function generateJsonOrText(model, schemaDefinitions, prompt, optio
62
62
  generatedJson: output,
63
63
  }),
64
64
  });
65
- return options?.fullResponse === true
66
- ? {
67
- ...result.output,
68
- response: result.response,
69
- metadata: result.metadata,
70
- }
71
- : result.output;
72
65
  }