ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
67 lines (66 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PineconeVectorIndex = void 0;
class PineconeVectorIndex {
constructor({ index, namespace, schema, }) {
Object.defineProperty(this, "index", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "namespace", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "schema", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.index = index;
this.namespace = namespace;
this.schema = schema;
}
async upsertMany(data) {
this.index.upsert({
upsertRequest: {
namespace: this.namespace,
vectors: data.map((entry) => ({
id: entry.id,
values: entry.vector,
metadata: entry.data,
})),
},
});
}
async queryByVector({ queryVector, similarityThreshold, maxResults, }) {
const { matches } = await this.index.query({
queryRequest: {
namespace: this.namespace,
vector: queryVector,
topK: maxResults,
includeMetadata: true,
},
});
if (matches == undefined) {
return [];
}
return matches
.filter((match) => similarityThreshold == undefined ||
match.score == undefined ||
match.score > similarityThreshold)
.map((match) => ({
id: match.id,
data: this.schema.parse(match.metadata),
similarity: match.score,
}));
}
asIndex() {
return this;
}
}
exports.PineconeVectorIndex = PineconeVectorIndex;