modelfusion 0.53.2 → 0.55.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/README.md +71 -4
- package/browser/convertAudioChunksToBase64.cjs +8 -0
- package/browser/convertAudioChunksToBase64.d.ts +4 -0
- package/browser/convertAudioChunksToBase64.js +4 -0
- package/browser/convertBlobToBase64.cjs +23 -0
- package/browser/convertBlobToBase64.d.ts +1 -0
- package/browser/convertBlobToBase64.js +19 -0
- package/{event-source → browser}/index.cjs +4 -1
- package/browser/index.d.ts +6 -0
- package/browser/index.js +6 -0
- package/browser/invokeFlow.cjs +23 -0
- package/browser/invokeFlow.d.ts +8 -0
- package/browser/invokeFlow.js +19 -0
- package/{event-source → browser}/readEventSource.cjs +8 -3
- package/{event-source → browser}/readEventSource.d.ts +3 -1
- package/{event-source → browser}/readEventSource.js +8 -3
- package/{event-source → browser}/readEventSourceStream.cjs +1 -1
- package/{event-source → browser}/readEventSourceStream.js +1 -1
- package/index.cjs +0 -2
- package/index.d.ts +0 -2
- package/index.js +0 -2
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +1 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +31 -50
- package/model-provider/cohere/CohereTextGenerationModel.js +31 -50
- package/model-provider/index.cjs +1 -0
- package/model-provider/index.d.ts +1 -0
- package/model-provider/index.js +1 -0
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +1 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +1 -1
- package/model-provider/ollama/OllamaApiConfiguration.cjs +15 -0
- package/model-provider/ollama/OllamaApiConfiguration.d.ts +10 -0
- package/model-provider/ollama/OllamaApiConfiguration.js +11 -0
- package/model-provider/ollama/OllamaError.cjs +29 -0
- package/model-provider/ollama/OllamaError.d.ts +22 -0
- package/model-provider/ollama/OllamaError.js +24 -0
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +216 -0
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +134 -0
- package/model-provider/ollama/OllamaTextGenerationModel.js +212 -0
- package/model-provider/ollama/index.cjs +21 -0
- package/model-provider/ollama/index.d.ts +3 -0
- package/model-provider/ollama/index.js +3 -0
- package/model-provider/openai/OpenAICompletionModel.cjs +2 -2
- package/model-provider/openai/OpenAICompletionModel.js +2 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +1 -1
- package/package.json +13 -4
- package/server/fastify/AssetStorage.cjs +2 -0
- package/server/fastify/AssetStorage.d.ts +17 -0
- package/server/fastify/AssetStorage.js +1 -0
- package/server/fastify/DefaultFlow.cjs +22 -0
- package/server/fastify/DefaultFlow.d.ts +16 -0
- package/server/fastify/DefaultFlow.js +18 -0
- package/server/fastify/FileSystemAssetStorage.cjs +60 -0
- package/server/fastify/FileSystemAssetStorage.d.ts +19 -0
- package/server/fastify/FileSystemAssetStorage.js +56 -0
- package/server/fastify/FileSystemLogger.cjs +48 -0
- package/server/fastify/FileSystemLogger.d.ts +18 -0
- package/server/fastify/FileSystemLogger.js +44 -0
- package/server/fastify/Flow.cjs +2 -0
- package/server/fastify/Flow.d.ts +9 -0
- package/server/fastify/Flow.js +1 -0
- package/server/fastify/FlowRun.cjs +71 -0
- package/server/fastify/FlowRun.d.ts +28 -0
- package/server/fastify/FlowRun.js +67 -0
- package/server/fastify/FlowSchema.cjs +2 -0
- package/server/fastify/FlowSchema.d.ts +5 -0
- package/server/fastify/FlowSchema.js +1 -0
- package/server/fastify/Logger.cjs +2 -0
- package/server/fastify/Logger.d.ts +13 -0
- package/server/fastify/Logger.js +1 -0
- package/server/fastify/PathProvider.cjs +34 -0
- package/server/fastify/PathProvider.d.ts +12 -0
- package/server/fastify/PathProvider.js +30 -0
- package/server/fastify/index.cjs +24 -0
- package/server/fastify/index.d.ts +8 -0
- package/server/fastify/index.js +8 -0
- package/server/fastify/modelFusionFlowPlugin.cjs +102 -0
- package/server/fastify/modelFusionFlowPlugin.d.ts +12 -0
- package/server/fastify/modelFusionFlowPlugin.js +98 -0
- package/util/index.cjs +1 -0
- package/util/index.d.ts +1 -0
- package/util/index.js +1 -0
- package/{event-source → util/streaming}/createEventSourceStream.cjs +7 -3
- package/util/streaming/createEventSourceStream.js +15 -0
- package/{ui → util/streaming}/index.cjs +1 -1
- package/util/streaming/index.d.ts +1 -0
- package/util/streaming/index.js +1 -0
- package/util/streaming/parseJsonStream.cjs +35 -0
- package/util/streaming/parseJsonStream.d.ts +6 -0
- package/util/streaming/parseJsonStream.js +31 -0
- package/event-source/createEventSourceStream.js +0 -11
- package/event-source/index.d.ts +0 -3
- package/event-source/index.js +0 -3
- package/ui/index.d.ts +0 -1
- package/ui/index.js +0 -1
- /package/{ui → browser}/MediaSourceAppender.cjs +0 -0
- /package/{ui → browser}/MediaSourceAppender.d.ts +0 -0
- /package/{ui → browser}/MediaSourceAppender.js +0 -0
- /package/{event-source → browser}/readEventSourceStream.d.ts +0 -0
- /package/{event-source → util/streaming}/EventSourceParserStream.cjs +0 -0
- /package/{event-source → util/streaming}/EventSourceParserStream.d.ts +0 -0
- /package/{event-source → util/streaming}/EventSourceParserStream.js +0 -0
- /package/{event-source → util/streaming}/convertReadableStreamToAsyncIterable.cjs +0 -0
- /package/{event-source → util/streaming}/convertReadableStreamToAsyncIterable.d.ts +0 -0
- /package/{event-source → util/streaming}/convertReadableStreamToAsyncIterable.js +0 -0
- /package/{event-source → util/streaming}/createEventSourceStream.d.ts +0 -0
- /package/{event-source → util/streaming}/parseEventSourceStream.cjs +0 -0
- /package/{event-source → util/streaming}/parseEventSourceStream.d.ts +0 -0
- /package/{event-source → util/streaming}/parseEventSourceStream.js +0 -0
package/README.md
CHANGED
@@ -20,6 +20,7 @@
|
|
20
20
|
- **Type inference and validation**: ModelFusion infers TypeScript types wherever possible and to validates model responses.
|
21
21
|
- **Observability and logging**: ModelFusion provides an observer framework and out-of-the-box logging support.
|
22
22
|
- **Resilience and Robustness**: ModelFusion ensures seamless operation through automatic retries, throttling, and error handling mechanisms.
|
23
|
+
- **Server**: ModelFusion provides a Fastify plugin that exposes a ModelFusion flow as a REST endpoint that uses server-sent events.
|
23
24
|
|
24
25
|
## Quick Install
|
25
26
|
|
@@ -53,7 +54,7 @@ const text = await generateText(
|
|
53
54
|
);
|
54
55
|
```
|
55
56
|
|
56
|
-
Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface)
|
57
|
+
Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface)
|
57
58
|
|
58
59
|
#### streamText
|
59
60
|
|
@@ -70,7 +71,7 @@ for await (const textPart of textStream) {
|
|
70
71
|
}
|
71
72
|
```
|
72
73
|
|
73
|
-
Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp)
|
74
|
+
Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama)
|
74
75
|
|
75
76
|
### [Generate Image](https://modelfusion.dev/guide/function/generate-image)
|
76
77
|
|
@@ -543,6 +544,71 @@ ModelFusion provides an [observer framework](https://modelfusion.dev/guide/util/
|
|
543
544
|
setGlobalFunctionLogging("detailed-object"); // log full events
|
544
545
|
```
|
545
546
|
|
547
|
+
### [Server](https://modelfusion.dev/guide/server/)
|
548
|
+
|
549
|
+
> [!WARNING]
|
550
|
+
> 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.
|
551
|
+
|
552
|
+
ModelFusion Server is desigend for running multi-modal generative AI flows that take up to several minutes to complete. It provides the following benefits:
|
553
|
+
|
554
|
+
- 🔄 Real-time progress updates via custom server-sent events
|
555
|
+
- 🔒Type-safety with Zod-schema for inputs/events
|
556
|
+
- 📦 Efficient handling of dynamically created binary assets (images, audio)
|
557
|
+
- 📜 Auto-logging for AI model interactions within flows
|
558
|
+
|
559
|
+
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.
|
560
|
+
|
561
|
+
```ts
|
562
|
+
import {
|
563
|
+
FileSystemAssetStorage,
|
564
|
+
FileSystemLogger,
|
565
|
+
modelFusionFastifyPlugin,
|
566
|
+
} from "modelfusion/fastify-server"; // '/fastify-server' import path
|
567
|
+
|
568
|
+
// configurable logging for all runs using ModelFusion observability:
|
569
|
+
const logger = new FileSystemLogger({
|
570
|
+
path: (run) => path.join(fsBasePath, run.runId, "logs"),
|
571
|
+
});
|
572
|
+
|
573
|
+
// configurable storage for large files like images and audio files:
|
574
|
+
const assetStorage = new FileSystemAssetStorage({
|
575
|
+
path: (run) => path.join(fsBasePath, run.runId, "assets"),
|
576
|
+
logger,
|
577
|
+
});
|
578
|
+
|
579
|
+
fastify.register(modelFusionFastifyPlugin, {
|
580
|
+
baseUrl,
|
581
|
+
basePath: "/myFlow",
|
582
|
+
logger,
|
583
|
+
assetStorage,
|
584
|
+
flow: exampleFlow,
|
585
|
+
});
|
586
|
+
```
|
587
|
+
|
588
|
+
Using `invokeFlow`, you can easily connect your client to a ModelFusion flow endpoint:
|
589
|
+
|
590
|
+
```ts
|
591
|
+
import { invokeFlow } from "modelfusion/browser"; // '/browser' import path
|
592
|
+
|
593
|
+
invokeFlow({
|
594
|
+
url: `${BASE_URL}/myFlow`,
|
595
|
+
schema: myFlowSchema,
|
596
|
+
input: { prompt },
|
597
|
+
onEvent(event) {
|
598
|
+
switch (event.type) {
|
599
|
+
case "my-event": {
|
600
|
+
// do something with the event
|
601
|
+
break;
|
602
|
+
}
|
603
|
+
// more events...
|
604
|
+
}
|
605
|
+
},
|
606
|
+
onStop() {
|
607
|
+
// flow finished
|
608
|
+
},
|
609
|
+
});
|
610
|
+
```
|
611
|
+
|
546
612
|
## Documentation
|
547
613
|
|
548
614
|
### [Guide](https://modelfusion.dev/guide)
|
@@ -563,6 +629,7 @@ setGlobalFunctionLogging("detailed-object"); // log full events
|
|
563
629
|
- [Retrieve](https://modelfusion.dev/guide/vector-index/retrieve)
|
564
630
|
- [Text Chunks](https://modelfusion.dev/guide/text-chunk/)
|
565
631
|
- [Split Text](https://modelfusion.dev/guide/text-chunk/split)
|
632
|
+
- [Server](https://modelfusion.dev/guide/server/)
|
566
633
|
- [Utilities](https://modelfusion.dev/guide/util/)
|
567
634
|
- [API Configuration](https://modelfusion.dev/guide/util/api-configuration)
|
568
635
|
- [Retry strategies](https://modelfusion.dev/guide/util/api-configuration/retry)
|
@@ -623,9 +690,9 @@ Create an 19th century painting image for your input.
|
|
623
690
|
|
624
691
|
Record audio with push-to-talk and transcribe it using Whisper, implemented as a Next.js app. The app shows a list of the transcriptions.
|
625
692
|
|
626
|
-
### [Speech Streaming (Vite
|
693
|
+
### [Duplex Speech Streaming (using Vite/React & ModelFusion Server/Fastify)](https://github.com/lgrammel/modelfusion/tree/main/examples/speech-streaming-vite-react-fastify)
|
627
694
|
|
628
|
-
> _Speech Streaming_, _OpenAI_, _Elevenlabs_ _streaming_, _Vite_, _Fastify_
|
695
|
+
> _Speech Streaming_, _OpenAI_, _Elevenlabs_ _streaming_, _Vite_, _Fastify_, _ModelFusion Server_
|
629
696
|
|
630
697
|
Given a prompt, the server returns both a text and a speech stream response.
|
631
698
|
|
@@ -0,0 +1,8 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.convertAudioChunksToBase64 = void 0;
|
4
|
+
const convertBlobToBase64_js_1 = require("./convertBlobToBase64.cjs");
|
5
|
+
function convertAudioChunksToBase64({ audioChunks, mimeType, }) {
|
6
|
+
return (0, convertBlobToBase64_js_1.convertBlobToBase64)(new Blob(audioChunks, { type: mimeType }));
|
7
|
+
}
|
8
|
+
exports.convertAudioChunksToBase64 = convertAudioChunksToBase64;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.convertBlobToBase64 = void 0;
|
4
|
+
async function convertBlobToBase64(blob) {
|
5
|
+
return new Promise((resolve, reject) => {
|
6
|
+
const reader = new FileReader();
|
7
|
+
reader.onloadend = () => {
|
8
|
+
if (reader.result) {
|
9
|
+
const base64String = btoa(new Uint8Array(reader.result).reduce((data, byte) => data + String.fromCharCode(byte), ""));
|
10
|
+
resolve(base64String);
|
11
|
+
}
|
12
|
+
else {
|
13
|
+
reject(new Error("Failed to read blob."));
|
14
|
+
}
|
15
|
+
};
|
16
|
+
reader.onerror = () => {
|
17
|
+
reader.abort();
|
18
|
+
reject(new DOMException("Problem parsing input blob."));
|
19
|
+
};
|
20
|
+
reader.readAsArrayBuffer(blob);
|
21
|
+
});
|
22
|
+
}
|
23
|
+
exports.convertBlobToBase64 = convertBlobToBase64;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function convertBlobToBase64(blob: Blob): Promise<string>;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
export async function convertBlobToBase64(blob) {
|
2
|
+
return new Promise((resolve, reject) => {
|
3
|
+
const reader = new FileReader();
|
4
|
+
reader.onloadend = () => {
|
5
|
+
if (reader.result) {
|
6
|
+
const base64String = btoa(new Uint8Array(reader.result).reduce((data, byte) => data + String.fromCharCode(byte), ""));
|
7
|
+
resolve(base64String);
|
8
|
+
}
|
9
|
+
else {
|
10
|
+
reject(new Error("Failed to read blob."));
|
11
|
+
}
|
12
|
+
};
|
13
|
+
reader.onerror = () => {
|
14
|
+
reader.abort();
|
15
|
+
reject(new DOMException("Problem parsing input blob."));
|
16
|
+
};
|
17
|
+
reader.readAsArrayBuffer(blob);
|
18
|
+
});
|
19
|
+
}
|
@@ -14,6 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./
|
17
|
+
__exportStar(require("./MediaSourceAppender.cjs"), exports);
|
18
|
+
__exportStar(require("./convertAudioChunksToBase64.cjs"), exports);
|
19
|
+
__exportStar(require("./convertBlobToBase64.cjs"), exports);
|
20
|
+
__exportStar(require("./invokeFlow.cjs"), exports);
|
18
21
|
__exportStar(require("./readEventSource.cjs"), exports);
|
19
22
|
__exportStar(require("./readEventSourceStream.cjs"), exports);
|
package/browser/index.js
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.invokeFlow = void 0;
|
4
|
+
const ZodSchema_1 = require("../core/structure/ZodSchema");
|
5
|
+
const readEventSource_1 = require("./readEventSource");
|
6
|
+
async function invokeFlow({ url, input, schema, onEvent, onStop, }) {
|
7
|
+
const response = await fetch(url, {
|
8
|
+
method: "POST",
|
9
|
+
headers: { "Content-Type": "application/json" },
|
10
|
+
body: JSON.stringify(input),
|
11
|
+
});
|
12
|
+
const eventSourceUrl = (await response.json()).url;
|
13
|
+
(0, readEventSource_1.readEventSource)({
|
14
|
+
url: eventSourceUrl,
|
15
|
+
schema: new ZodSchema_1.ZodSchema(schema.events),
|
16
|
+
isStopEvent(event) {
|
17
|
+
return event.data === "[DONE]";
|
18
|
+
},
|
19
|
+
onEvent,
|
20
|
+
onStop,
|
21
|
+
});
|
22
|
+
}
|
23
|
+
exports.invokeFlow = invokeFlow;
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { FlowSchema } from "../server/fastify/FlowSchema";
|
2
|
+
export declare function invokeFlow<INPUT, EVENT>({ url, input, schema, onEvent, onStop, }: {
|
3
|
+
url: string;
|
4
|
+
input: INPUT;
|
5
|
+
schema: FlowSchema<INPUT, EVENT>;
|
6
|
+
onEvent: (event: EVENT, eventSource: EventSource) => void;
|
7
|
+
onStop?: (eventSource: EventSource) => void;
|
8
|
+
}): Promise<void>;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { ZodSchema } from "../core/structure/ZodSchema";
|
2
|
+
import { readEventSource } from "./readEventSource";
|
3
|
+
export async function invokeFlow({ url, input, schema, onEvent, onStop, }) {
|
4
|
+
const response = await fetch(url, {
|
5
|
+
method: "POST",
|
6
|
+
headers: { "Content-Type": "application/json" },
|
7
|
+
body: JSON.stringify(input),
|
8
|
+
});
|
9
|
+
const eventSourceUrl = (await response.json()).url;
|
10
|
+
readEventSource({
|
11
|
+
url: eventSourceUrl,
|
12
|
+
schema: new ZodSchema(schema.events),
|
13
|
+
isStopEvent(event) {
|
14
|
+
return event.data === "[DONE]";
|
15
|
+
},
|
16
|
+
onEvent,
|
17
|
+
onStop,
|
18
|
+
});
|
19
|
+
}
|
@@ -2,11 +2,16 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.readEventSource = void 0;
|
4
4
|
const parseJSON_js_1 = require("../util/parseJSON.cjs");
|
5
|
-
function readEventSource({ url, schema, onEvent, onError = console.error, }) {
|
5
|
+
function readEventSource({ url, schema, onEvent, onError = console.error, onStop, isStopEvent, }) {
|
6
6
|
const eventSource = new EventSource(url);
|
7
|
-
eventSource.onmessage = (
|
7
|
+
eventSource.onmessage = (event) => {
|
8
8
|
try {
|
9
|
-
|
9
|
+
if (isStopEvent?.(event)) {
|
10
|
+
eventSource.close();
|
11
|
+
onStop?.(eventSource);
|
12
|
+
return;
|
13
|
+
}
|
14
|
+
const parseResult = (0, parseJSON_js_1.safeParseJsonWithSchema)(event.data, schema);
|
10
15
|
if (!parseResult.success) {
|
11
16
|
onError(parseResult.error, eventSource);
|
12
17
|
return;
|
@@ -1,7 +1,9 @@
|
|
1
1
|
import { Schema } from "../core/structure/Schema.js";
|
2
|
-
export declare function readEventSource<T>({ url, schema, onEvent, onError, }: {
|
2
|
+
export declare function readEventSource<T>({ url, schema, onEvent, onError, onStop, isStopEvent, }: {
|
3
3
|
url: string;
|
4
4
|
schema: Schema<T>;
|
5
5
|
onEvent: (event: T, eventSource: EventSource) => void;
|
6
6
|
onError?: (error: unknown, eventSource: EventSource) => void;
|
7
|
+
onStop?: (eventSource: EventSource) => void;
|
8
|
+
isStopEvent?: (event: MessageEvent<unknown>) => boolean;
|
7
9
|
}): void;
|
@@ -1,9 +1,14 @@
|
|
1
1
|
import { safeParseJsonWithSchema } from "../util/parseJSON.js";
|
2
|
-
export function readEventSource({ url, schema, onEvent, onError = console.error, }) {
|
2
|
+
export function readEventSource({ url, schema, onEvent, onError = console.error, onStop, isStopEvent, }) {
|
3
3
|
const eventSource = new EventSource(url);
|
4
|
-
eventSource.onmessage = (
|
4
|
+
eventSource.onmessage = (event) => {
|
5
5
|
try {
|
6
|
-
|
6
|
+
if (isStopEvent?.(event)) {
|
7
|
+
eventSource.close();
|
8
|
+
onStop?.(eventSource);
|
9
|
+
return;
|
10
|
+
}
|
11
|
+
const parseResult = safeParseJsonWithSchema(event.data, schema);
|
7
12
|
if (!parseResult.success) {
|
8
13
|
onError(parseResult.error, eventSource);
|
9
14
|
return;
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.readEventSourceStream = void 0;
|
4
4
|
const parseJSON_js_1 = require("../util/parseJSON.cjs");
|
5
5
|
const AsyncQueue_js_1 = require("../util/AsyncQueue.cjs");
|
6
|
-
const parseEventSourceStream_js_1 = require("
|
6
|
+
const parseEventSourceStream_js_1 = require("../util/streaming/parseEventSourceStream.cjs");
|
7
7
|
function readEventSourceStream({ stream, schema, errorHandler, }) {
|
8
8
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
9
9
|
// run async (no await on purpose):
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { safeParseJsonWithSchema } from "../util/parseJSON.js";
|
2
2
|
import { AsyncQueue } from "../util/AsyncQueue.js";
|
3
|
-
import { parseEventSourceStream } from "
|
3
|
+
import { parseEventSourceStream } from "../util/streaming/parseEventSourceStream.js";
|
4
4
|
export function readEventSourceStream({ stream, schema, errorHandler, }) {
|
5
5
|
const queue = new AsyncQueue();
|
6
6
|
// run async (no await on purpose):
|
package/index.cjs
CHANGED
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./composed-function/index.cjs"), exports);
|
18
18
|
__exportStar(require("./core/index.cjs"), exports);
|
19
19
|
__exportStar(require("./cost/index.cjs"), exports);
|
20
|
-
__exportStar(require("./event-source/index.cjs"), exports);
|
21
20
|
__exportStar(require("./guard/index.cjs"), exports);
|
22
21
|
__exportStar(require("./model-function/index.cjs"), exports);
|
23
22
|
__exportStar(require("./model-provider/index.cjs"), exports);
|
@@ -25,6 +24,5 @@ __exportStar(require("./observability/index.cjs"), exports);
|
|
25
24
|
__exportStar(require("./retriever/index.cjs"), exports);
|
26
25
|
__exportStar(require("./text-chunk/index.cjs"), exports);
|
27
26
|
__exportStar(require("./tool/index.cjs"), exports);
|
28
|
-
__exportStar(require("./ui/index.cjs"), exports);
|
29
27
|
__exportStar(require("./util/index.cjs"), exports);
|
30
28
|
__exportStar(require("./vector-index/index.cjs"), exports);
|
package/index.d.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
export * from "./composed-function/index.js";
|
2
2
|
export * from "./core/index.js";
|
3
3
|
export * from "./cost/index.js";
|
4
|
-
export * from "./event-source/index.js";
|
5
4
|
export * from "./guard/index.js";
|
6
5
|
export * from "./model-function/index.js";
|
7
6
|
export * from "./model-provider/index.js";
|
@@ -9,6 +8,5 @@ export * from "./observability/index.js";
|
|
9
8
|
export * from "./retriever/index.js";
|
10
9
|
export * from "./text-chunk/index.js";
|
11
10
|
export * from "./tool/index.js";
|
12
|
-
export * from "./ui/index.js";
|
13
11
|
export * from "./util/index.js";
|
14
12
|
export * from "./vector-index/index.js";
|
package/index.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
export * from "./composed-function/index.js";
|
2
2
|
export * from "./core/index.js";
|
3
3
|
export * from "./cost/index.js";
|
4
|
-
export * from "./event-source/index.js";
|
5
4
|
export * from "./guard/index.js";
|
6
5
|
export * from "./model-function/index.js";
|
7
6
|
export * from "./model-provider/index.js";
|
@@ -9,6 +8,5 @@ export * from "./observability/index.js";
|
|
9
8
|
export * from "./retriever/index.js";
|
10
9
|
export * from "./text-chunk/index.js";
|
11
10
|
export * from "./tool/index.js";
|
12
|
-
export * from "./ui/index.js";
|
13
11
|
export * from "./util/index.js";
|
14
12
|
export * from "./vector-index/index.js";
|
@@ -4,7 +4,7 @@ exports.AnthropicTextGenerationResponseFormat = exports.AnthropicTextGenerationM
|
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
|
-
const parseEventSourceStream_js_1 = require("../../
|
7
|
+
const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
|
8
8
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
9
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
10
10
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
-
import { parseEventSourceStream } from "../../
|
4
|
+
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
5
5
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
6
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
7
7
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
@@ -4,12 +4,12 @@ exports.CohereTextGenerationResponseFormat = exports.CohereTextGenerationModel =
|
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
|
-
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
8
7
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
8
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
10
9
|
const TextPromptFormat_js_1 = require("../../model-function/generate-text/TextPromptFormat.cjs");
|
11
10
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
12
|
-
const
|
11
|
+
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
12
|
+
const parseJsonStream_js_1 = require("../../util/streaming/parseJsonStream.cjs");
|
13
13
|
const CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
|
14
14
|
const CohereError_js_1 = require("./CohereError.cjs");
|
15
15
|
const CohereTokenizer_js_1 = require("./CohereTokenizer.cjs");
|
@@ -216,58 +216,39 @@ const cohereTextStreamingResponseSchema = zod_1.z.discriminatedUnion("is_finishe
|
|
216
216
|
async function createCohereTextGenerationFullDeltaIterableQueue(stream) {
|
217
217
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
218
218
|
let accumulatedText = "";
|
219
|
-
function processLine(line) {
|
220
|
-
const event = (0, parseJSON_js_1.parseJsonWithZod)(line, cohereTextStreamingResponseSchema);
|
221
|
-
if (event.is_finished === true) {
|
222
|
-
queue.push({
|
223
|
-
type: "delta",
|
224
|
-
fullDelta: {
|
225
|
-
content: accumulatedText,
|
226
|
-
isComplete: true,
|
227
|
-
delta: "",
|
228
|
-
},
|
229
|
-
valueDelta: "",
|
230
|
-
});
|
231
|
-
}
|
232
|
-
else {
|
233
|
-
accumulatedText += event.text;
|
234
|
-
queue.push({
|
235
|
-
type: "delta",
|
236
|
-
fullDelta: {
|
237
|
-
content: accumulatedText,
|
238
|
-
isComplete: false,
|
239
|
-
delta: event.text,
|
240
|
-
},
|
241
|
-
valueDelta: event.text,
|
242
|
-
});
|
243
|
-
}
|
244
|
-
}
|
245
219
|
// process the stream asynchonously (no 'await' on purpose):
|
246
|
-
(
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
processableLines.forEach(processLine);
|
220
|
+
(0, parseJsonStream_js_1.parseJsonStream)({
|
221
|
+
stream,
|
222
|
+
schema: cohereTextStreamingResponseSchema,
|
223
|
+
process(event) {
|
224
|
+
if (event.is_finished === true) {
|
225
|
+
queue.push({
|
226
|
+
type: "delta",
|
227
|
+
fullDelta: {
|
228
|
+
content: accumulatedText,
|
229
|
+
isComplete: true,
|
230
|
+
delta: "",
|
231
|
+
},
|
232
|
+
valueDelta: "",
|
233
|
+
});
|
261
234
|
}
|
262
|
-
|
263
|
-
|
264
|
-
|
235
|
+
else {
|
236
|
+
accumulatedText += event.text;
|
237
|
+
queue.push({
|
238
|
+
type: "delta",
|
239
|
+
fullDelta: {
|
240
|
+
content: accumulatedText,
|
241
|
+
isComplete: false,
|
242
|
+
delta: event.text,
|
243
|
+
},
|
244
|
+
valueDelta: event.text,
|
245
|
+
});
|
265
246
|
}
|
266
|
-
}
|
267
|
-
|
247
|
+
},
|
248
|
+
onDone() {
|
268
249
|
queue.close();
|
269
|
-
}
|
270
|
-
})
|
250
|
+
},
|
251
|
+
});
|
271
252
|
return queue;
|
272
253
|
}
|
273
254
|
exports.CohereTextGenerationResponseFormat = {
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
-
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
5
4
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
5
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
7
6
|
import { mapChatPromptToTextFormat, mapInstructionPromptToTextFormat, } from "../../model-function/generate-text/TextPromptFormat.js";
|
8
7
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
9
|
-
import {
|
8
|
+
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
9
|
+
import { parseJsonStream } from "../../util/streaming/parseJsonStream.js";
|
10
10
|
import { CohereApiConfiguration } from "./CohereApiConfiguration.js";
|
11
11
|
import { failedCohereCallResponseHandler } from "./CohereError.js";
|
12
12
|
import { CohereTokenizer } from "./CohereTokenizer.js";
|
@@ -212,58 +212,39 @@ const cohereTextStreamingResponseSchema = z.discriminatedUnion("is_finished", [
|
|
212
212
|
async function createCohereTextGenerationFullDeltaIterableQueue(stream) {
|
213
213
|
const queue = new AsyncQueue();
|
214
214
|
let accumulatedText = "";
|
215
|
-
function processLine(line) {
|
216
|
-
const event = parseJsonWithZod(line, cohereTextStreamingResponseSchema);
|
217
|
-
if (event.is_finished === true) {
|
218
|
-
queue.push({
|
219
|
-
type: "delta",
|
220
|
-
fullDelta: {
|
221
|
-
content: accumulatedText,
|
222
|
-
isComplete: true,
|
223
|
-
delta: "",
|
224
|
-
},
|
225
|
-
valueDelta: "",
|
226
|
-
});
|
227
|
-
}
|
228
|
-
else {
|
229
|
-
accumulatedText += event.text;
|
230
|
-
queue.push({
|
231
|
-
type: "delta",
|
232
|
-
fullDelta: {
|
233
|
-
content: accumulatedText,
|
234
|
-
isComplete: false,
|
235
|
-
delta: event.text,
|
236
|
-
},
|
237
|
-
valueDelta: event.text,
|
238
|
-
});
|
239
|
-
}
|
240
|
-
}
|
241
215
|
// process the stream asynchonously (no 'await' on purpose):
|
242
|
-
(
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
processableLines.forEach(processLine);
|
216
|
+
parseJsonStream({
|
217
|
+
stream,
|
218
|
+
schema: cohereTextStreamingResponseSchema,
|
219
|
+
process(event) {
|
220
|
+
if (event.is_finished === true) {
|
221
|
+
queue.push({
|
222
|
+
type: "delta",
|
223
|
+
fullDelta: {
|
224
|
+
content: accumulatedText,
|
225
|
+
isComplete: true,
|
226
|
+
delta: "",
|
227
|
+
},
|
228
|
+
valueDelta: "",
|
229
|
+
});
|
257
230
|
}
|
258
|
-
|
259
|
-
|
260
|
-
|
231
|
+
else {
|
232
|
+
accumulatedText += event.text;
|
233
|
+
queue.push({
|
234
|
+
type: "delta",
|
235
|
+
fullDelta: {
|
236
|
+
content: accumulatedText,
|
237
|
+
isComplete: false,
|
238
|
+
delta: event.text,
|
239
|
+
},
|
240
|
+
valueDelta: event.text,
|
241
|
+
});
|
261
242
|
}
|
262
|
-
}
|
263
|
-
|
243
|
+
},
|
244
|
+
onDone() {
|
264
245
|
queue.close();
|
265
|
-
}
|
266
|
-
})
|
246
|
+
},
|
247
|
+
});
|
267
248
|
return queue;
|
268
249
|
}
|
269
250
|
export const CohereTextGenerationResponseFormat = {
|
package/model-provider/index.cjs
CHANGED
@@ -21,5 +21,6 @@ __exportStar(require("./elevenlabs/index.cjs"), exports);
|
|
21
21
|
__exportStar(require("./huggingface/index.cjs"), exports);
|
22
22
|
__exportStar(require("./llamacpp/index.cjs"), exports);
|
23
23
|
__exportStar(require("./lmnt/index.cjs"), exports);
|
24
|
+
__exportStar(require("./ollama/index.cjs"), exports);
|
24
25
|
__exportStar(require("./openai/index.cjs"), exports);
|
25
26
|
__exportStar(require("./stability/index.cjs"), exports);
|
package/model-provider/index.js
CHANGED