@@ -31,8 +31,8 @@ def get_context(
3131 path_prefix : str | None = None ,
3232 tags_any : list [str ] | None = None ,
3333 tags_all : list [str ] | None = None ,
34- top_k : Annotated [int , Field (ge = 1 , le = 50 )] = runtime .default_top_k ,
35- expand_top_k : Annotated [int , Field (ge = 0 , le = 50 )] = 5 ,
34+ top_k : Annotated [int , Field (ge = 1 , le = 20 )] = runtime .default_top_k ,
35+ expand_top_k : Annotated [int , Field (ge = 0 , le = 20 )] = 5 ,
3636 hit_chunks_per_note : Annotated [int , Field (ge = 1 , le = 5 )] = 2 ,
3737 neighbor_window : Annotated [int , Field (ge = 0 , le = 3 )] = 1 ,
3838 max_evidence_chars_per_note : Annotated [int , Field (ge = 200 , le = 20_000 )] = 1500 ,
@@ -74,6 +74,7 @@ def run() -> dict[str, object]:
7474 runtime .settings ,
7575 )
7676 results = response .results [:top_k ]
77+ expanded_result_count = min (max (0 , expand_top_k ), len (results ))
7778 return {
7879 "query" : query ,
7980 "top_k" : top_k ,
@@ -85,7 +86,7 @@ def run() -> dict[str, object]:
8586 "tags_all" : normalized_tags_all ,
8687 },
8788 "params" : {
88- "expand_top_k" : min ( max ( 0 , expand_top_k ), top_k ) ,
89+ "expand_top_k" : expanded_result_count ,
8990 "hit_chunks_per_note" : hit_chunks_per_note ,
9091 "neighbor_window" : neighbor_window ,
9192 "max_evidence_chars_per_note" : max_evidence_chars_per_note ,
@@ -103,23 +104,33 @@ def run() -> dict[str, object]:
103104 "heading" : item .heading ,
104105 "heading_path" : item .heading_path ,
105106 "snippet" : item .snippet ,
106- "evidence_text" : item .evidence_text ,
107- "evidence_truncated" : item .evidence_truncated ,
108- "evidence_chunks" : [
109- {
110- "chunk_id" : chunk .chunk_id ,
111- "chunk_index" : chunk .chunk_index or 0 ,
112- "kind" : chunk .kind if chunk .kind in {"hit" , "neighbor" } else "hit" ,
113- "distance" : chunk .distance ,
114- "heading" : chunk .heading ,
115- "heading_path" : chunk .heading_path ,
116- }
117- for chunk in item .evidence
118- ],
107+ "evidence_text" : item .evidence_text
108+ if index < expanded_result_count
109+ else None ,
110+ "evidence_truncated" : (
111+ item .evidence_truncated if index < expanded_result_count else False
112+ ),
113+ "evidence_chunks" : (
114+ [
115+ {
116+ "chunk_id" : chunk .chunk_id ,
117+ "chunk_index" : chunk .chunk_index or 0 ,
118+ "kind" : (
119+ chunk .kind if chunk .kind in {"hit" , "neighbor" } else "hit"
120+ ),
121+ "distance" : chunk .distance ,
122+ "heading" : chunk .heading ,
123+ "heading_path" : chunk .heading_path ,
124+ }
125+ for chunk in item .evidence
126+ ]
127+ if index < expanded_result_count
128+ else []
129+ ),
119130 "preview" : item .preview ,
120131 "preview_truncated" : item .preview_truncated ,
121132 }
122- for item in results
133+ for index , item in enumerate ( results )
123134 ],
124135 }
125136
0 commit comments