@roadiehq/rag-ai-storage-pgvector
Version:
137 lines (133 loc) • 6.11 kB
TypeScript
import { LoggerService, DatabaseService } from '@backstage/backend-plugin-api';
import { RoadieVectorStore, EmbeddingDoc, EmbeddingDocMetadata } from '@roadiehq/rag-ai-node';
import { Config } from '@backstage/config';
import { Embeddings } from '@langchain/core/embeddings';
import { Knex } from 'knex';
interface PgVectorStoreInitConfig {
logger: LoggerService;
database: DatabaseService;
config: Config;
}
interface RoadiePgVectorStoreOptions {
chunkSize?: number;
amount?: number;
}
declare function createRoadiePgVectorStore({ logger, database, config, }: PgVectorStoreInitConfig): Promise<RoadieVectorStore>;
interface RoadiePgVectorStoreConfig {
logger: LoggerService;
db: Knex;
/**
* The amount of documents to chunk by when
* adding vectors.
* @default 500
*/
chunkSize?: number;
/**
* The default amount of embeddings to return when querying vectors with similarity search
*/
amount?: number;
}
/**
* A class representing a vector store that uses PostgreSQL as the backend.
*/
declare class RoadiePgVectorStore implements RoadieVectorStore {
protected readonly tableName: string;
protected readonly client: Knex;
protected readonly chunkSize: number;
protected readonly amount: number;
protected embeddings?: Embeddings;
protected readonly logger: LoggerService;
/**
* Initializes the RoadiePgVectorStore.
*
* @param {RoadiePgVectorStoreConfig} config - The configuration for RoadiePgVectorStore.
*
* @return {Promise<RoadiePgVectorStore>} A Promise that resolves to an instance of RoadiePgVectorStore.
*/
static initialize(config: RoadiePgVectorStoreConfig): Promise<RoadiePgVectorStore>;
/**
* Constructor for RoadiePgVectorStore class.
*
* @param {RoadiePgVectorStoreConfig} config - The configuration object for RoadiePgVectorStore.
* @param {string} config.db - The database client to connect.
* @param {Object} config.logger - The logger object for logging.
* @param {number} [config.chunkSize=500] - The size of chunks for processing.
*/
protected constructor(config: RoadiePgVectorStoreConfig);
connectEmbeddings(embeddings: Embeddings): void;
table(): Knex.QueryBuilder<any, {
_base: any;
_hasSelection: false;
_keys: never;
_aliases: {};
_single: false;
_intersectProps: {};
_unionProps: never;
}[]>;
/**
* Add documents to the vector store.
*
* @param {EmbeddingDoc[]} documents - The array of documents to be added.
* @throws {Error} When no embeddings are configured for the vector store.
* @returns {Promise<void>} Resolves when the documents have been added successfully.
*/
addDocuments(documents: EmbeddingDoc[]): Promise<void>;
/**
* Adds vectors to the database along with corresponding documents.
*
* @param {number[][]} vectors - The vectors to be added.
* @param {EmbeddingDoc[]} documents - The corresponding documents.
* @return {Promise<void>} - A promise that resolves when the vectors are added successfully.
* @throws {Error} - If there is an error inserting the vectors.
*/
protected addVectors(vectors: number[][], documents: EmbeddingDoc[]): Promise<void>;
/**
* Deletes records from the database table by their ids.
*
* @param {string[]} ids - The array of ids of the records to be deleted.
* @returns {Promise<void>} - A promise that resolves when the deletion is complete.
*/
protected deleteById(ids: string[]): Promise<void>;
/**
* Deletes rows from the table based on the specified filter.
*
* @param {EmbeddingDocMetadata} filter - The filter to apply for deletion.
* @returns {Promise} - A Promise that resolves when the deletion is complete.
*/
protected deleteByFilter(filter: EmbeddingDocMetadata): Promise<any>;
/**
* Deletes documents based on the provided deletion parameters.
* Either `ids` or `filter` must be specified.
*
* @param {Object} deletionParams - The deletion parameters.
* @param {Array<string>} [deletionParams.ids] - The document IDs to delete.
* @param {EmbeddingDocMetadata} [deletionParams.filter] - The filter to match documents to be deleted.
*
* @return {Promise<void>} - A Promise that resolves once the documents have been deleted.
*/
deleteDocuments(deletionParams: {
ids?: string[];
filter?: EmbeddingDocMetadata;
}): Promise<void>;
/**
* Finds the most similar documents to a given query vector, along with their similarity scores.
*
* @param {number[]} query - The query vector to compare against.
* @param {number} amount - The maximum number of results to return.
* @param {EmbeddingDocMetadata} [filter] - Optional filter to limit the search results.
* @returns {Promise<[EmbeddingDoc, number][]>} - An array of document similarity results, where each
* result is a tuple containing the document and its similarity score.
*/
protected similaritySearchVectorWithScore(query: number[], amount: number, filter?: EmbeddingDocMetadata): Promise<[EmbeddingDoc, number][]>;
/**
* Performs a similarity search using the given query and filter.
*
* @param {string} query - The query to perform the similarity search on.
* @param {EmbeddingDocMetadata} filter - The filter to apply to the search results.
* @param {number} [amount=4] - The number of results to return.
* @return {Promise<EmbeddingDoc[]>} - A promise that resolves to an array of RoadieEmbeddingDoc objects representing the search results.
* @throws {Error} - Throws an error if there are no embeddings configured for the vector store.
*/
similaritySearch(query: string, filter: EmbeddingDocMetadata, amount?: number): Promise<EmbeddingDoc[]>;
}
export { type PgVectorStoreInitConfig, RoadiePgVectorStore, type RoadiePgVectorStoreConfig, type RoadiePgVectorStoreOptions, createRoadiePgVectorStore };