import { Tokenizer } from "./tokenizer.js"; import { Chunk } from "./types.js"; //#region src/semantic.d.ts /** A function that returns embeddings for an array of texts. */ type EmbedFunction = (texts: string[]) => Promise; /** Object-style embedding model (e.g. a class with an embed method). */ interface EmbeddingModel { embed(texts: string[]): Promise; } interface SemanticChunkerOptions { /** * Embedding provider. Either a plain async function `(texts) => number[][]` * or any object with an `embed(texts)` method. */ embeddings: EmbedFunction | EmbeddingModel; /** * Similarity threshold (0–1). Valleys below this value are considered * candidate split points. Lower → more splits. (default: 0.8) */ threshold?: number; /** Maximum tokens per chunk. (default: 2048) */ chunkSize?: number; /** * Number of sentences combined into each sliding window embedding. * (default: 3) */ similarityWindow?: number; /** Minimum sentences that must appear in a chunk. (default: 1) */ minSentencesPerChunk?: number; /** * Minimum characters per sentence. Sentences shorter than this are merged * with adjacent sentences until the minimum length is satisfied; content is * not dropped. (default: 24) */ minCharactersPerSentence?: number; /** * Sentence-splitting delimiters. Defaults to ['. ', '! ', '? ', '\n']. */ delimiters?: string | string[]; /** * Where to place the delimiter character in the output sentence. * (default: 'prev') */ includeDelim?: 'prev' | 'next' | 'none'; /** Tokenizer instance or HuggingFace model name. (default: 'character') */ tokenizer?: Tokenizer | string; /** * Smoothing window for valley detection (analogous to the Python filter_window). * Must be odd and ≥ 3. (default: 5) */ filterWindow?: number; /** * Polynomial order for the Savitzky-Golay filter. * Must be less than filterWindow. (default: 3) */ filterPolyorder?: number; /** * Tolerance for the Savitzky-Golay filter. (default: 0.2) */ filterTolerance?: number; /** * Number of groups to look ahead when merging semantically similar * consecutive groups. 0 disables skip-and-merge. (default: 0) */ skipWindow?: number; } /** * Chunks text by detecting semantic boundaries using embedding similarity. * * Algorithm (mirrors the Python SemanticChunker): * 1. Split text into sentences via WASM. * 2. Compute sliding-window embeddings of `similarityWindow` consecutive sentences. * 3. Compute cosine similarity between each window and the sentence immediately after it. * 4. Detect valleys (local minima below threshold) in the similarity signal. * 5. Group sentences between valley positions into candidate chunks. * 6. Optionally merge semantically similar adjacent groups (skipWindow > 0). * 7. Split any group that exceeds `chunkSize` tokens. */ declare class SemanticChunker { readonly threshold: number; readonly chunkSize: number; readonly similarityWindow: number; readonly minSentencesPerChunk: number; readonly minCharactersPerSentence: number; readonly delimiters: string[]; readonly includeDelim: 'prev' | 'next' | 'none'; readonly filterWindow: number; readonly filterPolyorder: number; readonly filterTolerance: number; readonly skipWindow: number; private readonly embed; private tokenizer; private readonly delimPattern; private constructor(); /** * Create a SemanticChunker. * * @example * // With a plain embed function * const chunker = await SemanticChunker.create({ * embeddings: async (texts) => myModel.encode(texts), * threshold: 0.5, * chunkSize: 512, * }); * * @example * // With an object model * const chunker = await SemanticChunker.create({ * embeddings: myEmbeddingModel, // must have .embed(texts) method * chunkSize: 1024, * }); */ static create(options: SemanticChunkerOptions): Promise; private prepareSentences; /** * Build window texts: for each i, join sentences[i..i+window]. * Produces len(sentences) - similarityWindow windows. */ private buildWindows; /** * Compute cosine similarity between each window embedding and the sentence * embedding immediately following that window (index i + similarityWindow). */ private getSimilarities; /** * Find sentence-level split indices from the similarity signal. * Returns absolute indices into the `sentences` array (including 0 and len). */ private getSplitIndices; private groupSentences; /** Split any group that exceeds chunkSize into sub-groups. */ private splitOversizedGroups; /** Optionally merge semantically similar adjacent groups. */ private skipAndMerge; private createChunks; /** * Chunk a single text into semantic chunks. * * @param text - The text to chunk * @returns Array of chunks with semantic boundaries */ chunk(text: string): Promise; /** * Chunk multiple texts. * * @param texts - Array of texts to chunk * @returns Array of chunk arrays, one per input text */ chunkBatch(texts: string[]): Promise; toString(): string; } //#endregion export { EmbedFunction, EmbeddingModel, SemanticChunker, SemanticChunkerOptions }; //# sourceMappingURL=semantic.d.ts.map