@gravityai-dev/pinecone
Version:
Pinecone vector database nodes for GravityWorkflow - knowledge management and vector operations
154 lines • 6.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveVectorsWithEmbeddingsWithContext = saveVectorsWithEmbeddingsWithContext;
exports.saveVectorsWithEmbeddings = saveVectorsWithEmbeddings;
exports.loadVectorsForEditingWithContext = loadVectorsForEditingWithContext;
exports.loadVectorsForEditing = loadVectorsForEditing;
/**
* Pinecone vector save and load operations
*/
const logger_1 = require("../../../logger");
const client_1 = require("../client");
const delete_1 = require("./delete");
const upsert_1 = require("./upsert");
const fetch_1 = require("./fetch");
/**
* Save vectors with embeddings to Pinecone with context
*/
async function saveVectorsWithEmbeddingsWithContext(options, context) {
const { indexName, namespace = "default", vectors, workflowId, executionId, deleteExisting = true } = options;
try {
logger_1.logger.info("Saving vectors to Pinecone", "saveVectorsWithEmbeddings", {
indexName,
namespace,
vectorCount: vectors.length,
deleteExisting,
});
// Initialize Pinecone client
const pinecone = await (0, client_1.initializePineconeWithContext)(context);
const index = pinecone.index(indexName);
const indexOperation = index.namespace(namespace);
// Delete existing vectors if requested
if (deleteExisting) {
await (0, delete_1.deleteAllVectorsFromNamespaceWithContext)(indexName, namespace, context);
}
// Filter out vectors without embeddings
const validVectors = vectors.filter((v) => v.values && v.values.length > 0);
if (validVectors.length === 0) {
logger_1.logger.warn("No vectors with embeddings to save", "saveVectorsWithEmbeddings");
return { success: true, vectorCount: 0 };
}
// Upsert vectors in batches
await (0, upsert_1.upsertVectorsInBatches)(indexOperation, validVectors, 100, workflowId, executionId);
logger_1.logger.info("Successfully saved vectors to Pinecone", "saveVectorsWithEmbeddings", {
savedCount: validVectors.length,
skippedCount: vectors.length - validVectors.length,
});
return { success: true, vectorCount: validVectors.length };
}
catch (error) {
logger_1.logger.error("Failed to save vectors to Pinecone", "saveVectorsWithEmbeddings", {
error: error.message,
stack: error.stack,
});
throw error;
}
}
/**
* Save vectors with embeddings to Pinecone (legacy without context)
*/
async function saveVectorsWithEmbeddings(apiKey, indexName, namespace, vectors, workflowId, executionId, deleteExisting = true) {
try {
logger_1.logger.info("Saving vectors to Pinecone", "saveVectorsWithEmbeddings", {
indexName,
namespace,
vectorCount: vectors.length,
deleteExisting,
});
// Initialize Pinecone client
const pinecone = (0, client_1.initializePinecone)(apiKey);
const index = pinecone.index(indexName);
const indexOperation = index.namespace(namespace);
// Delete existing vectors if requested
if (deleteExisting) {
await (0, delete_1.deleteAllVectorsFromNamespace)(apiKey, indexName, namespace);
}
// Filter out vectors without embeddings
const validVectors = vectors.filter((v) => v.values && v.values.length > 0);
if (validVectors.length === 0) {
logger_1.logger.warn("No vectors with embeddings to save", "saveVectorsWithEmbeddings");
return { success: true, vectorCount: 0 };
}
// Upsert vectors in batches
await (0, upsert_1.upsertVectorsInBatches)(indexOperation, validVectors, 100, workflowId, executionId);
logger_1.logger.info("Successfully saved vectors to Pinecone", "saveVectorsWithEmbeddings", {
savedCount: validVectors.length,
skippedCount: vectors.length - validVectors.length,
});
return { success: true, vectorCount: validVectors.length };
}
catch (error) {
logger_1.logger.error("Failed to save vectors to Pinecone", "saveVectorsWithEmbeddings", {
error: error.message,
stack: error.stack,
});
throw error;
}
}
/**
* Load vectors for editing with context
*/
async function loadVectorsForEditingWithContext(indexName, namespace, context) {
try {
logger_1.logger.info("Loading vectors for editing", "loadVectorsForEditing", {
indexName,
namespace,
});
// Initialize Pinecone client
const pinecone = await (0, client_1.initializePineconeWithContext)(context);
const index = pinecone.index(indexName);
const indexOperation = index.namespace(namespace);
// Fetch all vectors
const vectors = await (0, fetch_1.fetchAllVectors)(indexOperation, 10000);
logger_1.logger.info("Successfully loaded vectors", "loadVectorsForEditing", {
vectorCount: vectors.length,
});
return vectors;
}
catch (error) {
logger_1.logger.error("Failed to load vectors for editing", "loadVectorsForEditing", {
error: error.message,
stack: error.stack,
});
throw error;
}
}
/**
* Load vectors for editing (legacy without context)
*/
async function loadVectorsForEditing(apiKey, indexName, namespace) {
try {
logger_1.logger.info("Loading vectors for editing", "loadVectorsForEditing", {
indexName,
namespace,
});
// Initialize Pinecone client
const pinecone = (0, client_1.initializePinecone)(apiKey);
const index = pinecone.index(indexName);
const indexOperation = index.namespace(namespace);
// Fetch all vectors
const vectors = await (0, fetch_1.fetchAllVectors)(indexOperation, 10000);
logger_1.logger.info("Successfully loaded vectors", "loadVectorsForEditing", {
vectorCount: vectors.length,
});
return vectors;
}
catch (error) {
logger_1.logger.error("Failed to load vectors for editing", "loadVectorsForEditing", {
error: error.message,
stack: error.stack,
});
throw error;
}
}
//# sourceMappingURL=vectors.js.map