Decompose *query* into standalone sub-queries. Parameters ---------- query : str The latest user message. chat_history : list[dict] | None Recent conversation turns (each item should contain at least the original user query under t
(self, query: str, chat_history: List[Dict[str, Any]] | None = None)
| 8 | self.llm_model = llm_model |
| 9 | |
| 10 | def decompose(self, query: str, chat_history: List[Dict[str, Any]] | None = None) -> List[str]: |
| 11 | """Decompose *query* into standalone sub-queries. |
| 12 | |
| 13 | Parameters |
| 14 | ---------- |
| 15 | query : str |
| 16 | The latest user message. |
| 17 | chat_history : list[dict] | None |
| 18 | Recent conversation turns (each item should contain at least the original |
| 19 | user query under the key ``"query"``). Only the **last 5** turns are |
| 20 | included to keep the prompt short. |
| 21 | """ |
| 22 | |
| 23 | # ---- Limit history to last 5 user turns and extract the queries ---- |
| 24 | history_snippets: List[str] = [] |
| 25 | if chat_history: |
| 26 | # Keep only the last 5 turns |
| 27 | recent_turns = chat_history[-5:] |
| 28 | # Extract user queries (fallback: full dict as string if key missing) |
| 29 | for turn in recent_turns: |
| 30 | history_snippets.append(str(turn.get("query", turn))) |
| 31 | |
| 32 | # Serialize chat_history for the prompt (single string) |
| 33 | chat_history_text = " | ".join(history_snippets) |
| 34 | |
| 35 | # ---- Build the new SYSTEM prompt with added legacy examples ---- |
| 36 | system_prompt = """ |
| 37 | You are an expert at query decomposition for a Retrieval-Augmented Generation (RAG) system. |
| 38 | |
| 39 | Return one RFC-8259-compliant JSON object and nothing else. |
| 40 | Schema: |
| 41 | { |
| 42 | “requires_decomposition”: <bool>, |
| 43 | “reasoning”: <string>, // ≤ 50 words |
| 44 | “resolved_query”: <string>, // query after context resolution |
| 45 | “sub_queries”: <string[]> // 1–10 standalone items |
| 46 | } |
| 47 | |
| 48 | Think step-by-step internally, but reveal only the concise reasoning. |
| 49 | |
| 50 | ⸻ |
| 51 | |
| 52 | Context Resolution (perform FIRST) |
| 53 | |
| 54 | You will receive: |
| 55 | • query – the current user message |
| 56 | • chat_history – the most recent user turns (may be empty) |
| 57 | |
| 58 | If query contains pronouns, ellipsis, or shorthand that can be unambiguously linked to something in chat_history, rewrite it to a fully self-contained question and place the result in resolved_query. |
| 59 | Otherwise, copy query into resolved_query unchanged. |
| 60 | |
| 61 | ⸻ |
| 62 | |
| 63 | When is decomposition REQUIRED? |
| 64 | • MULTI-PART questions joined by “and”, “or”, “also”, list commas, etc. |
| 65 | • COMPARATIVE / SUPERLATIVE questions (two or more entities, e.g. “bigger, better, fastest”). |
| 66 | • TEMPORAL / SEQUENTIAL questions (changes over time, event timelines). |
| 67 | • ENUMERATIONS (pros, cons, impacts). |
no test coverage detected