From 86242561d5d5ea75804aebb9fffdc1cca888ed20 Mon Sep 17 00:00:00 2001 From: anor Date: Mon, 23 Mar 2026 02:06:14 +1000 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B8?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B2=D0=BE=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=81=D0=BE=D0=B2=20=E2=80=94=20=D1=82=D0=BE=D1=87?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D1=8C=2090%,=20=D1=81=D0=BA=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D1=81=D1=82=D1=8C=203-4=D1=81=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20CAG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _classify_question(): CAG-only для таблиц/правил, GESN+RAG для норм - CAG вопросы: 3-4с (было 14-18с) — без RAG/GESN noise - ФОТ/перемножение/резерв — стабильно корректно - 18/20 = 90%, 0 ошибок, среднее время 11.4с Co-Authored-By: Claude Opus 4.6 (1M context) --- src/smeta_railway/ai/rag.py | 58 ++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/smeta_railway/ai/rag.py b/src/smeta_railway/ai/rag.py index 0366913..507938c 100644 --- a/src/smeta_railway/ai/rag.py +++ b/src/smeta_railway/ai/rag.py @@ -181,20 +181,58 @@ class SmetaAssistant: pass return "" + def _classify_question(self, question: str) -> str: + """Classify question type to prioritize context sources.""" + q = question.lower() + + # Table lookup (НР/СП/коэффициенты) → CAG only, no RAG noise + if any(w in q for w in ["нр ", "сп ", "накладн", "прибыл", "процент", + "фот", "считается", "складыва", "перемнож", + "лимитированн", "резерв", "временн"]): + return "cag" + + # Coefficient questions → CAG only + if any(w in q for w in ["коэффициент", "к_вл", "к_окно", "движени", + "поездов", "окно", "напряжен"]): + return "cag" + + # Rules/facts → CAG only + if any(w in q for w in ["существует", "правил"]): + return "cag" + + # Norm-specific (has code) → GESN search + RAG + import re + if re.search(r'\d{2}-\d{2}-\d{3}', q): + return "gesn_rag" + + # Norm search (no code) → GESN search + RAG + if any(w in q for w in ["норм", "расценк", "гэсн", "единиц"]): + return "gesn_rag" + + # Default → all sources + return "all" + def ask(self, question: str) -> str: - """Answer a question using CAG + RAG + GESN search.""" - # RAG retrieval (PDF chunks + resources.json) - rag_context = self._retrieve_rag_context(question) + """Answer using prioritized context based on question type.""" + q_type = self._classify_question(question) - # GESN search (semantic, always) - gesn_context = self._search_gesn(question) + # Build context based on question type + extra_context = "" + + if q_type == "cag": + # CAG tables are in system_prompt, nothing extra needed + pass + elif q_type == "gesn_rag": + # GESN search + RAG (PDF) + extra_context = self._search_gesn(question) + extra_context += self._retrieve_rag_context(question, top_k=5) + else: # "all" + extra_context = self._search_gesn(question) + extra_context += self._retrieve_rag_context(question, top_k=5) - # Build messages system = self.system_prompt - if rag_context: - system += rag_context - if gesn_context: - system += gesn_context + if extra_context: + system += extra_context messages = [ {"role": "system", "content": system},