modelfusion 0.37.0 → 0.39.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 +7 -8
- package/core/structure/JsonSchemaProducer.d.ts +9 -0
- package/core/structure/Schema.d.ts +1 -5
- package/core/structure/StructureDefinition.d.ts +2 -1
- package/core/structure/{UncheckedJsonSchemaSchema.cjs → UncheckedSchema.cjs} +3 -3
- package/core/structure/{UncheckedJsonSchemaSchema.d.ts → UncheckedSchema.d.ts} +3 -3
- package/core/structure/{UncheckedJsonSchemaSchema.js → UncheckedSchema.js} +1 -1
- package/core/structure/{UncheckedJsonSchemaStructureDefinition.cjs → UncheckedStructureDefinition.cjs} +5 -5
- package/core/structure/{UncheckedJsonSchemaStructureDefinition.d.ts → UncheckedStructureDefinition.d.ts} +3 -3
- package/core/structure/{UncheckedJsonSchemaStructureDefinition.js → UncheckedStructureDefinition.js} +3 -3
- package/core/structure/ZodStructureDefinition.d.ts +2 -2
- package/core/structure/index.cjs +3 -2
- package/core/structure/index.d.ts +3 -2
- package/core/structure/index.js +3 -2
- package/event-source/index.cjs +1 -0
- package/event-source/index.d.ts +1 -0
- package/event-source/index.js +1 -0
- package/event-source/readEventSource.cjs +27 -0
- package/event-source/readEventSource.d.ts +7 -0
- package/event-source/readEventSource.js +20 -0
- package/event-source/readEventSourceStream.cjs +6 -1
- package/event-source/readEventSourceStream.d.ts +2 -1
- package/event-source/readEventSourceStream.js +6 -1
- package/model-function/ModelCallEvent.d.ts +3 -3
- package/model-function/embed/EmbeddingEvent.d.ts +21 -0
- package/model-function/embed/EmbeddingModel.cjs +2 -0
- package/model-function/embed/EmbeddingModel.d.ts +17 -0
- package/model-function/embed/EmbeddingModel.js +1 -0
- package/model-function/{embed-text/embedText.cjs → embed/embed.cjs} +22 -22
- package/model-function/embed/embed.d.ts +27 -0
- package/model-function/{embed-text/embedText.js → embed/embed.js} +19 -19
- package/model-function/index.cjs +3 -3
- package/model-function/index.d.ts +3 -3
- package/model-function/index.js +3 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +5 -5
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +6 -6
- package/model-provider/cohere/CohereTextEmbeddingModel.js +5 -5
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +5 -5
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +6 -6
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +5 -5
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +4 -4
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +5 -5
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +4 -4
- package/model-provider/openai/OpenAICostCalculator.cjs +1 -1
- package/model-provider/openai/OpenAICostCalculator.js +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +4 -4
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +5 -5
- package/model-provider/openai/OpenAITextEmbeddingModel.js +4 -4
- package/package.json +1 -1
- package/tool/Tool.d.ts +3 -2
- package/vector-index/VectorIndexRetriever.cjs +3 -8
- package/vector-index/VectorIndexRetriever.d.ts +4 -9
- package/vector-index/VectorIndexRetriever.js +3 -8
- package/vector-index/memory/MemoryVectorIndex.cjs +15 -8
- package/vector-index/memory/MemoryVectorIndex.d.ts +2 -2
- package/vector-index/memory/MemoryVectorIndex.js +15 -8
- package/vector-index/upsertIntoVectorIndex.cjs +2 -2
- package/vector-index/upsertIntoVectorIndex.d.ts +4 -4
- package/vector-index/upsertIntoVectorIndex.js +2 -2
- package/model-function/embed-text/TextEmbeddingEvent.d.ts +0 -21
- package/model-function/embed-text/TextEmbeddingModel.d.ts +0 -21
- package/model-function/embed-text/embedText.d.ts +0 -27
- /package/{model-function/embed-text/TextEmbeddingEvent.cjs → core/structure/JsonSchemaProducer.cjs} +0 -0
- /package/{model-function/embed-text/TextEmbeddingEvent.js → core/structure/JsonSchemaProducer.js} +0 -0
- /package/model-function/{embed-text/TextEmbeddingModel.cjs → embed/EmbeddingEvent.cjs} +0 -0
- /package/model-function/{embed-text/TextEmbeddingModel.js → embed/EmbeddingEvent.js} +0 -0
package/README.md
CHANGED
@@ -17,10 +17,9 @@
|
|
17
17
|
|
18
18
|
ModelFusion is a library for building AI apps, chatbots, and agents. It provides abstractions for AI models, vector indices, and tools.
|
19
19
|
|
20
|
-
- **Type inference and validation**: ModelFusion uses TypeScript and [Zod](https://github.com/colinhacks/zod) to infer types wherever possible and to validate model responses.
|
21
|
-
- **Flexibility and control**: AI application development can be complex and unique to each project. With ModelFusion, you have complete control over the prompts and model settings, and you can access the raw responses from the models quickly to build what you need.
|
22
|
-
- **No chains and predefined prompts**: Use the concepts provided by JavaScript (variables, functions, etc.) and explicit prompts to build applications you can easily understand and control. Not hidden prompts and logic.
|
23
20
|
- **Multimodal Support**: Beyond just LLMs, ModelFusion encompasses a diverse array of models including text generation, text-to-speech, speech-to-text, and image generation, allowing you to build multifaceted AI applications with ease.
|
21
|
+
- **Flexibility and control**: AI application development can be complex and unique to each project. With ModelFusion, you have complete control over the prompts and model settings, and you can access the raw responses from the models quickly to build what you need.
|
22
|
+
- **Type inference and validation**: ModelFusion uses TypeScript and [Zod](https://github.com/colinhacks/zod) to infer types wherever possible and to validate model responses.
|
24
23
|
- **Integrated support features**: Essential features like logging, retries, throttling, tracing, and error handling are built-in, helping you focus more on building your application.
|
25
24
|
|
26
25
|
## Quick Install
|
@@ -358,7 +357,7 @@ const text = await describeImage(
|
|
358
357
|
new HuggingFaceImageDescriptionModel({
|
359
358
|
model: "nlpconnect/vit-gpt2-image-captioning",
|
360
359
|
}),
|
361
|
-
data
|
360
|
+
data // buffer with image data
|
362
361
|
);
|
363
362
|
```
|
364
363
|
|
@@ -377,12 +376,12 @@ const image = await generateImage(
|
|
377
376
|
|
378
377
|
Providers: [OpenAI (Dall·E)](https://modelfusion.dev/integration/model-provider/openai), [Stability AI](https://modelfusion.dev/integration/model-provider/stability), [Automatic1111](https://modelfusion.dev/integration/model-provider/automatic1111)
|
379
378
|
|
380
|
-
### [Embed
|
379
|
+
### [Embed Value](https://modelfusion.dev/guide/function/embed)
|
381
380
|
|
382
|
-
Create embeddings for text. Embeddings are vectors that represent the
|
381
|
+
Create embeddings for text and other values. Embeddings are vectors that represent the essence of the values in the context of the model.
|
383
382
|
|
384
383
|
```ts
|
385
|
-
const embeddings = await
|
384
|
+
const embeddings = await embedMany(
|
386
385
|
new OpenAITextEmbeddingModel({ model: "text-embedding-ada-002" }),
|
387
386
|
[
|
388
387
|
"At first, Nox didn't know what to do with the pup.",
|
@@ -459,7 +458,7 @@ Integrations: [Helicone](https://modelfusion.dev/integration/observability/helic
|
|
459
458
|
- [Generate and stream text](https://modelfusion.dev/guide/function/generate-text)
|
460
459
|
- [Generate structure](https://modelfusion.dev/guide/function/generate-structure)
|
461
460
|
- [Generate structure or text](https://modelfusion.dev/guide/function/generate-structure-or-text)
|
462
|
-
- [Embed
|
461
|
+
- [Embed Value](https://modelfusion.dev/guide/function/embed)
|
463
462
|
- [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
|
464
463
|
- [Transcribe Speech](https://modelfusion.dev/guide/function/transcribe-speech)
|
465
464
|
- [Synthesize Speech](https://modelfusion.dev/guide/function/synthesize-speech)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/**
|
2
|
-
*
|
2
|
+
* Validates that the structure of `value` matches the structure of this schema.
|
3
3
|
*/
|
4
4
|
export interface Schema<STRUCTURE> {
|
5
5
|
/**
|
@@ -13,10 +13,6 @@ export interface Schema<STRUCTURE> {
|
|
13
13
|
success: false;
|
14
14
|
error: unknown;
|
15
15
|
};
|
16
|
-
/**
|
17
|
-
* Returns the JSON schema for this schema. The schema has to be a valid JSON schema in object form.
|
18
|
-
*/
|
19
|
-
getJsonSchema(): unknown;
|
20
16
|
/**
|
21
17
|
* Only used for type inference.
|
22
18
|
*/
|
@@ -1,6 +1,7 @@
|
|
1
|
+
import { JsonSchemaProducer } from "./JsonSchemaProducer.js";
|
1
2
|
import { Schema } from "./Schema.js";
|
2
3
|
export interface StructureDefinition<NAME extends string, STRUCTURE> {
|
3
4
|
name: NAME;
|
4
5
|
description?: string;
|
5
|
-
schema: Schema<STRUCTURE
|
6
|
+
schema: Schema<STRUCTURE> & JsonSchemaProducer;
|
6
7
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
class
|
3
|
+
exports.UncheckedSchema = void 0;
|
4
|
+
class UncheckedSchema {
|
5
5
|
constructor(jsonSchema) {
|
6
6
|
Object.defineProperty(this, "jsonSchema", {
|
7
7
|
enumerable: true,
|
@@ -23,4 +23,4 @@ class UncheckedJsonSchemaSchema {
|
|
23
23
|
return this.jsonSchema;
|
24
24
|
}
|
25
25
|
}
|
26
|
-
exports.
|
26
|
+
exports.UncheckedSchema = UncheckedSchema;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { Schema } from "./Schema.js";
|
2
|
-
export declare class
|
3
|
-
private readonly jsonSchema
|
4
|
-
constructor(jsonSchema
|
2
|
+
export declare class UncheckedSchema<STRUCTURE> implements Schema<STRUCTURE> {
|
3
|
+
private readonly jsonSchema?;
|
4
|
+
constructor(jsonSchema?: unknown);
|
5
5
|
validate(value: unknown): {
|
6
6
|
success: true;
|
7
7
|
value: STRUCTURE;
|
@@ -1,8 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
const
|
5
|
-
class
|
3
|
+
exports.UncheckedStructureDefinition = void 0;
|
4
|
+
const UncheckedSchema_js_1 = require("./UncheckedSchema.cjs");
|
5
|
+
class UncheckedStructureDefinition {
|
6
6
|
constructor({ name, description, jsonSchema, }) {
|
7
7
|
Object.defineProperty(this, "name", {
|
8
8
|
enumerable: true,
|
@@ -24,7 +24,7 @@ class UncheckedJsonSchemaStructureDefinition {
|
|
24
24
|
});
|
25
25
|
this.name = name;
|
26
26
|
this.description = description;
|
27
|
-
this.schema = new
|
27
|
+
this.schema = new UncheckedSchema_js_1.UncheckedSchema(jsonSchema);
|
28
28
|
}
|
29
29
|
}
|
30
|
-
exports.
|
30
|
+
exports.UncheckedStructureDefinition = UncheckedStructureDefinition;
|
@@ -1,9 +1,9 @@
|
|
1
|
-
import { Schema } from "./Schema.js";
|
2
1
|
import { StructureDefinition } from "./StructureDefinition.js";
|
3
|
-
|
2
|
+
import { UncheckedSchema } from "./UncheckedSchema.js";
|
3
|
+
export declare class UncheckedStructureDefinition<NAME extends string, STRUCTURE> implements StructureDefinition<NAME, STRUCTURE> {
|
4
4
|
name: NAME;
|
5
5
|
description?: string;
|
6
|
-
schema:
|
6
|
+
schema: UncheckedSchema<STRUCTURE>;
|
7
7
|
constructor({ name, description, jsonSchema, }: {
|
8
8
|
name: NAME;
|
9
9
|
description?: string;
|
package/core/structure/{UncheckedJsonSchemaStructureDefinition.js → UncheckedStructureDefinition.js}
RENAMED
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
export class
|
1
|
+
import { UncheckedSchema } from "./UncheckedSchema.js";
|
2
|
+
export class UncheckedStructureDefinition {
|
3
3
|
constructor({ name, description, jsonSchema, }) {
|
4
4
|
Object.defineProperty(this, "name", {
|
5
5
|
enumerable: true,
|
@@ -21,6 +21,6 @@ export class UncheckedJsonSchemaStructureDefinition {
|
|
21
21
|
});
|
22
22
|
this.name = name;
|
23
23
|
this.description = description;
|
24
|
-
this.schema = new
|
24
|
+
this.schema = new UncheckedSchema(jsonSchema);
|
25
25
|
}
|
26
26
|
}
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import { Schema } from "./Schema.js";
|
3
2
|
import { StructureDefinition } from "./StructureDefinition.js";
|
3
|
+
import { ZodSchema } from "./ZodSchema.js";
|
4
4
|
export declare class ZodStructureDefinition<NAME extends string, STRUCTURE> implements StructureDefinition<NAME, STRUCTURE> {
|
5
5
|
name: NAME;
|
6
6
|
description?: string;
|
7
|
-
schema:
|
7
|
+
schema: ZodSchema<STRUCTURE>;
|
8
8
|
constructor({ name, description, schema, }: {
|
9
9
|
name: NAME;
|
10
10
|
description?: string;
|
package/core/structure/index.cjs
CHANGED
@@ -14,9 +14,10 @@ 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("./JsonSchemaProducer.cjs"), exports);
|
17
18
|
__exportStar(require("./Schema.cjs"), exports);
|
18
19
|
__exportStar(require("./StructureDefinition.cjs"), exports);
|
19
|
-
__exportStar(require("./
|
20
|
-
__exportStar(require("./
|
20
|
+
__exportStar(require("./UncheckedSchema.cjs"), exports);
|
21
|
+
__exportStar(require("./UncheckedStructureDefinition.cjs"), exports);
|
21
22
|
__exportStar(require("./ZodSchema.cjs"), exports);
|
22
23
|
__exportStar(require("./ZodStructureDefinition.cjs"), exports);
|
@@ -1,6 +1,7 @@
|
|
1
|
+
export * from "./JsonSchemaProducer.js";
|
1
2
|
export * from "./Schema.js";
|
2
3
|
export * from "./StructureDefinition.js";
|
3
|
-
export * from "./
|
4
|
-
export * from "./
|
4
|
+
export * from "./UncheckedSchema.js";
|
5
|
+
export * from "./UncheckedStructureDefinition.js";
|
5
6
|
export * from "./ZodSchema.js";
|
6
7
|
export * from "./ZodStructureDefinition.js";
|
package/core/structure/index.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
export * from "./JsonSchemaProducer.js";
|
1
2
|
export * from "./Schema.js";
|
2
3
|
export * from "./StructureDefinition.js";
|
3
|
-
export * from "./
|
4
|
-
export * from "./
|
4
|
+
export * from "./UncheckedSchema.js";
|
5
|
+
export * from "./UncheckedStructureDefinition.js";
|
5
6
|
export * from "./ZodSchema.js";
|
6
7
|
export * from "./ZodStructureDefinition.js";
|
package/event-source/index.cjs
CHANGED
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
__exportStar(require("./AsyncQueue.cjs"), exports);
|
18
18
|
__exportStar(require("./createEventSourceStream.cjs"), exports);
|
19
|
+
__exportStar(require("./readEventSource.cjs"), exports);
|
19
20
|
__exportStar(require("./readEventSourceStream.cjs"), exports);
|
package/event-source/index.d.ts
CHANGED
package/event-source/index.js
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.readEventSource = void 0;
|
7
|
+
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
|
+
function readEventSource({ url, schema, onEvent, onError = console.error, }) {
|
9
|
+
const eventSource = new EventSource(url);
|
10
|
+
eventSource.onmessage = (e) => {
|
11
|
+
try {
|
12
|
+
const validationResult = schema.validate(secure_json_parse_1.default.parse(e.data));
|
13
|
+
if (!validationResult.success) {
|
14
|
+
onError(validationResult.error, eventSource);
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
onEvent(validationResult.value, eventSource);
|
18
|
+
}
|
19
|
+
catch (error) {
|
20
|
+
onError(error, eventSource);
|
21
|
+
}
|
22
|
+
};
|
23
|
+
eventSource.onerror = (e) => {
|
24
|
+
onError(e, eventSource);
|
25
|
+
};
|
26
|
+
}
|
27
|
+
exports.readEventSource = readEventSource;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { Schema } from "../core/structure/Schema";
|
2
|
+
export declare function readEventSource<T>({ url, schema, onEvent, onError, }: {
|
3
|
+
url: string;
|
4
|
+
schema: Schema<T>;
|
5
|
+
onEvent: (event: T, eventSource: EventSource) => void;
|
6
|
+
onError?: (error: unknown, eventSource: EventSource) => void;
|
7
|
+
}): void;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
2
|
+
export function readEventSource({ url, schema, onEvent, onError = console.error, }) {
|
3
|
+
const eventSource = new EventSource(url);
|
4
|
+
eventSource.onmessage = (e) => {
|
5
|
+
try {
|
6
|
+
const validationResult = schema.validate(SecureJSON.parse(e.data));
|
7
|
+
if (!validationResult.success) {
|
8
|
+
onError(validationResult.error, eventSource);
|
9
|
+
return;
|
10
|
+
}
|
11
|
+
onEvent(validationResult.value, eventSource);
|
12
|
+
}
|
13
|
+
catch (error) {
|
14
|
+
onError(error, eventSource);
|
15
|
+
}
|
16
|
+
};
|
17
|
+
eventSource.onerror = (e) => {
|
18
|
+
onError(e, eventSource);
|
19
|
+
};
|
20
|
+
}
|
@@ -14,7 +14,12 @@ function readEventSourceStream({ stream, schema, errorHandler, }) {
|
|
14
14
|
.then(async (events) => {
|
15
15
|
try {
|
16
16
|
for await (const event of events) {
|
17
|
-
|
17
|
+
const validationResult = schema.validate(secure_json_parse_1.default.parse(event.data));
|
18
|
+
if (!validationResult.success) {
|
19
|
+
errorHandler?.(validationResult.error);
|
20
|
+
continue;
|
21
|
+
}
|
22
|
+
queue.push(validationResult.value);
|
18
23
|
}
|
19
24
|
}
|
20
25
|
catch (error) {
|
@@ -1,6 +1,7 @@
|
|
1
|
+
import { Schema } from "../core/structure/Schema.js";
|
1
2
|
import { ErrorHandler } from "../util/ErrorHandler.js";
|
2
3
|
export declare function readEventSourceStream<T>({ stream, schema, errorHandler, }: {
|
3
4
|
stream: ReadableStream<Uint8Array>;
|
4
|
-
schema:
|
5
|
+
schema: Schema<T>;
|
5
6
|
errorHandler?: ErrorHandler;
|
6
7
|
}): AsyncIterable<T>;
|
@@ -8,7 +8,12 @@ export function readEventSourceStream({ stream, schema, errorHandler, }) {
|
|
8
8
|
.then(async (events) => {
|
9
9
|
try {
|
10
10
|
for await (const event of events) {
|
11
|
-
|
11
|
+
const validationResult = schema.validate(SecureJSON.parse(event.data));
|
12
|
+
if (!validationResult.success) {
|
13
|
+
errorHandler?.(validationResult.error);
|
14
|
+
continue;
|
15
|
+
}
|
16
|
+
queue.push(validationResult.value);
|
12
17
|
}
|
13
18
|
}
|
14
19
|
catch (error) {
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
|
2
2
|
import { ModelInformation } from "./ModelInformation.js";
|
3
3
|
import { ImageDescriptionFinishedEvent, ImageDescriptionStartedEvent } from "./describe-image/ImageDescriptionEvent.js";
|
4
|
-
import {
|
4
|
+
import { EmbeddingFinishedEvent, EmbeddingStartedEvent } from "./embed/EmbeddingEvent.js";
|
5
5
|
import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
|
6
6
|
import { StructureGenerationFinishedEvent, StructureGenerationStartedEvent } from "./generate-structure/StructureGenerationEvent.js";
|
7
7
|
import { StructureStreamingFinishedEvent, StructureStreamingStartedEvent } from "./generate-structure/StructureStreamingEvent.js";
|
@@ -49,5 +49,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
|
|
49
49
|
*/
|
50
50
|
result: BaseModelCallFinishedEventResult;
|
51
51
|
}
|
52
|
-
export type ModelCallStartedEvent = ImageDescriptionStartedEvent | ImageGenerationStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | SpeechSynthesisStartedEvent |
|
53
|
-
export type ModelCallFinishedEvent = ImageDescriptionFinishedEvent | ImageGenerationFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | SpeechSynthesisFinishedEvent |
|
52
|
+
export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageDescriptionStartedEvent | ImageGenerationStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | SpeechSynthesisStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
|
53
|
+
export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageDescriptionFinishedEvent | ImageGenerationFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | SpeechSynthesisFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { Vector } from "../../core/Vector.js";
|
2
|
+
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
3
|
+
export interface EmbeddingStartedEvent extends BaseModelCallStartedEvent {
|
4
|
+
functionType: "embedding";
|
5
|
+
input: unknown | Array<unknown>;
|
6
|
+
}
|
7
|
+
export type EmbeddingFinishedEventResult = {
|
8
|
+
status: "success";
|
9
|
+
response: unknown;
|
10
|
+
output: Vector | Array<Vector>;
|
11
|
+
} | {
|
12
|
+
status: "error";
|
13
|
+
error: unknown;
|
14
|
+
} | {
|
15
|
+
status: "abort";
|
16
|
+
};
|
17
|
+
export interface EmbeddingFinishedEvent extends BaseModelCallFinishedEvent {
|
18
|
+
functionType: "embedding";
|
19
|
+
input: unknown | Array<unknown>;
|
20
|
+
result: EmbeddingFinishedEventResult;
|
21
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { Vector } from "../../core/Vector.js";
|
2
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
3
|
+
import { Model, ModelSettings } from "../Model.js";
|
4
|
+
export interface EmbeddingModelSettings extends ModelSettings {
|
5
|
+
}
|
6
|
+
export interface EmbeddingModel<VALUE, RESPONSE, SETTINGS extends EmbeddingModelSettings> extends Model<SETTINGS> {
|
7
|
+
/**
|
8
|
+
* The size of the embedding vector.
|
9
|
+
*/
|
10
|
+
readonly embeddingDimensions: number | undefined;
|
11
|
+
/**
|
12
|
+
* Limit of how many values can be sent in a single API call.
|
13
|
+
*/
|
14
|
+
readonly maxValuesPerCall: number | undefined;
|
15
|
+
generateEmbeddingResponse(values: VALUE[], options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
16
|
+
extractEmbeddings(response: RESPONSE): Vector[];
|
17
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -1,12 +1,12 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.embed = exports.embedMany = void 0;
|
4
4
|
const executeCall_js_1 = require("../executeCall.cjs");
|
5
5
|
/**
|
6
|
-
* Generate embeddings for multiple
|
6
|
+
* Generate embeddings for multiple values.
|
7
7
|
*
|
8
8
|
* @example
|
9
|
-
* const embeddings = await
|
9
|
+
* const embeddings = await embedMany(
|
10
10
|
* new OpenAITextEmbeddingModel(...),
|
11
11
|
* [
|
12
12
|
* "At first, Nox didn't know what to do with the pup.",
|
@@ -14,25 +14,25 @@ const executeCall_js_1 = require("../executeCall.cjs");
|
|
14
14
|
* ]
|
15
15
|
* );
|
16
16
|
*/
|
17
|
-
function
|
17
|
+
function embedMany(model, values, options) {
|
18
18
|
return (0, executeCall_js_1.executeCall)({
|
19
|
-
functionType: "
|
20
|
-
input:
|
19
|
+
functionType: "embedding",
|
20
|
+
input: values,
|
21
21
|
model,
|
22
22
|
options,
|
23
23
|
generateResponse: (options) => {
|
24
|
-
// split the
|
25
|
-
const
|
26
|
-
const
|
27
|
-
if (
|
28
|
-
|
24
|
+
// split the values into groups that are small enough to be sent in one call:
|
25
|
+
const maxValuesPerCall = model.maxValuesPerCall;
|
26
|
+
const valueGroups = [];
|
27
|
+
if (maxValuesPerCall == null) {
|
28
|
+
valueGroups.push(values);
|
29
29
|
}
|
30
30
|
else {
|
31
|
-
for (let i = 0; i <
|
32
|
-
|
31
|
+
for (let i = 0; i < values.length; i += maxValuesPerCall) {
|
32
|
+
valueGroups.push(values.slice(i, i + maxValuesPerCall));
|
33
33
|
}
|
34
34
|
}
|
35
|
-
return Promise.all(
|
35
|
+
return Promise.all(valueGroups.map((valueGroup) => model.generateEmbeddingResponse(valueGroup, options)));
|
36
36
|
},
|
37
37
|
extractOutputValue: (result) => {
|
38
38
|
const embeddings = [];
|
@@ -43,26 +43,26 @@ function embedTexts(model, texts, options) {
|
|
43
43
|
},
|
44
44
|
});
|
45
45
|
}
|
46
|
-
exports.
|
46
|
+
exports.embedMany = embedMany;
|
47
47
|
/**
|
48
|
-
* Generate an embedding for a single
|
48
|
+
* Generate an embedding for a single value.
|
49
49
|
*
|
50
50
|
* @example
|
51
|
-
* const embedding = await
|
51
|
+
* const embedding = await embed(
|
52
52
|
* new OpenAITextEmbeddingModel(...),
|
53
53
|
* "At first, Nox didn't know what to do with the pup."
|
54
54
|
* );
|
55
55
|
*/
|
56
|
-
function
|
56
|
+
function embed(model, value, options) {
|
57
57
|
return (0, executeCall_js_1.executeCall)({
|
58
|
-
functionType: "
|
59
|
-
input:
|
58
|
+
functionType: "embedding",
|
59
|
+
input: value,
|
60
60
|
model,
|
61
61
|
options,
|
62
62
|
generateResponse: async (options) => [
|
63
|
-
await model.generateEmbeddingResponse([
|
63
|
+
await model.generateEmbeddingResponse([value], options),
|
64
64
|
],
|
65
65
|
extractOutputValue: (result) => model.extractEmbeddings(result[0])[0],
|
66
66
|
});
|
67
67
|
}
|
68
|
-
exports.
|
68
|
+
exports.embed = embed;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { Vector } from "../../core/Vector.js";
|
2
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
3
|
+
import { ModelFunctionPromise } from "../executeCall.js";
|
4
|
+
import { EmbeddingModel, EmbeddingModelSettings } from "./EmbeddingModel.js";
|
5
|
+
/**
|
6
|
+
* Generate embeddings for multiple values.
|
7
|
+
*
|
8
|
+
* @example
|
9
|
+
* const embeddings = await embedMany(
|
10
|
+
* new OpenAITextEmbeddingModel(...),
|
11
|
+
* [
|
12
|
+
* "At first, Nox didn't know what to do with the pup.",
|
13
|
+
* "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
|
14
|
+
* ]
|
15
|
+
* );
|
16
|
+
*/
|
17
|
+
export declare function embedMany<VALUE, RESPONSE, SETTINGS extends EmbeddingModelSettings>(model: EmbeddingModel<VALUE, RESPONSE, SETTINGS>, values: VALUE[], options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<Vector[], RESPONSE[]>;
|
18
|
+
/**
|
19
|
+
* Generate an embedding for a single value.
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* const embedding = await embed(
|
23
|
+
* new OpenAITextEmbeddingModel(...),
|
24
|
+
* "At first, Nox didn't know what to do with the pup."
|
25
|
+
* );
|
26
|
+
*/
|
27
|
+
export declare function embed<VALUE, RESPONSE, SETTINGS extends EmbeddingModelSettings>(model: EmbeddingModel<VALUE, RESPONSE, SETTINGS>, value: VALUE, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<Vector, RESPONSE[]>;
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { executeCall } from "../executeCall.js";
|
2
2
|
/**
|
3
|
-
* Generate embeddings for multiple
|
3
|
+
* Generate embeddings for multiple values.
|
4
4
|
*
|
5
5
|
* @example
|
6
|
-
* const embeddings = await
|
6
|
+
* const embeddings = await embedMany(
|
7
7
|
* new OpenAITextEmbeddingModel(...),
|
8
8
|
* [
|
9
9
|
* "At first, Nox didn't know what to do with the pup.",
|
@@ -11,25 +11,25 @@ import { executeCall } from "../executeCall.js";
|
|
11
11
|
* ]
|
12
12
|
* );
|
13
13
|
*/
|
14
|
-
export function
|
14
|
+
export function embedMany(model, values, options) {
|
15
15
|
return executeCall({
|
16
|
-
functionType: "
|
17
|
-
input:
|
16
|
+
functionType: "embedding",
|
17
|
+
input: values,
|
18
18
|
model,
|
19
19
|
options,
|
20
20
|
generateResponse: (options) => {
|
21
|
-
// split the
|
22
|
-
const
|
23
|
-
const
|
24
|
-
if (
|
25
|
-
|
21
|
+
// split the values into groups that are small enough to be sent in one call:
|
22
|
+
const maxValuesPerCall = model.maxValuesPerCall;
|
23
|
+
const valueGroups = [];
|
24
|
+
if (maxValuesPerCall == null) {
|
25
|
+
valueGroups.push(values);
|
26
26
|
}
|
27
27
|
else {
|
28
|
-
for (let i = 0; i <
|
29
|
-
|
28
|
+
for (let i = 0; i < values.length; i += maxValuesPerCall) {
|
29
|
+
valueGroups.push(values.slice(i, i + maxValuesPerCall));
|
30
30
|
}
|
31
31
|
}
|
32
|
-
return Promise.all(
|
32
|
+
return Promise.all(valueGroups.map((valueGroup) => model.generateEmbeddingResponse(valueGroup, options)));
|
33
33
|
},
|
34
34
|
extractOutputValue: (result) => {
|
35
35
|
const embeddings = [];
|
@@ -41,22 +41,22 @@ export function embedTexts(model, texts, options) {
|
|
41
41
|
});
|
42
42
|
}
|
43
43
|
/**
|
44
|
-
* Generate an embedding for a single
|
44
|
+
* Generate an embedding for a single value.
|
45
45
|
*
|
46
46
|
* @example
|
47
|
-
* const embedding = await
|
47
|
+
* const embedding = await embed(
|
48
48
|
* new OpenAITextEmbeddingModel(...),
|
49
49
|
* "At first, Nox didn't know what to do with the pup."
|
50
50
|
* );
|
51
51
|
*/
|
52
|
-
export function
|
52
|
+
export function embed(model, value, options) {
|
53
53
|
return executeCall({
|
54
|
-
functionType: "
|
55
|
-
input:
|
54
|
+
functionType: "embedding",
|
55
|
+
input: value,
|
56
56
|
model,
|
57
57
|
options,
|
58
58
|
generateResponse: async (options) => [
|
59
|
-
await model.generateEmbeddingResponse([
|
59
|
+
await model.generateEmbeddingResponse([value], options),
|
60
60
|
],
|
61
61
|
extractOutputValue: (result) => model.extractEmbeddings(result[0])[0],
|
62
62
|
});
|
package/model-function/index.cjs
CHANGED
@@ -23,9 +23,9 @@ __exportStar(require("./SuccessfulModelCall.cjs"), exports);
|
|
23
23
|
__exportStar(require("./describe-image/ImageDescriptionEvent.cjs"), exports);
|
24
24
|
__exportStar(require("./describe-image/ImageDescriptionModel.cjs"), exports);
|
25
25
|
__exportStar(require("./describe-image/describeImage.cjs"), exports);
|
26
|
-
__exportStar(require("./embed
|
27
|
-
__exportStar(require("./embed
|
28
|
-
__exportStar(require("./embed
|
26
|
+
__exportStar(require("./embed/EmbeddingEvent.cjs"), exports);
|
27
|
+
__exportStar(require("./embed/EmbeddingModel.cjs"), exports);
|
28
|
+
__exportStar(require("./embed/embed.cjs"), exports);
|
29
29
|
__exportStar(require("./generate-image/ImageGenerationEvent.cjs"), exports);
|
30
30
|
__exportStar(require("./generate-image/ImageGenerationModel.cjs"), exports);
|
31
31
|
__exportStar(require("./generate-image/generateImage.cjs"), exports);
|
@@ -7,9 +7,9 @@ export * from "./SuccessfulModelCall.js";
|
|
7
7
|
export * from "./describe-image/ImageDescriptionEvent.js";
|
8
8
|
export * from "./describe-image/ImageDescriptionModel.js";
|
9
9
|
export * from "./describe-image/describeImage.js";
|
10
|
-
export * from "./embed
|
11
|
-
export * from "./embed
|
12
|
-
export * from "./embed
|
10
|
+
export * from "./embed/EmbeddingEvent.js";
|
11
|
+
export * from "./embed/EmbeddingModel.js";
|
12
|
+
export * from "./embed/embed.js";
|
13
13
|
export * from "./generate-image/ImageGenerationEvent.js";
|
14
14
|
export * from "./generate-image/ImageGenerationModel.js";
|
15
15
|
export * from "./generate-image/generateImage.js";
|