Mokhtar
fix(retrieval & providers): cap max_context_chars to 3500 to keep prompt tokens < 1800 and update free model routing
d1ecc9b Download knowledge/context.py from mokh2x/HNU-Final: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/mokh2x/HNU-Final/resolve/main/knowledge/context.py
- Command line
-
hf download hf://spaces/mokh2x/HNU-Final/knowledge/context.py
-
curl -L -o context.py https://huggingface.co/spaces/mokh2x/HNU-Final/resolve/main/knowledge/context.py
1.31 kB
| # knowledge/context.py — Context Builder for Compact CAG Prompting | |
| from typing import List, Set | |
| from knowledge.models import Chunk | |
| class ContextBuilder: | |
| """Aggregates, deduplicates, and formats retrieved chunks into a compact LLM context.""" | |
| def __init__(self, max_context_chars: int = 3500): | |
| self.max_context_chars = max_context_chars | |
| def build_context(self, chunks: List[Chunk]) -> str: | |
| """Format retrieved chunks into a compact, structured context block.""" | |
| if not chunks: | |
| return "لا توجد سياقات مسترجعة." | |
| formatted_blocks = [] | |
| seen_texts: Set[str] = set() | |
| total_chars = 0 | |
| for chunk in chunks: | |
| # Deduplicate identical or near-identical text snippets | |
| clean_text = chunk.text.strip() | |
| if clean_text in seen_texts: | |
| continue | |
| seen_texts.add(clean_text) | |
| chunk_block = chunk.formatted_for_context() | |
| if total_chars + len(chunk_block) > self.max_context_chars and formatted_blocks: | |
| # Cap context size to avoid bloating prompt payload | |
| break | |
| formatted_blocks.append(chunk_block) | |
| total_chars += len(chunk_block) | |
| return "\n".join(formatted_blocks) | |