import { ChunkWithoutID } from "../.."; import { countTokens } from "../../llm/countTokens"; export function* basicChunker( contents: string, maxChunkSize: number ): Generator { let chunkContent = ""; let chunkTokens = 0; let startLine = 0; let currLine = 0; for (const line of contents.split("\n")) { const lineTokens = countTokens(line, "gpt-4"); if (chunkTokens + lineTokens > maxChunkSize) { yield { content: chunkContent, startLine, endLine: currLine - 1 }; chunkContent = ""; chunkTokens = 0; startLine = currLine; } chunkContent += line + "\n"; chunkTokens += lineTokens; currLine++; } yield { content: chunkContent, startLine, endLine: currLine - 1, }; }