modelfusion 0.38.0 → 0.40.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 +3 -4
- package/core/structure/JsonSchemaProducer.cjs +2 -0
- package/core/structure/JsonSchemaProducer.d.ts +9 -0
- package/core/structure/JsonSchemaProducer.js +1 -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/package.json +1 -10
- package/tool/Tool.d.ts +3 -2
- package/vector-index/index.cjs +0 -1
- package/vector-index/index.d.ts +0 -1
- package/vector-index/index.js +0 -1
- 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/pinecone/PineconeVectorIndex.cjs +0 -66
- package/vector-index/pinecone/PineconeVectorIndex.d.ts +0 -29
- package/vector-index/pinecone/PineconeVectorIndex.js +0 -62
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
|
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -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) {
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.40.0",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -61,7 +61,6 @@
|
|
61
61
|
"zod-to-json-schema": "3.21.4"
|
62
62
|
},
|
63
63
|
"devDependencies": {
|
64
|
-
"@pinecone-database/pinecone": "^0.1.6",
|
65
64
|
"@tsconfig/recommended": "1.0.3",
|
66
65
|
"@types/deep-equal": "^1.0.2",
|
67
66
|
"@types/node": "18.11.9",
|
@@ -76,13 +75,5 @@
|
|
76
75
|
"rimraf": "5.0.5",
|
77
76
|
"typescript": "5.2.2",
|
78
77
|
"vitest": "^0.34.5"
|
79
|
-
},
|
80
|
-
"peerDependencies": {
|
81
|
-
"@pinecone-database/pinecone": "0.1.6"
|
82
|
-
},
|
83
|
-
"peerDependenciesMeta": {
|
84
|
-
"@pinecone-database/pinecone": {
|
85
|
-
"optional": true
|
86
|
-
}
|
87
78
|
}
|
88
79
|
}
|
package/tool/Tool.d.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
+
import { JsonSchemaProducer } from "../core/structure/JsonSchemaProducer.js";
|
2
3
|
import { Schema } from "../core/structure/Schema.js";
|
3
4
|
import { StructureDefinition } from "../core/structure/StructureDefinition.js";
|
4
5
|
/**
|
@@ -20,7 +21,7 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
|
20
21
|
* The schema of the input that the tool expects. The language model will use this to generate the input.
|
21
22
|
* Use descriptions to make the input understandable for the language model.
|
22
23
|
*/
|
23
|
-
readonly inputSchema: Schema<INPUT
|
24
|
+
readonly inputSchema: Schema<INPUT> & JsonSchemaProducer;
|
24
25
|
/**
|
25
26
|
* An optional schema of the output that the tool produces. This will be used to validate the output.
|
26
27
|
*/
|
@@ -32,7 +33,7 @@ export declare class Tool<NAME extends string, INPUT, OUTPUT> {
|
|
32
33
|
constructor({ name, description, inputSchema, outputSchema, execute, }: {
|
33
34
|
name: NAME;
|
34
35
|
description: string;
|
35
|
-
inputSchema: Schema<INPUT
|
36
|
+
inputSchema: Schema<INPUT> & JsonSchemaProducer;
|
36
37
|
outputSchema?: Schema<OUTPUT>;
|
37
38
|
execute(input: INPUT, options?: FunctionOptions): PromiseLike<OUTPUT>;
|
38
39
|
});
|
package/vector-index/index.cjs
CHANGED
@@ -17,5 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./VectorIndex.cjs"), exports);
|
18
18
|
__exportStar(require("./VectorIndexRetriever.cjs"), exports);
|
19
19
|
__exportStar(require("./memory/MemoryVectorIndex.cjs"), exports);
|
20
|
-
__exportStar(require("./pinecone/PineconeVectorIndex.cjs"), exports);
|
21
20
|
__exportStar(require("./upsertIntoVectorIndex.cjs"), exports);
|
package/vector-index/index.d.ts
CHANGED
package/vector-index/index.js
CHANGED
@@ -4,8 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.MemoryVectorIndex = void 0;
|
7
|
+
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
7
8
|
const zod_1 = __importDefault(require("zod"));
|
8
9
|
const cosineSimilarity_js_1 = require("../../util/cosineSimilarity.cjs");
|
10
|
+
const jsonDataSchema = zod_1.default.array(zod_1.default.object({
|
11
|
+
id: zod_1.default.string(),
|
12
|
+
vector: zod_1.default.array(zod_1.default.number()),
|
13
|
+
data: zod_1.default.unknown(),
|
14
|
+
}));
|
9
15
|
/**
|
10
16
|
* A very simple vector index that stores all entries in memory. Useful when you only have
|
11
17
|
* a small number of entries and don't want to set up a real database, e.g. for conversational memory
|
@@ -21,15 +27,16 @@ class MemoryVectorIndex {
|
|
21
27
|
});
|
22
28
|
}
|
23
29
|
static async deserialize({ serializedData, schema, }) {
|
24
|
-
|
30
|
+
// validate the outer structure:
|
31
|
+
const json = jsonDataSchema.parse(secure_json_parse_1.default.parse(serializedData));
|
25
32
|
if (schema != null) {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
+
// when a schema is provided, validate all entries:
|
34
|
+
for (const entry of json) {
|
35
|
+
const validationResult = schema.validate(entry.data);
|
36
|
+
if (!validationResult.success) {
|
37
|
+
throw validationResult.error;
|
38
|
+
}
|
39
|
+
}
|
33
40
|
}
|
34
41
|
const vectorIndex = new MemoryVectorIndex();
|
35
42
|
vectorIndex.upsertMany(json);
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import z from "zod";
|
2
1
|
import { Vector } from "../../core/Vector.js";
|
2
|
+
import { Schema } from "../../core/structure/Schema.js";
|
3
3
|
import { VectorIndex } from "../VectorIndex.js";
|
4
4
|
/**
|
5
5
|
* A very simple vector index that stores all entries in memory. Useful when you only have
|
@@ -9,7 +9,7 @@ import { VectorIndex } from "../VectorIndex.js";
|
|
9
9
|
export declare class MemoryVectorIndex<DATA> implements VectorIndex<DATA, MemoryVectorIndex<DATA>, (value: DATA) => boolean> {
|
10
10
|
static deserialize<DATA>({ serializedData, schema, }: {
|
11
11
|
serializedData: string;
|
12
|
-
schema?:
|
12
|
+
schema?: Schema<DATA>;
|
13
13
|
}): Promise<MemoryVectorIndex<DATA>>;
|
14
14
|
private readonly entries;
|
15
15
|
upsertMany(data: Array<{
|
@@ -1,5 +1,11 @@
|
|
1
|
+
import SecureJSON from "secure-json-parse";
|
1
2
|
import z from "zod";
|
2
3
|
import { cosineSimilarity } from "../../util/cosineSimilarity.js";
|
4
|
+
const jsonDataSchema = z.array(z.object({
|
5
|
+
id: z.string(),
|
6
|
+
vector: z.array(z.number()),
|
7
|
+
data: z.unknown(),
|
8
|
+
}));
|
3
9
|
/**
|
4
10
|
* A very simple vector index that stores all entries in memory. Useful when you only have
|
5
11
|
* a small number of entries and don't want to set up a real database, e.g. for conversational memory
|
@@ -15,15 +21,16 @@ export class MemoryVectorIndex {
|
|
15
21
|
});
|
16
22
|
}
|
17
23
|
static async deserialize({ serializedData, schema, }) {
|
18
|
-
|
24
|
+
// validate the outer structure:
|
25
|
+
const json = jsonDataSchema.parse(SecureJSON.parse(serializedData));
|
19
26
|
if (schema != null) {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
// when a schema is provided, validate all entries:
|
28
|
+
for (const entry of json) {
|
29
|
+
const validationResult = schema.validate(entry.data);
|
30
|
+
if (!validationResult.success) {
|
31
|
+
throw validationResult.error;
|
32
|
+
}
|
33
|
+
}
|
27
34
|
}
|
28
35
|
const vectorIndex = new MemoryVectorIndex();
|
29
36
|
vectorIndex.upsertMany(json);
|
@@ -1,66 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.PineconeVectorIndex = void 0;
|
4
|
-
class PineconeVectorIndex {
|
5
|
-
constructor({ index, namespace, schema, }) {
|
6
|
-
Object.defineProperty(this, "index", {
|
7
|
-
enumerable: true,
|
8
|
-
configurable: true,
|
9
|
-
writable: true,
|
10
|
-
value: void 0
|
11
|
-
});
|
12
|
-
Object.defineProperty(this, "namespace", {
|
13
|
-
enumerable: true,
|
14
|
-
configurable: true,
|
15
|
-
writable: true,
|
16
|
-
value: void 0
|
17
|
-
});
|
18
|
-
Object.defineProperty(this, "schema", {
|
19
|
-
enumerable: true,
|
20
|
-
configurable: true,
|
21
|
-
writable: true,
|
22
|
-
value: void 0
|
23
|
-
});
|
24
|
-
this.index = index;
|
25
|
-
this.namespace = namespace;
|
26
|
-
this.schema = schema;
|
27
|
-
}
|
28
|
-
async upsertMany(data) {
|
29
|
-
this.index.upsert({
|
30
|
-
upsertRequest: {
|
31
|
-
namespace: this.namespace,
|
32
|
-
vectors: data.map((entry) => ({
|
33
|
-
id: entry.id,
|
34
|
-
values: entry.vector,
|
35
|
-
metadata: entry.data,
|
36
|
-
})),
|
37
|
-
},
|
38
|
-
});
|
39
|
-
}
|
40
|
-
async queryByVector({ queryVector, similarityThreshold, maxResults, }) {
|
41
|
-
const { matches } = await this.index.query({
|
42
|
-
queryRequest: {
|
43
|
-
namespace: this.namespace,
|
44
|
-
vector: queryVector,
|
45
|
-
topK: maxResults,
|
46
|
-
includeMetadata: true,
|
47
|
-
},
|
48
|
-
});
|
49
|
-
if (matches == undefined) {
|
50
|
-
return [];
|
51
|
-
}
|
52
|
-
return matches
|
53
|
-
.filter((match) => similarityThreshold == undefined ||
|
54
|
-
match.score == undefined ||
|
55
|
-
match.score > similarityThreshold)
|
56
|
-
.map((match) => ({
|
57
|
-
id: match.id,
|
58
|
-
data: this.schema.parse(match.metadata),
|
59
|
-
similarity: match.score,
|
60
|
-
}));
|
61
|
-
}
|
62
|
-
asIndex() {
|
63
|
-
return this;
|
64
|
-
}
|
65
|
-
}
|
66
|
-
exports.PineconeVectorIndex = PineconeVectorIndex;
|
@@ -1,29 +0,0 @@
|
|
1
|
-
import { VectorOperationsApi } from "@pinecone-database/pinecone/dist/pinecone-generated-ts-fetch/index.js";
|
2
|
-
import { z } from "zod";
|
3
|
-
import { Vector } from "../../core/Vector.js";
|
4
|
-
import { VectorIndex } from "../VectorIndex.js";
|
5
|
-
export declare class PineconeVectorIndex<DATA extends object | undefined> implements VectorIndex<DATA, PineconeVectorIndex<DATA>, null> {
|
6
|
-
readonly index: VectorOperationsApi;
|
7
|
-
readonly namespace?: string;
|
8
|
-
readonly schema: z.ZodSchema<DATA>;
|
9
|
-
constructor({ index, namespace, schema, }: {
|
10
|
-
index: VectorOperationsApi;
|
11
|
-
namespace?: string;
|
12
|
-
schema: z.ZodSchema<DATA>;
|
13
|
-
});
|
14
|
-
upsertMany(data: Array<{
|
15
|
-
id: string;
|
16
|
-
vector: Vector;
|
17
|
-
data: DATA;
|
18
|
-
}>): Promise<void>;
|
19
|
-
queryByVector({ queryVector, similarityThreshold, maxResults, }: {
|
20
|
-
queryVector: Vector;
|
21
|
-
maxResults: number;
|
22
|
-
similarityThreshold?: number;
|
23
|
-
}): Promise<Array<{
|
24
|
-
id: string;
|
25
|
-
data: DATA;
|
26
|
-
similarity?: number;
|
27
|
-
}>>;
|
28
|
-
asIndex(): PineconeVectorIndex<DATA>;
|
29
|
-
}
|
@@ -1,62 +0,0 @@
|
|
1
|
-
export class PineconeVectorIndex {
|
2
|
-
constructor({ index, namespace, schema, }) {
|
3
|
-
Object.defineProperty(this, "index", {
|
4
|
-
enumerable: true,
|
5
|
-
configurable: true,
|
6
|
-
writable: true,
|
7
|
-
value: void 0
|
8
|
-
});
|
9
|
-
Object.defineProperty(this, "namespace", {
|
10
|
-
enumerable: true,
|
11
|
-
configurable: true,
|
12
|
-
writable: true,
|
13
|
-
value: void 0
|
14
|
-
});
|
15
|
-
Object.defineProperty(this, "schema", {
|
16
|
-
enumerable: true,
|
17
|
-
configurable: true,
|
18
|
-
writable: true,
|
19
|
-
value: void 0
|
20
|
-
});
|
21
|
-
this.index = index;
|
22
|
-
this.namespace = namespace;
|
23
|
-
this.schema = schema;
|
24
|
-
}
|
25
|
-
async upsertMany(data) {
|
26
|
-
this.index.upsert({
|
27
|
-
upsertRequest: {
|
28
|
-
namespace: this.namespace,
|
29
|
-
vectors: data.map((entry) => ({
|
30
|
-
id: entry.id,
|
31
|
-
values: entry.vector,
|
32
|
-
metadata: entry.data,
|
33
|
-
})),
|
34
|
-
},
|
35
|
-
});
|
36
|
-
}
|
37
|
-
async queryByVector({ queryVector, similarityThreshold, maxResults, }) {
|
38
|
-
const { matches } = await this.index.query({
|
39
|
-
queryRequest: {
|
40
|
-
namespace: this.namespace,
|
41
|
-
vector: queryVector,
|
42
|
-
topK: maxResults,
|
43
|
-
includeMetadata: true,
|
44
|
-
},
|
45
|
-
});
|
46
|
-
if (matches == undefined) {
|
47
|
-
return [];
|
48
|
-
}
|
49
|
-
return matches
|
50
|
-
.filter((match) => similarityThreshold == undefined ||
|
51
|
-
match.score == undefined ||
|
52
|
-
match.score > similarityThreshold)
|
53
|
-
.map((match) => ({
|
54
|
-
id: match.id,
|
55
|
-
data: this.schema.parse(match.metadata),
|
56
|
-
similarity: match.score,
|
57
|
-
}));
|
58
|
-
}
|
59
|
-
asIndex() {
|
60
|
-
return this;
|
61
|
-
}
|
62
|
-
}
|