v1.7.0: refactor max_bot to flat structure, add VCF+UserModel+NLP history context, portal pages and proxy fixes
- Refactored max_bot from nested packages to flat module structure - Q2: Extended BotUser model (patronymic, email, org, address, vcf_raw, contact_hash, phone_verified, email_verified, last_interaction, total_conversations, total_tickets) - Q2: VCF parser (FN, N, TEL, EMAIL, ORG, ADR), upsert on re-contact, NLP history context (_get_user_history_context -> YandexGPT) - Q1: Broadcast preview modal with 10s confirmation timer - Q3: CSS var(--white)->var(--bg-card), var(--text)->var(--text-primary) - Q4: bot_settings showNotification(), editable max_bot_id - Q5: Webhook secret passthrough via X-Max-Bot-Api-Secret - Masking sensitive keys, dialog_cleared handler, migrate via _add_column_if_not_exists() - Rate limit (asyncio.sleep 0.5 per 10), dead code removed, conv.intent context in contact.py - Portal pages: bot_consent, bot_kb (edit), bot_settings, bot_test, bot_tickets, portal_settings - Tests: 21/21 passing, added test_yandex_gpt.py, test_email_sender.py - Deploy: deploy_full.sh, schema.sql, seed_knowledge_base.sql
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import httpx
|
||||
import json
|
||||
import logging
|
||||
from typing import Optional
|
||||
from app.config import settings
|
||||
from app.settings_cache import settings_cache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
YANDEX_GPT_URL = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
||||
YANDEX_GPT_MODEL = "yandexgpt-lite"
|
||||
|
||||
|
||||
class YandexGPT:
|
||||
def __init__(self):
|
||||
self._api_key: Optional[str] = None
|
||||
self._folder_id: Optional[str] = None
|
||||
|
||||
async def _load_credentials(self) -> bool:
|
||||
self._api_key = await settings_cache.get("yandex_gpt_key")
|
||||
self._folder_id = await settings_cache.get("yandex_folder_id")
|
||||
return bool(self._api_key and self._folder_id)
|
||||
|
||||
async def is_available(self) -> bool:
|
||||
return await self._load_credentials()
|
||||
|
||||
async def _request(self, prompt: str, system_prompt: str = "") -> Optional[str]:
|
||||
if not await self._load_credentials():
|
||||
return None
|
||||
|
||||
messages = []
|
||||
if system_prompt:
|
||||
messages.append({"role": "system", "text": system_prompt})
|
||||
messages.append({"role": "user", "text": prompt})
|
||||
|
||||
payload = {
|
||||
"modelUri": f"gpt://{self._folder_id}/{YANDEX_GPT_MODEL}",
|
||||
"completionOptions": {
|
||||
"stream": False,
|
||||
"temperature": 0.3,
|
||||
"maxTokens": 1000,
|
||||
},
|
||||
"messages": messages,
|
||||
}
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Api-Key {self._api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
r = await client.post(YANDEX_GPT_URL, headers=headers, json=payload)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
result = data.get("result", {})
|
||||
alternatives = result.get("alternatives", [])
|
||||
if alternatives:
|
||||
return alternatives[0].get("message", {}).get("text", "")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"YandexGPT request failed: {e}")
|
||||
return None
|
||||
|
||||
async def analyze_intent(self, text: str, history_context: str = "") -> str:
|
||||
system_prompt = (
|
||||
"Ты — ассистент София из компании AegisOne Engineering. "
|
||||
"Определи намерение пользователя. Ответь строго одним словом: "
|
||||
"'question' — если это общий вопрос о компании, услугах, контактах; "
|
||||
"'ticket' — если пользователь хочет оставить заявку, заказать услугу, попросить обратный звонок; "
|
||||
"'unknown' — если намерение неочевидно."
|
||||
)
|
||||
if history_context:
|
||||
system_prompt += f"\n\nИстория обращений пользователя:\n{history_context}"
|
||||
result = await self._request(text, system_prompt)
|
||||
if result and result.strip().lower() in ("question", "ticket", "unknown"):
|
||||
return result.strip().lower()
|
||||
return "unknown"
|
||||
|
||||
async def generate_answer(self, question: str, context: str, history_context: str = "") -> Optional[str]:
|
||||
system_prompt = (
|
||||
"Ты — ассистент София, представитель компании AegisOne Engineering. "
|
||||
"Отвечай на вопросы клиентов дружелюбно, профессионально и по делу. "
|
||||
"Используй информацию из базы знаний, но отвечай живым языком, не копируй текст дословно. "
|
||||
"Если информации недостаточно, предложи связаться с компанией по телефону или почте."
|
||||
)
|
||||
if history_context:
|
||||
system_prompt += f"\n\nДополнительный контекст об этом пользователе:\n{history_context}"
|
||||
user_prompt = f"Вопрос клиента: {question}\n\nКонтекст из базы знаний:\n{context}\n\nДай ответ клиенту:"
|
||||
return await self._request(user_prompt, system_prompt)
|
||||
|
||||
async def clarify_intent(self, text: str, history_context: str = "") -> str:
|
||||
system_prompt = (
|
||||
"Ты — ассистент София. "
|
||||
"Пользователь написал нечто неочевидное. "
|
||||
"Ответь дружелюбно, попроси уточнить, хочет ли он: "
|
||||
"1) задать вопрос о компании AegisOne Engineering, "
|
||||
"2) оставить заявку на услугу, "
|
||||
"3) или что-то другое."
|
||||
)
|
||||
if history_context:
|
||||
system_prompt += f"\n\nКонтекст о пользователе:\n{history_context}"
|
||||
result = await self._request(text, system_prompt)
|
||||
return result or "Извините, я не совсем поняла ваш запрос. Вы хотели бы задать вопрос о нашей компании или оставить заявку?"
|
||||
|
||||
|
||||
yandex_gpt = YandexGPT()
|
||||
Reference in New Issue
Block a user