ускоренная работа
This commit is contained in:
@@ -93,6 +93,63 @@ def ask_ai(messages_history: list) -> str:
|
||||
return response
|
||||
|
||||
|
||||
def ask_ai_stream(messages_history: list):
|
||||
"""
|
||||
Generator that yields chunks of the AI response as they arrive.
|
||||
"""
|
||||
if not messages_history:
|
||||
yield "Извините, я не расслышал вашу команду."
|
||||
return
|
||||
|
||||
# Log the last user message
|
||||
last_user_message = "Unknown"
|
||||
for msg in reversed(messages_history):
|
||||
if msg["role"] == "user":
|
||||
last_user_message = msg["content"]
|
||||
break
|
||||
print(f"🤖 Запрос к AI (Stream): {last_user_message}")
|
||||
|
||||
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + list(messages_history)
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {PERPLEXITY_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
payload = {
|
||||
"model": PERPLEXITY_MODEL,
|
||||
"messages": messages,
|
||||
"max_tokens": 500,
|
||||
"temperature": 1.0,
|
||||
"stream": True, # Enable streaming
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
PERPLEXITY_API_URL, headers=headers, json=payload, timeout=30, stream=True
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
import json
|
||||
|
||||
for line in response.iter_lines():
|
||||
if line:
|
||||
line_text = line.decode("utf-8")
|
||||
if line_text.startswith("data: "):
|
||||
data_str = line_text[6:] # Skip "data: "
|
||||
if data_str == "[DONE]":
|
||||
break
|
||||
try:
|
||||
data_json = json.loads(data_str)
|
||||
content = data_json["choices"][0]["delta"].get("content", "")
|
||||
if content:
|
||||
yield content
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
except Exception as e:
|
||||
print(f"❌ Streaming Error: {e}")
|
||||
yield "Произошла ошибка связи."
|
||||
|
||||
|
||||
def translate_text(text: str, source_lang: str, target_lang: str) -> str:
|
||||
"""
|
||||
Запрос к AI в режиме перевода.
|
||||
|
||||
Reference in New Issue
Block a user