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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,110 @@
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
+
62
+ ## v0.113.0 - 2024-01-03
63
+
64
+ [Structure generation](https://modelfusion.dev/guide/function/generate-structure) improvements.
65
+
66
+ ### Added
67
+
68
+ - `.asStructureGenerationModel(...)` function to `OpenAIChatModel` and `OllamaChatModel` to create structure generation models from chat models.
69
+ - `jsonStructurePrompt` helper function to create structure generation models.
70
+
71
+ ### Example
72
+
73
+ ```ts
74
+ import {
75
+ generateStructure,
76
+ jsonStructurePrompt,
77
+ ollama,
78
+ zodSchema,
79
+ } from "modelfusion";
80
+
81
+ const structure = await generateStructure(
82
+ ollama
83
+ .ChatTextGenerator({
84
+ model: "openhermes2.5-mistral",
85
+ maxGenerationTokens: 1024,
86
+ temperature: 0,
87
+ })
88
+ .asStructureGenerationModel(jsonStructurePrompt.text()),
89
+
90
+ zodSchema(
91
+ z.object({
92
+ characters: z.array(
93
+ z.object({
94
+ name: z.string(),
95
+ class: z
96
+ .string()
97
+ .describe("Character class, e.g. warrior, mage, or thief."),
98
+ description: z.string(),
99
+ })
100
+ ),
101
+ })
102
+ ),
103
+
104
+ "Generate 3 character descriptions for a fantasy role playing game. "
105
+ );
106
+ ```
107
+
3
108
  ## v0.112.0 - 2024-01-02
4
109
 
5
110
  ### Changed
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ModelFusion
2
2
 
3
- > ### The TypeScript library for building multi-modal AI 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)
@@ -12,7 +12,7 @@
12
12
 
13
13
  ## Introduction
14
14
 
15
- **ModelFusion** is an abstraction layer for integrating AI models into JavaScript and TypeScript applications, unifying the API for common operations such as text streaming, structure generation, and tool usage. It provides features to support production environments, including observability hooks, logging, and automatic retries. You can use ModelFusion to build AI applications, chatbots, and agents.
15
+ **ModelFusion** is an abstraction layer for integrating AI models into JavaScript and TypeScript applications, unifying the API for common operations such as **text streaming**, **structure generation**, and **tool usage**. It provides features to support production environments, including observability hooks, logging, and automatic retries. You can use ModelFusion to build AI applications, chatbots, and agents.
16
16
 
17
17
  - **Vendor-neutral**: ModelFusion is a non-commercial open source project that is community-driven. You can use it with any supported provider.
18
18
  - **Multi-modal**: ModelFusion supports a wide range of models including text generation, image generation, vision, text-to-speech, speech-to-text, and embedding models.
@@ -105,6 +105,101 @@ 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 Structure](https://modelfusion.dev/guide/function/generate-structure)
109
+
110
+ Generate typed objects using a language model and a schema.
111
+
112
+ #### generateStructure
113
+
114
+ Generate a structure that matches a schema.
115
+
116
+ ```ts
117
+ import {
118
+ ollama,
119
+ zodSchema,
120
+ generateStructure,
121
+ jsonStructurePrompt,
122
+ } from "modelfusion";
123
+
124
+ const sentiment = await generateStructure(
125
+ // model:
126
+ ollama
127
+ .ChatTextGenerator({
128
+ model: "openhermes2.5-mistral",
129
+ maxGenerationTokens: 1024,
130
+ temperature: 0,
131
+ })
132
+ .asStructureGenerationModel(jsonStructurePrompt.instruction()),
133
+
134
+ // schema:
135
+ zodSchema(
136
+ z.object({
137
+ sentiment: z
138
+ .enum(["positive", "neutral", "negative"])
139
+ .describe("Sentiment."),
140
+ })
141
+ ),
142
+
143
+ // prompt:
144
+ {
145
+ system:
146
+ "You are a sentiment evaluator. " +
147
+ "Analyze the sentiment of the following product review:",
148
+ instruction:
149
+ "After I opened the package, I was met by a very unpleasant smell " +
150
+ "that did not disappear even after washing. Never again!",
151
+ }
152
+ );
153
+ ```
154
+
155
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
156
+
157
+ #### streamStructure
158
+
159
+ Stream a structure that matches a schema. Partial structures before the final part are untyped JSON.
160
+
161
+ ```ts
162
+ import { zodSchema, openai, streamStructure } from "modelfusion";
163
+
164
+ const structureStream = await streamStructure(
165
+ openai
166
+ .ChatTextGenerator(/* ... */)
167
+ .asFunctionCallStructureGenerationModel({
168
+ fnName: "generateCharacter",
169
+ fnDescription: "Generate character descriptions.",
170
+ })
171
+ .withTextPrompt(),
172
+
173
+ zodSchema(
174
+ z.object({
175
+ characters: z.array(
176
+ z.object({
177
+ name: z.string(),
178
+ class: z
179
+ .string()
180
+ .describe("Character class, e.g. warrior, mage, or thief."),
181
+ description: z.string(),
182
+ })
183
+ ),
184
+ })
185
+ ),
186
+
187
+ "Generate 3 character descriptions for a fantasy role playing game."
188
+ );
189
+
190
+ for await (const part of structureStream) {
191
+ if (!part.isComplete) {
192
+ const unknownPartialStructure = part.value;
193
+ console.log("partial value", unknownPartialStructure);
194
+ } else {
195
+ const fullyTypedStructure = part.value;
196
+ console.log("final value", fullyTypedStructure);
197
+ }
198
+ }
199
+ ```
200
+
201
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
202
+
108
203
  ### [Generate Image](https://modelfusion.dev/guide/function/generate-image)
109
204
 
110
205
  Generate an image from a prompt.
@@ -192,97 +287,6 @@ const transcription = await generateTranscription(
192
287
 
193
288
  Providers: [OpenAI (Whisper)](https://modelfusion.dev/integration/model-provider/openai), [Whisper.cpp](https://modelfusion.dev/integration/model-provider/whispercpp)
194
289
 
195
- ### [Generate Structure](https://modelfusion.dev/guide/function/generate-structure#generatestructure)
196
-
197
- Generate typed objects using a language model and a schema.
198
-
199
- #### generateStructure
200
-
201
- Generate a structure that matches a schema.
202
-
203
- ```ts
204
- import { zodSchema, generateStructure, openai } from "modelfusion";
205
-
206
- const sentiment = await generateStructure(
207
- // model:
208
- openai
209
- .ChatTextGenerator({
210
- model: "gpt-3.5-turbo",
211
- temperature: 0,
212
- maxGenerationTokens: 50,
213
- })
214
- .asFunctionCallStructureGenerationModel({ fnName: "sentiment" })
215
- .withInstructionPrompt(),
216
-
217
- // schema:
218
- zodSchema(
219
- z.object({
220
- sentiment: z
221
- .enum(["positive", "neutral", "negative"])
222
- .describe("Sentiment."),
223
- })
224
- ),
225
-
226
- // prompt:
227
- {
228
- system:
229
- "You are a sentiment evaluator. " +
230
- "Analyze the sentiment of the following product review:",
231
- instruction:
232
- "After I opened the package, I was met by a very unpleasant smell " +
233
- "that did not disappear even after washing. Never again!",
234
- }
235
- );
236
- ```
237
-
238
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
239
-
240
- #### streamStructure
241
-
242
- Stream a structure that matches a schema. Partial structures before the final part are untyped JSON.
243
-
244
- ```ts
245
- import { zodSchema, openai, streamStructure } from "modelfusion";
246
-
247
- const structureStream = await streamStructure(
248
- openai
249
- .ChatTextGenerator(/* ... */)
250
- .asFunctionCallStructureGenerationModel({
251
- fnName: "generateCharacter",
252
- fnDescription: "Generate character descriptions.",
253
- })
254
- .withTextPrompt(),
255
-
256
- zodSchema(
257
- z.object({
258
- characters: z.array(
259
- z.object({
260
- name: z.string(),
261
- class: z
262
- .string()
263
- .describe("Character class, e.g. warrior, mage, or thief."),
264
- description: z.string(),
265
- })
266
- ),
267
- })
268
- ),
269
-
270
- "Generate 3 character descriptions for a fantasy role playing game."
271
- );
272
-
273
- for await (const part of structureStream) {
274
- if (!part.isComplete) {
275
- const unknownPartialStructure = part.value;
276
- console.log("partial value", unknownPartialStructure);
277
- } else {
278
- const fullyTypedStructure = part.value;
279
- console.log("final value", fullyTypedStructure);
280
- }
281
- }
282
- ```
283
-
284
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Ollama](https://modelfusion.dev//integration/model-provider/ollama)
285
-
286
290
  ### [Embed Value](https://modelfusion.dev/guide/function/embed)
287
291
 
288
292
  Create embeddings for text and other values. Embeddings are vectors that represent the essence of the values in the context of the model.
@@ -326,25 +330,13 @@ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai),
326
330
 
327
331
  ### [Tools](https://modelfusion.dev/guide/tools)
328
332
 
329
- Tools are functions that can be executed by an AI model. They are useful for building chatbots and agents.
330
-
331
- Predefined tools: [Math.js](https://modelfusion.dev/guide/tools/predefined-tools/mathjs), [MediaWiki Search](https://modelfusion.dev/guide/tools/predefined-tools/mediawiki-search), [SerpAPI](https://modelfusion.dev/guide/tools/predefined-tools/serpapi), [Google Custom Search](https://modelfusion.dev/guide/tools/predefined-tools/google-custom-search)
332
-
333
- #### [executeTool](https://modelfusion.dev/guide/tools/execute-tool)
334
-
335
- You can directly invoke a tool with `executeTool`:
333
+ Tools are functions (and associated metadata) that can be executed by an AI model. They are useful for building chatbots and agents.
336
334
 
337
- ```ts
338
- const result = await executeTool(calculator, {
339
- a: 14,
340
- b: 12,
341
- operator: "*",
342
- });
343
- ```
335
+ ModelFusion offers several tools out-of-the-box: [Math.js](https://modelfusion.dev/guide/tools/available-tools/mathjs), [MediaWiki Search](https://modelfusion.dev/guide/tools/available-tools/mediawiki-search), [SerpAPI](https://modelfusion.dev/guide/tools/available-tools/serpapi), [Google Custom Search](https://modelfusion.dev/guide/tools/available-tools/google-custom-search). You can also create [custom tools](https://modelfusion.dev/guide/tools).
344
336
 
345
337
  #### [useTool](https://modelfusion.dev/guide/tools/use-tool)
346
338
 
347
- With `useTool`, you can use a tool with a language model that supports tools calls (e.g. OpenAI Chat). `useTool` first generates a tool call and then executes the tool with the arguments.
339
+ With `useTool`, you can ask a tool-compatible language model (e.g. OpenAI chat) to invoke a single tool. `useTool` first generates a tool call and then executes the tool with the arguments.
348
340
 
349
341
  ```ts
350
342
  const { tool, toolCall, args, ok, result } = await useTool(
@@ -360,7 +352,7 @@ console.log(`Ok:`, ok);
360
352
  console.log(`Result or Error:`, result);
361
353
  ```
362
354
 
363
- #### [useTools](https://modelfusion.dev/guide/tools/use-tools-or-generate-text)
355
+ #### [useTools](https://modelfusion.dev/guide/tools/use-tools)
364
356
 
365
357
  With `useTools`, you can ask a language model to generate several tool calls as well as text. The model will choose which tools (if any) should be called with which arguments. Both the text and the tool calls are optional. This function executes the tools.
366
358
 
@@ -372,42 +364,6 @@ const { text, toolResults } = await useTools(
372
364
  );
373
365
  ```
374
366
 
375
- #### [Creating Tools](https://modelfusion.dev/guide/tools/create-tools)
376
-
377
- A tool is comprised of an async execute function, a name, a description, and a schema for the input parameters.
378
-
379
- ```ts
380
- const calculator = new Tool({
381
- name: "calculator",
382
- description: "Execute a calculation",
383
-
384
- parameters: zodSchema(
385
- z.object({
386
- a: z.number().describe("The first number."),
387
- b: z.number().describe("The second number."),
388
- operator: z
389
- .enum(["+", "-", "*", "/"])
390
- .describe("The operator (+, -, *, /)."),
391
- })
392
- ),
393
-
394
- execute: async ({ a, b, operator }) => {
395
- switch (operator) {
396
- case "+":
397
- return a + b;
398
- case "-":
399
- return a - b;
400
- case "*":
401
- return a * b;
402
- case "/":
403
- return a / b;
404
- default:
405
- throw new Error(`Unknown operator: ${operator}`);
406
- }
407
- },
408
- });
409
- ```
410
-
411
367
  #### [Agent Loop](https://modelfusion.dev/guide/tools/agent-loop)
412
368
 
413
369
  You can use `useTools` to implement an agent loop that responds to user messages and executes tools. [Learn more](https://modelfusion.dev/guide/tools/agent-loop).
@@ -582,85 +538,25 @@ import { modelfusion } from "modelfusion";
582
538
  modelfusion.setLogFormat("detailed-object"); // log full events
583
539
  ```
584
540
 
585
- ### [Server](https://modelfusion.dev/guide/experimental/server/)
586
-
587
- > [!WARNING]
588
- > ModelFusion Server is in its initial development phase and not feature-complete. The API is experimental and breaking changes are likely. Feedback and suggestions are welcome.
589
-
590
- ModelFusion Server is desigend for running multi-modal generative AI flows that take up to several minutes to complete. It provides the following benefits:
591
-
592
- - 🔄 Real-time progress updates via custom server-sent events
593
- - 🔒Type-safety with Zod-schema for inputs/events
594
- - 📦 Efficient handling of dynamically created binary assets (images, audio)
595
- - 📜 Auto-logging for AI model interactions within flows
596
-
597
- ModelFusion provides a [Fastify](https://fastify.dev/) plugin that allows you to set up a server that exposes your ModelFusion flows as REST endpoints using server-sent events.
598
-
599
- ```ts
600
- import {
601
- FileSystemAssetStorage,
602
- FileSystemLogger,
603
- modelFusionFastifyPlugin,
604
- } from "modelfusion-experimental/fastify-server"; // '/fastify-server' import path
605
-
606
- // configurable logging for all runs using ModelFusion observability:
607
- const logger = new FileSystemLogger({
608
- path: (run) => path.join(fsBasePath, run.runId, "logs"),
609
- });
610
-
611
- // configurable storage for large files like images and audio files:
612
- const assetStorage = new FileSystemAssetStorage({
613
- path: (run) => path.join(fsBasePath, run.runId, "assets"),
614
- logger,
615
- });
616
-
617
- fastify.register(modelFusionFastifyPlugin, {
618
- baseUrl,
619
- basePath: "/myFlow",
620
- logger,
621
- assetStorage,
622
- flow: exampleFlow,
623
- });
624
- ```
625
-
626
- Using `invokeFlow`, you can easily connect your client to a ModelFusion flow endpoint:
627
-
628
- ```ts
629
- import { invokeFlow } from "modelfusion-experimental/browser"; // '/browser' import path
630
-
631
- invokeFlow({
632
- url: `${BASE_URL}/myFlow`,
633
- schema: myFlowSchema,
634
- input: { prompt },
635
- onEvent(event) {
636
- switch (event.type) {
637
- case "my-event": {
638
- // do something with the event
639
- break;
640
- }
641
- // more events...
642
- }
643
- },
644
- onStop() {
645
- // flow finished
646
- },
647
- });
648
- ```
649
-
650
541
  ## Documentation
651
542
 
652
543
  ### [Guide](https://modelfusion.dev/guide)
653
544
 
654
545
  - [Model Functions](https://modelfusion.dev/guide/function/)
655
546
  - [Generate text](https://modelfusion.dev/guide/function/generate-text)
547
+ - [Generate structure](https://modelfusion.dev/guide/function/generate-structure)
656
548
  - [Generate image](https://modelfusion.dev/guide/function/generate-image)
657
549
  - [Generate speech](https://modelfusion.dev/guide/function/generate-speech)
658
550
  - [Generate transcription](https://modelfusion.dev/guide/function/generation-transcription)
659
- - [Generate structure](https://modelfusion.dev/guide/function/generate-structure)
660
- - [Generate structure or text](https://modelfusion.dev/guide/function/generate-structure-or-text)
661
551
  - [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
662
552
  - [Embed Value](https://modelfusion.dev/guide/function/embed)
663
553
  - [Tools](https://modelfusion.dev/guide/tools)
554
+ - [Use Tool](https://modelfusion.dev/guide/tools/use-tool)
555
+ - [Use Tools](https://modelfusion.dev/guide/tools/use-tools)
556
+ - [Agent Loop](https://modelfusion.dev/guide/tools/agent-loop)
557
+ - [Available Tools](https://modelfusion.dev/guide/tools/available-tools/)
558
+ - [Custom Tools](https://modelfusion.dev/guide/tools/custom-tools)
559
+ - [Advanced](https://modelfusion.dev/guide/tools/advanced)
664
560
  - [Vector Indices](https://modelfusion.dev/guide/vector-index)
665
561
  - [Upsert](https://modelfusion.dev/guide/vector-index/upsert)
666
562
  - [Retrieve](https://modelfusion.dev/guide/vector-index/retrieve)
@@ -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
  }