Spaces:
Sleeping
Sleeping
Update src/query_engine.py
Browse files- src/query_engine.py +31 -1
src/query_engine.py
CHANGED
|
@@ -6,7 +6,7 @@ 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:
|
|
@@ -51,6 +51,36 @@ def categorize_query(user_query:str,
|
|
| 51 |
"action": "continue",
|
| 52 |
}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def understand_query(
|
| 55 |
user_query: str,
|
| 56 |
cfg: Dict,
|
|
|
|
| 6 |
from typing import Dict
|
| 7 |
|
| 8 |
from src.llm import generate
|
| 9 |
+
from src.prompts import build_query_understanding_prompt, build_categorizer_prompt, build_init_query
|
| 10 |
|
| 11 |
|
| 12 |
def _load_sql_schema_summary(sql_db_dir: str) -> str:
|
|
|
|
| 51 |
"action": "continue",
|
| 52 |
}
|
| 53 |
|
| 54 |
+
def init_query(user_query:str,
|
| 55 |
+
cfg:Dict,
|
| 56 |
+
conversation_history: list[dict] | None = None,):
|
| 57 |
+
domain = cfg.get("chatbot", {}).get("domain", "research")
|
| 58 |
+
cat_cfg = cfg.get("query_categorization", {})
|
| 59 |
+
max_history = cat_cfg.get("max_history", 6)
|
| 60 |
+
|
| 61 |
+
# Trim conversation history
|
| 62 |
+
history = conversation_history or []
|
| 63 |
+
if len(history) > max_history:
|
| 64 |
+
history = history[-max_history:]
|
| 65 |
+
|
| 66 |
+
prompt = build_init_prompt(user_query, domain)
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
raw = generate(
|
| 70 |
+
"You are a query-routing classifier. Return only JSON.",
|
| 71 |
+
prompt,
|
| 72 |
+
cfg,
|
| 73 |
+
max_tokens=512,
|
| 74 |
+
)
|
| 75 |
+
return _parse_qcat_result(raw)
|
| 76 |
+
except Exception:
|
| 77 |
+
# LLM failure: fall back to raw query
|
| 78 |
+
return {
|
| 79 |
+
"country": None,
|
| 80 |
+
"value_chain": None,
|
| 81 |
+
"investment": None,
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
def understand_query(
|
| 85 |
user_query: str,
|
| 86 |
cfg: Dict,
|