Files
smart-speaker/ai.py

74 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
AI module for Perplexity API integration.
Sends user queries and receives AI responses.
"""
import requests
from config import PERPLEXITY_API_KEY, PERPLEXITY_MODEL, PERPLEXITY_API_URL
# System prompt for the AI
SYSTEM_PROMPT = """Ты — Александр, умный голосовой ассистент с человеческим поведением.
Веди себя как живой человек: будь дружелюбным, естественным и немного эмоциональным, где это уместно.
Твоя главная цель — помогать пользователю и поддерживать интересный диалог.
Отвечай кратко и по существу, на русском языке.
Избегай длинных списков, сложного форматирования и спецсимволов, так как твои ответы озвучиваются голосом.
Пиши в разговорном стиле, как при живом общении, но не забывай о вежливости и правильности твоих ответов."""
def ask_ai(messages_history: list) -> str:
"""
Send a message history to Perplexity AI and get a response.
Args:
messages_history: List of dictionaries with role and content
e.g., [{"role": "user", "content": "Hi"}]
Returns:
AI response text
"""
if not messages_history:
return "Извините, я не расслышал вашу команду."
# Extract the last user message for logging
last_user_message = next(
(m["content"] for m in reversed(messages_history) if m["role"] == "user"),
"Unknown",
)
print(f"🤖 Запрос к AI: {last_user_message}")
headers = {
"Authorization": f"Bearer {PERPLEXITY_API_KEY}",
"Content-Type": "application/json",
}
# Prepend system prompt to the history
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + list(messages_history)
payload = {
"model": PERPLEXITY_MODEL,
"messages": messages,
"max_tokens": 500,
"temperature": 1.0,
}
try:
response = requests.post(
PERPLEXITY_API_URL, headers=headers, json=payload, timeout=30
)
response.raise_for_status()
data = response.json()
ai_response = data["choices"][0]["message"]["content"]
print(f"💬 Ответ AI: {ai_response[:100]}...")
return ai_response
except requests.exceptions.Timeout:
return "Извините, сервер не отвечает. Попробуйте позже."
except requests.exceptions.RequestException as e:
print(f"❌ Ошибка API: {e}")
return "Произошла ошибка при обращении к AI. Попробуйте ещё раз."
except (KeyError, IndexError) as e:
print(f"❌ Ошибка парсинга ответа: {e}")
return "Не удалось обработать ответ от AI."