modelfusion 0.112.0 → 0.114.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 (159) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +108 -212
  3. package/core/FunctionOptions.d.ts +14 -0
  4. package/core/api/AbstractApiConfiguration.cjs +16 -1
  5. package/core/api/AbstractApiConfiguration.d.ts +7 -3
  6. package/core/api/AbstractApiConfiguration.js +16 -1
  7. package/core/api/ApiConfiguration.d.ts +10 -1
  8. package/core/api/BaseUrlApiConfiguration.cjs +9 -5
  9. package/core/api/BaseUrlApiConfiguration.d.ts +7 -7
  10. package/core/api/BaseUrlApiConfiguration.js +9 -5
  11. package/core/api/CustomHeaderProvider.cjs +2 -0
  12. package/core/api/CustomHeaderProvider.d.ts +2 -0
  13. package/core/api/CustomHeaderProvider.js +1 -0
  14. package/core/api/index.cjs +1 -0
  15. package/core/api/index.d.ts +1 -0
  16. package/core/api/index.js +1 -0
  17. package/core/cache/Cache.cjs +2 -0
  18. package/core/cache/Cache.d.ts +12 -0
  19. package/core/cache/Cache.js +1 -0
  20. package/core/cache/MemoryCache.cjs +23 -0
  21. package/core/cache/MemoryCache.d.ts +15 -0
  22. package/core/cache/MemoryCache.js +19 -0
  23. package/core/cache/index.cjs +18 -0
  24. package/core/cache/index.d.ts +2 -0
  25. package/core/cache/index.js +2 -0
  26. package/core/index.cjs +1 -0
  27. package/core/index.d.ts +1 -0
  28. package/core/index.js +1 -0
  29. package/core/schema/TypeValidationError.cjs +36 -0
  30. package/core/schema/TypeValidationError.d.ts +15 -0
  31. package/core/schema/TypeValidationError.js +32 -0
  32. package/core/schema/index.cjs +2 -0
  33. package/core/schema/index.d.ts +2 -0
  34. package/core/schema/index.js +2 -0
  35. package/core/schema/parseJSON.cjs +6 -14
  36. package/core/schema/parseJSON.d.ts +3 -2
  37. package/core/schema/parseJSON.js +6 -14
  38. package/core/schema/validateTypes.cjs +65 -0
  39. package/core/schema/validateTypes.d.ts +34 -0
  40. package/core/schema/validateTypes.js +60 -0
  41. package/model-function/embed/EmbeddingModel.d.ts +2 -2
  42. package/model-function/executeStandardCall.cjs +3 -1
  43. package/model-function/executeStandardCall.d.ts +2 -2
  44. package/model-function/executeStandardCall.js +3 -1
  45. package/model-function/executeStreamCall.cjs +2 -1
  46. package/model-function/executeStreamCall.d.ts +2 -2
  47. package/model-function/executeStreamCall.js +2 -1
  48. package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
  49. package/model-function/generate-image/PromptTemplateImageGenerationModel.d.ts +2 -2
  50. package/model-function/generate-speech/SpeechGenerationModel.d.ts +3 -3
  51. package/model-function/generate-structure/StructureFromTextPromptTemplate.d.ts +13 -0
  52. package/model-function/generate-structure/generateStructure.cjs +4 -1
  53. package/model-function/generate-structure/generateStructure.js +4 -1
  54. package/model-function/generate-structure/jsonStructurePrompt.cjs +12 -0
  55. package/model-function/generate-structure/jsonStructurePrompt.d.ts +3 -3
  56. package/model-function/generate-structure/jsonStructurePrompt.js +12 -0
  57. package/model-function/generate-structure/streamStructure.cjs +4 -1
  58. package/model-function/generate-structure/streamStructure.js +4 -1
  59. package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +3 -0
  60. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +11 -2
  61. package/model-function/generate-text/PromptTemplateTextGenerationModel.js +3 -0
  62. package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +2 -2
  63. package/model-function/generate-text/TextGenerationModel.d.ts +16 -3
  64. package/model-function/generate-text/generateText.cjs +43 -1
  65. package/model-function/generate-text/generateText.js +43 -1
  66. package/model-function/generate-transcription/TranscriptionModel.d.ts +2 -2
  67. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +20 -8
  68. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +27 -5
  69. package/model-provider/anthropic/AnthropicTextGenerationModel.js +20 -8
  70. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +8 -3
  71. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  72. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +8 -3
  73. package/model-provider/cohere/CohereTextEmbeddingModel.cjs +8 -3
  74. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  75. package/model-provider/cohere/CohereTextEmbeddingModel.js +8 -3
  76. package/model-provider/cohere/CohereTextGenerationModel.cjs +20 -8
  77. package/model-provider/cohere/CohereTextGenerationModel.d.ts +45 -5
  78. package/model-provider/cohere/CohereTextGenerationModel.js +20 -8
  79. package/model-provider/cohere/CohereTokenizer.cjs +16 -6
  80. package/model-provider/cohere/CohereTokenizer.d.ts +3 -3
  81. package/model-provider/cohere/CohereTokenizer.js +16 -6
  82. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +1 -1
  83. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +1 -1
  84. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +8 -3
  85. package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +2 -2
  86. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +8 -3
  87. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +8 -3
  88. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
  89. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +8 -3
  90. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +18 -4
  91. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +21 -3
  92. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +18 -4
  93. package/model-provider/llamacpp/LlamaCppCompletionModel.cjs +20 -8
  94. package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +125 -5
  95. package/model-provider/llamacpp/LlamaCppCompletionModel.js +20 -8
  96. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +8 -3
  97. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
  98. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +8 -3
  99. package/model-provider/llamacpp/LlamaCppTokenizer.cjs +8 -3
  100. package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +2 -2
  101. package/model-provider/llamacpp/LlamaCppTokenizer.js +8 -3
  102. package/model-provider/lmnt/LmntSpeechModel.cjs +8 -3
  103. package/model-provider/lmnt/LmntSpeechModel.d.ts +2 -2
  104. package/model-provider/lmnt/LmntSpeechModel.js +8 -3
  105. package/model-provider/mistral/MistralChatModel.cjs +20 -8
  106. package/model-provider/mistral/MistralChatModel.d.ts +55 -5
  107. package/model-provider/mistral/MistralChatModel.js +20 -8
  108. package/model-provider/mistral/MistralTextEmbeddingModel.cjs +8 -3
  109. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +3 -3
  110. package/model-provider/mistral/MistralTextEmbeddingModel.js +8 -3
  111. package/model-provider/ollama/OllamaChatModel.cjs +35 -8
  112. package/model-provider/ollama/OllamaChatModel.d.ts +31 -5
  113. package/model-provider/ollama/OllamaChatModel.js +35 -8
  114. package/model-provider/ollama/OllamaCompletionModel.cjs +20 -7
  115. package/model-provider/ollama/OllamaCompletionModel.d.ts +43 -5
  116. package/model-provider/ollama/OllamaCompletionModel.js +20 -7
  117. package/model-provider/ollama/OllamaTextEmbeddingModel.cjs +8 -3
  118. package/model-provider/ollama/OllamaTextEmbeddingModel.d.ts +3 -3
  119. package/model-provider/ollama/OllamaTextEmbeddingModel.js +8 -3
  120. package/model-provider/openai/AbstractOpenAIChatModel.cjs +23 -13
  121. package/model-provider/openai/AbstractOpenAIChatModel.d.ts +94 -7
  122. package/model-provider/openai/AbstractOpenAIChatModel.js +23 -13
  123. package/model-provider/openai/AbstractOpenAICompletionModel.cjs +21 -9
  124. package/model-provider/openai/AbstractOpenAICompletionModel.d.ts +35 -5
  125. package/model-provider/openai/AbstractOpenAICompletionModel.js +21 -9
  126. package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +5 -2
  127. package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +2 -1
  128. package/model-provider/openai/AzureOpenAIApiConfiguration.js +5 -2
  129. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.cjs +12 -6
  130. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -5
  131. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.js +12 -6
  132. package/model-provider/openai/OpenAIChatModel.cjs +12 -4
  133. package/model-provider/openai/OpenAIChatModel.d.ts +3 -2
  134. package/model-provider/openai/OpenAIChatModel.js +12 -4
  135. package/model-provider/openai/OpenAIImageGenerationModel.cjs +10 -6
  136. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
  137. package/model-provider/openai/OpenAIImageGenerationModel.js +10 -6
  138. package/model-provider/openai/OpenAISpeechModel.cjs +9 -4
  139. package/model-provider/openai/OpenAISpeechModel.d.ts +3 -3
  140. package/model-provider/openai/OpenAISpeechModel.js +9 -4
  141. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +11 -6
  142. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
  143. package/model-provider/openai/OpenAITextEmbeddingModel.js +11 -6
  144. package/model-provider/openai/OpenAITranscriptionModel.cjs +9 -6
  145. package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -4
  146. package/model-provider/openai/OpenAITranscriptionModel.js +9 -6
  147. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +12 -4
  148. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +3 -2
  149. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +12 -4
  150. package/model-provider/stability/StabilityImageGenerationModel.cjs +10 -5
  151. package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
  152. package/model-provider/stability/StabilityImageGenerationModel.js +10 -5
  153. package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +9 -7
  154. package/model-provider/whispercpp/WhisperCppTranscriptionModel.d.ts +3 -3
  155. package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +9 -7
  156. package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +2 -1
  157. package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +3 -1
  158. package/observability/helicone/HeliconeOpenAIApiConfiguration.js +2 -1
  159. package/package.json +2 -2
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { ResponseHandler } from "../../core/api/postToApi.js";
5
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
@@ -27,10 +27,10 @@ export interface AbstractOpenAICompletionModelSettings extends TextGenerationMod
27
27
  */
28
28
  export declare abstract class AbstractOpenAICompletionModel<SETTINGS extends AbstractOpenAICompletionModelSettings> extends AbstractModel<SETTINGS> {
29
29
  constructor(settings: SETTINGS);
30
- callAPI<RESULT>(prompt: string, options: {
30
+ callAPI<RESULT>(prompt: string, callOptions: FunctionCallOptions, options: {
31
31
  responseFormat: OpenAITextResponseFormatType<RESULT>;
32
- } & FunctionOptions): Promise<RESULT>;
33
- doGenerateTexts(prompt: string, options?: FunctionOptions): Promise<{
32
+ }): Promise<RESULT>;
33
+ doGenerateTexts(prompt: string, options: FunctionCallOptions): Promise<{
34
34
  response: {
35
35
  object: "text_completion";
36
36
  model: string;
@@ -59,8 +59,38 @@ export declare abstract class AbstractOpenAICompletionModel<SETTINGS extends Abs
59
59
  totalTokens: number;
60
60
  };
61
61
  }>;
62
+ restoreGeneratedTexts(rawResponse: unknown): {
63
+ response: {
64
+ object: "text_completion";
65
+ model: string;
66
+ usage: {
67
+ prompt_tokens: number;
68
+ completion_tokens: number;
69
+ total_tokens: number;
70
+ };
71
+ id: string;
72
+ created: number;
73
+ choices: {
74
+ text: string;
75
+ index: number;
76
+ finish_reason?: "length" | "stop" | "content_filter" | null | undefined;
77
+ logprobs?: any;
78
+ }[];
79
+ system_fingerprint?: string | undefined;
80
+ };
81
+ textGenerationResults: {
82
+ finishReason: TextGenerationFinishReason;
83
+ text: string;
84
+ }[];
85
+ usage: {
86
+ promptTokens: number;
87
+ completionTokens: number;
88
+ totalTokens: number;
89
+ };
90
+ };
91
+ private processTextGenerationResponse;
62
92
  private translateFinishReason;
63
- doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
93
+ doStreamText(prompt: string, options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
64
94
  object: "text_completion";
65
95
  model: string;
66
96
  id: string;
@@ -2,6 +2,7 @@ import { z } from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
4
  import { zodSchema } from "../../core/schema/ZodSchema.js";
5
+ import { validateTypes } from "../../core/schema/validateTypes.js";
5
6
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
7
  import { createEventSourceResponseHandler } from "../../util/streaming/createEventSourceResponseHandler.js";
7
8
  import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
@@ -15,12 +16,12 @@ export class AbstractOpenAICompletionModel extends AbstractModel {
15
16
  constructor(settings) {
16
17
  super({ settings });
17
18
  }
18
- async callAPI(prompt, options) {
19
+ async callAPI(prompt, callOptions, options) {
19
20
  const api = this.settings.api ?? new OpenAIApiConfiguration();
20
21
  const user = this.settings.isUserIdForwardingEnabled
21
- ? options.run?.userId
22
+ ? callOptions.run?.userId
22
23
  : undefined;
23
- const abortSignal = options.run?.abortSignal;
24
+ const abortSignal = callOptions.run?.abortSignal;
24
25
  const openaiResponseFormat = options.responseFormat;
25
26
  // empty arrays are not allowed for stop:
26
27
  const stopSequences = this.settings.stopSequences != null &&
@@ -33,7 +34,12 @@ export class AbstractOpenAICompletionModel extends AbstractModel {
33
34
  throttle: api.throttle,
34
35
  call: async () => postJsonToApi({
35
36
  url: api.assembleUrl("/completions"),
36
- headers: api.headers,
37
+ headers: api.headers({
38
+ functionType: callOptions.functionType,
39
+ functionId: callOptions.functionId,
40
+ run: callOptions.run,
41
+ callId: callOptions.callId,
42
+ }),
37
43
  body: {
38
44
  stream: openaiResponseFormat.stream,
39
45
  model: this.settings.model,
@@ -60,10 +66,17 @@ export class AbstractOpenAICompletionModel extends AbstractModel {
60
66
  });
61
67
  }
62
68
  async doGenerateTexts(prompt, options) {
63
- const response = await this.callAPI(prompt, {
64
- ...options,
69
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
65
70
  responseFormat: OpenAITextResponseFormat.json,
66
- });
71
+ }));
72
+ }
73
+ restoreGeneratedTexts(rawResponse) {
74
+ return this.processTextGenerationResponse(validateTypes({
75
+ structure: rawResponse,
76
+ schema: zodSchema(OpenAICompletionResponseSchema),
77
+ }));
78
+ }
79
+ processTextGenerationResponse(response) {
67
80
  return {
68
81
  response,
69
82
  textGenerationResults: response.choices.map((choice) => {
@@ -92,8 +105,7 @@ export class AbstractOpenAICompletionModel extends AbstractModel {
92
105
  }
93
106
  }
94
107
  doStreamText(prompt, options) {
95
- return this.callAPI(prompt, {
96
- ...options,
108
+ return this.callAPI(prompt, options, {
97
109
  responseFormat: OpenAITextResponseFormat.deltaIterable,
98
110
  });
99
111
  }
@@ -31,7 +31,7 @@ class AzureOpenAIApiConfiguration extends AbstractApiConfiguration_js_1.Abstract
31
31
  writable: true,
32
32
  value: void 0
33
33
  });
34
- Object.defineProperty(this, "headers", {
34
+ Object.defineProperty(this, "fixedHeaderValue", {
35
35
  enumerable: true,
36
36
  configurable: true,
37
37
  writable: true,
@@ -40,7 +40,7 @@ class AzureOpenAIApiConfiguration extends AbstractApiConfiguration_js_1.Abstract
40
40
  this.resourceName = resourceName;
41
41
  this.deploymentId = deploymentId;
42
42
  this.apiVersion = apiVersion;
43
- this.headers = {
43
+ this.fixedHeaderValue = {
44
44
  "api-key": (0, loadApiKey_js_1.loadApiKey)({
45
45
  apiKey,
46
46
  environmentVariableName: "AZURE_OPENAI_API_KEY",
@@ -51,5 +51,8 @@ class AzureOpenAIApiConfiguration extends AbstractApiConfiguration_js_1.Abstract
51
51
  assembleUrl(path) {
52
52
  return `https://${this.resourceName}.openai.azure.com/openai/deployments/${this.deploymentId}${path}?api-version=${this.apiVersion}`;
53
53
  }
54
+ fixedHeaders() {
55
+ return this.fixedHeaderValue;
56
+ }
54
57
  }
55
58
  exports.AzureOpenAIApiConfiguration = AzureOpenAIApiConfiguration;
@@ -20,7 +20,8 @@ export declare class AzureOpenAIApiConfiguration extends AbstractApiConfiguratio
20
20
  readonly resourceName: string;
21
21
  readonly deploymentId: string;
22
22
  readonly apiVersion: string;
23
- readonly headers: Record<string, string>;
23
+ readonly fixedHeaderValue: Record<string, string>;
24
24
  constructor({ resourceName, deploymentId, apiVersion, apiKey, retry, throttle, }: AzureOpenAIApiConfigurationOptions);
25
25
  assembleUrl(path: string): string;
26
+ fixedHeaders(): Record<string, string>;
26
27
  }
@@ -28,7 +28,7 @@ export class AzureOpenAIApiConfiguration extends AbstractApiConfiguration {
28
28
  writable: true,
29
29
  value: void 0
30
30
  });
31
- Object.defineProperty(this, "headers", {
31
+ Object.defineProperty(this, "fixedHeaderValue", {
32
32
  enumerable: true,
33
33
  configurable: true,
34
34
  writable: true,
@@ -37,7 +37,7 @@ export class AzureOpenAIApiConfiguration extends AbstractApiConfiguration {
37
37
  this.resourceName = resourceName;
38
38
  this.deploymentId = deploymentId;
39
39
  this.apiVersion = apiVersion;
40
- this.headers = {
40
+ this.fixedHeaderValue = {
41
41
  "api-key": loadApiKey({
42
42
  apiKey,
43
43
  environmentVariableName: "AZURE_OPENAI_API_KEY",
@@ -48,4 +48,7 @@ export class AzureOpenAIApiConfiguration extends AbstractApiConfiguration {
48
48
  assembleUrl(path) {
49
49
  return `https://${this.resourceName}.openai.azure.com/openai/deployments/${this.deploymentId}${path}?api-version=${this.apiVersion}`;
50
50
  }
51
+ fixedHeaders() {
52
+ return this.fixedHeaderValue;
53
+ }
51
54
  }
@@ -44,10 +44,18 @@ class OpenAIChatFunctionCallStructureGenerationModel {
44
44
  return this.model.modelInformation;
45
45
  }
46
46
  get settings() {
47
- return this.model.settings;
47
+ return {
48
+ ...this.model.settings,
49
+ fnName: this.fnName,
50
+ fnDescription: this.fnDescription,
51
+ };
48
52
  }
49
53
  get settingsForEvent() {
50
- return this.model.settingsForEvent;
54
+ return {
55
+ ...this.model.settingsForEvent,
56
+ fnName: this.fnName,
57
+ fnDescription: this.fnDescription,
58
+ };
51
59
  }
52
60
  /**
53
61
  * Returns this model with a text prompt template.
@@ -100,8 +108,7 @@ class OpenAIChatFunctionCallStructureGenerationModel {
100
108
  ...this.promptTemplate.stopSequences,
101
109
  ],
102
110
  })
103
- .callAPI(expandedPrompt, {
104
- ...options,
111
+ .callAPI(expandedPrompt, options, {
105
112
  responseFormat: AbstractOpenAIChatModel_js_1.OpenAIChatResponseFormat.json,
106
113
  functionCall: { name: this.fnName },
107
114
  functions: [
@@ -131,8 +138,7 @@ class OpenAIChatFunctionCallStructureGenerationModel {
131
138
  async doStreamStructure(schema, prompt, // first argument of the function
132
139
  options) {
133
140
  const expandedPrompt = this.promptTemplate.format(prompt);
134
- return this.model.callAPI(expandedPrompt, {
135
- ...options,
141
+ return this.model.callAPI(expandedPrompt, options, {
136
142
  responseFormat: AbstractOpenAIChatModel_js_1.OpenAIChatResponseFormat.deltaIterable,
137
143
  functionCall: { name: this.fnName },
138
144
  functions: [
@@ -1,4 +1,4 @@
1
- import { FunctionOptions } from "../../core/FunctionOptions.js";
1
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
2
2
  import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
3
3
  import { Schema } from "../../core/schema/Schema.js";
4
4
  import { StructureStreamingModel } from "../../model-function/generate-structure/StructureGenerationModel.js";
@@ -18,8 +18,92 @@ OpenAIChatSettings> {
18
18
  promptTemplate: PROMPT_TEMPLATE;
19
19
  });
20
20
  get modelInformation(): import("../../index.js").ModelInformation;
21
- get settings(): OpenAIChatSettings;
22
- get settingsForEvent(): Partial<OpenAIChatSettings>;
21
+ get settings(): {
22
+ fnName: string;
23
+ fnDescription: string | undefined;
24
+ model: import("./OpenAIChatModel.js").OpenAIChatModelType;
25
+ api?: import("../../index.js").ApiConfiguration | undefined;
26
+ functions?: {
27
+ name: string;
28
+ description?: string | undefined;
29
+ parameters: unknown;
30
+ }[] | undefined;
31
+ functionCall?: "auto" | {
32
+ name: string;
33
+ } | "none" | undefined;
34
+ tools?: {
35
+ type: "function";
36
+ function: {
37
+ name: string;
38
+ description?: string | undefined;
39
+ parameters: unknown;
40
+ };
41
+ }[] | undefined;
42
+ toolChoice?: "auto" | "none" | {
43
+ type: "function";
44
+ function: {
45
+ name: string;
46
+ };
47
+ } | undefined;
48
+ temperature?: number | undefined;
49
+ topP?: number | undefined;
50
+ seed?: number | null | undefined;
51
+ presencePenalty?: number | undefined;
52
+ frequencyPenalty?: number | undefined;
53
+ responseFormat?: {
54
+ type?: "text" | "json_object" | undefined;
55
+ } | undefined;
56
+ logitBias?: Record<number, number> | undefined;
57
+ isUserIdForwardingEnabled?: boolean | undefined;
58
+ maxGenerationTokens?: number | undefined;
59
+ stopSequences?: string[] | undefined;
60
+ numberOfGenerations?: number | undefined;
61
+ trimWhitespace?: boolean | undefined;
62
+ observers?: import("../../index.js").FunctionObserver[] | undefined;
63
+ };
64
+ get settingsForEvent(): {
65
+ fnName: string;
66
+ fnDescription: string | undefined;
67
+ model?: import("./OpenAIChatModel.js").OpenAIChatModelType | undefined;
68
+ api?: import("../../index.js").ApiConfiguration | undefined;
69
+ functions?: {
70
+ name: string;
71
+ description?: string | undefined;
72
+ parameters: unknown;
73
+ }[] | undefined;
74
+ functionCall?: "auto" | {
75
+ name: string;
76
+ } | "none" | undefined;
77
+ tools?: {
78
+ type: "function";
79
+ function: {
80
+ name: string;
81
+ description?: string | undefined;
82
+ parameters: unknown;
83
+ };
84
+ }[] | undefined;
85
+ toolChoice?: "auto" | "none" | {
86
+ type: "function";
87
+ function: {
88
+ name: string;
89
+ };
90
+ } | undefined;
91
+ temperature?: number | undefined;
92
+ topP?: number | undefined;
93
+ seed?: number | null | undefined;
94
+ presencePenalty?: number | undefined;
95
+ frequencyPenalty?: number | undefined;
96
+ responseFormat?: {
97
+ type?: "text" | "json_object" | undefined;
98
+ } | undefined;
99
+ logitBias?: Record<number, number> | undefined;
100
+ isUserIdForwardingEnabled?: boolean | undefined;
101
+ maxGenerationTokens?: number | undefined;
102
+ stopSequences?: string[] | undefined;
103
+ numberOfGenerations?: number | undefined;
104
+ trimWhitespace?: boolean | undefined;
105
+ observers?: import("../../index.js").FunctionObserver[] | undefined;
106
+ };
23
107
  /**
24
108
  * Returns this model with a text prompt template.
25
109
  */
@@ -42,7 +126,7 @@ OpenAIChatSettings> {
42
126
  * @see https://platform.openai.com/docs/guides/gpt/function-calling
43
127
  */
44
128
  doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: Parameters<PROMPT_TEMPLATE["format"]>[0], // first argument of the function
45
- options?: FunctionOptions): Promise<{
129
+ options: FunctionCallOptions): Promise<{
46
130
  response: {
47
131
  object: "chat.completion";
48
132
  model: string;
@@ -85,7 +169,7 @@ OpenAIChatSettings> {
85
169
  };
86
170
  }>;
87
171
  doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: Parameters<PROMPT_TEMPLATE["format"]>[0], // first argument of the function
88
- options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
172
+ options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
89
173
  object: "chat.completion.chunk";
90
174
  model: string;
91
175
  id: string;
@@ -38,10 +38,18 @@ export class OpenAIChatFunctionCallStructureGenerationModel {
38
38
  return this.model.modelInformation;
39
39
  }
40
40
  get settings() {
41
- return this.model.settings;
41
+ return {
42
+ ...this.model.settings,
43
+ fnName: this.fnName,
44
+ fnDescription: this.fnDescription,
45
+ };
42
46
  }
43
47
  get settingsForEvent() {
44
- return this.model.settingsForEvent;
48
+ return {
49
+ ...this.model.settingsForEvent,
50
+ fnName: this.fnName,
51
+ fnDescription: this.fnDescription,
52
+ };
45
53
  }
46
54
  /**
47
55
  * Returns this model with a text prompt template.
@@ -94,8 +102,7 @@ export class OpenAIChatFunctionCallStructureGenerationModel {
94
102
  ...this.promptTemplate.stopSequences,
95
103
  ],
96
104
  })
97
- .callAPI(expandedPrompt, {
98
- ...options,
105
+ .callAPI(expandedPrompt, options, {
99
106
  responseFormat: OpenAIChatResponseFormat.json,
100
107
  functionCall: { name: this.fnName },
101
108
  functions: [
@@ -125,8 +132,7 @@ export class OpenAIChatFunctionCallStructureGenerationModel {
125
132
  async doStreamStructure(schema, prompt, // first argument of the function
126
133
  options) {
127
134
  const expandedPrompt = this.promptTemplate.format(prompt);
128
- return this.model.callAPI(expandedPrompt, {
129
- ...options,
135
+ return this.model.callAPI(expandedPrompt, options, {
130
136
  responseFormat: OpenAIChatResponseFormat.deltaIterable,
131
137
  functionCall: { name: this.fnName },
132
138
  functions: [
@@ -219,10 +219,15 @@ class OpenAIChatModel extends AbstractOpenAIChatModel_js_1.AbstractOpenAIChatMod
219
219
  });
220
220
  }
221
221
  asStructureGenerationModel(promptTemplate) {
222
- return new StructureFromTextStreamingModel_js_1.StructureFromTextStreamingModel({
223
- model: this,
224
- template: promptTemplate,
225
- });
222
+ return "adaptModel" in promptTemplate
223
+ ? new StructureFromTextStreamingModel_js_1.StructureFromTextStreamingModel({
224
+ model: promptTemplate.adaptModel(this),
225
+ template: promptTemplate,
226
+ })
227
+ : new StructureFromTextStreamingModel_js_1.StructureFromTextStreamingModel({
228
+ model: this,
229
+ template: promptTemplate,
230
+ });
226
231
  }
227
232
  /**
228
233
  * Returns this model with a text prompt template.
@@ -253,6 +258,9 @@ class OpenAIChatModel extends AbstractOpenAIChatModel_js_1.AbstractOpenAIChatMod
253
258
  promptTemplate,
254
259
  });
255
260
  }
261
+ withJsonOutput() {
262
+ return this.withSettings({ responseFormat: { type: "json_object" } });
263
+ }
256
264
  withSettings(additionalSettings) {
257
265
  return new OpenAIChatModel(Object.assign({}, this.settings, additionalSettings));
258
266
  }
@@ -1,4 +1,4 @@
1
- import { StructureFromTextPromptTemplate } from "../../model-function/generate-structure/StructureFromTextPromptTemplate.js";
1
+ import { FlexibleStructureFromTextPromptTemplate, StructureFromTextPromptTemplate } from "../../model-function/generate-structure/StructureFromTextPromptTemplate.js";
2
2
  import { StructureFromTextStreamingModel } from "../../model-function/generate-structure/StructureFromTextStreamingModel.js";
3
3
  import { PromptTemplateFullTextModel } from "../../model-function/generate-text/PromptTemplateFullTextModel.js";
4
4
  import { TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
@@ -140,7 +140,7 @@ export declare class OpenAIChatModel extends AbstractOpenAIChatModel<OpenAIChatS
140
140
  fnName: string;
141
141
  fnDescription?: string;
142
142
  }): OpenAIChatFunctionCallStructureGenerationModel<TextGenerationPromptTemplate<OpenAIChatPrompt, OpenAIChatPrompt>>;
143
- asStructureGenerationModel<INPUT_PROMPT>(promptTemplate: StructureFromTextPromptTemplate<INPUT_PROMPT, OpenAIChatPrompt>): StructureFromTextStreamingModel<INPUT_PROMPT, OpenAIChatPrompt, this>;
143
+ asStructureGenerationModel<INPUT_PROMPT, OpenAIChatPrompt>(promptTemplate: StructureFromTextPromptTemplate<INPUT_PROMPT, OpenAIChatPrompt> | FlexibleStructureFromTextPromptTemplate<INPUT_PROMPT, unknown>): StructureFromTextStreamingModel<INPUT_PROMPT, unknown, TextStreamingModel<unknown, import("../../model-function/generate-text/TextGenerationModel.js").TextGenerationModelSettings>> | StructureFromTextStreamingModel<INPUT_PROMPT, OpenAIChatPrompt, TextStreamingModel<OpenAIChatPrompt, import("../../model-function/generate-text/TextGenerationModel.js").TextGenerationModelSettings>>;
144
144
  /**
145
145
  * Returns this model with a text prompt template.
146
146
  */
@@ -154,6 +154,7 @@ export declare class OpenAIChatModel extends AbstractOpenAIChatModel<OpenAIChatS
154
154
  */
155
155
  withChatPrompt(): PromptTemplateFullTextModel<import("../../index.js").ChatPrompt, OpenAIChatPrompt, OpenAIChatSettings, this>;
156
156
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, OpenAIChatPrompt>): PromptTemplateFullTextModel<INPUT_PROMPT, OpenAIChatPrompt, OpenAIChatSettings, this>;
157
+ withJsonOutput(): this;
157
158
  withSettings(additionalSettings: Partial<OpenAIChatSettings>): this;
158
159
  }
159
160
  export {};
@@ -213,10 +213,15 @@ export class OpenAIChatModel extends AbstractOpenAIChatModel {
213
213
  });
214
214
  }
215
215
  asStructureGenerationModel(promptTemplate) {
216
- return new StructureFromTextStreamingModel({
217
- model: this,
218
- template: promptTemplate,
219
- });
216
+ return "adaptModel" in promptTemplate
217
+ ? new StructureFromTextStreamingModel({
218
+ model: promptTemplate.adaptModel(this),
219
+ template: promptTemplate,
220
+ })
221
+ : new StructureFromTextStreamingModel({
222
+ model: this,
223
+ template: promptTemplate,
224
+ });
220
225
  }
221
226
  /**
222
227
  * Returns this model with a text prompt template.
@@ -247,6 +252,9 @@ export class OpenAIChatModel extends AbstractOpenAIChatModel {
247
252
  promptTemplate,
248
253
  });
249
254
  }
255
+ withJsonOutput() {
256
+ return this.withSettings({ responseFormat: { type: "json_object" } });
257
+ }
250
258
  withSettings(additionalSettings) {
251
259
  return new OpenAIChatModel(Object.assign({}, this.settings, additionalSettings));
252
260
  }
@@ -88,17 +88,22 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
88
88
  get modelName() {
89
89
  return this.settings.model;
90
90
  }
91
- async callAPI(prompt, options) {
91
+ async callAPI(prompt, callOptions, options) {
92
92
  const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
93
- const abortSignal = options.run?.abortSignal;
94
- const userId = options.run?.userId;
93
+ const abortSignal = callOptions.run?.abortSignal;
94
+ const userId = callOptions.run?.userId;
95
95
  const responseFormat = options.responseFormat;
96
96
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
97
97
  retry: api.retry,
98
98
  throttle: api.throttle,
99
99
  call: async () => (0, postToApi_js_1.postJsonToApi)({
100
100
  url: api.assembleUrl("/images/generations"),
101
- headers: api.headers,
101
+ headers: api.headers({
102
+ functionType: callOptions.functionType,
103
+ functionId: callOptions.functionId,
104
+ run: callOptions.run,
105
+ callId: callOptions.callId,
106
+ }),
102
107
  body: {
103
108
  prompt,
104
109
  n: this.settings.numberOfGenerations,
@@ -122,9 +127,8 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
122
127
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
123
128
  }
124
129
  async doGenerateImages(prompt, options) {
125
- const response = await this.callAPI(prompt, {
130
+ const response = await this.callAPI(prompt, options, {
126
131
  responseFormat: exports.OpenAIImageGenerationResponseFormat.base64Json,
127
- ...options,
128
132
  });
129
133
  return {
130
134
  response,
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { ResponseHandler } from "../../core/api/postToApi.js";
5
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
@@ -47,11 +47,11 @@ export declare class OpenAIImageGenerationModel extends AbstractModel<OpenAIImag
47
47
  constructor(settings: OpenAIImageGenerationSettings);
48
48
  readonly provider: "openai";
49
49
  get modelName(): "dall-e-2" | "dall-e-3";
50
- callAPI<RESULT>(prompt: string, options: {
50
+ callAPI<RESULT>(prompt: string, callOptions: FunctionCallOptions, options: {
51
51
  responseFormat: OpenAIImageGenerationResponseFormatType<RESULT>;
52
- } & FunctionOptions): Promise<RESULT>;
52
+ }): Promise<RESULT>;
53
53
  get settingsForEvent(): Partial<OpenAIImageGenerationSettings>;
54
- doGenerateImages(prompt: string, options?: FunctionOptions): Promise<{
54
+ doGenerateImages(prompt: string, options: FunctionCallOptions): Promise<{
55
55
  response: {
56
56
  data: {
57
57
  b64_json: string;
@@ -84,17 +84,22 @@ export class OpenAIImageGenerationModel extends AbstractModel {
84
84
  get modelName() {
85
85
  return this.settings.model;
86
86
  }
87
- async callAPI(prompt, options) {
87
+ async callAPI(prompt, callOptions, options) {
88
88
  const api = this.settings.api ?? new OpenAIApiConfiguration();
89
- const abortSignal = options.run?.abortSignal;
90
- const userId = options.run?.userId;
89
+ const abortSignal = callOptions.run?.abortSignal;
90
+ const userId = callOptions.run?.userId;
91
91
  const responseFormat = options.responseFormat;
92
92
  return callWithRetryAndThrottle({
93
93
  retry: api.retry,
94
94
  throttle: api.throttle,
95
95
  call: async () => postJsonToApi({
96
96
  url: api.assembleUrl("/images/generations"),
97
- headers: api.headers,
97
+ headers: api.headers({
98
+ functionType: callOptions.functionType,
99
+ functionId: callOptions.functionId,
100
+ run: callOptions.run,
101
+ callId: callOptions.callId,
102
+ }),
98
103
  body: {
99
104
  prompt,
100
105
  n: this.settings.numberOfGenerations,
@@ -118,9 +123,8 @@ export class OpenAIImageGenerationModel extends AbstractModel {
118
123
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
119
124
  }
120
125
  async doGenerateImages(prompt, options) {
121
- const response = await this.callAPI(prompt, {
126
+ const response = await this.callAPI(prompt, options, {
122
127
  responseFormat: OpenAIImageGenerationResponseFormat.base64Json,
123
- ...options,
124
128
  });
125
129
  return {
126
130
  response,
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpenAISpeechModel = exports.calculateOpenAISpeechCostInMillicents = exports.OPENAI_SPEECH_MODELS = void 0;
4
- const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
5
4
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
5
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
6
+ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
7
7
  const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
8
8
  const OpenAIError_js_1 = require("./OpenAIError.cjs");
9
9
  /**
@@ -45,15 +45,20 @@ class OpenAISpeechModel extends AbstractModel_js_1.AbstractModel {
45
45
  get modelName() {
46
46
  return this.settings.model;
47
47
  }
48
- async callAPI(text, options) {
48
+ async callAPI(text, callOptions) {
49
49
  const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
50
- const abortSignal = options?.run?.abortSignal;
50
+ const abortSignal = callOptions.run?.abortSignal;
51
51
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
52
52
  retry: api.retry,
53
53
  throttle: api.throttle,
54
54
  call: async () => (0, postToApi_js_1.postJsonToApi)({
55
55
  url: api.assembleUrl(`/audio/speech`),
56
- headers: api.headers,
56
+ headers: api.headers({
57
+ functionType: callOptions.functionType,
58
+ functionId: callOptions.functionId,
59
+ run: callOptions.run,
60
+ callId: callOptions.callId,
61
+ }),
57
62
  body: {
58
63
  input: text,
59
64
  voice: this.settings.voice,
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
- import { AbstractModel } from "../../model-function/AbstractModel.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
- import { FunctionOptions } from "../../core/FunctionOptions.js";
4
+ import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { SpeechGenerationModel, SpeechGenerationModelSettings } from "../../model-function/generate-speech/SpeechGenerationModel.js";
6
6
  /**
7
7
  * @see https://openai.com/pricing
@@ -46,7 +46,7 @@ export declare class OpenAISpeechModel extends AbstractModel<OpenAISpeechModelSe
46
46
  get modelName(): "tts-1" | "tts-1-hd";
47
47
  private callAPI;
48
48
  get settingsForEvent(): Partial<OpenAISpeechModelSettings>;
49
- doGenerateSpeechStandard(text: string, options?: FunctionOptions): Promise<Buffer>;
49
+ doGenerateSpeechStandard(text: string, options: FunctionCallOptions): Promise<Buffer>;
50
50
  withSettings(additionalSettings: Partial<OpenAISpeechModelSettings>): this;
51
51
  }
52
52
  export {};