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
@@ -7,6 +7,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
7
7
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
8
8
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
9
9
  const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
10
+ const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
10
11
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
11
12
  const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
12
13
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
@@ -43,16 +44,21 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
43
44
  get contextWindowSize() {
44
45
  return this.settings.contextWindowSize;
45
46
  }
46
- async callAPI(prompt, options) {
47
+ async callAPI(prompt, callOptions, options) {
47
48
  const { responseFormat } = options;
48
49
  const api = this.settings.api ?? new OllamaApiConfiguration_js_1.OllamaApiConfiguration();
49
- const abortSignal = options.run?.abortSignal;
50
+ const abortSignal = callOptions.run?.abortSignal;
50
51
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
51
52
  retry: api.retry,
52
53
  throttle: api.throttle,
53
54
  call: async () => (0, postToApi_js_1.postJsonToApi)({
54
55
  url: api.assembleUrl(`/api/generate`),
55
- headers: api.headers,
56
+ headers: api.headers({
57
+ functionType: callOptions.functionType,
58
+ functionId: callOptions.functionId,
59
+ run: callOptions.run,
60
+ callId: callOptions.callId,
61
+ }),
56
62
  body: {
57
63
  stream: responseFormat.stream,
58
64
  model: this.settings.model,
@@ -114,10 +120,17 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
114
120
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
115
121
  }
116
122
  async doGenerateTexts(prompt, options) {
117
- const response = await this.callAPI(prompt, {
118
- ...options,
123
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
119
124
  responseFormat: exports.OllamaCompletionResponseFormat.json,
120
- });
125
+ }));
126
+ }
127
+ restoreGeneratedTexts(rawResponse) {
128
+ return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
129
+ structure: rawResponse,
130
+ schema: (0, ZodSchema_js_1.zodSchema)(ollamaCompletionResponseSchema),
131
+ }));
132
+ }
133
+ processTextGenerationResponse(response) {
121
134
  return {
122
135
  response,
123
136
  textGenerationResults: [
@@ -129,7 +142,7 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
129
142
  };
130
143
  }
131
144
  doStreamText(prompt, options) {
132
- return this.callAPI(prompt, {
145
+ return this.callAPI(prompt, options, {
133
146
  ...options,
134
147
  responseFormat: exports.OllamaCompletionResponseFormat.deltaIterable,
135
148
  });
@@ -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 OllamaCompletionModel<CONTEXT_WINDOW_SIZE extends number |
47
47
  readonly tokenizer: undefined;
48
48
  readonly countPromptTokens: undefined;
49
49
  get contextWindowSize(): CONTEXT_WINDOW_SIZE;
50
- callAPI<RESPONSE>(prompt: OllamaCompletionPrompt, options: {
50
+ callAPI<RESPONSE>(prompt: OllamaCompletionPrompt, callOptions: FunctionCallOptions, options: {
51
51
  responseFormat: OllamaCompletionResponseFormatType<RESPONSE>;
52
- } & FunctionOptions): Promise<RESPONSE>;
52
+ }): Promise<RESPONSE>;
53
53
  get settingsForEvent(): Partial<OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>>;
54
- doGenerateTexts(prompt: OllamaCompletionPrompt, options?: FunctionOptions): Promise<{
54
+ doGenerateTexts(prompt: OllamaCompletionPrompt, options: FunctionCallOptions): Promise<{
55
55
  response: {
56
56
  response: string;
57
57
  model: string;
@@ -70,7 +70,45 @@ export declare class OllamaCompletionModel<CONTEXT_WINDOW_SIZE extends number |
70
70
  finishReason: "unknown";
71
71
  }[];
72
72
  }>;
73
- doStreamText(prompt: OllamaCompletionPrompt, options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
73
+ restoreGeneratedTexts(rawResponse: unknown): {
74
+ response: {
75
+ response: string;
76
+ model: string;
77
+ done: true;
78
+ created_at: string;
79
+ total_duration: number;
80
+ prompt_eval_count: number;
81
+ eval_count: number;
82
+ eval_duration: number;
83
+ load_duration?: number | undefined;
84
+ prompt_eval_duration?: number | undefined;
85
+ context?: number[] | undefined;
86
+ };
87
+ textGenerationResults: {
88
+ text: string;
89
+ finishReason: "unknown";
90
+ }[];
91
+ };
92
+ processTextGenerationResponse(response: OllamaCompletionResponse): {
93
+ response: {
94
+ response: string;
95
+ model: string;
96
+ done: true;
97
+ created_at: string;
98
+ total_duration: number;
99
+ prompt_eval_count: number;
100
+ eval_count: number;
101
+ eval_duration: number;
102
+ load_duration?: number | undefined;
103
+ prompt_eval_duration?: number | undefined;
104
+ context?: number[] | undefined;
105
+ };
106
+ textGenerationResults: {
107
+ text: string;
108
+ finishReason: "unknown";
109
+ }[];
110
+ };
111
+ doStreamText(prompt: OllamaCompletionPrompt, options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
74
112
  response: string;
75
113
  model: string;
76
114
  done: false;
@@ -4,6 +4,7 @@ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottl
4
4
  import { postJsonToApi } from "../../core/api/postToApi.js";
5
5
  import { zodSchema } from "../../core/schema/ZodSchema.js";
6
6
  import { safeParseJSON } from "../../core/schema/parseJSON.js";
7
+ import { validateTypes } from "../../core/schema/validateTypes.js";
7
8
  import { AbstractModel } from "../../model-function/AbstractModel.js";
8
9
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
9
10
  import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
@@ -40,16 +41,21 @@ export class OllamaCompletionModel extends AbstractModel {
40
41
  get contextWindowSize() {
41
42
  return this.settings.contextWindowSize;
42
43
  }
43
- async callAPI(prompt, options) {
44
+ async callAPI(prompt, callOptions, options) {
44
45
  const { responseFormat } = options;
45
46
  const api = this.settings.api ?? new OllamaApiConfiguration();
46
- const abortSignal = options.run?.abortSignal;
47
+ const abortSignal = callOptions.run?.abortSignal;
47
48
  return callWithRetryAndThrottle({
48
49
  retry: api.retry,
49
50
  throttle: api.throttle,
50
51
  call: async () => postJsonToApi({
51
52
  url: api.assembleUrl(`/api/generate`),
52
- headers: api.headers,
53
+ headers: api.headers({
54
+ functionType: callOptions.functionType,
55
+ functionId: callOptions.functionId,
56
+ run: callOptions.run,
57
+ callId: callOptions.callId,
58
+ }),
53
59
  body: {
54
60
  stream: responseFormat.stream,
55
61
  model: this.settings.model,
@@ -111,10 +117,17 @@ export class OllamaCompletionModel extends AbstractModel {
111
117
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
112
118
  }
113
119
  async doGenerateTexts(prompt, options) {
114
- const response = await this.callAPI(prompt, {
115
- ...options,
120
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
116
121
  responseFormat: OllamaCompletionResponseFormat.json,
117
- });
122
+ }));
123
+ }
124
+ restoreGeneratedTexts(rawResponse) {
125
+ return this.processTextGenerationResponse(validateTypes({
126
+ structure: rawResponse,
127
+ schema: zodSchema(ollamaCompletionResponseSchema),
128
+ }));
129
+ }
130
+ processTextGenerationResponse(response) {
118
131
  return {
119
132
  response,
120
133
  textGenerationResults: [
@@ -126,7 +139,7 @@ export class OllamaCompletionModel extends AbstractModel {
126
139
  };
127
140
  }
128
141
  doStreamText(prompt, options) {
129
- return this.callAPI(prompt, {
142
+ return this.callAPI(prompt, options, {
130
143
  ...options,
131
144
  responseFormat: OllamaCompletionResponseFormat.deltaIterable,
132
145
  });
@@ -33,18 +33,23 @@ class OllamaTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
33
33
  get embeddingDimensions() {
34
34
  return this.settings.embeddingDimensions;
35
35
  }
36
- async callAPI(texts, options) {
36
+ async callAPI(texts, callOptions) {
37
37
  if (texts.length > this.maxValuesPerCall) {
38
38
  throw new Error(`The Ollama embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
39
39
  }
40
40
  const api = this.settings.api ?? new OllamaApiConfiguration_js_1.OllamaApiConfiguration();
41
- const abortSignal = options?.run?.abortSignal;
41
+ const abortSignal = callOptions.run?.abortSignal;
42
42
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
43
43
  retry: api.retry,
44
44
  throttle: api.throttle,
45
45
  call: async () => (0, postToApi_js_1.postJsonToApi)({
46
46
  url: api.assembleUrl(`/api/embeddings`),
47
- headers: api.headers,
47
+ headers: api.headers({
48
+ functionType: callOptions.functionType,
49
+ functionId: callOptions.functionId,
50
+ run: callOptions.run,
51
+ callId: callOptions.callId,
52
+ }),
48
53
  body: {
49
54
  model: this.settings.model,
50
55
  prompt: texts[0],
@@ -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 { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { EmbeddingModel, EmbeddingModelSettings } from "../../model-function/embed/EmbeddingModel.js";
@@ -16,9 +16,9 @@ export declare class OllamaTextEmbeddingModel extends AbstractModel<OllamaTextEm
16
16
  readonly maxValuesPerCall = 1;
17
17
  get isParallelizable(): boolean;
18
18
  get embeddingDimensions(): number | undefined;
19
- callAPI(texts: Array<string>, options?: FunctionOptions): Promise<OllamaTextEmbeddingResponse>;
19
+ callAPI(texts: Array<string>, callOptions: FunctionCallOptions): Promise<OllamaTextEmbeddingResponse>;
20
20
  get settingsForEvent(): Partial<OllamaTextEmbeddingModelSettings>;
21
- doEmbedValues(texts: string[], options?: FunctionOptions): Promise<{
21
+ doEmbedValues(texts: string[], options: FunctionCallOptions): Promise<{
22
22
  response: {
23
23
  embedding: number[];
24
24
  };
@@ -30,18 +30,23 @@ export class OllamaTextEmbeddingModel extends AbstractModel {
30
30
  get embeddingDimensions() {
31
31
  return this.settings.embeddingDimensions;
32
32
  }
33
- async callAPI(texts, options) {
33
+ async callAPI(texts, callOptions) {
34
34
  if (texts.length > this.maxValuesPerCall) {
35
35
  throw new Error(`The Ollama embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
36
36
  }
37
37
  const api = this.settings.api ?? new OllamaApiConfiguration();
38
- const abortSignal = options?.run?.abortSignal;
38
+ const abortSignal = callOptions.run?.abortSignal;
39
39
  return callWithRetryAndThrottle({
40
40
  retry: api.retry,
41
41
  throttle: api.throttle,
42
42
  call: async () => postJsonToApi({
43
43
  url: api.assembleUrl(`/api/embeddings`),
44
- headers: api.headers,
44
+ headers: api.headers({
45
+ functionType: callOptions.functionType,
46
+ functionId: callOptions.functionId,
47
+ run: callOptions.run,
48
+ callId: callOptions.callId,
49
+ }),
45
50
  body: {
46
51
  model: this.settings.model,
47
52
  prompt: texts[0],
@@ -6,6 +6,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
7
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
8
  const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
9
+ const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
9
10
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
10
11
  const createEventSourceResponseHandler_js_1 = require("../../util/streaming/createEventSourceResponseHandler.cjs");
11
12
  const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
@@ -19,12 +20,12 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
19
20
  constructor(settings) {
20
21
  super({ settings });
21
22
  }
22
- async callAPI(messages, options) {
23
+ async callAPI(messages, callOptions, options) {
23
24
  const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
24
25
  const responseFormat = options.responseFormat;
25
- const abortSignal = options.run?.abortSignal;
26
+ const abortSignal = callOptions.run?.abortSignal;
26
27
  const user = this.settings.isUserIdForwardingEnabled
27
- ? options.run?.userId
28
+ ? callOptions.run?.userId
28
29
  : undefined;
29
30
  const openAIResponseFormat = this.settings.responseFormat;
30
31
  // function & tool calling:
@@ -45,7 +46,12 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
45
46
  }
46
47
  return (0, postToApi_js_1.postJsonToApi)({
47
48
  url: api.assembleUrl("/chat/completions"),
48
- headers: api.headers,
49
+ headers: api.headers({
50
+ functionType: callOptions.functionType,
51
+ functionId: callOptions.functionId,
52
+ run: callOptions.run,
53
+ callId: callOptions.callId,
54
+ }),
49
55
  body: {
50
56
  stream: responseFormat.stream,
51
57
  model: this.settings.model,
@@ -74,10 +80,17 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
74
80
  });
75
81
  }
76
82
  async doGenerateTexts(prompt, options) {
77
- const response = await this.callAPI(prompt, {
78
- ...options,
83
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
79
84
  responseFormat: exports.OpenAIChatResponseFormat.json,
80
- });
85
+ }));
86
+ }
87
+ restoreGeneratedTexts(rawResponse) {
88
+ return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
89
+ structure: rawResponse,
90
+ schema: (0, ZodSchema_js_1.zodSchema)(openAIChatResponseSchema),
91
+ }));
92
+ }
93
+ processTextGenerationResponse(response) {
81
94
  return {
82
95
  response,
83
96
  textGenerationResults: response.choices.map((choice) => ({
@@ -103,8 +116,7 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
103
116
  }
104
117
  }
105
118
  doStreamText(prompt, options) {
106
- return this.callAPI(prompt, {
107
- ...options,
119
+ return this.callAPI(prompt, options, {
108
120
  responseFormat: exports.OpenAIChatResponseFormat.deltaIterable,
109
121
  });
110
122
  }
@@ -121,8 +133,7 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
121
133
  return firstChoice.delta.content ?? undefined;
122
134
  }
123
135
  async doGenerateToolCall(tool, prompt, options) {
124
- const response = await this.callAPI(prompt, {
125
- ...options,
136
+ const response = await this.callAPI(prompt, options, {
126
137
  responseFormat: exports.OpenAIChatResponseFormat.json,
127
138
  toolChoice: {
128
139
  type: "function",
@@ -152,8 +163,7 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
152
163
  };
153
164
  }
154
165
  async doGenerateToolCalls(tools, prompt, options) {
155
- const response = await this.callAPI(prompt, {
156
- ...options,
166
+ const response = await this.callAPI(prompt, options, {
157
167
  responseFormat: exports.OpenAIChatResponseFormat.json,
158
168
  toolChoice: "auto",
159
169
  tools: tools.map((tool) => ({
@@ -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";
@@ -82,15 +82,14 @@ export type OpenAIChatPrompt = OpenAIChatMessage[];
82
82
  */
83
83
  export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractOpenAIChatSettings> extends AbstractModel<SETTINGS> {
84
84
  constructor(settings: SETTINGS);
85
- callAPI<RESULT>(messages: OpenAIChatPrompt, options: {
85
+ callAPI<RESULT>(messages: OpenAIChatPrompt, callOptions: FunctionCallOptions, options: {
86
86
  responseFormat: OpenAIChatResponseFormatType<RESULT>;
87
- } & FunctionOptions & {
88
87
  functions?: AbstractOpenAIChatSettings["functions"];
89
88
  functionCall?: AbstractOpenAIChatSettings["functionCall"];
90
89
  tools?: AbstractOpenAIChatSettings["tools"];
91
90
  toolChoice?: AbstractOpenAIChatSettings["toolChoice"];
92
91
  }): Promise<RESULT>;
93
- doGenerateTexts(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
92
+ doGenerateTexts(prompt: OpenAIChatPrompt, options: FunctionCallOptions): Promise<{
94
93
  response: {
95
94
  object: "chat.completion";
96
95
  model: string;
@@ -134,8 +133,96 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
134
133
  totalTokens: number;
135
134
  };
136
135
  }>;
136
+ restoreGeneratedTexts(rawResponse: unknown): {
137
+ response: {
138
+ object: "chat.completion";
139
+ model: string;
140
+ usage: {
141
+ prompt_tokens: number;
142
+ completion_tokens: number;
143
+ total_tokens: number;
144
+ };
145
+ id: string;
146
+ created: number;
147
+ choices: {
148
+ message: {
149
+ role: "assistant";
150
+ content: string | null;
151
+ function_call?: {
152
+ name: string;
153
+ arguments: string;
154
+ } | undefined;
155
+ tool_calls?: {
156
+ function: {
157
+ name: string;
158
+ arguments: string;
159
+ };
160
+ type: "function";
161
+ id: string;
162
+ }[] | undefined;
163
+ };
164
+ index: number;
165
+ logprobs?: any;
166
+ finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
167
+ }[];
168
+ system_fingerprint?: string | null | undefined;
169
+ };
170
+ textGenerationResults: {
171
+ text: string;
172
+ finishReason: TextGenerationFinishReason;
173
+ }[];
174
+ usage: {
175
+ promptTokens: number;
176
+ completionTokens: number;
177
+ totalTokens: number;
178
+ };
179
+ };
180
+ processTextGenerationResponse(response: OpenAIChatResponse): {
181
+ response: {
182
+ object: "chat.completion";
183
+ model: string;
184
+ usage: {
185
+ prompt_tokens: number;
186
+ completion_tokens: number;
187
+ total_tokens: number;
188
+ };
189
+ id: string;
190
+ created: number;
191
+ choices: {
192
+ message: {
193
+ role: "assistant";
194
+ content: string | null;
195
+ function_call?: {
196
+ name: string;
197
+ arguments: string;
198
+ } | undefined;
199
+ tool_calls?: {
200
+ function: {
201
+ name: string;
202
+ arguments: string;
203
+ };
204
+ type: "function";
205
+ id: string;
206
+ }[] | undefined;
207
+ };
208
+ index: number;
209
+ logprobs?: any;
210
+ finish_reason?: "length" | "stop" | "function_call" | "tool_calls" | "content_filter" | null | undefined;
211
+ }[];
212
+ system_fingerprint?: string | null | undefined;
213
+ };
214
+ textGenerationResults: {
215
+ text: string;
216
+ finishReason: TextGenerationFinishReason;
217
+ }[];
218
+ usage: {
219
+ promptTokens: number;
220
+ completionTokens: number;
221
+ totalTokens: number;
222
+ };
223
+ };
137
224
  private translateFinishReason;
138
- doStreamText(prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
225
+ doStreamText(prompt: OpenAIChatPrompt, options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
139
226
  object: "chat.completion.chunk";
140
227
  model: string;
141
228
  id: string;
@@ -163,7 +250,7 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
163
250
  system_fingerprint?: string | null | undefined;
164
251
  }>>>;
165
252
  extractTextDelta(delta: unknown): string | undefined;
166
- doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
253
+ doGenerateToolCall(tool: ToolDefinition<string, unknown>, prompt: OpenAIChatPrompt, options: FunctionCallOptions): Promise<{
167
254
  response: {
168
255
  object: "chat.completion";
169
256
  model: string;
@@ -207,7 +294,7 @@ export declare abstract class AbstractOpenAIChatModel<SETTINGS extends AbstractO
207
294
  totalTokens: number;
208
295
  };
209
296
  }>;
210
- doGenerateToolCalls(tools: Array<ToolDefinition<string, unknown>>, prompt: OpenAIChatPrompt, options?: FunctionOptions): Promise<{
297
+ doGenerateToolCalls(tools: Array<ToolDefinition<string, unknown>>, prompt: OpenAIChatPrompt, options: FunctionCallOptions): Promise<{
211
298
  response: {
212
299
  object: "chat.completion";
213
300
  model: string;
@@ -3,6 +3,7 @@ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottl
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
4
  import { zodSchema } from "../../core/schema/ZodSchema.js";
5
5
  import { parseJSON } from "../../core/schema/parseJSON.js";
6
+ import { validateTypes } from "../../core/schema/validateTypes.js";
6
7
  import { AbstractModel } from "../../model-function/AbstractModel.js";
7
8
  import { createEventSourceResponseHandler } from "../../util/streaming/createEventSourceResponseHandler.js";
8
9
  import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
@@ -16,12 +17,12 @@ export class AbstractOpenAIChatModel extends AbstractModel {
16
17
  constructor(settings) {
17
18
  super({ settings });
18
19
  }
19
- async callAPI(messages, options) {
20
+ async callAPI(messages, callOptions, options) {
20
21
  const api = this.settings.api ?? new OpenAIApiConfiguration();
21
22
  const responseFormat = options.responseFormat;
22
- const abortSignal = options.run?.abortSignal;
23
+ const abortSignal = callOptions.run?.abortSignal;
23
24
  const user = this.settings.isUserIdForwardingEnabled
24
- ? options.run?.userId
25
+ ? callOptions.run?.userId
25
26
  : undefined;
26
27
  const openAIResponseFormat = this.settings.responseFormat;
27
28
  // function & tool calling:
@@ -42,7 +43,12 @@ export class AbstractOpenAIChatModel extends AbstractModel {
42
43
  }
43
44
  return postJsonToApi({
44
45
  url: api.assembleUrl("/chat/completions"),
45
- headers: api.headers,
46
+ headers: api.headers({
47
+ functionType: callOptions.functionType,
48
+ functionId: callOptions.functionId,
49
+ run: callOptions.run,
50
+ callId: callOptions.callId,
51
+ }),
46
52
  body: {
47
53
  stream: responseFormat.stream,
48
54
  model: this.settings.model,
@@ -71,10 +77,17 @@ export class AbstractOpenAIChatModel extends AbstractModel {
71
77
  });
72
78
  }
73
79
  async doGenerateTexts(prompt, options) {
74
- const response = await this.callAPI(prompt, {
75
- ...options,
80
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
76
81
  responseFormat: OpenAIChatResponseFormat.json,
77
- });
82
+ }));
83
+ }
84
+ restoreGeneratedTexts(rawResponse) {
85
+ return this.processTextGenerationResponse(validateTypes({
86
+ structure: rawResponse,
87
+ schema: zodSchema(openAIChatResponseSchema),
88
+ }));
89
+ }
90
+ processTextGenerationResponse(response) {
78
91
  return {
79
92
  response,
80
93
  textGenerationResults: response.choices.map((choice) => ({
@@ -100,8 +113,7 @@ export class AbstractOpenAIChatModel extends AbstractModel {
100
113
  }
101
114
  }
102
115
  doStreamText(prompt, options) {
103
- return this.callAPI(prompt, {
104
- ...options,
116
+ return this.callAPI(prompt, options, {
105
117
  responseFormat: OpenAIChatResponseFormat.deltaIterable,
106
118
  });
107
119
  }
@@ -118,8 +130,7 @@ export class AbstractOpenAIChatModel extends AbstractModel {
118
130
  return firstChoice.delta.content ?? undefined;
119
131
  }
120
132
  async doGenerateToolCall(tool, prompt, options) {
121
- const response = await this.callAPI(prompt, {
122
- ...options,
133
+ const response = await this.callAPI(prompt, options, {
123
134
  responseFormat: OpenAIChatResponseFormat.json,
124
135
  toolChoice: {
125
136
  type: "function",
@@ -149,8 +160,7 @@ export class AbstractOpenAIChatModel extends AbstractModel {
149
160
  };
150
161
  }
151
162
  async doGenerateToolCalls(tools, prompt, options) {
152
- const response = await this.callAPI(prompt, {
153
- ...options,
163
+ const response = await this.callAPI(prompt, options, {
154
164
  responseFormat: OpenAIChatResponseFormat.json,
155
165
  toolChoice: "auto",
156
166
  tools: tools.map((tool) => ({
@@ -5,6 +5,7 @@ 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
7
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
+ const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
8
9
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
10
  const createEventSourceResponseHandler_js_1 = require("../../util/streaming/createEventSourceResponseHandler.cjs");
10
11
  const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
@@ -18,12 +19,12 @@ class AbstractOpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
18
19
  constructor(settings) {
19
20
  super({ settings });
20
21
  }
21
- async callAPI(prompt, options) {
22
+ async callAPI(prompt, callOptions, options) {
22
23
  const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
23
24
  const user = this.settings.isUserIdForwardingEnabled
24
- ? options.run?.userId
25
+ ? callOptions.run?.userId
25
26
  : undefined;
26
- const abortSignal = options.run?.abortSignal;
27
+ const abortSignal = callOptions.run?.abortSignal;
27
28
  const openaiResponseFormat = options.responseFormat;
28
29
  // empty arrays are not allowed for stop:
29
30
  const stopSequences = this.settings.stopSequences != null &&
@@ -36,7 +37,12 @@ class AbstractOpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
36
37
  throttle: api.throttle,
37
38
  call: async () => (0, postToApi_js_1.postJsonToApi)({
38
39
  url: api.assembleUrl("/completions"),
39
- headers: api.headers,
40
+ headers: api.headers({
41
+ functionType: callOptions.functionType,
42
+ functionId: callOptions.functionId,
43
+ run: callOptions.run,
44
+ callId: callOptions.callId,
45
+ }),
40
46
  body: {
41
47
  stream: openaiResponseFormat.stream,
42
48
  model: this.settings.model,
@@ -63,10 +69,17 @@ class AbstractOpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
63
69
  });
64
70
  }
65
71
  async doGenerateTexts(prompt, options) {
66
- const response = await this.callAPI(prompt, {
67
- ...options,
72
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
68
73
  responseFormat: exports.OpenAITextResponseFormat.json,
69
- });
74
+ }));
75
+ }
76
+ restoreGeneratedTexts(rawResponse) {
77
+ return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
78
+ structure: rawResponse,
79
+ schema: (0, ZodSchema_js_1.zodSchema)(OpenAICompletionResponseSchema),
80
+ }));
81
+ }
82
+ processTextGenerationResponse(response) {
70
83
  return {
71
84
  response,
72
85
  textGenerationResults: response.choices.map((choice) => {
@@ -95,8 +108,7 @@ class AbstractOpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
95
108
  }
96
109
  }
97
110
  doStreamText(prompt, options) {
98
- return this.callAPI(prompt, {
99
- ...options,
111
+ return this.callAPI(prompt, options, {
100
112
  responseFormat: exports.OpenAITextResponseFormat.deltaIterable,
101
113
  });
102
114
  }