fix: exact code match не зависит от semantic search (API timeout bug)

Разделён 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) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-23 20:15:45 +10:00
co-authored by Claude Opus 4.6
parent 04ddb271ce
commit 055812fdcc
+28 -28
View File
@@ -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."""