langchain 0.0.155 → 0.0.156
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/chat_models/bedrock.cjs +1 -0
- package/chat_models/bedrock.d.ts +1 -0
- package/chat_models/bedrock.js +1 -0
- package/dist/chat_models/bedrock.cjs +260 -0
- package/dist/chat_models/bedrock.d.ts +58 -0
- package/dist/chat_models/bedrock.js +254 -0
- package/dist/embeddings/cloudflare_workersai.cjs +69 -0
- package/dist/embeddings/cloudflare_workersai.d.ts +28 -0
- package/dist/embeddings/cloudflare_workersai.js +65 -0
- package/dist/llms/bedrock.cjs +57 -67
- package/dist/llms/bedrock.d.ts +8 -35
- package/dist/llms/bedrock.js +57 -67
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/util/bedrock.cjs +54 -0
- package/dist/util/bedrock.d.ts +59 -0
- package/dist/util/bedrock.js +50 -0
- package/dist/vectorstores/cloudflare_vectorize.cjs +200 -0
- package/dist/vectorstores/cloudflare_vectorize.d.ts +90 -0
- package/dist/vectorstores/cloudflare_vectorize.js +173 -0
- package/dist/vectorstores/supabase.d.ts +1 -1
- package/embeddings/cloudflare_workersai.cjs +1 -0
- package/embeddings/cloudflare_workersai.d.ts +1 -0
- package/embeddings/cloudflare_workersai.js +1 -0
- package/package.json +39 -14
- package/vectorstores/cloudflare_vectorize.cjs +1 -0
- package/vectorstores/cloudflare_vectorize.d.ts +1 -0
- package/vectorstores/cloudflare_vectorize.js +1 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { AwsCredentialIdentity, Provider } from "@aws-sdk/types";
|
|
2
|
+
export type CredentialType = AwsCredentialIdentity | Provider<AwsCredentialIdentity>;
|
|
3
|
+
/** Bedrock models.
|
|
4
|
+
To authenticate, the AWS client uses the following methods to automatically load credentials:
|
|
5
|
+
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
|
|
6
|
+
If a specific credential profile should be used, you must pass the name of the profile from the ~/.aws/credentials file that is to be used.
|
|
7
|
+
Make sure the credentials / roles used have the required policies to access the Bedrock service.
|
|
8
|
+
*/
|
|
9
|
+
export interface BaseBedrockInput {
|
|
10
|
+
/** Model to use.
|
|
11
|
+
For example, "amazon.titan-tg1-large", this is equivalent to the modelId property in the list-foundation-models api.
|
|
12
|
+
*/
|
|
13
|
+
model: string;
|
|
14
|
+
/** The AWS region e.g. `us-west-2`.
|
|
15
|
+
Fallback to AWS_DEFAULT_REGION env variable or region specified in ~/.aws/config in case it is not provided here.
|
|
16
|
+
*/
|
|
17
|
+
region?: string;
|
|
18
|
+
/** AWS Credentials.
|
|
19
|
+
If no credentials are provided, the default credentials from `@aws-sdk/credential-provider-node` will be used.
|
|
20
|
+
*/
|
|
21
|
+
credentials?: CredentialType;
|
|
22
|
+
/** Temperature. */
|
|
23
|
+
temperature?: number;
|
|
24
|
+
/** Max tokens. */
|
|
25
|
+
maxTokens?: number;
|
|
26
|
+
/** A custom fetch function for low-level access to AWS API. Defaults to fetch(). */
|
|
27
|
+
fetchFn?: typeof fetch;
|
|
28
|
+
/** @deprecated Use endpointHost instead Override the default endpoint url. */
|
|
29
|
+
endpointUrl?: string;
|
|
30
|
+
/** Override the default endpoint hostname. */
|
|
31
|
+
endpointHost?: string;
|
|
32
|
+
/** Optional additional stop sequences to pass to the model. Currently only supported for Anthropic and AI21. */
|
|
33
|
+
stopSequences?: string[];
|
|
34
|
+
/** Additional kwargs to pass to the model. */
|
|
35
|
+
modelKwargs?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
type Dict = {
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* A helper class used within the `Bedrock` class. It is responsible for
|
|
42
|
+
* preparing the input and output for the Bedrock service. It formats the
|
|
43
|
+
* input prompt based on the provider (e.g., "anthropic", "ai21",
|
|
44
|
+
* "amazon") and extracts the generated text from the service response.
|
|
45
|
+
*/
|
|
46
|
+
export declare class BedrockLLMInputOutputAdapter {
|
|
47
|
+
/** Adapter class to prepare the inputs from Langchain to a format
|
|
48
|
+
that LLM model expects. Also, provides a helper function to extract
|
|
49
|
+
the generated text from the model response. */
|
|
50
|
+
static prepareInput(provider: string, prompt: string, maxTokens?: number, temperature?: number, stopSequences?: string[] | undefined, modelKwargs?: Record<string, unknown>): Dict;
|
|
51
|
+
/**
|
|
52
|
+
* Extracts the generated text from the service response.
|
|
53
|
+
* @param provider The provider name.
|
|
54
|
+
* @param responseBody The response body from the service.
|
|
55
|
+
* @returns The generated text.
|
|
56
|
+
*/
|
|
57
|
+
static prepareOutput(provider: string, responseBody: any): string;
|
|
58
|
+
}
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A helper class used within the `Bedrock` class. It is responsible for
|
|
3
|
+
* preparing the input and output for the Bedrock service. It formats the
|
|
4
|
+
* input prompt based on the provider (e.g., "anthropic", "ai21",
|
|
5
|
+
* "amazon") and extracts the generated text from the service response.
|
|
6
|
+
*/
|
|
7
|
+
export class BedrockLLMInputOutputAdapter {
|
|
8
|
+
/** Adapter class to prepare the inputs from Langchain to a format
|
|
9
|
+
that LLM model expects. Also, provides a helper function to extract
|
|
10
|
+
the generated text from the model response. */
|
|
11
|
+
static prepareInput(provider, prompt, maxTokens = 50, temperature = 0, stopSequences = undefined, modelKwargs = {}) {
|
|
12
|
+
const inputBody = {};
|
|
13
|
+
if (provider === "anthropic") {
|
|
14
|
+
inputBody.prompt = prompt;
|
|
15
|
+
inputBody.max_tokens_to_sample = maxTokens;
|
|
16
|
+
inputBody.temperature = temperature;
|
|
17
|
+
inputBody.stop_sequences = stopSequences;
|
|
18
|
+
}
|
|
19
|
+
else if (provider === "ai21") {
|
|
20
|
+
inputBody.prompt = prompt;
|
|
21
|
+
inputBody.maxTokens = maxTokens;
|
|
22
|
+
inputBody.temperature = temperature;
|
|
23
|
+
inputBody.stopSequences = stopSequences;
|
|
24
|
+
}
|
|
25
|
+
else if (provider === "amazon") {
|
|
26
|
+
inputBody.inputText = prompt;
|
|
27
|
+
inputBody.textGenerationConfig = {
|
|
28
|
+
maxTokenCount: maxTokens,
|
|
29
|
+
temperature,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return { ...inputBody, ...modelKwargs };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Extracts the generated text from the service response.
|
|
36
|
+
* @param provider The provider name.
|
|
37
|
+
* @param responseBody The response body from the service.
|
|
38
|
+
* @returns The generated text.
|
|
39
|
+
*/
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
static prepareOutput(provider, responseBody) {
|
|
42
|
+
if (provider === "anthropic") {
|
|
43
|
+
return responseBody.completion;
|
|
44
|
+
}
|
|
45
|
+
else if (provider === "ai21") {
|
|
46
|
+
return responseBody?.completions?.[0]?.data?.text ?? "";
|
|
47
|
+
}
|
|
48
|
+
return responseBody.outputText;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.CloudflareVectorizeStore = void 0;
|
|
27
|
+
const uuid = __importStar(require("uuid"));
|
|
28
|
+
const base_js_1 = require("./base.cjs");
|
|
29
|
+
const document_js_1 = require("../document.cjs");
|
|
30
|
+
const chunk_js_1 = require("../util/chunk.cjs");
|
|
31
|
+
const async_caller_js_1 = require("../util/async_caller.cjs");
|
|
32
|
+
/**
|
|
33
|
+
* Class that extends the VectorStore class and provides methods to
|
|
34
|
+
* interact with the Cloudflare Vectorize vector database.
|
|
35
|
+
*/
|
|
36
|
+
class CloudflareVectorizeStore extends base_js_1.VectorStore {
|
|
37
|
+
_vectorstoreType() {
|
|
38
|
+
return "cloudflare_vectorize";
|
|
39
|
+
}
|
|
40
|
+
constructor(embeddings, args) {
|
|
41
|
+
super(embeddings, args);
|
|
42
|
+
Object.defineProperty(this, "textKey", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(this, "namespace", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
writable: true,
|
|
52
|
+
value: void 0
|
|
53
|
+
});
|
|
54
|
+
Object.defineProperty(this, "index", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: void 0
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(this, "caller", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
value: void 0
|
|
65
|
+
});
|
|
66
|
+
this.embeddings = embeddings;
|
|
67
|
+
const { index, textKey, ...asyncCallerArgs } = args;
|
|
68
|
+
if (!index) {
|
|
69
|
+
throw new Error("Must supply a Vectorize index binding, eg { index: env.VECTORIZE }");
|
|
70
|
+
}
|
|
71
|
+
this.index = index;
|
|
72
|
+
this.textKey = textKey ?? "text";
|
|
73
|
+
this.caller = new async_caller_js_1.AsyncCaller({
|
|
74
|
+
maxConcurrency: 6,
|
|
75
|
+
maxRetries: 0,
|
|
76
|
+
...asyncCallerArgs,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Method that adds documents to the Vectorize database.
|
|
81
|
+
* @param documents Array of documents to add.
|
|
82
|
+
* @param options Optional ids for the documents.
|
|
83
|
+
* @returns Promise that resolves with the ids of the added documents.
|
|
84
|
+
*/
|
|
85
|
+
async addDocuments(documents, options) {
|
|
86
|
+
const texts = documents.map(({ pageContent }) => pageContent);
|
|
87
|
+
return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Method that adds vectors to the Vectorize database.
|
|
91
|
+
* @param vectors Array of vectors to add.
|
|
92
|
+
* @param documents Array of documents associated with the vectors.
|
|
93
|
+
* @param options Optional ids for the vectors.
|
|
94
|
+
* @returns Promise that resolves with the ids of the added vectors.
|
|
95
|
+
*/
|
|
96
|
+
async addVectors(vectors, documents, options) {
|
|
97
|
+
const ids = Array.isArray(options) ? options : options?.ids;
|
|
98
|
+
const documentIds = ids == null ? documents.map(() => uuid.v4()) : ids;
|
|
99
|
+
const vectorizeVectors = vectors.map((values, idx) => {
|
|
100
|
+
const metadata = {
|
|
101
|
+
...documents[idx].metadata,
|
|
102
|
+
[this.textKey]: documents[idx].pageContent,
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
id: documentIds[idx],
|
|
106
|
+
metadata,
|
|
107
|
+
values,
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
// Stick to a limit of 500 vectors per upsert request
|
|
111
|
+
const chunkSize = 500;
|
|
112
|
+
const chunkedVectors = (0, chunk_js_1.chunkArray)(vectorizeVectors, chunkSize);
|
|
113
|
+
const batchRequests = chunkedVectors.map((chunk) => this.caller.call(async () => this.index.upsert(chunk)));
|
|
114
|
+
await Promise.all(batchRequests);
|
|
115
|
+
return documentIds;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Method that deletes vectors from the Vectorize database.
|
|
119
|
+
* @param params Parameters for the delete operation.
|
|
120
|
+
* @returns Promise that resolves when the delete operation is complete.
|
|
121
|
+
*/
|
|
122
|
+
async delete(params) {
|
|
123
|
+
const batchSize = 1000;
|
|
124
|
+
const batchedIds = (0, chunk_js_1.chunkArray)(params.ids, batchSize);
|
|
125
|
+
const batchRequests = batchedIds.map((batchIds) => this.caller.call(async () => this.index.deleteByIds(batchIds)));
|
|
126
|
+
await Promise.all(batchRequests);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Method that performs a similarity search in the Vectorize database and
|
|
130
|
+
* returns the results along with their scores.
|
|
131
|
+
* @param query Query vector for the similarity search.
|
|
132
|
+
* @param k Number of top results to return.
|
|
133
|
+
* @returns Promise that resolves with an array of documents and their scores.
|
|
134
|
+
*/
|
|
135
|
+
async similaritySearchVectorWithScore(query, k) {
|
|
136
|
+
const results = await this.index.query(query, {
|
|
137
|
+
returnVectors: true,
|
|
138
|
+
topK: k,
|
|
139
|
+
});
|
|
140
|
+
const result = [];
|
|
141
|
+
if (results.matches) {
|
|
142
|
+
for (const res of results.matches) {
|
|
143
|
+
const { [this.textKey]: pageContent, ...metadata } = res.vector?.metadata ?? {};
|
|
144
|
+
result.push([
|
|
145
|
+
new document_js_1.Document({ metadata, pageContent: pageContent }),
|
|
146
|
+
res.score,
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
154
|
+
* from texts.
|
|
155
|
+
* @param texts Array of texts to add to the Vectorize database.
|
|
156
|
+
* @param metadatas Metadata associated with the texts.
|
|
157
|
+
* @param embeddings Embeddings to use for the texts.
|
|
158
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
159
|
+
* @param options Optional ids for the vectors.
|
|
160
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
161
|
+
*/
|
|
162
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
163
|
+
const docs = [];
|
|
164
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
165
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
166
|
+
const newDoc = new document_js_1.Document({
|
|
167
|
+
pageContent: texts[i],
|
|
168
|
+
metadata,
|
|
169
|
+
});
|
|
170
|
+
docs.push(newDoc);
|
|
171
|
+
}
|
|
172
|
+
return CloudflareVectorizeStore.fromDocuments(docs, embeddings, dbConfig);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
176
|
+
* from documents.
|
|
177
|
+
* @param docs Array of documents to add to the Vectorize database.
|
|
178
|
+
* @param embeddings Embeddings to use for the documents.
|
|
179
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
180
|
+
* @param options Optional ids for the vectors.
|
|
181
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
182
|
+
*/
|
|
183
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
184
|
+
const instance = new this(embeddings, dbConfig);
|
|
185
|
+
await instance.addDocuments(docs);
|
|
186
|
+
return instance;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
190
|
+
* from an existing index.
|
|
191
|
+
* @param embeddings Embeddings to use for the documents.
|
|
192
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
193
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
194
|
+
*/
|
|
195
|
+
static async fromExistingIndex(embeddings, dbConfig) {
|
|
196
|
+
const instance = new this(embeddings, dbConfig);
|
|
197
|
+
return instance;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.CloudflareVectorizeStore = CloudflareVectorizeStore;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { VectorizeIndex, VectorizeVectorMetadata } from "@cloudflare/workers-types";
|
|
2
|
+
import { VectorStore } from "./base.js";
|
|
3
|
+
import { Embeddings } from "../embeddings/base.js";
|
|
4
|
+
import { Document } from "../document.js";
|
|
5
|
+
import { AsyncCaller, type AsyncCallerParams } from "../util/async_caller.js";
|
|
6
|
+
export interface VectorizeLibArgs extends AsyncCallerParams {
|
|
7
|
+
index: VectorizeIndex;
|
|
8
|
+
textKey?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Type that defines the parameters for the delete operation in the
|
|
12
|
+
* CloudflareVectorizeStore class. It includes ids, deleteAll flag, and namespace.
|
|
13
|
+
*/
|
|
14
|
+
export type VectorizeDeleteParams = {
|
|
15
|
+
ids: string[];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Class that extends the VectorStore class and provides methods to
|
|
19
|
+
* interact with the Cloudflare Vectorize vector database.
|
|
20
|
+
*/
|
|
21
|
+
export declare class CloudflareVectorizeStore extends VectorStore {
|
|
22
|
+
textKey: string;
|
|
23
|
+
namespace?: string;
|
|
24
|
+
index: VectorizeIndex;
|
|
25
|
+
caller: AsyncCaller;
|
|
26
|
+
_vectorstoreType(): string;
|
|
27
|
+
constructor(embeddings: Embeddings, args: VectorizeLibArgs);
|
|
28
|
+
/**
|
|
29
|
+
* Method that adds documents to the Vectorize database.
|
|
30
|
+
* @param documents Array of documents to add.
|
|
31
|
+
* @param options Optional ids for the documents.
|
|
32
|
+
* @returns Promise that resolves with the ids of the added documents.
|
|
33
|
+
*/
|
|
34
|
+
addDocuments(documents: Document[], options?: {
|
|
35
|
+
ids?: string[];
|
|
36
|
+
} | string[]): Promise<string[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Method that adds vectors to the Vectorize database.
|
|
39
|
+
* @param vectors Array of vectors to add.
|
|
40
|
+
* @param documents Array of documents associated with the vectors.
|
|
41
|
+
* @param options Optional ids for the vectors.
|
|
42
|
+
* @returns Promise that resolves with the ids of the added vectors.
|
|
43
|
+
*/
|
|
44
|
+
addVectors(vectors: number[][], documents: Document[], options?: {
|
|
45
|
+
ids?: string[];
|
|
46
|
+
} | string[]): Promise<string[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Method that deletes vectors from the Vectorize database.
|
|
49
|
+
* @param params Parameters for the delete operation.
|
|
50
|
+
* @returns Promise that resolves when the delete operation is complete.
|
|
51
|
+
*/
|
|
52
|
+
delete(params: VectorizeDeleteParams): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Method that performs a similarity search in the Vectorize database and
|
|
55
|
+
* returns the results along with their scores.
|
|
56
|
+
* @param query Query vector for the similarity search.
|
|
57
|
+
* @param k Number of top results to return.
|
|
58
|
+
* @returns Promise that resolves with an array of documents and their scores.
|
|
59
|
+
*/
|
|
60
|
+
similaritySearchVectorWithScore(query: number[], k: number): Promise<[Document, number][]>;
|
|
61
|
+
/**
|
|
62
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
63
|
+
* from texts.
|
|
64
|
+
* @param texts Array of texts to add to the Vectorize database.
|
|
65
|
+
* @param metadatas Metadata associated with the texts.
|
|
66
|
+
* @param embeddings Embeddings to use for the texts.
|
|
67
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
68
|
+
* @param options Optional ids for the vectors.
|
|
69
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
70
|
+
*/
|
|
71
|
+
static fromTexts(texts: string[], metadatas: Record<string, VectorizeVectorMetadata>[] | Record<string, VectorizeVectorMetadata>, embeddings: Embeddings, dbConfig: VectorizeLibArgs): Promise<CloudflareVectorizeStore>;
|
|
72
|
+
/**
|
|
73
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
74
|
+
* from documents.
|
|
75
|
+
* @param docs Array of documents to add to the Vectorize database.
|
|
76
|
+
* @param embeddings Embeddings to use for the documents.
|
|
77
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
78
|
+
* @param options Optional ids for the vectors.
|
|
79
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
80
|
+
*/
|
|
81
|
+
static fromDocuments(docs: Document[], embeddings: Embeddings, dbConfig: VectorizeLibArgs): Promise<CloudflareVectorizeStore>;
|
|
82
|
+
/**
|
|
83
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
84
|
+
* from an existing index.
|
|
85
|
+
* @param embeddings Embeddings to use for the documents.
|
|
86
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
87
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
88
|
+
*/
|
|
89
|
+
static fromExistingIndex(embeddings: Embeddings, dbConfig: VectorizeLibArgs): Promise<CloudflareVectorizeStore>;
|
|
90
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import * as uuid from "uuid";
|
|
2
|
+
import { VectorStore } from "./base.js";
|
|
3
|
+
import { Document } from "../document.js";
|
|
4
|
+
import { chunkArray } from "../util/chunk.js";
|
|
5
|
+
import { AsyncCaller } from "../util/async_caller.js";
|
|
6
|
+
/**
|
|
7
|
+
* Class that extends the VectorStore class and provides methods to
|
|
8
|
+
* interact with the Cloudflare Vectorize vector database.
|
|
9
|
+
*/
|
|
10
|
+
export class CloudflareVectorizeStore extends VectorStore {
|
|
11
|
+
_vectorstoreType() {
|
|
12
|
+
return "cloudflare_vectorize";
|
|
13
|
+
}
|
|
14
|
+
constructor(embeddings, args) {
|
|
15
|
+
super(embeddings, args);
|
|
16
|
+
Object.defineProperty(this, "textKey", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "namespace", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: void 0
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "index", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "caller", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: void 0
|
|
39
|
+
});
|
|
40
|
+
this.embeddings = embeddings;
|
|
41
|
+
const { index, textKey, ...asyncCallerArgs } = args;
|
|
42
|
+
if (!index) {
|
|
43
|
+
throw new Error("Must supply a Vectorize index binding, eg { index: env.VECTORIZE }");
|
|
44
|
+
}
|
|
45
|
+
this.index = index;
|
|
46
|
+
this.textKey = textKey ?? "text";
|
|
47
|
+
this.caller = new AsyncCaller({
|
|
48
|
+
maxConcurrency: 6,
|
|
49
|
+
maxRetries: 0,
|
|
50
|
+
...asyncCallerArgs,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Method that adds documents to the Vectorize database.
|
|
55
|
+
* @param documents Array of documents to add.
|
|
56
|
+
* @param options Optional ids for the documents.
|
|
57
|
+
* @returns Promise that resolves with the ids of the added documents.
|
|
58
|
+
*/
|
|
59
|
+
async addDocuments(documents, options) {
|
|
60
|
+
const texts = documents.map(({ pageContent }) => pageContent);
|
|
61
|
+
return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Method that adds vectors to the Vectorize database.
|
|
65
|
+
* @param vectors Array of vectors to add.
|
|
66
|
+
* @param documents Array of documents associated with the vectors.
|
|
67
|
+
* @param options Optional ids for the vectors.
|
|
68
|
+
* @returns Promise that resolves with the ids of the added vectors.
|
|
69
|
+
*/
|
|
70
|
+
async addVectors(vectors, documents, options) {
|
|
71
|
+
const ids = Array.isArray(options) ? options : options?.ids;
|
|
72
|
+
const documentIds = ids == null ? documents.map(() => uuid.v4()) : ids;
|
|
73
|
+
const vectorizeVectors = vectors.map((values, idx) => {
|
|
74
|
+
const metadata = {
|
|
75
|
+
...documents[idx].metadata,
|
|
76
|
+
[this.textKey]: documents[idx].pageContent,
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
id: documentIds[idx],
|
|
80
|
+
metadata,
|
|
81
|
+
values,
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
// Stick to a limit of 500 vectors per upsert request
|
|
85
|
+
const chunkSize = 500;
|
|
86
|
+
const chunkedVectors = chunkArray(vectorizeVectors, chunkSize);
|
|
87
|
+
const batchRequests = chunkedVectors.map((chunk) => this.caller.call(async () => this.index.upsert(chunk)));
|
|
88
|
+
await Promise.all(batchRequests);
|
|
89
|
+
return documentIds;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Method that deletes vectors from the Vectorize database.
|
|
93
|
+
* @param params Parameters for the delete operation.
|
|
94
|
+
* @returns Promise that resolves when the delete operation is complete.
|
|
95
|
+
*/
|
|
96
|
+
async delete(params) {
|
|
97
|
+
const batchSize = 1000;
|
|
98
|
+
const batchedIds = chunkArray(params.ids, batchSize);
|
|
99
|
+
const batchRequests = batchedIds.map((batchIds) => this.caller.call(async () => this.index.deleteByIds(batchIds)));
|
|
100
|
+
await Promise.all(batchRequests);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Method that performs a similarity search in the Vectorize database and
|
|
104
|
+
* returns the results along with their scores.
|
|
105
|
+
* @param query Query vector for the similarity search.
|
|
106
|
+
* @param k Number of top results to return.
|
|
107
|
+
* @returns Promise that resolves with an array of documents and their scores.
|
|
108
|
+
*/
|
|
109
|
+
async similaritySearchVectorWithScore(query, k) {
|
|
110
|
+
const results = await this.index.query(query, {
|
|
111
|
+
returnVectors: true,
|
|
112
|
+
topK: k,
|
|
113
|
+
});
|
|
114
|
+
const result = [];
|
|
115
|
+
if (results.matches) {
|
|
116
|
+
for (const res of results.matches) {
|
|
117
|
+
const { [this.textKey]: pageContent, ...metadata } = res.vector?.metadata ?? {};
|
|
118
|
+
result.push([
|
|
119
|
+
new Document({ metadata, pageContent: pageContent }),
|
|
120
|
+
res.score,
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
128
|
+
* from texts.
|
|
129
|
+
* @param texts Array of texts to add to the Vectorize database.
|
|
130
|
+
* @param metadatas Metadata associated with the texts.
|
|
131
|
+
* @param embeddings Embeddings to use for the texts.
|
|
132
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
133
|
+
* @param options Optional ids for the vectors.
|
|
134
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
135
|
+
*/
|
|
136
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
137
|
+
const docs = [];
|
|
138
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
139
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
140
|
+
const newDoc = new Document({
|
|
141
|
+
pageContent: texts[i],
|
|
142
|
+
metadata,
|
|
143
|
+
});
|
|
144
|
+
docs.push(newDoc);
|
|
145
|
+
}
|
|
146
|
+
return CloudflareVectorizeStore.fromDocuments(docs, embeddings, dbConfig);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
150
|
+
* from documents.
|
|
151
|
+
* @param docs Array of documents to add to the Vectorize database.
|
|
152
|
+
* @param embeddings Embeddings to use for the documents.
|
|
153
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
154
|
+
* @param options Optional ids for the vectors.
|
|
155
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
156
|
+
*/
|
|
157
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
158
|
+
const instance = new this(embeddings, dbConfig);
|
|
159
|
+
await instance.addDocuments(docs);
|
|
160
|
+
return instance;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Static method that creates a new instance of the CloudflareVectorizeStore class
|
|
164
|
+
* from an existing index.
|
|
165
|
+
* @param embeddings Embeddings to use for the documents.
|
|
166
|
+
* @param dbConfig Configuration for the Vectorize database.
|
|
167
|
+
* @returns Promise that resolves with a new instance of the CloudflareVectorizeStore class.
|
|
168
|
+
*/
|
|
169
|
+
static async fromExistingIndex(embeddings, dbConfig) {
|
|
170
|
+
const instance = new this(embeddings, dbConfig);
|
|
171
|
+
return instance;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -54,7 +54,7 @@ export declare class SupabaseVectorStore extends VectorStore {
|
|
|
54
54
|
* @returns A promise that resolves when the vectors have been deleted.
|
|
55
55
|
*/
|
|
56
56
|
delete(params: {
|
|
57
|
-
ids: string[];
|
|
57
|
+
ids: string[] | number[];
|
|
58
58
|
}): Promise<void>;
|
|
59
59
|
/**
|
|
60
60
|
* Performs a similarity search on the vector store.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/embeddings/cloudflare_workersai.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/embeddings/cloudflare_workersai.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/embeddings/cloudflare_workersai.js'
|