-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement python-specific chunker using astroid * use LiteDB by default * minor version bump * format fixes
- Loading branch information
1 parent
593b805
commit 5d1e8ac
Showing
18 changed files
with
509 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.6.2" | ||
__version__ = "0.7.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import astroid | ||
|
||
from ragdaemon.annotators.chunker.utils import Chunk, RawChunk, resolve_raw_chunks | ||
from ragdaemon.errors import RagdaemonError | ||
|
||
|
||
async def chunk_document(document: str) -> list[Chunk]: | ||
# Parse the code into an astroid AST | ||
lines = document.split("\n") | ||
file_path = lines[0].strip() | ||
code = "\n".join(lines[1:]) | ||
|
||
tree = astroid.parse(code) | ||
|
||
chunks = list[RawChunk]() | ||
|
||
def extract_chunks(node, parent_path=None): | ||
if isinstance(node, (astroid.FunctionDef, astroid.ClassDef)): | ||
delimiter = ":" if parent_path == file_path else "." | ||
current_path = f"{parent_path}{delimiter}{node.name}" | ||
start_line, end_line = node.lineno, node.end_lineno | ||
if start_line is None or end_line is None: | ||
raise RagdaemonError(f"Function {node.name} has no line numbers.") | ||
chunks.append( | ||
RawChunk(id=current_path, start_line=start_line, end_line=end_line) | ||
) | ||
# Recursively handle nested functions | ||
for child in node.body: | ||
extract_chunks(child, parent_path=current_path) | ||
|
||
# Recursively extract chunks from the AST | ||
for node in tree.body: | ||
extract_chunks(node, parent_path=file_path) | ||
|
||
return resolve_raw_chunks(document, chunks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
async def chunk_document( | ||
document: str, lines_per_chunk: int = 100 | ||
) -> list[dict[str, str]]: | ||
lines = document.split("\n") | ||
file = lines[0] | ||
file_lines = lines[1:] | ||
if not file_lines or not any(line for line in file_lines): | ||
return [] | ||
|
||
chunks = list[dict[str, str]]() | ||
if len(file_lines) > lines_per_chunk: | ||
chunks.append( | ||
{ | ||
"id": f"{file}:BASE", | ||
"ref": f"{file}:1-{lines_per_chunk}", | ||
} | ||
) # First N lines is always the base chunk | ||
for i, start_line in enumerate( | ||
range(lines_per_chunk + 1, len(file_lines), lines_per_chunk) | ||
): | ||
end_line = min(start_line + lines_per_chunk - 1, len(file_lines)) | ||
chunks.append( | ||
{ | ||
"id": f"{file}:chunk_{i + 1}", | ||
"ref": f"{file}:{start_line}-{end_line}", | ||
} | ||
) | ||
return chunks |
Oops, something went wrong.