modelfusion 0.111.0 → 0.113.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.
- package/CHANGELOG.md +57 -0
- package/README.md +26 -154
- package/core/FunctionEvent.d.ts +3 -3
- package/model-function/ModelCallEvent.d.ts +3 -3
- package/model-function/generate-structure/StructureFromTextPromptTemplate.d.ts +13 -0
- package/model-function/generate-structure/jsonStructurePrompt.cjs +12 -0
- package/model-function/generate-structure/jsonStructurePrompt.d.ts +3 -3
- package/model-function/generate-structure/jsonStructurePrompt.js +12 -0
- package/model-function/generate-text/PromptTemplateFullTextModel.cjs +2 -2
- package/model-function/generate-text/PromptTemplateFullTextModel.d.ts +3 -3
- package/model-function/generate-text/PromptTemplateFullTextModel.js +2 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +2 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +3 -3
- package/model-function/generate-text/PromptTemplateTextGenerationModel.js +2 -2
- package/model-function/generate-text/TextGenerationModel.d.ts +4 -0
- package/model-provider/mistral/MistralChatModel.d.ts +4 -4
- package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +3 -3
- package/model-provider/ollama/OllamaChatModel.cjs +17 -2
- package/model-provider/ollama/OllamaChatModel.d.ts +7 -3
- package/model-provider/ollama/OllamaChatModel.js +17 -2
- package/model-provider/ollama/OllamaCompletionModel.cjs +2 -2
- package/model-provider/ollama/OllamaCompletionModel.d.ts +3 -3
- package/model-provider/ollama/OllamaCompletionModel.js +2 -2
- package/model-provider/openai/AbstractOpenAIChatModel.cjs +1 -1
- package/model-provider/openai/AbstractOpenAIChatModel.d.ts +7 -7
- package/model-provider/openai/AbstractOpenAIChatModel.js +1 -1
- package/model-provider/openai/AbstractOpenAICompletionModel.d.ts +4 -4
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +1 -1
- package/model-provider/openai/OpenAIChatModel.cjs +12 -4
- package/model-provider/openai/OpenAIChatModel.d.ts +5 -4
- package/model-provider/openai/OpenAIChatModel.js +12 -4
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +12 -4
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +5 -4
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +12 -4
- package/package.json +2 -2
- package/tool/Tool.cjs +0 -10
- package/tool/Tool.d.ts +0 -1
- package/tool/Tool.js +0 -10
- package/tool/{generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs → generate-tool-calls/TextGenerationToolCallsModel.cjs} +7 -7
- package/tool/{generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.d.ts → generate-tool-calls/TextGenerationToolCallsModel.d.ts} +6 -6
- package/tool/{generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js → generate-tool-calls/TextGenerationToolCallsModel.js} +5 -5
- package/tool/generate-tool-calls/ToolCallsGenerationEvent.d.ts +23 -0
- package/tool/{generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.d.ts → generate-tool-calls/ToolCallsGenerationModel.d.ts} +3 -3
- package/tool/{generate-tool-calls-or-text/ToolCallsOrTextParseError.cjs → generate-tool-calls/ToolCallsParseError.cjs} +5 -5
- package/tool/{generate-tool-calls-or-text/ToolCallsOrTextParseError.d.ts → generate-tool-calls/ToolCallsParseError.d.ts} +1 -1
- package/tool/{generate-tool-calls-or-text/ToolCallsOrTextParseError.js → generate-tool-calls/ToolCallsParseError.js} +3 -3
- package/tool/{generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.d.ts → generate-tool-calls/ToolCallsPromptTemplate.d.ts} +1 -1
- package/tool/{generate-tool-calls-or-text/generateToolCallsOrText.cjs → generate-tool-calls/generateToolCalls.cjs} +5 -5
- package/tool/{generate-tool-calls-or-text/generateToolCallsOrText.d.ts → generate-tool-calls/generateToolCalls.d.ts} +3 -3
- package/tool/{generate-tool-calls-or-text/generateToolCallsOrText.js → generate-tool-calls/generateToolCalls.js} +3 -3
- package/tool/{generate-tool-calls-or-text → generate-tool-calls}/index.cjs +6 -6
- package/tool/generate-tool-calls/index.d.ts +6 -0
- package/tool/generate-tool-calls/index.js +6 -0
- package/tool/index.cjs +2 -3
- package/tool/index.d.ts +2 -3
- package/tool/index.js +2 -3
- package/tool/use-tools/UseToolsEvent.d.ts +7 -0
- package/tool/{use-tools-or-generate-text → use-tools}/index.cjs +2 -2
- package/tool/use-tools/index.d.ts +2 -0
- package/tool/use-tools/index.js +2 -0
- package/tool/{use-tools-or-generate-text/useToolsOrGenerateText.cjs → use-tools/useTools.cjs} +6 -6
- package/tool/{use-tools-or-generate-text/useToolsOrGenerateText.d.ts → use-tools/useTools.d.ts} +2 -2
- package/tool/{use-tools-or-generate-text/useToolsOrGenerateText.js → use-tools/useTools.js} +4 -4
- package/tool/InvalidToolNameError.cjs +0 -17
- package/tool/InvalidToolNameError.d.ts +0 -7
- package/tool/InvalidToolNameError.js +0 -13
- package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.d.ts +0 -23
- package/tool/generate-tool-calls-or-text/index.d.ts +0 -6
- package/tool/generate-tool-calls-or-text/index.js +0 -6
- package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +0 -7
- package/tool/use-tools-or-generate-text/index.d.ts +0 -2
- package/tool/use-tools-or-generate-text/index.js +0 -2
- /package/tool/{generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.cjs → generate-tool-calls/ToolCallsGenerationEvent.cjs} +0 -0
- /package/tool/{generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.js → generate-tool-calls/ToolCallsGenerationEvent.js} +0 -0
- /package/tool/{generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.cjs → generate-tool-calls/ToolCallsGenerationModel.cjs} +0 -0
- /package/tool/{generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.js → generate-tool-calls/ToolCallsGenerationModel.js} +0 -0
- /package/tool/{generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.cjs → generate-tool-calls/ToolCallsPromptTemplate.cjs} +0 -0
- /package/tool/{generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js → generate-tool-calls/ToolCallsPromptTemplate.js} +0 -0
- /package/tool/{use-tools-or-generate-text/UseToolsOrGenerateTextEvent.cjs → use-tools/UseToolsEvent.cjs} +0 -0
- /package/tool/{use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js → use-tools/UseToolsEvent.js} +0 -0
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,62 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v0.113.0 - 2024-01-03
|
4
|
+
|
5
|
+
[Structure generation](https://modelfusion.dev/guide/function/generate-structure) improvements.
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- `.asStructureGenerationModel(...)` function to `OpenAIChatModel` and `OllamaChatModel` to create structure generation models from chat models.
|
10
|
+
- `jsonStructurePrompt` helper function to create structure generation models.
|
11
|
+
|
12
|
+
### Example
|
13
|
+
|
14
|
+
```ts
|
15
|
+
import {
|
16
|
+
generateStructure,
|
17
|
+
jsonStructurePrompt,
|
18
|
+
ollama,
|
19
|
+
zodSchema,
|
20
|
+
} from "modelfusion";
|
21
|
+
|
22
|
+
const structure = await generateStructure(
|
23
|
+
ollama
|
24
|
+
.ChatTextGenerator({
|
25
|
+
model: "openhermes2.5-mistral",
|
26
|
+
maxGenerationTokens: 1024,
|
27
|
+
temperature: 0,
|
28
|
+
})
|
29
|
+
.asStructureGenerationModel(jsonStructurePrompt.text()),
|
30
|
+
|
31
|
+
zodSchema(
|
32
|
+
z.object({
|
33
|
+
characters: z.array(
|
34
|
+
z.object({
|
35
|
+
name: z.string(),
|
36
|
+
class: z
|
37
|
+
.string()
|
38
|
+
.describe("Character class, e.g. warrior, mage, or thief."),
|
39
|
+
description: z.string(),
|
40
|
+
})
|
41
|
+
),
|
42
|
+
})
|
43
|
+
),
|
44
|
+
|
45
|
+
"Generate 3 character descriptions for a fantasy role playing game. "
|
46
|
+
);
|
47
|
+
```
|
48
|
+
|
49
|
+
## v0.112.0 - 2024-01-02
|
50
|
+
|
51
|
+
### Changed
|
52
|
+
|
53
|
+
- **breaking change**: renamed `useToolsOrGenerateText` to `useTools`
|
54
|
+
- **breaking change**: renamed `generateToolCallsOrText` to `generateToolCalls`
|
55
|
+
|
56
|
+
### Removed
|
57
|
+
|
58
|
+
- Restriction on tool names. OpenAI tool calls do not have such a restriction.
|
59
|
+
|
3
60
|
## v0.111.0 - 2024-01-01
|
4
61
|
|
5
62
|
Reworked API configuration support.
|
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ModelFusion
|
2
2
|
|
3
|
-
> ### The TypeScript library for building
|
3
|
+
> ### The TypeScript library for building sAI applications.
|
4
4
|
|
5
5
|
[](https://www.npmjs.com/package/modelfusion)
|
6
6
|
[](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
|
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.
|
@@ -201,18 +201,22 @@ Generate typed objects using a language model and a schema.
|
|
201
201
|
Generate a structure that matches a schema.
|
202
202
|
|
203
203
|
```ts
|
204
|
-
import {
|
204
|
+
import {
|
205
|
+
ollama,
|
206
|
+
zodSchema,
|
207
|
+
generateStructure,
|
208
|
+
jsonStructurePrompt,
|
209
|
+
} from "modelfusion";
|
205
210
|
|
206
211
|
const sentiment = await generateStructure(
|
207
212
|
// model:
|
208
|
-
|
213
|
+
ollama
|
209
214
|
.ChatTextGenerator({
|
210
|
-
model: "
|
215
|
+
model: "openhermes2.5-mistral",
|
216
|
+
maxGenerationTokens: 1024,
|
211
217
|
temperature: 0,
|
212
|
-
maxGenerationTokens: 50,
|
213
218
|
})
|
214
|
-
.
|
215
|
-
.withInstructionPrompt(),
|
219
|
+
.asStructureGenerationModel(jsonStructurePrompt.instruction()),
|
216
220
|
|
217
221
|
// schema:
|
218
222
|
zodSchema(
|
@@ -326,85 +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), [SerpAPI](https://modelfusion.dev/guide/tools/predefined-tools/serpapi), [Google Custom Search](https://modelfusion.dev/guide/tools/predefined-tools/google-custom-search)
|
332
|
-
|
333
|
-
#### [Creating Tools](https://modelfusion.dev/guide/tools/create-tools)
|
334
|
-
|
335
|
-
A tool is comprised of an async execute function, a name, a description, and a schema for the input parameters.
|
336
|
-
|
337
|
-
```ts
|
338
|
-
const calculator = new Tool({
|
339
|
-
name: "calculator",
|
340
|
-
description: "Execute a calculation",
|
341
|
-
|
342
|
-
parameters: zodSchema(
|
343
|
-
z.object({
|
344
|
-
a: z.number().describe("The first number."),
|
345
|
-
b: z.number().describe("The second number."),
|
346
|
-
operator: z
|
347
|
-
.enum(["+", "-", "*", "/"])
|
348
|
-
.describe("The operator (+, -, *, /)."),
|
349
|
-
})
|
350
|
-
),
|
351
|
-
|
352
|
-
execute: async ({ a, b, operator }) => {
|
353
|
-
switch (operator) {
|
354
|
-
case "+":
|
355
|
-
return a + b;
|
356
|
-
case "-":
|
357
|
-
return a - b;
|
358
|
-
case "*":
|
359
|
-
return a * b;
|
360
|
-
case "/":
|
361
|
-
return a / b;
|
362
|
-
default:
|
363
|
-
throw new Error(`Unknown operator: ${operator}`);
|
364
|
-
}
|
365
|
-
},
|
366
|
-
});
|
367
|
-
```
|
333
|
+
Tools are functions (and associated metadata) that can be executed by an AI model. They are useful for building chatbots and agents.
|
368
334
|
|
369
|
-
|
370
|
-
|
371
|
-
With `generateToolCall`, you can generate a tool call for a specific tool with a language model that supports tools calls (e.g. OpenAI Chat). This function does not execute the tools.
|
372
|
-
|
373
|
-
```ts
|
374
|
-
const { id, name, args } = await generateToolCall(
|
375
|
-
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
376
|
-
calculator,
|
377
|
-
[openai.ChatMessage.user("What's fourteen times twelve?")]
|
378
|
-
);
|
379
|
-
```
|
380
|
-
|
381
|
-
#### [generateToolCallsOrText](https://modelfusion.dev/guide/tools/generate-tool-calls-or-text)
|
382
|
-
|
383
|
-
With `generateToolCallsOrText`, 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 does not execute the tools.
|
384
|
-
|
385
|
-
```ts
|
386
|
-
const { text, toolCalls } = await generateToolCallsOrText(
|
387
|
-
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
388
|
-
[toolA, toolB, toolC],
|
389
|
-
[openai.ChatMessage.user(query)]
|
390
|
-
);
|
391
|
-
```
|
392
|
-
|
393
|
-
#### [executeTool](https://modelfusion.dev/guide/tools/execute-tool)
|
394
|
-
|
395
|
-
You can directly invoke a tool with `executeTool`:
|
396
|
-
|
397
|
-
```ts
|
398
|
-
const result = await executeTool(calculator, {
|
399
|
-
a: 14,
|
400
|
-
b: 12,
|
401
|
-
operator: "*",
|
402
|
-
});
|
403
|
-
```
|
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).
|
404
336
|
|
405
337
|
#### [useTool](https://modelfusion.dev/guide/tools/use-tool)
|
406
338
|
|
407
|
-
With `useTool`, you can
|
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.
|
408
340
|
|
409
341
|
```ts
|
410
342
|
const { tool, toolCall, args, ok, result } = await useTool(
|
@@ -420,12 +352,12 @@ console.log(`Ok:`, ok);
|
|
420
352
|
console.log(`Result or Error:`, result);
|
421
353
|
```
|
422
354
|
|
423
|
-
#### [
|
355
|
+
#### [useTools](https://modelfusion.dev/guide/tools/use-tools)
|
424
356
|
|
425
|
-
With `
|
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.
|
426
358
|
|
427
359
|
```ts
|
428
|
-
const { text, toolResults } = await
|
360
|
+
const { text, toolResults } = await useTools(
|
429
361
|
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
430
362
|
[calculator /* ... */],
|
431
363
|
[openai.ChatMessage.user("What's fourteen times twelve?")]
|
@@ -434,7 +366,7 @@ const { text, toolResults } = await useToolsOrGenerateText(
|
|
434
366
|
|
435
367
|
#### [Agent Loop](https://modelfusion.dev/guide/tools/agent-loop)
|
436
368
|
|
437
|
-
You can use `
|
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).
|
438
370
|
|
439
371
|
### [Vector Indices](https://modelfusion.dev/guide/vector-index)
|
440
372
|
|
@@ -606,85 +538,25 @@ import { modelfusion } from "modelfusion";
|
|
606
538
|
modelfusion.setLogFormat("detailed-object"); // log full events
|
607
539
|
```
|
608
540
|
|
609
|
-
### [Server](https://modelfusion.dev/guide/experimental/server/)
|
610
|
-
|
611
|
-
> [!WARNING]
|
612
|
-
> 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.
|
613
|
-
|
614
|
-
ModelFusion Server is desigend for running multi-modal generative AI flows that take up to several minutes to complete. It provides the following benefits:
|
615
|
-
|
616
|
-
- 🔄 Real-time progress updates via custom server-sent events
|
617
|
-
- 🔒Type-safety with Zod-schema for inputs/events
|
618
|
-
- 📦 Efficient handling of dynamically created binary assets (images, audio)
|
619
|
-
- 📜 Auto-logging for AI model interactions within flows
|
620
|
-
|
621
|
-
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.
|
622
|
-
|
623
|
-
```ts
|
624
|
-
import {
|
625
|
-
FileSystemAssetStorage,
|
626
|
-
FileSystemLogger,
|
627
|
-
modelFusionFastifyPlugin,
|
628
|
-
} from "modelfusion-experimental/fastify-server"; // '/fastify-server' import path
|
629
|
-
|
630
|
-
// configurable logging for all runs using ModelFusion observability:
|
631
|
-
const logger = new FileSystemLogger({
|
632
|
-
path: (run) => path.join(fsBasePath, run.runId, "logs"),
|
633
|
-
});
|
634
|
-
|
635
|
-
// configurable storage for large files like images and audio files:
|
636
|
-
const assetStorage = new FileSystemAssetStorage({
|
637
|
-
path: (run) => path.join(fsBasePath, run.runId, "assets"),
|
638
|
-
logger,
|
639
|
-
});
|
640
|
-
|
641
|
-
fastify.register(modelFusionFastifyPlugin, {
|
642
|
-
baseUrl,
|
643
|
-
basePath: "/myFlow",
|
644
|
-
logger,
|
645
|
-
assetStorage,
|
646
|
-
flow: exampleFlow,
|
647
|
-
});
|
648
|
-
```
|
649
|
-
|
650
|
-
Using `invokeFlow`, you can easily connect your client to a ModelFusion flow endpoint:
|
651
|
-
|
652
|
-
```ts
|
653
|
-
import { invokeFlow } from "modelfusion-experimental/browser"; // '/browser' import path
|
654
|
-
|
655
|
-
invokeFlow({
|
656
|
-
url: `${BASE_URL}/myFlow`,
|
657
|
-
schema: myFlowSchema,
|
658
|
-
input: { prompt },
|
659
|
-
onEvent(event) {
|
660
|
-
switch (event.type) {
|
661
|
-
case "my-event": {
|
662
|
-
// do something with the event
|
663
|
-
break;
|
664
|
-
}
|
665
|
-
// more events...
|
666
|
-
}
|
667
|
-
},
|
668
|
-
onStop() {
|
669
|
-
// flow finished
|
670
|
-
},
|
671
|
-
});
|
672
|
-
```
|
673
|
-
|
674
541
|
## Documentation
|
675
542
|
|
676
543
|
### [Guide](https://modelfusion.dev/guide)
|
677
544
|
|
678
545
|
- [Model Functions](https://modelfusion.dev/guide/function/)
|
679
546
|
- [Generate text](https://modelfusion.dev/guide/function/generate-text)
|
547
|
+
- [Generate structure](https://modelfusion.dev/guide/function/generate-structure)
|
680
548
|
- [Generate image](https://modelfusion.dev/guide/function/generate-image)
|
681
549
|
- [Generate speech](https://modelfusion.dev/guide/function/generate-speech)
|
682
550
|
- [Generate transcription](https://modelfusion.dev/guide/function/generation-transcription)
|
683
|
-
- [Generate structure](https://modelfusion.dev/guide/function/generate-structure)
|
684
|
-
- [Generate structure or text](https://modelfusion.dev/guide/function/generate-structure-or-text)
|
685
551
|
- [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
|
686
552
|
- [Embed Value](https://modelfusion.dev/guide/function/embed)
|
687
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)
|
688
560
|
- [Vector Indices](https://modelfusion.dev/guide/vector-index)
|
689
561
|
- [Upsert](https://modelfusion.dev/guide/vector-index/upsert)
|
690
562
|
- [Retrieve](https://modelfusion.dev/guide/vector-index/retrieve)
|
package/core/FunctionEvent.d.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
import { ExtensionFunctionStartedEvent, ExtensionFunctionFinishedEvent } from "./ExtensionFunctionEvent.js";
|
2
1
|
import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
|
3
2
|
import { RetrieveFinishedEvent, RetrieveStartedEvent } from "../retriever/RetrieveEvent.js";
|
4
3
|
import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/execute-tool/ExecuteToolEvent.js";
|
5
4
|
import { UseToolFinishedEvent, UseToolStartedEvent } from "../tool/use-tool/UseToolEvent.js";
|
6
|
-
import {
|
5
|
+
import { useToolsFinishedEvent, useToolsStartedEvent } from "../tool/use-tools/UseToolsEvent.js";
|
7
6
|
import { UpsertIntoVectorIndexFinishedEvent, UpsertIntoVectorIndexStartedEvent } from "../vector-index/UpsertIntoVectorIndexEvent.js";
|
8
7
|
import { ExecuteFunctionFinishedEvent, ExecuteFunctionStartedEvent } from "./ExecuteFunctionEvent.js";
|
8
|
+
import { ExtensionFunctionFinishedEvent, ExtensionFunctionStartedEvent } from "./ExtensionFunctionEvent.js";
|
9
9
|
export interface BaseFunctionEvent {
|
10
10
|
/**
|
11
11
|
* Unique identifier for the function call.
|
@@ -82,4 +82,4 @@ export interface BaseFunctionFinishedEvent extends BaseFunctionEvent {
|
|
82
82
|
*/
|
83
83
|
result: BaseFunctionFinishedEventResult;
|
84
84
|
}
|
85
|
-
export type FunctionEvent = ExecuteFunctionStartedEvent | ExecuteFunctionFinishedEvent | ExecuteToolStartedEvent | ExecuteToolFinishedEvent | ExtensionFunctionStartedEvent | ExtensionFunctionFinishedEvent | ModelCallStartedEvent | ModelCallFinishedEvent | RetrieveStartedEvent | RetrieveFinishedEvent | UpsertIntoVectorIndexStartedEvent | UpsertIntoVectorIndexFinishedEvent | UseToolStartedEvent | UseToolFinishedEvent |
|
85
|
+
export type FunctionEvent = ExecuteFunctionStartedEvent | ExecuteFunctionFinishedEvent | ExecuteToolStartedEvent | ExecuteToolFinishedEvent | ExtensionFunctionStartedEvent | ExtensionFunctionFinishedEvent | ModelCallStartedEvent | ModelCallFinishedEvent | RetrieveStartedEvent | RetrieveFinishedEvent | UpsertIntoVectorIndexStartedEvent | UpsertIntoVectorIndexFinishedEvent | UseToolStartedEvent | UseToolFinishedEvent | useToolsStartedEvent | useToolsFinishedEvent;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
|
2
2
|
import { ToolCallGenerationFinishedEvent, ToolCallGenerationStartedEvent } from "../tool/generate-tool-call/ToolCallGenerationEvent.js";
|
3
|
-
import {
|
3
|
+
import { ToolCallsGenerationFinishedEvent, ToolCallsGenerationStartedEvent } from "../tool/generate-tool-calls/ToolCallsGenerationEvent.js";
|
4
4
|
import { ModelInformation } from "./ModelInformation.js";
|
5
5
|
import { EmbeddingFinishedEvent, EmbeddingStartedEvent } from "./embed/EmbeddingEvent.js";
|
6
6
|
import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
|
@@ -49,5 +49,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
|
|
49
49
|
*/
|
50
50
|
result: BaseModelCallFinishedEventResult;
|
51
51
|
}
|
52
|
-
export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent |
|
53
|
-
export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent |
|
52
|
+
export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | ToolCallsGenerationStartedEvent | TranscriptionStartedEvent;
|
53
|
+
export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | ToolCallsGenerationFinishedEvent | TranscriptionFinishedEvent;
|
@@ -1,6 +1,19 @@
|
|
1
1
|
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
2
2
|
import { Schema } from "../../core/schema/Schema.js";
|
3
|
+
import { TextStreamingModel } from "../generate-text/TextGenerationModel.js";
|
4
|
+
import { ChatPrompt } from "../generate-text/prompt-template/ChatPrompt.js";
|
5
|
+
import { InstructionPrompt } from "../generate-text/prompt-template/InstructionPrompt.js";
|
3
6
|
export type StructureFromTextPromptTemplate<SOURCE_PROMPT, TARGET_PROMPT> = {
|
4
7
|
createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => TARGET_PROMPT;
|
5
8
|
extractStructure: (response: string) => unknown;
|
6
9
|
};
|
10
|
+
export type FlexibleStructureFromTextPromptTemplate<SOURCE_PROMPT, INTERMEDIATE_PROMPT> = {
|
11
|
+
createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => INTERMEDIATE_PROMPT;
|
12
|
+
extractStructure: (response: string) => unknown;
|
13
|
+
adaptModel: (model: TextStreamingModel<never> & {
|
14
|
+
withTextPrompt(): TextStreamingModel<string>;
|
15
|
+
withInstructionPrompt(): TextStreamingModel<InstructionPrompt>;
|
16
|
+
withChatPrompt(): TextStreamingModel<ChatPrompt>;
|
17
|
+
withJsonOutput?: () => typeof model;
|
18
|
+
}) => TextStreamingModel<INTERMEDIATE_PROMPT>;
|
19
|
+
};
|
@@ -15,6 +15,12 @@ exports.jsonStructurePrompt = {
|
|
15
15
|
instruction: prompt,
|
16
16
|
}),
|
17
17
|
extractStructure,
|
18
|
+
adaptModel: (model) => {
|
19
|
+
if (model.withJsonOutput != null) {
|
20
|
+
model = model.withJsonOutput();
|
21
|
+
}
|
22
|
+
return model.withInstructionPrompt();
|
23
|
+
},
|
18
24
|
};
|
19
25
|
},
|
20
26
|
instruction({ schemaPrefix, schemaSuffix, } = {}) {
|
@@ -29,6 +35,12 @@ exports.jsonStructurePrompt = {
|
|
29
35
|
instruction: prompt.instruction,
|
30
36
|
}),
|
31
37
|
extractStructure,
|
38
|
+
adaptModel: (model) => {
|
39
|
+
if (model.withJsonOutput != null) {
|
40
|
+
model = model.withJsonOutput();
|
41
|
+
}
|
42
|
+
return model.withInstructionPrompt();
|
43
|
+
},
|
32
44
|
};
|
33
45
|
},
|
34
46
|
};
|
@@ -1,15 +1,15 @@
|
|
1
1
|
import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
|
2
2
|
import { Schema } from "../../core/schema/Schema.js";
|
3
3
|
import { InstructionPrompt } from "../../model-function/generate-text/prompt-template/InstructionPrompt.js";
|
4
|
-
import { StructureFromTextPromptTemplate } from "./StructureFromTextPromptTemplate.js";
|
4
|
+
import { FlexibleStructureFromTextPromptTemplate, StructureFromTextPromptTemplate } from "./StructureFromTextPromptTemplate.js";
|
5
5
|
export declare const jsonStructurePrompt: {
|
6
6
|
custom<SOURCE_PROMPT, TARGET_PROMPT>(createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => TARGET_PROMPT): StructureFromTextPromptTemplate<SOURCE_PROMPT, TARGET_PROMPT>;
|
7
7
|
text({ schemaPrefix, schemaSuffix, }?: {
|
8
8
|
schemaPrefix?: string | undefined;
|
9
9
|
schemaSuffix?: string | undefined;
|
10
|
-
}):
|
10
|
+
}): FlexibleStructureFromTextPromptTemplate<string, InstructionPrompt>;
|
11
11
|
instruction({ schemaPrefix, schemaSuffix, }?: {
|
12
12
|
schemaPrefix?: string | undefined;
|
13
13
|
schemaSuffix?: string | undefined;
|
14
|
-
}):
|
14
|
+
}): FlexibleStructureFromTextPromptTemplate<InstructionPrompt, InstructionPrompt>;
|
15
15
|
};
|
@@ -12,6 +12,12 @@ export const jsonStructurePrompt = {
|
|
12
12
|
instruction: prompt,
|
13
13
|
}),
|
14
14
|
extractStructure,
|
15
|
+
adaptModel: (model) => {
|
16
|
+
if (model.withJsonOutput != null) {
|
17
|
+
model = model.withJsonOutput();
|
18
|
+
}
|
19
|
+
return model.withInstructionPrompt();
|
20
|
+
},
|
15
21
|
};
|
16
22
|
},
|
17
23
|
instruction({ schemaPrefix, schemaSuffix, } = {}) {
|
@@ -26,6 +32,12 @@ export const jsonStructurePrompt = {
|
|
26
32
|
instruction: prompt.instruction,
|
27
33
|
}),
|
28
34
|
extractStructure,
|
35
|
+
adaptModel: (model) => {
|
36
|
+
if (model.withJsonOutput != null) {
|
37
|
+
model = model.withJsonOutput();
|
38
|
+
}
|
39
|
+
return model.withInstructionPrompt();
|
40
|
+
},
|
29
41
|
};
|
30
42
|
},
|
31
43
|
};
|
@@ -10,9 +10,9 @@ class PromptTemplateFullTextModel extends PromptTemplateTextStreamingModel_js_1.
|
|
10
10
|
const mappedPrompt = this.promptTemplate.format(prompt);
|
11
11
|
return this.model.doGenerateToolCall(tool, mappedPrompt, options);
|
12
12
|
}
|
13
|
-
|
13
|
+
doGenerateToolCalls(tools, prompt, options) {
|
14
14
|
const mappedPrompt = this.promptTemplate.format(prompt);
|
15
|
-
return this.model.
|
15
|
+
return this.model.doGenerateToolCalls(tools, mappedPrompt, options);
|
16
16
|
}
|
17
17
|
withPromptTemplate(promptTemplate) {
|
18
18
|
return new PromptTemplateFullTextModel({
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { ToolDefinition } from "../../tool/ToolDefinition.js";
|
3
3
|
import { ToolCallGenerationModel } from "../../tool/generate-tool-call/ToolCallGenerationModel.js";
|
4
|
-
import {
|
4
|
+
import { ToolCallsGenerationModel } from "../../tool/generate-tool-calls/ToolCallsGenerationModel.js";
|
5
5
|
import { PromptTemplateTextStreamingModel } from "./PromptTemplateTextStreamingModel.js";
|
6
6
|
import { TextGenerationModelSettings, TextStreamingModel } from "./TextGenerationModel.js";
|
7
7
|
import { TextGenerationPromptTemplate } from "./TextGenerationPromptTemplate.js";
|
8
|
-
export declare class PromptTemplateFullTextModel<PROMPT, MODEL_PROMPT, SETTINGS extends TextGenerationModelSettings, MODEL extends TextStreamingModel<MODEL_PROMPT, SETTINGS> & ToolCallGenerationModel<MODEL_PROMPT, SETTINGS> &
|
8
|
+
export declare class PromptTemplateFullTextModel<PROMPT, MODEL_PROMPT, SETTINGS extends TextGenerationModelSettings, MODEL extends TextStreamingModel<MODEL_PROMPT, SETTINGS> & ToolCallGenerationModel<MODEL_PROMPT, SETTINGS> & ToolCallsGenerationModel<MODEL_PROMPT, SETTINGS>> extends PromptTemplateTextStreamingModel<PROMPT, MODEL_PROMPT, SETTINGS, MODEL> implements TextStreamingModel<PROMPT, SETTINGS>, ToolCallGenerationModel<PROMPT, SETTINGS>, ToolCallsGenerationModel<PROMPT, SETTINGS> {
|
9
9
|
constructor(options: {
|
10
10
|
model: MODEL;
|
11
11
|
promptTemplate: TextGenerationPromptTemplate<PROMPT, MODEL_PROMPT>;
|
@@ -22,7 +22,7 @@ export declare class PromptTemplateFullTextModel<PROMPT, MODEL_PROMPT, SETTINGS
|
|
22
22
|
totalTokens: number;
|
23
23
|
} | undefined;
|
24
24
|
}>;
|
25
|
-
|
25
|
+
doGenerateToolCalls(tools: ToolDefinition<string, unknown>[], prompt: PROMPT, options?: FunctionOptions | undefined): PromiseLike<{
|
26
26
|
response: unknown;
|
27
27
|
text: string | null;
|
28
28
|
toolCalls: {
|
@@ -7,9 +7,9 @@ export class PromptTemplateFullTextModel extends PromptTemplateTextStreamingMode
|
|
7
7
|
const mappedPrompt = this.promptTemplate.format(prompt);
|
8
8
|
return this.model.doGenerateToolCall(tool, mappedPrompt, options);
|
9
9
|
}
|
10
|
-
|
10
|
+
doGenerateToolCalls(tools, prompt, options) {
|
11
11
|
const mappedPrompt = this.promptTemplate.format(prompt);
|
12
|
-
return this.model.
|
12
|
+
return this.model.doGenerateToolCalls(tools, mappedPrompt, options);
|
13
13
|
}
|
14
14
|
withPromptTemplate(promptTemplate) {
|
15
15
|
return new PromptTemplateFullTextModel({
|
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.PromptTemplateTextGenerationModel = void 0;
|
4
4
|
const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
|
5
|
-
const
|
5
|
+
const TextGenerationToolCallsModel_js_1 = require("../../tool/generate-tool-calls/TextGenerationToolCallsModel.cjs");
|
6
6
|
const StructureFromTextGenerationModel_js_1 = require("../generate-structure/StructureFromTextGenerationModel.cjs");
|
7
7
|
class PromptTemplateTextGenerationModel {
|
8
8
|
constructor({ model, promptTemplate, }) {
|
@@ -54,7 +54,7 @@ class PromptTemplateTextGenerationModel {
|
|
54
54
|
});
|
55
55
|
}
|
56
56
|
asToolCallsOrTextGenerationModel(promptTemplate) {
|
57
|
-
return new
|
57
|
+
return new TextGenerationToolCallsModel_js_1.TextGenerationToolCallsModel({
|
58
58
|
model: this,
|
59
59
|
template: promptTemplate,
|
60
60
|
});
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { TextGenerationToolCallModel, ToolCallPromptTemplate } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import { TextGenerationToolCallsModel } from "../../tool/generate-tool-calls/TextGenerationToolCallsModel.js";
|
4
|
+
import { ToolCallsPromptTemplate } from "../../tool/generate-tool-calls/ToolCallsPromptTemplate.js";
|
5
5
|
import { StructureFromTextGenerationModel } from "../generate-structure/StructureFromTextGenerationModel.js";
|
6
6
|
import { StructureFromTextPromptTemplate } from "../generate-structure/StructureFromTextPromptTemplate.js";
|
7
7
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
@@ -29,7 +29,7 @@ export declare class PromptTemplateTextGenerationModel<PROMPT, MODEL_PROMPT, SET
|
|
29
29
|
}>;
|
30
30
|
get settingsForEvent(): Partial<SETTINGS>;
|
31
31
|
asToolCallGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallPromptTemplate<INPUT_PROMPT, PROMPT>): TextGenerationToolCallModel<INPUT_PROMPT, PROMPT, this>;
|
32
|
-
asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptTemplate:
|
32
|
+
asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptTemplate: ToolCallsPromptTemplate<INPUT_PROMPT, PROMPT>): TextGenerationToolCallsModel<INPUT_PROMPT, PROMPT, this>;
|
33
33
|
asStructureGenerationModel<INPUT_PROMPT>(promptTemplate: StructureFromTextPromptTemplate<INPUT_PROMPT, PROMPT>): StructureFromTextGenerationModel<INPUT_PROMPT, PROMPT, this>;
|
34
34
|
withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, PROMPT>): PromptTemplateTextGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
|
35
35
|
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
2
|
-
import {
|
2
|
+
import { TextGenerationToolCallsModel } from "../../tool/generate-tool-calls/TextGenerationToolCallsModel.js";
|
3
3
|
import { StructureFromTextGenerationModel } from "../generate-structure/StructureFromTextGenerationModel.js";
|
4
4
|
export class PromptTemplateTextGenerationModel {
|
5
5
|
constructor({ model, promptTemplate, }) {
|
@@ -51,7 +51,7 @@ export class PromptTemplateTextGenerationModel {
|
|
51
51
|
});
|
52
52
|
}
|
53
53
|
asToolCallsOrTextGenerationModel(promptTemplate) {
|
54
|
-
return new
|
54
|
+
return new TextGenerationToolCallsModel({
|
55
55
|
model: this,
|
56
56
|
template: promptTemplate,
|
57
57
|
});
|
@@ -72,6 +72,10 @@ export interface TextGenerationModel<PROMPT, SETTINGS extends TextGenerationMode
|
|
72
72
|
};
|
73
73
|
}>;
|
74
74
|
withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, PROMPT>): TextGenerationModel<INPUT_PROMPT, SETTINGS>;
|
75
|
+
/**
|
76
|
+
* Optional. When available, forces the model to return JSON as the text output.
|
77
|
+
*/
|
78
|
+
withJsonOutput?(): this;
|
75
79
|
}
|
76
80
|
export interface TextStreamingModel<PROMPT, SETTINGS extends TextGenerationModelSettings = TextGenerationModelSettings> extends TextGenerationModel<PROMPT, SETTINGS> {
|
77
81
|
doStreamText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<unknown>>>;
|
@@ -61,12 +61,12 @@ export declare class MistralChatModel extends AbstractModel<MistralChatModelSett
|
|
61
61
|
doGenerateTexts(prompt: MistralChatPrompt, options?: FunctionOptions): Promise<{
|
62
62
|
response: {
|
63
63
|
object: string;
|
64
|
+
model: string;
|
64
65
|
usage: {
|
65
66
|
prompt_tokens: number;
|
66
67
|
completion_tokens: number;
|
67
68
|
total_tokens: number;
|
68
69
|
};
|
69
|
-
model: string;
|
70
70
|
id: string;
|
71
71
|
created: number;
|
72
72
|
choices: {
|
@@ -162,12 +162,12 @@ declare const mistralChatResponseSchema: z.ZodObject<{
|
|
162
162
|
}>;
|
163
163
|
}, "strip", z.ZodTypeAny, {
|
164
164
|
object: string;
|
165
|
+
model: string;
|
165
166
|
usage: {
|
166
167
|
prompt_tokens: number;
|
167
168
|
completion_tokens: number;
|
168
169
|
total_tokens: number;
|
169
170
|
};
|
170
|
-
model: string;
|
171
171
|
id: string;
|
172
172
|
created: number;
|
173
173
|
choices: {
|
@@ -180,12 +180,12 @@ declare const mistralChatResponseSchema: z.ZodObject<{
|
|
180
180
|
}[];
|
181
181
|
}, {
|
182
182
|
object: string;
|
183
|
+
model: string;
|
183
184
|
usage: {
|
184
185
|
prompt_tokens: number;
|
185
186
|
completion_tokens: number;
|
186
187
|
total_tokens: number;
|
187
188
|
};
|
188
|
-
model: string;
|
189
189
|
id: string;
|
190
190
|
created: number;
|
191
191
|
choices: {
|
@@ -271,12 +271,12 @@ export declare const MistralChatResponseFormat: {
|
|
271
271
|
stream: boolean;
|
272
272
|
handler: ResponseHandler<{
|
273
273
|
object: string;
|
274
|
+
model: string;
|
274
275
|
usage: {
|
275
276
|
prompt_tokens: number;
|
276
277
|
completion_tokens: number;
|
277
278
|
total_tokens: number;
|
278
279
|
};
|
279
|
-
model: string;
|
280
280
|
id: string;
|
281
281
|
created: number;
|
282
282
|
choices: {
|