Skip to content

Commit c5cd5cc

Browse files
committed
fix: prevent division by zero in _limit_chunkspans
1 parent 8a49af4 commit c5cd5cc

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
@@ -206,6 +206,14 @@ def _limit_chunkspans(
206206
# Early exit if we're already under the limit
207207
if total_tokens <= max_tokens:
208208
return tool_chunk_spans
209+
# If the context window is completely exhausted, return empty spans.
210+
if max_tokens <= 0 or total_tokens == 0:
211+
logger.warning(
212+
"RAG context was limited to 0 out of %d chunks due to context window size. "
213+
"Consider using a model with a bigger context window or reducing the number of retrieved chunks.",
214+
total_chunk_spans,
215+
)
216+
return {tool_id: [] for tool_id in tool_chunk_spans}
209217
# Allocate tokens proportionally and truncate
210218
new_total_chunk_spans = 0
211219
scale_ratio = max_tokens / total_tokens

0 commit comments

Comments
 (0)