(
query: str,
top_k: int = 4,
window: int = 1,
chapter: str = "",
section: str = "",
semantic_weight: float = 0.86,
)
| 247 | ), |
| 248 | ) |
| 249 | def afml_get_context( |
| 250 | query: str, |
| 251 | top_k: int = 4, |
| 252 | window: int = 1, |
| 253 | chapter: str = "", |
| 254 | section: str = "", |
| 255 | semantic_weight: float = 0.86, |
| 256 | ) -> dict[str, Any]: |
| 257 | if window < 0: |
| 258 | window = 0 |
| 259 | if window > 5: |
| 260 | window = 5 |
| 261 | |
| 262 | hits = _score_results( |
| 263 | query=query, |
| 264 | top_k=top_k, |
| 265 | chapter=chapter, |
| 266 | section=section, |
| 267 | semantic_weight=semantic_weight, |
| 268 | ) |
| 269 | |
| 270 | contexts: list[dict[str, Any]] = [] |
| 271 | for hit in hits: |
| 272 | bundle = _chunk_with_neighbors(hit["chunk_id"], window=window) |
| 273 | contexts.append({"hit": hit, "context": bundle}) |
| 274 | |
| 275 | return { |
| 276 | "query": query, |
| 277 | "top_k": top_k, |
| 278 | "window": window, |
| 279 | "filters": {"chapter": chapter, "section": section}, |
| 280 | "contexts": contexts, |
| 281 | } |
| 282 | |
| 283 | |
| 284 | if __name__ == "__main__": |
no test coverage detected