Skip to content

Commit c0b4a01

Browse files
committed
fix: prevent division by zero in _limit_chunkspans
1 parent b9bda63 commit c0b4a01

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/raglite/_rag.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ def _limit_chunkspans(
152152
# Early exit if we're already under the limit
153153
if total_tokens <= max_tokens:
154154
return tool_chunk_spans
155+
# If the context window is completely exhausted, return empty spans.
156+
if max_tokens <= 0 or total_tokens == 0:
157+
logger.warning(
158+
"RAG context was limited to 0 out of %d chunks due to context window size. "
159+
"Consider using a model with a bigger context window or reducing the number of retrieved chunks.",
160+
total_chunk_spans,
161+
)
162+
return {tool_id: [] for tool_id in tool_chunk_spans}
155163
# Allocate tokens proportionally and truncate
156164
new_total_chunk_spans = 0
157165
scale_ratio = max_tokens / total_tokens

0 commit comments

Comments
 (0)