feat: GLM-5 по умолчанию + typing indicator + время ответа

- LLM: GLM-5 (бесплатный, 100% точность на тесте)
- Typing: бот показывает "печатает..." пока думает
- Время ожидания: "~5 сек" для таблиц, "~15-20 сек" для норм
- Время ответа: отображается в конце (⏱ X.Xс)
- Промежуточное сообщение удаляется после ответа

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-23 02:20:14 +10:00
co-authored by Claude Opus 4.6
parent 86242561d5
commit f93c329f08
+32 -11
View File
@@ -43,8 +43,8 @@ def _get_assistant():
"""Lazy-load SmetaAssistant."""
from smeta_railway.ai.rag import SmetaAssistant
index_dir = Path(__file__).parent / "index"
# Use OpenAI GPT-4o for quality (fallback to GLM-5 if no key)
provider = "openai" if os.environ.get("OPENAI_API_KEY") else "glm"
# GLM-5 by default (free, 100% accuracy), OpenAI as fallback
provider = "glm"
return SmetaAssistant(rag_index_dir=index_dir, llm_provider=provider)
@@ -110,7 +110,7 @@ async def search_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Укажите описание работ. Пример: /search очистка щебня")
return
await update.message.reply_text("🔍 Ищу...")
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action="typing")
try:
search = get_search()
@@ -142,22 +142,42 @@ async def ask_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Задайте вопрос. Пример: /ask какой НР для ж/д работ?")
return
await update.message.reply_text("🤔 Думаю...")
import time
# Classify to estimate wait time
assistant = get_assistant()
q_type = assistant._classify_question(question)
if q_type == "cag":
wait_msg = await update.message.reply_text("🤔 Ищу в таблицах... ~5 сек")
else:
wait_msg = await update.message.reply_text("🤔 Ищу в базе норм и документах... ~15-20 сек")
# Show typing indicator
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action="typing")
try:
assistant = get_assistant()
t0 = time.time()
answer = assistant.ask(question)
elapsed = time.time() - t0
if len(answer) > 4000:
answer = answer[:3900] + "\n\n... (ответ сокращён)"
answer += f"\n\n⏱ {elapsed:.1f}с"
# Delete "thinking" message and send answer
try:
await wait_msg.delete()
except Exception:
pass
await update.message.reply_text(answer)
# Track billing + alert
from smeta_railway.ai.billing import track_usage, check_and_alert
track_usage("gpt-4o", 5000, 500) # approximate tokens
if ADMIN_CHAT_ID:
await check_and_alert(context.bot, ADMIN_CHAT_ID, threshold=5.0)
except Exception as e:
logger.error(f"Ask error: {e}")
try:
await wait_msg.delete()
except Exception:
pass
await update.message.reply_text(f"Ошибка: {e}")
@@ -211,7 +231,8 @@ async def auto_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
)
return
await update.message.reply_text("⚙️ Генерирую смету...")
await update.message.reply_text("⚙️ Генерирую смету... ~30-60 сек")
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action="typing")
try:
from smeta_railway.ai.generator import EstimateGenerator