UNPKG

chonkie

Version:

🦛 CHONK your texts in TS with Chonkie!✨The no-nonsense lightweight and efficient chunking library.

178 lines (177 loc) • 6.22 kB
import { Chunk } from './base'; /** Type for include delimiter options * * @enum {string} */ type IncludeDelim = 'prev' | 'next'; /** Interface for RecursiveLevel data * * @interface RecursiveLevelData * @property {string | string[]} [delimiters] - The delimiters to use for chunking. * @property {boolean} [whitespace] - Whether to use whitespace as a delimiter. * @property {IncludeDelim} [includeDelim] - Whether to include the delimiter in the previous or next chunk. */ interface RecursiveLevelData { delimiters?: string | string[]; whitespace?: boolean; includeDelim?: IncludeDelim; } /** Class to represent recursive chunking rules at a specific level * * @class RecursiveLevel * @property {string | string[]} [delimiters] - The delimiters to use for chunking. * @property {boolean} [whitespace] - Whether to use whitespace as a delimiter. * @property {IncludeDelim} [includeDelim] - Whether to include the delimiter in the previous or next chunk. */ export declare class RecursiveLevel { /** Custom delimiters for chunking */ delimiters?: string | string[]; /** Whether to use whitespace as a delimiter */ whitespace: boolean; /** Whether to include the delimiter in the previous or next chunk */ includeDelim: IncludeDelim; /** * Constructs a new RecursiveLevel object. * * @param {RecursiveLevelData} data - The data to construct the RecursiveLevel from. */ constructor(data?: RecursiveLevelData); /** * Validates the RecursiveLevel object. * * @private */ private validate; /** Return a string representation of the RecursiveLevel * * @returns {string} The string representation of the RecursiveLevel. */ toString(): string; /** Return the RecursiveLevel as a dictionary-like object * * @returns {RecursiveLevelData} The dictionary-like object. */ toDict(): RecursiveLevelData; /** Create RecursiveLevel object from a dictionary * * @param {RecursiveLevelData} data - The dictionary-like object. * @returns {RecursiveLevel} The RecursiveLevel object. */ static fromDict(data: RecursiveLevelData): RecursiveLevel; /** Create RecursiveLevel object from a recipe * * @param {string} name - The name of the recipe. * @param {string} lang - The language of the recipe. * @returns {Promise<RecursiveLevel>} The RecursiveLevel object. */ static fromRecipe(name: string, lang?: string): Promise<RecursiveLevel>; } /** Interface for RecursiveRules data * * @interface RecursiveRulesData * @property {RecursiveLevelData[]} [levels] - The recursive levels. */ interface RecursiveRulesData { levels?: RecursiveLevelData[]; } /** Class to represent recursive chunking rules * * @class RecursiveRules * @property {RecursiveLevel[]} [levels] - The recursive levels. */ export declare class RecursiveRules { /** List of recursive levels */ levels: RecursiveLevel[]; constructor(data?: RecursiveRulesData); /** Return a string representation of the RecursiveRules * * @returns {string} The string representation of the RecursiveRules. */ toString(): string; /** Return the number of levels * * @returns {number} The number of levels. */ get length(): number; /** Get a level by index * * @param {number} index - The index of the level. * @returns {RecursiveLevel | undefined} The level. */ getLevel(index: number): RecursiveLevel | undefined; /** Return an iterator over the levels * * @returns {Iterator<RecursiveLevel>} The iterator over the levels. */ [Symbol.iterator](): Iterator<RecursiveLevel>; /** Create a RecursiveRules object from a dictionary * * @param {RecursiveRulesData} data - The dictionary-like object. * @returns {RecursiveRules} The RecursiveRules object. */ static fromDict(data: RecursiveRulesData): RecursiveRules; /** Return the RecursiveRules as a dictionary-like object * * @returns {RecursiveRulesData} The dictionary-like object. */ toDict(): RecursiveRulesData; /** Create a RecursiveRules object from a recipe * * @param {string} name - The name of the recipe. * @param {string} lang - The language of the recipe. * @param {string} path - The path to the recipe. * @returns {Promise<RecursiveRules>} The RecursiveRules object. */ static fromRecipe(name?: string, lang?: string, path?: string): Promise<RecursiveRules>; } /** Interface for RecursiveChunk data * * @interface RecursiveChunkData * @property {string} text - The text of the chunk. * @property {number} startIndex - The starting index of the chunk. * @property {number} endIndex - The ending index of the chunk. * @property {number} tokenCount - The number of tokens in the chunk. * @property {number} [level] - The level of recursion for the chunk. */ interface RecursiveChunkData { text: string; startIndex: number; endIndex: number; tokenCount: number; level?: number; embedding?: number[]; } /** Class to represent recursive chunks * * @class RecursiveChunk * @property {number} [level] - The level of recursion for the chunk. */ export declare class RecursiveChunk extends Chunk { /** The level of recursion for the chunk */ level?: number; constructor(data: { text: string; startIndex: number; endIndex: number; tokenCount: number; level?: number; embedding?: number[]; }); /** Return a string representation of the RecursiveChunk * * @returns {string} The string representation of the RecursiveChunk. */ toString(): string; /** Return the RecursiveChunk as a dictionary-like object * * @returns {RecursiveChunkData} The dictionary-like object. */ toDict(): RecursiveChunkData; /** Create a RecursiveChunk object from a dictionary * * @param {RecursiveChunkData} data - The dictionary-like object. * @returns {RecursiveChunk} The RecursiveChunk object. */ static fromDict(data: RecursiveChunkData): RecursiveChunk; } export {};