Optimize hot paths and reuse HTTP sessions
This commit is contained in:
@@ -7,6 +7,7 @@ import requests
|
||||
import re
|
||||
from .config import PERPLEXITY_API_KEY, PERPLEXITY_MODEL, PERPLEXITY_API_URL
|
||||
|
||||
_HTTP = requests.Session()
|
||||
|
||||
# Системный промпт (инструкция) для AI.
|
||||
# Задает личность ассистента: имя "Александр", стиль общения, краткость.
|
||||
@@ -53,7 +54,7 @@ def _send_request(messages, max_tokens, temperature, error_text):
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
response = _HTTP.post(
|
||||
PERPLEXITY_API_URL, headers=headers, json=payload, timeout=15 # Уменьшаем таймаут
|
||||
)
|
||||
response.raise_for_status() # Проверка на ошибки HTTP (4xx, 5xx)
|
||||
@@ -134,16 +135,16 @@ def ask_ai_stream(messages_history: list):
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
response = _HTTP.post(
|
||||
PERPLEXITY_API_URL, headers=headers, json=payload, timeout=15, stream=True # Уменьшаем таймаут
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
import json
|
||||
|
||||
for line in response.iter_lines():
|
||||
for line in response.iter_lines(decode_unicode=True):
|
||||
if line:
|
||||
line_text = line.decode("utf-8")
|
||||
line_text = line
|
||||
if line_text.startswith("data: "):
|
||||
data_str = line_text[6:] # Skip "data: "
|
||||
if data_str == "[DONE]":
|
||||
|
||||
Reference in New Issue
Block a user