ai-functions
Version:
Core AI primitives for building intelligent applications
123 lines • 3.74 kB
TypeScript
/**
* Embedding utilities from AI SDK
*
* Re-exports embed, embedMany, and cosineSimilarity from the Vercel AI SDK
* with additional convenience wrappers.
*
* Default model: Cloudflare Workers AI @cf/baai/bge-m3
*
* @packageDocumentation
*/
export { embed, embedMany, cosineSimilarity } from 'ai';
export type { EmbeddingModel, Embedding } from 'ai';
export { cloudflare, cloudflareEmbedding, DEFAULT_CF_EMBEDDING_MODEL } from 'ai-providers/cloudflare';
/**
* Get the default embedding model (Cloudflare @cf/baai/bge-m3)
*/
export declare function getDefaultEmbeddingModel(): import("ai").EmbeddingModel<string>;
/**
* Embed a single value using the default Cloudflare model
*
* @example
* ```ts
* import { embedText } from 'ai-functions'
*
* const { embedding } = await embedText('hello world')
* ```
*/
export declare function embedText(value: string): Promise<import("ai").EmbedResult>;
/**
* Embed multiple values using the default Cloudflare model
*
* @example
* ```ts
* import { embedTexts } from 'ai-functions'
*
* const { embeddings } = await embedTexts(['doc1', 'doc2', 'doc3'])
* ```
*/
export declare function embedTexts(values: string[]): Promise<import("ai").EmbedManyResult>;
/**
* Result of an embed operation
*/
export interface EmbedResult<T = string> {
/** The original input value */
value: T;
/** The generated embedding vector */
embedding: number[];
/** Token usage */
usage: {
tokens: number;
};
}
/**
* Result of an embedMany operation
*/
export interface EmbedManyResult<T = string> {
/** The original input values */
values: T[];
/** The generated embedding vectors */
embeddings: number[][];
/** Token usage */
usage: {
tokens: number;
};
}
/**
* Find the most similar items to a query embedding
*
* @example
* ```ts
* import { embed, embedMany, findSimilar } from 'ai-functions'
*
* const documents = ['doc1', 'doc2', 'doc3']
* const { embeddings } = await embedMany({ model, values: documents })
* const { embedding: queryEmbedding } = await embed({ model, value: 'search query' })
*
* const results = findSimilar(queryEmbedding, embeddings, documents, { topK: 2 })
* // [{ item: 'doc1', score: 0.95, index: 0 }, { item: 'doc2', score: 0.82, index: 1 }]
* ```
*/
export declare function findSimilar<T>(queryEmbedding: number[], embeddings: number[][], items: T[], options?: {
/** Number of results to return (default: 10) */
topK?: number;
/** Minimum similarity score (default: 0) */
minScore?: number;
}): Array<{
item: T;
score: number;
index: number;
}>;
/**
* Calculate pairwise similarities between all embeddings
*
* @example
* ```ts
* const matrix = pairwiseSimilarity(embeddings)
* // matrix[i][j] = similarity between embeddings[i] and embeddings[j]
* ```
*/
export declare function pairwiseSimilarity(embeddings: number[][]): number[][];
/**
* Cluster embeddings by similarity using a simple threshold-based approach
*
* @example
* ```ts
* const clusters = clusterBySimilarity(embeddings, items, { threshold: 0.8 })
* // [[item1, item2], [item3], [item4, item5, item6]]
* ```
*/
export declare function clusterBySimilarity<T>(embeddings: number[][], items: T[], options?: {
/** Similarity threshold for clustering (default: 0.8) */
threshold?: number;
}): T[][];
/**
* Average multiple embeddings into a single embedding
* Useful for creating document embeddings from chunk embeddings
*/
export declare function averageEmbeddings(embeddings: number[][]): number[];
/**
* Normalize an embedding to unit length
*/
export declare function normalizeEmbedding(embedding: number[]): number[];
//# sourceMappingURL=embeddings.d.ts.map