From 055812fdcc02caf33a10452ea3cbb6374048b551 Mon Sep 17 00:00:00 2001 From: anor Date: Mon, 23 Mar 2026 20:15:45 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20exact=20code=20match=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D1=82=20=D0=BE=D1=82=20sem?= =?UTF-8?q?antic=20search=20(API=20timeout=20bug)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Разделён try/except: exact code match (без API) работает всегда, semantic search (embedding API) — отдельный try/except. Раньше если embedding падал — exact match тоже терялся. 21144 нормы в search index, 31991 chunks в RAG index. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/smeta_railway/ai/rag.py | 56 ++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/smeta_railway/ai/rag.py b/src/smeta_railway/ai/rag.py index f09cdcc..fb6e4e6 100644 --- a/src/smeta_railway/ai/rag.py +++ b/src/smeta_railway/ai/rag.py @@ -138,36 +138,37 @@ class SmetaAssistant: return response.content[0].text def _search_gesn(self, query: str, top_k: int = 10) -> str: - """Search GESN norms by semantic similarity + exact code match.""" + """Search GESN norms by exact code match + semantic similarity.""" + lines = [] + try: from smeta_railway.ai.search import GESNSearch search = GESNSearch() + except Exception: + return "" - lines = [] + # 1. Exact code match (always works, no API call) + import re + code_match = re.search(r'(\d{2}-\d{2}-\d{3})', query) + if code_match: + prefix = code_match.group(1) + prefixes = [prefix] + table_num = int(prefix.split('-')[2]) + for delta in [-1, 1, -2, 2]: + prefixes.append(f"{prefix[:6]}{table_num + delta:03d}") - # 1. Exact code match (if query contains a code like 28-01-037 or 28-01-068) - import re - code_match = re.search(r'(\d{2}-\d{2}-\d{3})', query) - if code_match: - prefix = code_match.group(1) - # Also try nearby table numbers (067 for 068, etc.) - prefixes = [prefix] - table_num = int(prefix.split('-')[2]) - for delta in [-1, 1, -2, 2]: - nearby = f"{prefix[:6]}{table_num + delta:03d}" - prefixes.append(nearby) + exact = [n for n in search.norms + if any(n["code"].startswith(p) for p in prefixes)] + if exact: + lines.append("\n\n## НОРМЫ ГЭСН (по коду):") + for n in exact[:10]: + lines.append( + f"- ГЭСН {n['code']}: {n['name']}. " + f"Единица измерения: {n['unit']}." + ) - exact = [n for n in search.norms - if any(n["code"].startswith(p) for p in prefixes)] - if exact: - lines.append("\n\n## НОРМЫ ГЭСН (по коду):") - for n in exact[:10]: - lines.append( - f"- ГЭСН {n['code']}: {n['name']}. " - f"Единица измерения: {n['unit']}." - ) - - # 2. Semantic search + # 2. Semantic search (may fail on API timeout — non-critical) + try: results = search.find(query, top_k=top_k) if results: lines.append("\n\n## НАЙДЕННЫЕ НОРМЫ ГЭСН (семантический поиск):") @@ -176,11 +177,10 @@ class SmetaAssistant: f"- ГЭСН {r.code}: {r.name}. " f"Единица измерения: {r.unit}. (score={r.score:.2f})" ) - - return "\n".join(lines) if lines else "" except Exception: - pass - return "" + pass # semantic search failed, but exact match may have succeeded + + return "\n".join(lines) if lines else "" def _classify_question(self, question: str) -> str: """Classify question type to prioritize context sources."""