modelfusion 0.113.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 (149) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +89 -89
  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/generateStructure.cjs +4 -1
  52. package/model-function/generate-structure/generateStructure.js +4 -1
  53. package/model-function/generate-structure/streamStructure.cjs +4 -1
  54. package/model-function/generate-structure/streamStructure.js +4 -1
  55. package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +3 -0
  56. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +11 -2
  57. package/model-function/generate-text/PromptTemplateTextGenerationModel.js +3 -0
  58. package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +2 -2
  59. package/model-function/generate-text/TextGenerationModel.d.ts +12 -3
  60. package/model-function/generate-text/generateText.cjs +43 -1
  61. package/model-function/generate-text/generateText.js +43 -1
  62. package/model-function/generate-transcription/TranscriptionModel.d.ts +2 -2
  63. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +20 -8
  64. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +27 -5
  65. package/model-provider/anthropic/AnthropicTextGenerationModel.js +20 -8
  66. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +8 -3
  67. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  68. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +8 -3
  69. package/model-provider/cohere/CohereTextEmbeddingModel.cjs +8 -3
  70. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  71. package/model-provider/cohere/CohereTextEmbeddingModel.js +8 -3
  72. package/model-provider/cohere/CohereTextGenerationModel.cjs +20 -8
  73. package/model-provider/cohere/CohereTextGenerationModel.d.ts +45 -5
  74. package/model-provider/cohere/CohereTextGenerationModel.js +20 -8
  75. package/model-provider/cohere/CohereTokenizer.cjs +16 -6
  76. package/model-provider/cohere/CohereTokenizer.d.ts +3 -3
  77. package/model-provider/cohere/CohereTokenizer.js +16 -6
  78. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +1 -1
  79. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +1 -1
  80. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +8 -3
  81. package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +2 -2
  82. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +8 -3
  83. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +8 -3
  84. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
  85. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +8 -3
  86. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +18 -4
  87. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +21 -3
  88. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +18 -4
  89. package/model-provider/llamacpp/LlamaCppCompletionModel.cjs +20 -8
  90. package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +125 -5
  91. package/model-provider/llamacpp/LlamaCppCompletionModel.js +20 -8
  92. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +8 -3
  93. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
  94. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +8 -3
  95. package/model-provider/llamacpp/LlamaCppTokenizer.cjs +8 -3
  96. package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +2 -2
  97. package/model-provider/llamacpp/LlamaCppTokenizer.js +8 -3
  98. package/model-provider/lmnt/LmntSpeechModel.cjs +8 -3
  99. package/model-provider/lmnt/LmntSpeechModel.d.ts +2 -2
  100. package/model-provider/lmnt/LmntSpeechModel.js +8 -3
  101. package/model-provider/mistral/MistralChatModel.cjs +20 -8
  102. package/model-provider/mistral/MistralChatModel.d.ts +55 -5
  103. package/model-provider/mistral/MistralChatModel.js +20 -8
  104. package/model-provider/mistral/MistralTextEmbeddingModel.cjs +8 -3
  105. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +3 -3
  106. package/model-provider/mistral/MistralTextEmbeddingModel.js +8 -3
  107. package/model-provider/ollama/OllamaChatModel.cjs +20 -8
  108. package/model-provider/ollama/OllamaChatModel.d.ts +27 -5
  109. package/model-provider/ollama/OllamaChatModel.js +20 -8
  110. package/model-provider/ollama/OllamaCompletionModel.cjs +20 -7
  111. package/model-provider/ollama/OllamaCompletionModel.d.ts +43 -5
  112. package/model-provider/ollama/OllamaCompletionModel.js +20 -7
  113. package/model-provider/ollama/OllamaTextEmbeddingModel.cjs +8 -3
  114. package/model-provider/ollama/OllamaTextEmbeddingModel.d.ts +3 -3
  115. package/model-provider/ollama/OllamaTextEmbeddingModel.js +8 -3
  116. package/model-provider/openai/AbstractOpenAIChatModel.cjs +23 -13
  117. package/model-provider/openai/AbstractOpenAIChatModel.d.ts +94 -7
  118. package/model-provider/openai/AbstractOpenAIChatModel.js +23 -13
  119. package/model-provider/openai/AbstractOpenAICompletionModel.cjs +21 -9
  120. package/model-provider/openai/AbstractOpenAICompletionModel.d.ts +35 -5
  121. package/model-provider/openai/AbstractOpenAICompletionModel.js +21 -9
  122. package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +5 -2
  123. package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +2 -1
  124. package/model-provider/openai/AzureOpenAIApiConfiguration.js +5 -2
  125. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.cjs +12 -6
  126. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -5
  127. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.js +12 -6
  128. package/model-provider/openai/OpenAIImageGenerationModel.cjs +10 -6
  129. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
  130. package/model-provider/openai/OpenAIImageGenerationModel.js +10 -6
  131. package/model-provider/openai/OpenAISpeechModel.cjs +9 -4
  132. package/model-provider/openai/OpenAISpeechModel.d.ts +3 -3
  133. package/model-provider/openai/OpenAISpeechModel.js +9 -4
  134. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +11 -6
  135. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
  136. package/model-provider/openai/OpenAITextEmbeddingModel.js +11 -6
  137. package/model-provider/openai/OpenAITranscriptionModel.cjs +9 -6
  138. package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -4
  139. package/model-provider/openai/OpenAITranscriptionModel.js +9 -6
  140. package/model-provider/stability/StabilityImageGenerationModel.cjs +10 -5
  141. package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
  142. package/model-provider/stability/StabilityImageGenerationModel.js +10 -5
  143. package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +9 -7
  144. package/model-provider/whispercpp/WhisperCppTranscriptionModel.d.ts +3 -3
  145. package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +9 -7
  146. package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +2 -1
  147. package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +3 -1
  148. package/observability/helicone/HeliconeOpenAIApiConfiguration.js +2 -1
  149. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,64 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.114.0 - 2024-01-05
4
+
5
+ ### Added
6
+
7
+ - Custom call header support for APIs. You can pass a `customCallHeaders` function into API configurations to add custom headers. The function is called with `functionType`, `functionId`, `run`, and `callId` parameters. Example for Helicone:
8
+
9
+ ```ts
10
+ const text = await generateText(
11
+ openai
12
+ .ChatTextGenerator({
13
+ api: new HeliconeOpenAIApiConfiguration({
14
+ customCallHeaders: ({ functionId, callId }) => ({
15
+ "Helicone-Property-FunctionId": functionId,
16
+ "Helicone-Property-CallId": callId,
17
+ }),
18
+ }),
19
+ model: "gpt-3.5-turbo",
20
+ temperature: 0.7,
21
+ maxGenerationTokens: 500,
22
+ })
23
+ .withTextPrompt(),
24
+
25
+ "Write a short story about a robot learning to love",
26
+
27
+ { functionId: "example-function" }
28
+ );
29
+ ```
30
+
31
+ - Rudimentary caching support for `generateText`. You can use a `MemoryCache` to store the response of a `generateText` call. Example:
32
+
33
+ ```ts
34
+ import { MemoryCache, generateText, ollama } from "modelfusion";
35
+
36
+ const model = ollama
37
+ .ChatTextGenerator({ model: "llama2:chat", maxGenerationTokens: 100 })
38
+ .withTextPrompt();
39
+
40
+ const cache = new MemoryCache();
41
+
42
+ const text1 = await generateText(
43
+ model,
44
+ "Write a short story about a robot learning to love:",
45
+ { cache }
46
+ );
47
+
48
+ console.log(text1);
49
+
50
+ // 2nd call will use cached response:
51
+ const text2 = await generateText(
52
+ model,
53
+ "Write a short story about a robot learning to love:", // same text
54
+ { cache }
55
+ );
56
+
57
+ console.log(text2);
58
+ ```
59
+
60
+ - `validateTypes` and `safeValidateTypes` helpers that perform type checking of an object against a `Schema` (e.g., a `zodSchema`).
61
+
3
62
  ## v0.113.0 - 2024-01-03
4
63
 
5
64
  [Structure generation](https://modelfusion.dev/guide/function/generate-structure) improvements.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ModelFusion
2
2
 
3
- > ### The TypeScript library for building sAI applications.
3
+ > ### The TypeScript library for building AI applications.
4
4
 
5
5
  [![NPM Version](https://img.shields.io/npm/v/modelfusion?color=33cd56&logo=npm)](https://www.npmjs.com/package/modelfusion)
6
6
  [![MIT License](https://img.shields.io/github/license/lgrammel/modelfusion)](https://opensource.org/licenses/MIT)
@@ -105,94 +105,7 @@ for await (const textPart of textStream) {
105
105
 
106
106
  Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [OpenAI compatible](https://modelfusion.dev/integration/model-provider/openaicompatible), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama)
107
107
 
108
- ### [Generate Image](https://modelfusion.dev/guide/function/generate-image)
109
-
110
- Generate an image from a prompt.
111
-
112
- ```ts
113
- import { generateImage, openai } from "modelfusion";
114
-
115
- const image = await generateImage(
116
- openai.ImageGenerator({ model: "dall-e-3", size: "1024x1024" }),
117
- "the wicked witch of the west in the style of early 19th century painting"
118
- );
119
- ```
120
-
121
- Providers: [OpenAI (Dall·E)](https://modelfusion.dev/integration/model-provider/openai), [Stability AI](https://modelfusion.dev/integration/model-provider/stability), [Automatic1111](https://modelfusion.dev/integration/model-provider/automatic1111)
122
-
123
- ### [Generate Speech](https://modelfusion.dev/guide/function/generate-speech)
124
-
125
- Synthesize speech (audio) from text. Also called TTS (text-to-speech).
126
-
127
- #### generateSpeech
128
-
129
- `generateSpeech` synthesizes speech from text.
130
-
131
- ```ts
132
- import { generateSpeech, lmnt } from "modelfusion";
133
-
134
- // `speech` is a Buffer with MP3 audio data
135
- const speech = await generateSpeech(
136
- lmnt.SpeechGenerator({
137
- voice: "034b632b-df71-46c8-b440-86a42ffc3cf3", // Henry
138
- }),
139
- "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
140
- "as The Rolling Stones unveil 'Hackney Diamonds,' their first collection of " +
141
- "fresh tunes in nearly twenty years, featuring the illustrious Lady Gaga, the " +
142
- "magical Stevie Wonder, and the final beats from the late Charlie Watts."
143
- );
144
- ```
145
-
146
- Providers: [Eleven Labs](https://modelfusion.dev/integration/model-provider/elevenlabs), [LMNT](https://modelfusion.dev/integration/model-provider/lmnt), [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
147
-
148
- #### streamSpeech
149
-
150
- `generateSpeech` generates a stream of speech chunks from text or from a text stream. Depending on the model, this can be fully duplex.
151
-
152
- ```ts
153
- import { streamSpeech, elevenlabs } from "modelfusion";
154
-
155
- const textStream: AsyncIterable<string>;
156
-
157
- const speechStream = await streamSpeech(
158
- elevenlabs.SpeechGenerator({
159
- model: "eleven_turbo_v2",
160
- voice: "pNInz6obpgDQGcFmaJgB", // Adam
161
- optimizeStreamingLatency: 1,
162
- voiceSettings: { stability: 1, similarityBoost: 0.35 },
163
- generationConfig: {
164
- chunkLengthSchedule: [50, 90, 120, 150, 200],
165
- },
166
- }),
167
- textStream
168
- );
169
-
170
- for await (const part of speechStream) {
171
- // each part is a Buffer with MP3 audio data
172
- }
173
- ```
174
-
175
- Providers: [Eleven Labs](https://modelfusion.dev/integration/model-provider/elevenlabs)
176
-
177
- ### [Generate Transcription](https://modelfusion.dev/guide/function/generate-transcription)
178
-
179
- Transcribe speech (audio) data into text. Also called speech-to-text (STT).
180
-
181
- ```ts
182
- import { generateTranscription, openai } from "modelfusion";
183
-
184
- const transcription = await generateTranscription(
185
- openai.Transcriber({ model: "whisper-1" }),
186
- {
187
- type: "mp3",
188
- data: await fs.promises.readFile("data/test.mp3"),
189
- }
190
- );
191
- ```
192
-
193
- Providers: [OpenAI (Whisper)](https://modelfusion.dev/integration/model-provider/openai), [Whisper.cpp](https://modelfusion.dev/integration/model-provider/whispercpp)
194
-
195
- ### [Generate Structure](https://modelfusion.dev/guide/function/generate-structure#generatestructure)
108
+ ### [Generate Structure](https://modelfusion.dev/guide/function/generate-structure)
196
109
 
197
110
  Generate typed objects using a language model and a schema.
198
111
 
@@ -287,6 +200,93 @@ for await (const part of structureStream) {
287
200
 
288
201
  Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
289
202
 
203
+ ### [Generate Image](https://modelfusion.dev/guide/function/generate-image)
204
+
205
+ Generate an image from a prompt.
206
+
207
+ ```ts
208
+ import { generateImage, openai } from "modelfusion";
209
+
210
+ const image = await generateImage(
211
+ openai.ImageGenerator({ model: "dall-e-3", size: "1024x1024" }),
212
+ "the wicked witch of the west in the style of early 19th century painting"
213
+ );
214
+ ```
215
+
216
+ Providers: [OpenAI (Dall·E)](https://modelfusion.dev/integration/model-provider/openai), [Stability AI](https://modelfusion.dev/integration/model-provider/stability), [Automatic1111](https://modelfusion.dev/integration/model-provider/automatic1111)
217
+
218
+ ### [Generate Speech](https://modelfusion.dev/guide/function/generate-speech)
219
+
220
+ Synthesize speech (audio) from text. Also called TTS (text-to-speech).
221
+
222
+ #### generateSpeech
223
+
224
+ `generateSpeech` synthesizes speech from text.
225
+
226
+ ```ts
227
+ import { generateSpeech, lmnt } from "modelfusion";
228
+
229
+ // `speech` is a Buffer with MP3 audio data
230
+ const speech = await generateSpeech(
231
+ lmnt.SpeechGenerator({
232
+ voice: "034b632b-df71-46c8-b440-86a42ffc3cf3", // Henry
233
+ }),
234
+ "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
235
+ "as The Rolling Stones unveil 'Hackney Diamonds,' their first collection of " +
236
+ "fresh tunes in nearly twenty years, featuring the illustrious Lady Gaga, the " +
237
+ "magical Stevie Wonder, and the final beats from the late Charlie Watts."
238
+ );
239
+ ```
240
+
241
+ Providers: [Eleven Labs](https://modelfusion.dev/integration/model-provider/elevenlabs), [LMNT](https://modelfusion.dev/integration/model-provider/lmnt), [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
242
+
243
+ #### streamSpeech
244
+
245
+ `generateSpeech` generates a stream of speech chunks from text or from a text stream. Depending on the model, this can be fully duplex.
246
+
247
+ ```ts
248
+ import { streamSpeech, elevenlabs } from "modelfusion";
249
+
250
+ const textStream: AsyncIterable<string>;
251
+
252
+ const speechStream = await streamSpeech(
253
+ elevenlabs.SpeechGenerator({
254
+ model: "eleven_turbo_v2",
255
+ voice: "pNInz6obpgDQGcFmaJgB", // Adam
256
+ optimizeStreamingLatency: 1,
257
+ voiceSettings: { stability: 1, similarityBoost: 0.35 },
258
+ generationConfig: {
259
+ chunkLengthSchedule: [50, 90, 120, 150, 200],
260
+ },
261
+ }),
262
+ textStream
263
+ );
264
+
265
+ for await (const part of speechStream) {
266
+ // each part is a Buffer with MP3 audio data
267
+ }
268
+ ```
269
+
270
+ Providers: [Eleven Labs](https://modelfusion.dev/integration/model-provider/elevenlabs)
271
+
272
+ ### [Generate Transcription](https://modelfusion.dev/guide/function/generate-transcription)
273
+
274
+ Transcribe speech (audio) data into text. Also called speech-to-text (STT).
275
+
276
+ ```ts
277
+ import { generateTranscription, openai } from "modelfusion";
278
+
279
+ const transcription = await generateTranscription(
280
+ openai.Transcriber({ model: "whisper-1" }),
281
+ {
282
+ type: "mp3",
283
+ data: await fs.promises.readFile("data/test.mp3"),
284
+ }
285
+ );
286
+ ```
287
+
288
+ Providers: [OpenAI (Whisper)](https://modelfusion.dev/integration/model-provider/openai), [Whisper.cpp](https://modelfusion.dev/integration/model-provider/whispercpp)
289
+
290
290
  ### [Embed Value](https://modelfusion.dev/guide/function/embed)
291
291
 
292
292
  Create embeddings for text and other values. Embeddings are vectors that represent the essence of the values in the context of the model.
@@ -1,6 +1,7 @@
1
1
  import { FunctionObserver } from "./FunctionObserver.js";
2
2
  import { LogFormat } from "./LogFormat.js";
3
3
  import { Run } from "./Run.js";
4
+ import { Cache } from "./cache/Cache.js";
4
5
  /**
5
6
  * Additional settings for ModelFusion functions.
6
7
  */
@@ -18,6 +19,11 @@ export type FunctionOptions = {
18
19
  * Optional observers that are called when the function is invoked.
19
20
  */
20
21
  observers?: Array<FunctionObserver>;
22
+ /**
23
+ * Optional cache that can be used by the function to store and retrieve cached values.
24
+ * Not supported by all functions.
25
+ */
26
+ cache?: Cache;
21
27
  /**
22
28
  * Optional run as part of which this function is called. Used in events and logging.
23
29
  * Run callbacks are invoked when it is provided.
@@ -28,3 +34,11 @@ export type FunctionOptions = {
28
34
  */
29
35
  parentCallId?: string | undefined;
30
36
  };
37
+ /**
38
+ * Extended options that are passed to models when functions are called. They are passed
39
+ * into e.g. API providers to create custom headers.
40
+ */
41
+ export type FunctionCallOptions = Omit<FunctionOptions, "parentCallId"> & {
42
+ functionType: string;
43
+ callId: string;
44
+ };
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractApiConfiguration = void 0;
4
4
  class AbstractApiConfiguration {
5
- constructor({ retry, throttle, }) {
5
+ constructor({ retry, throttle, customCallHeaders = () => ({}), }) {
6
6
  Object.defineProperty(this, "retry", {
7
7
  enumerable: true,
8
8
  configurable: true,
@@ -15,8 +15,23 @@ class AbstractApiConfiguration {
15
15
  writable: true,
16
16
  value: void 0
17
17
  });
18
+ Object.defineProperty(this, "customCallHeaders", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
18
24
  this.retry = retry;
19
25
  this.throttle = throttle;
26
+ this.customCallHeaders = customCallHeaders;
27
+ }
28
+ headers(params) {
29
+ return Object.fromEntries([
30
+ ...Object.entries(this.fixedHeaders(params)),
31
+ ...Object.entries(this.customCallHeaders(params)),
32
+ ].filter(
33
+ // remove undefined values:
34
+ (entry) => typeof entry[1] === "string"));
20
35
  }
21
36
  }
22
37
  exports.AbstractApiConfiguration = AbstractApiConfiguration;
@@ -1,13 +1,17 @@
1
1
  import { RetryFunction } from "./RetryFunction.js";
2
2
  import { ThrottleFunction } from "./ThrottleFunction.js";
3
- import { ApiConfiguration } from "./ApiConfiguration.js";
3
+ import { ApiConfiguration, HeaderParameters } from "./ApiConfiguration.js";
4
+ import { CustomHeaderProvider } from "./CustomHeaderProvider.js";
4
5
  export declare abstract class AbstractApiConfiguration implements ApiConfiguration {
5
6
  readonly retry?: RetryFunction;
6
7
  readonly throttle?: ThrottleFunction;
7
- constructor({ retry, throttle, }: {
8
+ protected readonly customCallHeaders: CustomHeaderProvider;
9
+ constructor({ retry, throttle, customCallHeaders, }: {
8
10
  retry?: RetryFunction;
9
11
  throttle?: ThrottleFunction;
12
+ customCallHeaders?: CustomHeaderProvider;
10
13
  });
11
14
  abstract assembleUrl(path: string): string;
12
- abstract readonly headers: Record<string, string>;
15
+ protected abstract fixedHeaders(params: HeaderParameters): Record<string, string>;
16
+ headers(params: HeaderParameters): Record<string, string>;
13
17
  }
@@ -1,5 +1,5 @@
1
1
  export class AbstractApiConfiguration {
2
- constructor({ retry, throttle, }) {
2
+ constructor({ retry, throttle, customCallHeaders = () => ({}), }) {
3
3
  Object.defineProperty(this, "retry", {
4
4
  enumerable: true,
5
5
  configurable: true,
@@ -12,7 +12,22 @@ export class AbstractApiConfiguration {
12
12
  writable: true,
13
13
  value: void 0
14
14
  });
15
+ Object.defineProperty(this, "customCallHeaders", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
15
21
  this.retry = retry;
16
22
  this.throttle = throttle;
23
+ this.customCallHeaders = customCallHeaders;
24
+ }
25
+ headers(params) {
26
+ return Object.fromEntries([
27
+ ...Object.entries(this.fixedHeaders(params)),
28
+ ...Object.entries(this.customCallHeaders(params)),
29
+ ].filter(
30
+ // remove undefined values:
31
+ (entry) => typeof entry[1] === "string"));
17
32
  }
18
33
  }
@@ -1,5 +1,12 @@
1
+ import { Run } from "../Run.js";
1
2
  import { RetryFunction } from "./RetryFunction.js";
2
3
  import { ThrottleFunction } from "./ThrottleFunction.js";
4
+ export type HeaderParameters = {
5
+ functionType: string;
6
+ functionId?: string;
7
+ run?: Run;
8
+ callId: string;
9
+ };
3
10
  /**
4
11
  * Settings for how to call an API, e.g. OpenAI.
5
12
  *
@@ -19,8 +26,10 @@ export interface ApiConfiguration {
19
26
  * This is intended for authentication headers and proxy settings.
20
27
  *
21
28
  * The model may add additional headers, in particular "Content-Type"."
29
+ *
30
+ * @param params Parameters that can be used to create the headers.
22
31
  */
23
- readonly headers: Record<string, string>;
32
+ headers(params: HeaderParameters): Record<string, string>;
24
33
  /**
25
34
  * Defines how failed API calls should be retried.
26
35
  */
@@ -8,22 +8,25 @@ const AbstractApiConfiguration_js_1 = require("./AbstractApiConfiguration.cjs");
8
8
  * You can use it to configure custom APIs for models, e.g. your own internal OpenAI proxy with custom headers.
9
9
  */
10
10
  class BaseUrlApiConfiguration extends AbstractApiConfiguration_js_1.AbstractApiConfiguration {
11
- constructor({ baseUrl, headers, retry, throttle, }) {
12
- super({ retry, throttle });
11
+ constructor({ baseUrl, headers, retry, throttle, customCallHeaders, }) {
12
+ super({ retry, throttle, customCallHeaders });
13
13
  Object.defineProperty(this, "baseUrl", {
14
14
  enumerable: true,
15
15
  configurable: true,
16
16
  writable: true,
17
17
  value: void 0
18
18
  });
19
- Object.defineProperty(this, "headers", {
19
+ Object.defineProperty(this, "fixedHeadersValue", {
20
20
  enumerable: true,
21
21
  configurable: true,
22
22
  writable: true,
23
23
  value: void 0
24
24
  });
25
25
  this.baseUrl = typeof baseUrl == "string" ? parseBaseUrl(baseUrl) : baseUrl;
26
- this.headers = headers ?? {};
26
+ this.fixedHeadersValue = headers ?? {};
27
+ }
28
+ fixedHeaders() {
29
+ return this.fixedHeadersValue;
27
30
  }
28
31
  assembleUrl(path) {
29
32
  let basePath = this.baseUrl.path;
@@ -40,12 +43,13 @@ class BaseUrlApiConfiguration extends AbstractApiConfiguration_js_1.AbstractApiC
40
43
  }
41
44
  exports.BaseUrlApiConfiguration = BaseUrlApiConfiguration;
42
45
  class BaseUrlApiConfigurationWithDefaults extends BaseUrlApiConfiguration {
43
- constructor({ baseUrlDefaults, baseUrl, headers, retry, throttle, }) {
46
+ constructor({ baseUrlDefaults, baseUrl, headers, retry, throttle, customCallHeaders, }) {
44
47
  super({
45
48
  baseUrl: resolveBaseUrl(baseUrl, baseUrlDefaults),
46
49
  headers,
47
50
  retry,
48
51
  throttle,
52
+ customCallHeaders,
49
53
  });
50
54
  }
51
55
  }
@@ -1,4 +1,5 @@
1
1
  import { AbstractApiConfiguration } from "./AbstractApiConfiguration.js";
2
+ import { CustomHeaderProvider } from "./CustomHeaderProvider.js";
2
3
  import { RetryFunction } from "./RetryFunction.js";
3
4
  import { ThrottleFunction } from "./ThrottleFunction.js";
4
5
  export type UrlParts = {
@@ -10,6 +11,7 @@ export type UrlParts = {
10
11
  export type BaseUrlPartsApiConfigurationOptions = {
11
12
  baseUrl: string | UrlParts;
12
13
  headers?: Record<string, string>;
14
+ customCallHeaders?: CustomHeaderProvider;
13
15
  retry?: RetryFunction;
14
16
  throttle?: ThrottleFunction;
15
17
  };
@@ -20,18 +22,16 @@ export type BaseUrlPartsApiConfigurationOptions = {
20
22
  */
21
23
  export declare class BaseUrlApiConfiguration extends AbstractApiConfiguration {
22
24
  readonly baseUrl: UrlParts;
23
- readonly headers: Record<string, string>;
24
- constructor({ baseUrl, headers, retry, throttle, }: BaseUrlPartsApiConfigurationOptions);
25
+ protected readonly fixedHeadersValue: Record<string, string>;
26
+ constructor({ baseUrl, headers, retry, throttle, customCallHeaders, }: BaseUrlPartsApiConfigurationOptions);
27
+ fixedHeaders(): Record<string, string>;
25
28
  assembleUrl(path: string): string;
26
29
  }
27
- export type PartialBaseUrlPartsApiConfigurationOptions = {
30
+ export type PartialBaseUrlPartsApiConfigurationOptions = Omit<BaseUrlPartsApiConfigurationOptions, "baseUrl"> & {
28
31
  baseUrl?: string | Partial<UrlParts>;
29
- headers?: Record<string, string>;
30
- retry?: RetryFunction;
31
- throttle?: ThrottleFunction;
32
32
  };
33
33
  export declare class BaseUrlApiConfigurationWithDefaults extends BaseUrlApiConfiguration {
34
- constructor({ baseUrlDefaults, baseUrl, headers, retry, throttle, }: {
34
+ constructor({ baseUrlDefaults, baseUrl, headers, retry, throttle, customCallHeaders, }: {
35
35
  baseUrlDefaults: UrlParts;
36
36
  } & PartialBaseUrlPartsApiConfigurationOptions);
37
37
  }
@@ -5,22 +5,25 @@ import { AbstractApiConfiguration } from "./AbstractApiConfiguration.js";
5
5
  * You can use it to configure custom APIs for models, e.g. your own internal OpenAI proxy with custom headers.
6
6
  */
7
7
  export class BaseUrlApiConfiguration extends AbstractApiConfiguration {
8
- constructor({ baseUrl, headers, retry, throttle, }) {
9
- super({ retry, throttle });
8
+ constructor({ baseUrl, headers, retry, throttle, customCallHeaders, }) {
9
+ super({ retry, throttle, customCallHeaders });
10
10
  Object.defineProperty(this, "baseUrl", {
11
11
  enumerable: true,
12
12
  configurable: true,
13
13
  writable: true,
14
14
  value: void 0
15
15
  });
16
- Object.defineProperty(this, "headers", {
16
+ Object.defineProperty(this, "fixedHeadersValue", {
17
17
  enumerable: true,
18
18
  configurable: true,
19
19
  writable: true,
20
20
  value: void 0
21
21
  });
22
22
  this.baseUrl = typeof baseUrl == "string" ? parseBaseUrl(baseUrl) : baseUrl;
23
- this.headers = headers ?? {};
23
+ this.fixedHeadersValue = headers ?? {};
24
+ }
25
+ fixedHeaders() {
26
+ return this.fixedHeadersValue;
24
27
  }
25
28
  assembleUrl(path) {
26
29
  let basePath = this.baseUrl.path;
@@ -36,12 +39,13 @@ export class BaseUrlApiConfiguration extends AbstractApiConfiguration {
36
39
  }
37
40
  }
38
41
  export class BaseUrlApiConfigurationWithDefaults extends BaseUrlApiConfiguration {
39
- constructor({ baseUrlDefaults, baseUrl, headers, retry, throttle, }) {
42
+ constructor({ baseUrlDefaults, baseUrl, headers, retry, throttle, customCallHeaders, }) {
40
43
  super({
41
44
  baseUrl: resolveBaseUrl(baseUrl, baseUrlDefaults),
42
45
  headers,
43
46
  retry,
44
47
  throttle,
48
+ customCallHeaders,
45
49
  });
46
50
  }
47
51
  }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { HeaderParameters } from "./ApiConfiguration.js";
2
+ export type CustomHeaderProvider = (headerParameters: HeaderParameters) => Record<string, string | undefined>;
@@ -0,0 +1 @@
1
+ export {};
@@ -32,6 +32,7 @@ __exportStar(require("./ApiCallError.cjs"), exports);
32
32
  __exportStar(require("./ApiConfiguration.cjs"), exports);
33
33
  exports.api = __importStar(require("./ApiFacade.cjs"));
34
34
  __exportStar(require("./BaseUrlApiConfiguration.cjs"), exports);
35
+ __exportStar(require("./CustomHeaderProvider.cjs"), exports);
35
36
  __exportStar(require("./RetryError.cjs"), exports);
36
37
  __exportStar(require("./RetryFunction.cjs"), exports);
37
38
  __exportStar(require("./ThrottleFunction.cjs"), exports);
@@ -3,6 +3,7 @@ export * from "./ApiCallError.js";
3
3
  export * from "./ApiConfiguration.js";
4
4
  export * as api from "./ApiFacade.js";
5
5
  export * from "./BaseUrlApiConfiguration.js";
6
+ export * from "./CustomHeaderProvider.js";
6
7
  export * from "./RetryError.js";
7
8
  export * from "./RetryFunction.js";
8
9
  export * from "./ThrottleFunction.js";
package/core/api/index.js CHANGED
@@ -3,6 +3,7 @@ export * from "./ApiCallError.js";
3
3
  export * from "./ApiConfiguration.js";
4
4
  export * as api from "./ApiFacade.js";
5
5
  export * from "./BaseUrlApiConfiguration.js";
6
+ export * from "./CustomHeaderProvider.js";
6
7
  export * from "./RetryError.js";
7
8
  export * from "./RetryFunction.js";
8
9
  export * from "./ThrottleFunction.js";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ export interface Cache {
2
+ lookupValue(key: {
3
+ functionType: string;
4
+ functionId?: string | undefined;
5
+ input: unknown;
6
+ }): Promise<object | null>;
7
+ storeValue(key: {
8
+ functionType: string;
9
+ functionId?: string | undefined;
10
+ input: unknown;
11
+ }, value: unknown): Promise<void>;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryCache = void 0;
4
+ class MemoryCache {
5
+ constructor() {
6
+ Object.defineProperty(this, "cache", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: new Map()
11
+ });
12
+ }
13
+ hashKey(key) {
14
+ return JSON.stringify(key);
15
+ }
16
+ async lookupValue(key) {
17
+ return this.cache.get(this.hashKey(key)) ?? null;
18
+ }
19
+ async storeValue(key, value) {
20
+ this.cache.set(this.hashKey(key), value);
21
+ }
22
+ }
23
+ exports.MemoryCache = MemoryCache;
@@ -0,0 +1,15 @@
1
+ import { Cache } from "./Cache.js";
2
+ export declare class MemoryCache implements Cache {
3
+ private readonly cache;
4
+ private hashKey;
5
+ lookupValue(key: {
6
+ functionType: string;
7
+ functionId?: string | undefined;
8
+ input: unknown;
9
+ }): Promise<object | null>;
10
+ storeValue(key: {
11
+ functionType: string;
12
+ functionId?: string | undefined;
13
+ input: unknown;
14
+ }, value: unknown): Promise<void>;
15
+ }
@@ -0,0 +1,19 @@
1
+ export class MemoryCache {
2
+ constructor() {
3
+ Object.defineProperty(this, "cache", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: new Map()
8
+ });
9
+ }
10
+ hashKey(key) {
11
+ return JSON.stringify(key);
12
+ }
13
+ async lookupValue(key) {
14
+ return this.cache.get(this.hashKey(key)) ?? null;
15
+ }
16
+ async storeValue(key, value) {
17
+ this.cache.set(this.hashKey(key), value);
18
+ }
19
+ }