feat: точность 90% (18/20) — GESN search + exact code match в RAG
- rag.py: _search_gesn() добавляет ГЭСН нормы в контекст LLM - Exact code match: если запрос содержит код (28-01-037) - Semantic search: top-5 по FAISS - RAG index: 19591 chunks (9547 PDF + 10044 resources.json) - CAG context: усилены К_ВЛ и резерв - Тест 20 вопросов: 18✅ 0❌ 2⚠️ (было 14✅ 2❌ 4⚠️) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -58,7 +58,7 @@ class SmetaAssistant:
|
|||||||
|
|
||||||
self.system_prompt = SYSTEM_PROMPT.format(cag_context=CAG_CONTEXT)
|
self.system_prompt = SYSTEM_PROMPT.format(cag_context=CAG_CONTEXT)
|
||||||
|
|
||||||
def _retrieve_rag_context(self, query: str, top_k: int = 5) -> str:
|
def _retrieve_rag_context(self, query: str, top_k: int = 8) -> str:
|
||||||
"""Retrieve relevant chunks from RAG index."""
|
"""Retrieve relevant chunks from RAG index."""
|
||||||
if self.rag_index is None:
|
if self.rag_index is None:
|
||||||
return ""
|
return ""
|
||||||
@@ -136,15 +136,57 @@ class SmetaAssistant:
|
|||||||
)
|
)
|
||||||
return response.content[0].text
|
return response.content[0].text
|
||||||
|
|
||||||
|
def _search_gesn(self, query: str, top_k: int = 5) -> str:
|
||||||
|
"""Search GESN norms by semantic similarity + exact code match."""
|
||||||
|
try:
|
||||||
|
from smeta_railway.ai.search import GESNSearch
|
||||||
|
search = GESNSearch()
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
|
||||||
|
# 1. Exact code match (if query contains a code like 28-01-037)
|
||||||
|
import re
|
||||||
|
code_match = re.search(r'(\d{2}-\d{2}-\d{3})', query)
|
||||||
|
if code_match:
|
||||||
|
prefix = code_match.group(1)
|
||||||
|
exact = [n for n in search.norms if n["code"].startswith(prefix)]
|
||||||
|
if exact:
|
||||||
|
lines.append("\n\n## НОРМЫ ГЭСН (точное совпадение по коду):")
|
||||||
|
for n in exact[:5]:
|
||||||
|
lines.append(
|
||||||
|
f"- ГЭСН {n['code']}: {n['name']}. "
|
||||||
|
f"Единица измерения: {n['unit']}."
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. Semantic search
|
||||||
|
results = search.find(query, top_k=top_k)
|
||||||
|
if results:
|
||||||
|
lines.append("\n\n## НАЙДЕННЫЕ НОРМЫ ГЭСН (семантический поиск):")
|
||||||
|
for r in results:
|
||||||
|
lines.append(
|
||||||
|
f"- ГЭСН {r.code}: {r.name}. "
|
||||||
|
f"Единица измерения: {r.unit}. (score={r.score:.2f})"
|
||||||
|
)
|
||||||
|
|
||||||
|
return "\n".join(lines) if lines else ""
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return ""
|
||||||
|
|
||||||
def ask(self, question: str) -> str:
|
def ask(self, question: str) -> str:
|
||||||
"""Answer a question using CAG + RAG hybrid approach."""
|
"""Answer a question using CAG + RAG + GESN search."""
|
||||||
# RAG retrieval
|
# RAG retrieval (PDF chunks + resources.json)
|
||||||
rag_context = self._retrieve_rag_context(question)
|
rag_context = self._retrieve_rag_context(question)
|
||||||
|
|
||||||
|
# GESN search (semantic, always)
|
||||||
|
gesn_context = self._search_gesn(question)
|
||||||
|
|
||||||
# Build messages
|
# Build messages
|
||||||
system = self.system_prompt
|
system = self.system_prompt
|
||||||
if rag_context:
|
if rag_context:
|
||||||
system += rag_context
|
system += rag_context
|
||||||
|
if gesn_context:
|
||||||
|
system += gesn_context
|
||||||
|
|
||||||
messages = [
|
messages = [
|
||||||
{"role": "system", "content": system},
|
{"role": "system", "content": system},
|
||||||
|
|||||||
Reference in New Issue
Block a user