Spaces:
Sleeping
Sleeping
Update src/query_engine.py
Browse files- src/query_engine.py +62 -4
src/query_engine.py
CHANGED
|
@@ -3,9 +3,10 @@
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import re
|
|
|
|
| 6 |
|
| 7 |
from src.llm import generate
|
| 8 |
-
from src.prompts import build_query_understanding_prompt
|
| 9 |
|
| 10 |
|
| 11 |
def _load_sql_schema_summary(sql_db_dir: str) -> str:
|
|
@@ -21,11 +22,39 @@ def _load_sql_schema_summary(sql_db_dir: str) -> str:
|
|
| 21 |
except Exception:
|
| 22 |
return ""
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def understand_query(
|
| 26 |
user_query: str,
|
| 27 |
-
cfg:
|
| 28 |
-
conversation_history: list[
|
| 29 |
) -> dict:
|
| 30 |
"""Analyze and reformulate a user query for better retrieval.
|
| 31 |
|
|
@@ -109,8 +138,37 @@ def understand_query(
|
|
| 109 |
|
| 110 |
return result
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
def _parse_qu_result(raw: str, original_query: str) ->
|
| 114 |
"""Parse the LLM's JSON response into a structured result.
|
| 115 |
|
| 116 |
Falls back to raw query passthrough if parsing fails.
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
+
from typing import Dict
|
| 7 |
|
| 8 |
from src.llm import generate
|
| 9 |
+
from src.prompts import build_query_understanding_prompt, build_categorizer_prompt
|
| 10 |
|
| 11 |
|
| 12 |
def _load_sql_schema_summary(sql_db_dir: str) -> str:
|
|
|
|
| 22 |
except Exception:
|
| 23 |
return ""
|
| 24 |
|
| 25 |
+
def categorize_query(user_query:str,
|
| 26 |
+
cfg:Dict,
|
| 27 |
+
conversation_history: list[dict] | None = None,):
|
| 28 |
+
domain = cfg.get("chatbot", {}).get("domain", "research")
|
| 29 |
+
cat_cfg = cfg.get("query_categorization", {})
|
| 30 |
+
max_history = cat_cfg.get("max_history", 6)
|
| 31 |
+
|
| 32 |
+
# Trim conversation history
|
| 33 |
+
history = conversation_history or []
|
| 34 |
+
if len(history) > max_history:
|
| 35 |
+
history = history[-max_history:]
|
| 36 |
+
|
| 37 |
+
prompt = build_categorizer_prompt(user_query, domain)
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
raw = generate(
|
| 41 |
+
"You are a query-routing classifier. Return only JSON.",
|
| 42 |
+
prompt,
|
| 43 |
+
cfg,
|
| 44 |
+
max_tokens=512,
|
| 45 |
+
)
|
| 46 |
+
return _parse_qcat_result(raw)
|
| 47 |
+
except Exception:
|
| 48 |
+
# LLM failure: fall back to raw query
|
| 49 |
+
return {
|
| 50 |
+
"category": "general",
|
| 51 |
+
"action": "continue",
|
| 52 |
+
}
|
| 53 |
|
| 54 |
def understand_query(
|
| 55 |
user_query: str,
|
| 56 |
+
cfg: Dict,
|
| 57 |
+
conversation_history: list[Dict] | None = None,
|
| 58 |
) -> dict:
|
| 59 |
"""Analyze and reformulate a user query for better retrieval.
|
| 60 |
|
|
|
|
| 138 |
|
| 139 |
return result
|
| 140 |
|
| 141 |
+
def _parse_qcat_result(raw:str) -> Dict:
|
| 142 |
+
parsed = None
|
| 143 |
+
|
| 144 |
+
# Try direct parse
|
| 145 |
+
try:
|
| 146 |
+
parsed = json.loads(raw)
|
| 147 |
+
except (json.JSONDecodeError, TypeError):
|
| 148 |
+
pass
|
| 149 |
+
|
| 150 |
+
# Fallback: scan for the first valid JSON object using raw_decode
|
| 151 |
+
if parsed is None:
|
| 152 |
+
decoder = json.JSONDecoder()
|
| 153 |
+
for i, ch in enumerate(raw):
|
| 154 |
+
if ch == '{':
|
| 155 |
+
try:
|
| 156 |
+
parsed, _ = decoder.raw_decode(raw, i)
|
| 157 |
+
break
|
| 158 |
+
except json.JSONDecodeError:
|
| 159 |
+
continue
|
| 160 |
+
|
| 161 |
+
# Unparseable: fall back to raw query
|
| 162 |
+
if not isinstance(parsed, dict) or "category" not in parsed:
|
| 163 |
+
return {
|
| 164 |
+
"category": "general",
|
| 165 |
+
"action": "continue",
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
return parsed
|
| 169 |
+
|
| 170 |
|
| 171 |
+
def _parse_qu_result(raw: str, original_query: str) -> Dict:
|
| 172 |
"""Parse the LLM's JSON response into a structured result.
|
| 173 |
|
| 174 |
Falls back to raw query passthrough if parsing fails.
|