clusterkw
Version:
A package for clustering keywords using OpenAI embeddings
60 lines (59 loc) • 1.98 kB
TypeScript
import { Cluster, KeywordClustererOptions } from './types';
/**
* KeywordClusterer class for clustering keywords using OpenAI embeddings
*/
export declare class KeywordClusterer {
private openai;
private embeddingModel;
private completionModel;
private minClusterSize;
private distanceThreshold;
private algorithm;
private k?;
private maxIterations?;
private linkage?;
private context?;
/**
* Creates a new KeywordClusterer instance
* @param options Configuration options
*/
constructor(options: KeywordClustererOptions);
/**
* Clusters the provided keywords
* @param keywords Array of keywords to cluster
* @returns Array of clusters with names and descriptions
*/
clusterKeywords(keywords: string[]): Promise<Cluster[]>;
/**
* Gets embeddings for the provided texts
* @param texts Array of texts to get embeddings for
* @returns Array of embeddings
*/
getEmbeddings(texts: string[]): Promise<number[][]>;
/**
* Calculates cosine distances between all embeddings
* @param embeddings Array of embeddings
* @returns Matrix of distances
*/
calculateDistances(embeddings: number[][]): number[][];
/**
* Calculates cosine similarity between two vectors
* @param a First vector
* @param b Second vector
* @returns Cosine similarity
*/
private cosineSimilarity;
/**
* Generates clusters based on the selected algorithm
* @param keywords Array of keywords
* @param distances Matrix of distances
* @returns Array of clusters
*/
generateClusters(keywords: string[], distances: number[][], embeddings?: number[][]): Cluster[];
/**
* Generates names and descriptions for clusters
* @param clusters Array of clusters
* @returns Array of clusters with names and descriptions
*/
nameAndDescribeClusters(clusters: Cluster[]): Promise<Cluster[]>;
}