modelfusion 0.54.0 → 0.55.1
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 +69 -2
- package/browser/readEventSourceStream.cjs +1 -1
- package/browser/readEventSourceStream.js +1 -1
- package/index.cjs +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/model-function/embed/EmbeddingModel.d.ts +4 -0
- package/model-function/embed/embed.cjs +12 -1
- package/model-function/embed/embed.js +12 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +1 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +6 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +1 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.js +6 -0
- package/model-provider/cohere/CohereTextGenerationModel.cjs +31 -50
- package/model-provider/cohere/CohereTextGenerationModel.js +31 -50
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +6 -0
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +1 -0
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +6 -0
- 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/LlamaCppTextEmbeddingModel.cjs +3 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +2 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +3 -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/OpenAITextEmbeddingModel.cjs +6 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +1 -0
- package/model-provider/openai/OpenAITextEmbeddingModel.js +6 -0
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +1 -1
- package/package.json +1 -1
- package/util/index.cjs +1 -0
- package/util/index.d.ts +1 -0
- package/util/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 → 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.cjs +0 -0
- /package/{event-source → util/streaming}/createEventSourceStream.d.ts +0 -0
- /package/{event-source → util/streaming}/createEventSourceStream.js +0 -0
- /package/{event-source → util/streaming}/index.cjs +0 -0
- /package/{event-source → util/streaming}/index.d.ts +0 -0
- /package/{event-source → util/streaming}/index.js +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)
|
@@ -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);
|
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";
|
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";
|
@@ -12,6 +12,10 @@ export interface EmbeddingModel<VALUE, SETTINGS extends EmbeddingModelSettings =
|
|
12
12
|
* Limit of how many values can be sent in a single API call.
|
13
13
|
*/
|
14
14
|
readonly maxValuesPerCall: number | undefined;
|
15
|
+
/**
|
16
|
+
* True if the model can handle multiple embedding calls in parallel.
|
17
|
+
*/
|
18
|
+
readonly isParallizable: boolean;
|
15
19
|
doEmbedValues(values: VALUE[], options?: FunctionOptions): PromiseLike<{
|
16
20
|
response: unknown;
|
17
21
|
embeddings: Vector[];
|
@@ -41,7 +41,18 @@ function embedMany(model, values, options) {
|
|
41
41
|
valueGroups.push(values.slice(i, i + maxValuesPerCall));
|
42
42
|
}
|
43
43
|
}
|
44
|
-
|
44
|
+
// call the model for each group:
|
45
|
+
let responses;
|
46
|
+
if (model.isParallizable) {
|
47
|
+
responses = await Promise.all(valueGroups.map((valueGroup) => model.doEmbedValues(valueGroup, options)));
|
48
|
+
}
|
49
|
+
else {
|
50
|
+
responses = [];
|
51
|
+
for (const valueGroup of valueGroups) {
|
52
|
+
const response = await model.doEmbedValues(valueGroup, options);
|
53
|
+
responses.push(response);
|
54
|
+
}
|
55
|
+
}
|
45
56
|
const rawResponses = responses.map((response) => response.response);
|
46
57
|
const embeddings = [];
|
47
58
|
for (const response of responses) {
|
@@ -38,7 +38,18 @@ export function embedMany(model, values, options) {
|
|
38
38
|
valueGroups.push(values.slice(i, i + maxValuesPerCall));
|
39
39
|
}
|
40
40
|
}
|
41
|
-
|
41
|
+
// call the model for each group:
|
42
|
+
let responses;
|
43
|
+
if (model.isParallizable) {
|
44
|
+
responses = await Promise.all(valueGroups.map((valueGroup) => model.doEmbedValues(valueGroup, options)));
|
45
|
+
}
|
46
|
+
else {
|
47
|
+
responses = [];
|
48
|
+
for (const valueGroup of valueGroups) {
|
49
|
+
const response = await model.doEmbedValues(valueGroup, options);
|
50
|
+
responses.push(response);
|
51
|
+
}
|
52
|
+
}
|
42
53
|
const rawResponses = responses.map((response) => response.response);
|
43
54
|
const embeddings = [];
|
44
55
|
for (const response of responses) {
|
@@ -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";
|
@@ -51,6 +51,12 @@ class CohereTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
51
51
|
writable: true,
|
52
52
|
value: 96
|
53
53
|
});
|
54
|
+
Object.defineProperty(this, "isParallizable", {
|
55
|
+
enumerable: true,
|
56
|
+
configurable: true,
|
57
|
+
writable: true,
|
58
|
+
value: true
|
59
|
+
});
|
54
60
|
Object.defineProperty(this, "embeddingDimensions", {
|
55
61
|
enumerable: true,
|
56
62
|
configurable: true,
|
@@ -43,6 +43,7 @@ export declare class CohereTextEmbeddingModel extends AbstractModel<CohereTextEm
|
|
43
43
|
readonly provider: "cohere";
|
44
44
|
get modelName(): "embed-english-light-v2.0" | "embed-english-v2.0" | "embed-multilingual-v2.0";
|
45
45
|
readonly maxValuesPerCall = 96;
|
46
|
+
readonly isParallizable = true;
|
46
47
|
readonly embeddingDimensions: number;
|
47
48
|
readonly contextWindowSize: number;
|
48
49
|
private readonly tokenizer;
|
@@ -48,6 +48,12 @@ export class CohereTextEmbeddingModel extends AbstractModel {
|
|
48
48
|
writable: true,
|
49
49
|
value: 96
|
50
50
|
});
|
51
|
+
Object.defineProperty(this, "isParallizable", {
|
52
|
+
enumerable: true,
|
53
|
+
configurable: true,
|
54
|
+
writable: true,
|
55
|
+
value: true
|
56
|
+
});
|
51
57
|
Object.defineProperty(this, "embeddingDimensions", {
|
52
58
|
enumerable: true,
|
53
59
|
configurable: true,
|
@@ -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 = {
|
@@ -42,6 +42,12 @@ class HuggingFaceTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
42
42
|
writable: true,
|
43
43
|
value: void 0
|
44
44
|
});
|
45
|
+
Object.defineProperty(this, "isParallizable", {
|
46
|
+
enumerable: true,
|
47
|
+
configurable: true,
|
48
|
+
writable: true,
|
49
|
+
value: true
|
50
|
+
});
|
45
51
|
Object.defineProperty(this, "contextWindowSize", {
|
46
52
|
enumerable: true,
|
47
53
|
configurable: true,
|
@@ -38,6 +38,7 @@ export declare class HuggingFaceTextEmbeddingModel extends AbstractModel<Hugging
|
|
38
38
|
readonly provider = "huggingface";
|
39
39
|
get modelName(): string;
|
40
40
|
readonly maxValuesPerCall: number;
|
41
|
+
readonly isParallizable = true;
|
41
42
|
readonly contextWindowSize: undefined;
|
42
43
|
readonly embeddingDimensions: number | undefined;
|
43
44
|
readonly tokenizer: undefined;
|
@@ -39,6 +39,12 @@ export class HuggingFaceTextEmbeddingModel extends AbstractModel {
|
|
39
39
|
writable: true,
|
40
40
|
value: void 0
|
41
41
|
});
|
42
|
+
Object.defineProperty(this, "isParallizable", {
|
43
|
+
enumerable: true,
|
44
|
+
configurable: true,
|
45
|
+
writable: true,
|
46
|
+
value: true
|
47
|
+
});
|
42
48
|
Object.defineProperty(this, "contextWindowSize", {
|
43
49
|
enumerable: true,
|
44
50
|
configurable: true,
|
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
@@ -47,6 +47,9 @@ class LlamaCppTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
47
47
|
get modelName() {
|
48
48
|
return null;
|
49
49
|
}
|
50
|
+
get isParallizable() {
|
51
|
+
return this.settings.isParallizable ?? false;
|
52
|
+
}
|
50
53
|
async tokenize(text) {
|
51
54
|
return this.tokenizer.tokenize(text);
|
52
55
|
}
|
@@ -6,12 +6,14 @@ import { EmbeddingModel, EmbeddingModelSettings } from "../../model-function/emb
|
|
6
6
|
export interface LlamaCppTextEmbeddingModelSettings extends EmbeddingModelSettings {
|
7
7
|
api?: ApiConfiguration;
|
8
8
|
embeddingDimensions?: number;
|
9
|
+
isParallizable?: boolean;
|
9
10
|
}
|
10
11
|
export declare class LlamaCppTextEmbeddingModel extends AbstractModel<LlamaCppTextEmbeddingModelSettings> implements EmbeddingModel<string, LlamaCppTextEmbeddingModelSettings> {
|
11
12
|
constructor(settings?: LlamaCppTextEmbeddingModelSettings);
|
12
13
|
readonly provider: "llamacpp";
|
13
14
|
get modelName(): null;
|
14
15
|
readonly maxValuesPerCall = 1;
|
16
|
+
get isParallizable(): boolean;
|
15
17
|
readonly contextWindowSize: undefined;
|
16
18
|
readonly embeddingDimensions: number | undefined;
|
17
19
|
private readonly tokenizer;
|
@@ -5,7 +5,7 @@ const zod_1 = require("zod");
|
|
5
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
7
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
8
|
-
const parseEventSourceStream_js_1 = require("../../
|
8
|
+
const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
|
9
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
10
10
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
11
11
|
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
4
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
5
|
-
import { parseEventSourceStream } from "../../
|
5
|
+
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
6
6
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
7
7
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
8
8
|
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.OllamaApiConfiguration = void 0;
|
4
|
+
const BaseUrlApiConfiguration_js_1 = require("../../core/api/BaseUrlApiConfiguration.cjs");
|
5
|
+
class OllamaApiConfiguration extends BaseUrlApiConfiguration_js_1.BaseUrlApiConfiguration {
|
6
|
+
constructor({ baseUrl = "http://127.0.0.1:11434", retry, throttle, } = {}) {
|
7
|
+
super({
|
8
|
+
baseUrl,
|
9
|
+
headers: {},
|
10
|
+
retry,
|
11
|
+
throttle,
|
12
|
+
});
|
13
|
+
}
|
14
|
+
}
|
15
|
+
exports.OllamaApiConfiguration = OllamaApiConfiguration;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
|
2
|
+
import { RetryFunction } from "../../core/api/RetryFunction.js";
|
3
|
+
import { ThrottleFunction } from "../../core/api/ThrottleFunction.js";
|
4
|
+
export declare class OllamaApiConfiguration extends BaseUrlApiConfiguration {
|
5
|
+
constructor({ baseUrl, retry, throttle, }?: {
|
6
|
+
baseUrl?: string;
|
7
|
+
retry?: RetryFunction;
|
8
|
+
throttle?: ThrottleFunction;
|
9
|
+
});
|
10
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
|
2
|
+
export class OllamaApiConfiguration extends BaseUrlApiConfiguration {
|
3
|
+
constructor({ baseUrl = "http://127.0.0.1:11434", retry, throttle, } = {}) {
|
4
|
+
super({
|
5
|
+
baseUrl,
|
6
|
+
headers: {},
|
7
|
+
retry,
|
8
|
+
throttle,
|
9
|
+
});
|
10
|
+
}
|
11
|
+
}
|