49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""Вызов DeepSeek API для диагностики."""
|
|
|
|
import requests
|
|
|
|
DEFAULT_BASE = "https://api.aillm.ru/v1"
|
|
DEFAULT_MODEL = "gpt-oss-20b"
|
|
|
|
|
|
class Diagnoser:
|
|
"""Отправляет данные в DeepSeek и возвращает диагноз."""
|
|
|
|
def __init__(self, api_key: str, model: str = DEFAULT_MODEL, base_url: str = DEFAULT_BASE):
|
|
self.api_key = api_key
|
|
self.model = model
|
|
self.base_url = base_url.rstrip("/")
|
|
|
|
def ask(self, messages: list[dict]) -> str:
|
|
"""Отправляет сообщения в DeepSeek, возвращает текст ответа."""
|
|
resp = requests.post(
|
|
f"{self.base_url}/chat/completions",
|
|
headers={
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"Content-Type": "application/json",
|
|
},
|
|
json={
|
|
"model": self.model,
|
|
"messages": messages,
|
|
"temperature": 0.3, # пониже — меньше фантазий
|
|
"max_tokens": 4096,
|
|
},
|
|
timeout=60,
|
|
)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
return data["choices"][0]["message"]["content"]
|
|
|
|
def diagnose(
|
|
self,
|
|
system: str,
|
|
user_prompt: str,
|
|
history: list[dict] | None = None,
|
|
) -> str:
|
|
"""Полный цикл: system + история + user_prompt → ответ."""
|
|
messages = [{"role": "system", "content": system}]
|
|
if history:
|
|
messages.extend(history)
|
|
messages.append({"role": "user", "content": user_prompt})
|
|
return self.ask(messages)
|