diff --git a/core/indexing/chunk/basic.ts b/core/indexing/chunk/basic.ts index c382e7463..60bda5995 100644 --- a/core/indexing/chunk/basic.ts +++ b/core/indexing/chunk/basic.ts @@ -12,15 +12,18 @@ export function* basicChunker( for (const line of contents.split("\n")) { const lineTokens = countTokens(line, "gpt-4"); - if (chunkTokens + lineTokens > maxChunkSize) { + if (chunkTokens + lineTokens > maxChunkSize - 5) { yield { content: chunkContent, startLine, endLine: currLine - 1 }; chunkContent = ""; chunkTokens = 0; startLine = currLine; } - chunkContent += line + "\n"; - chunkTokens += lineTokens; + if (lineTokens < maxChunkSize) { + chunkContent += line + "\n"; + chunkTokens += lineTokens + 1; + } + currLine++; } diff --git a/core/indexing/chunk/chunk.ts b/core/indexing/chunk/chunk.ts index 425dd377d..7ed7fb0a3 100644 --- a/core/indexing/chunk/chunk.ts +++ b/core/indexing/chunk/chunk.ts @@ -1,4 +1,5 @@ import { Chunk, ChunkWithoutID } from "../.."; +import { countTokens } from "../../llm/countTokens"; import { basicChunker } from "./basic"; import { codeChunker, supportedLanguages } from "./code"; @@ -40,6 +41,14 @@ export async function* chunkDocument( contents, maxChunkSize )) { + if (countTokens(chunkWithoutId.content, "gpt-4") > 512) { + console.warn( + `Chunk with more than ${maxChunkSize} tokens constructed: `, + filepath, + countTokens(chunkWithoutId.content, "gpt-4") + ); + continue; + } yield { ...chunkWithoutId, digest, diff --git a/core/indexing/chunk/code.ts b/core/indexing/chunk/code.ts index 7cc10edb1..d61ae4cb0 100644 --- a/core/indexing/chunk/code.ts +++ b/core/indexing/chunk/code.ts @@ -119,10 +119,13 @@ function collapseChildren( code: string, blockTypes: string[], collapseTypes: string[], - collapseBlockTypes: string[] + collapseBlockTypes: string[], + maxChunkSize: number ): string { code = code.slice(0, node.endIndex); const block = firstChild(node, blockTypes); + const collapsedChildren = []; + if (block) { const childrenToCollapse = block.children.filter((child) => collapseTypes.includes(child.type) @@ -132,31 +135,85 @@ function collapseChildren( if (grandChild) { const start = grandChild.startIndex; const end = grandChild.endIndex; + const collapsedChild = + code.slice(child.startIndex, start) + + collapsedReplacement(grandChild); code = code.slice(0, start) + collapsedReplacement(grandChild) + code.slice(end); + + collapsedChildren.unshift(collapsedChild); } } } - return code.slice(node.startIndex); + code = code.slice(node.startIndex); + let removedChild = false; + while ( + countTokens(code, "gpt-4") > maxChunkSize && + collapsedChildren.length > 0 + ) { + removedChild = true; + // Remove children starting at the end - TODO: Add multiple chunks so no children are missing + const childCode = collapsedChildren.pop()!; + const index = code.lastIndexOf(childCode); + if (index > 0) { + code = code.slice(0, index) + code.slice(index + childCode.length); + } + } + + if (removedChild) { + // Remove the extra blank lines + let lines = code.split("\n"); + let firstWhiteSpaceInGroup = -1; + for (let i = lines.length - 1; i >= 0; i--) { + if (lines[i].trim() === "") { + if (firstWhiteSpaceInGroup < 0) { + firstWhiteSpaceInGroup = i; + } + } else { + if (firstWhiteSpaceInGroup - i > 1) { + // Remove the lines + lines = [ + ...lines.slice(0, i + 1), + ...lines.slice(firstWhiteSpaceInGroup + 1), + ]; + } + firstWhiteSpaceInGroup = -1; + } + } + + code = lines.join("\n"); + } + + return code; } -function constructClassDefinitionChunk(node: SyntaxNode, code: string): string { +function constructClassDefinitionChunk( + node: SyntaxNode, + code: string, + maxChunkSize: number +): string { return collapseChildren( node, code, ["block", "class_body", "declaration_list"], ["method_definition", "function_definition", "function_item"], - ["block", "statement_block"] + ["block", "statement_block"], + maxChunkSize ); } function constructFunctionDefinitionChunk( node: SyntaxNode, - code: string + code: string, + maxChunkSize: number ): string { - const funcText = node.text; + const bodyNode = node.children[node.children.length - 1]; + const funcText = + code.slice(node.startIndex, bodyNode.startIndex) + + collapsedReplacement(bodyNode); + if ( node.parent && ["block", "declaration_list"].includes(node.parent.type) && @@ -177,7 +234,11 @@ function constructFunctionDefinitionChunk( } const collapsedNodeConstructors: { - [key: string]: (node: SyntaxNode, code: string) => string; + [key: string]: ( + node: SyntaxNode, + code: string, + maxChunkSize: number + ) => string; } = { // Classes, structs, etc class_definition: constructClassDefinitionChunk, @@ -211,7 +272,7 @@ function* getSmartCollapsedChunks( // If a collapsed form is defined, use that if (node.type in collapsedNodeConstructors) { yield { - content: collapsedNodeConstructors[node.type](node, code), + content: collapsedNodeConstructors[node.type](node, code, maxChunkSize), startLine: node.startPosition.row, endLine: node.endPosition.row, }; diff --git a/extensions/vscode/src/util/indexCodebase.ts b/extensions/vscode/src/util/indexCodebase.ts index 570262bc6..e0996f304 100644 --- a/extensions/vscode/src/util/indexCodebase.ts +++ b/extensions/vscode/src/util/indexCodebase.ts @@ -61,7 +61,7 @@ export async function vsCodeIndexCodebase(workspaceDirs: string[]) { (filepath) => ideProtocolClient.readFile(filepath) ); - console.log("RESULTS: ", results); + // console.log("RESULTS: ", results); for await (let progress of codebaseIndex.update( tag,