Загрузка данных


import json
import urllib.request
import urllib.error

API_URL = "https://api.deepseek.com/v1/chat/completions"

# Получаем ключ один раз при запуске
API_KEY
="sk-cbdbd560c90243f8a09e5c4ee02dbf2b".strip()
if not API_KEY:
    print("Ключ не введён. Выход.")
    exit(1)

def ask_deepseek(question: str) -> str | None:
    """Отправляет запрос к DeepSeek API и возвращает ответ."""
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}"
    }
    payload = {
        "model": "deepseek-reasoner",        # можно заменить на "deepseek-reasoner" для сложных задач
        "messages": [
            {"role": "user", "content": question}
        ],
        "temperature": 0.7,
        "max_tokens": 4000
    }

    data = json.dumps(payload).encode("utf-8")
    req = urllib.request.Request(API_URL, data=data, headers=headers, method="POST")

    try:
        with urllib.request.urlopen(req) as resp:
            result = json.loads(resp.read().decode("utf-8"))
            return result["choices"][0]["message"]["content"]
    except urllib.error.HTTPError as e:
        err = e.read().decode()
        print(f"\nОшибка HTTP {e.code}: {err}")
        return None
    except Exception as e:
        print(f"\nНе удалось выполнить запрос: {e}")
        return None

print("\nВводите условие задачи (для выхода введите 'exit').")
while True:
    task = input("\n>>> ")
    if task.lower() == "exit":
        print("До встречи!")
        break
    if not task.strip():
        continue

    print("Решаю...")
    answer = ask_deepseek(task)
    if answer:
        print("\n" + "="*60)
        print(answer)
        print("="*60)