Сделай код более грамотным, асинхронным без блокировки кода асинхронного и добавь код который заменяет куки если было сделано более 99 запросов. И сделай вместо двух запросов один с промтом и в случае ошибок возращай вместо текста - текст ошибки: import requests
import json
import time
import uuid
class yandexgpt:
    @classmethod
    def __init__(cls):
        
        cls.headers = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Accept-Language": "ru-RU,ru;q=0.9",
        "Cache-Control": "no-cache",

        "Content-Type": "application/json",
        
        "Device-Memory": "8",
        "Downlink": "5.4",
        "DPR": "1.5",
        "ECT": "4g",
        "Origin": "https://ya.ru",
        "Pragma": "no-cache",
        "Priority": "u=1, i",
        "Referer": "https://ya.ru/search/?text=%D1%87%D1%82%D0%BE+%D0%B7%D0%B0+%D1%80%D1%8B%D0%B1%D0%B0%D0%BB%D0%BA%D0%B0+%D1%81+%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1%82%D1%8E+%D1%83%D0%B4%D0%BE%D1%87%D0%BA%D0%B8&lr=47&search_source=yaru_desktop_common&search_domain=yaru",
        "RTT": "100",
        "Sec-CH-UA": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
        "Sec-CH-UA-Arch": "\"x86\"",
        "Sec-CH-UA-Bitness": "\"64\"",
        "Sec-CH-UA-Full-Version": "\"129.0.6668.101\"",
        "Sec-CH-UA-Full-Version-List": "\"Google Chrome\";v=\"129.0.6668.101\", \"Not=A?Brand\";v=\"8.0.0.0\", \"Chromium\";v=\"129.0.6668.101\"",
        "Sec-CH-UA-Mobile": "?0",
        "Sec-CH-UA-Model": "\"\"",
        "Sec-CH-UA-Platform": "\"Windows\"",
        "Sec-CH-UA-Platform-Version": "\"15.0.0\"",
        "Sec-CH-UA-WoW64": "?0",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        "Viewport-Width": "736",
        "X-Requested-With": "XMLHttpRequest",
        "X-Retpath-Y": "https://ya.ru/search/?text=%D1%87%D1%82%D0%BE+%D0%B7%D0%B0+%D1%80%D1%8B%D0%B1%D0%B0%D0%BB%D0%BA%D0%B0+%D1%81+%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1%82%D1%8E+%D1%83%D0%B4%D0%BE%D1%87%D0%BA%D0%B8&lr=47&search_source=yaru_desktop_common&search_domain=yaru"
        }
        cls.session = requests.Session()
        cls.session.get("https://ya.ru",headers=cls.headers)
    @classmethod
    def send_request(cls,prompt):
        url = f"https://ya.ru/neuralsearch/api/send_to_dialog?lr=11091&suggest_reqid=&primary_reqid={time.time()*1000000}-13515373250909564767-balancer-l7leveler-kubr-yp-sas-27-BAL&reqid={time.time()*1000000}-16383473252948964099-balancer-l7leveler-kubr-yp-sas-27-BAL&from=button"
        
        
        body = {
            "UserRequest": prompt
        }
        
        response = cls.session.post(url, headers=cls.headers, json=body)
        print(response.text)
        # Обработка ответа
        if response.ok:
            print("Response received:")
            return response.json()
        else:
            print("Failed to send request:", response.status_code)
    @classmethod
    def fetch_until_final(cls,ResponseMessageId):
        url = f"https://ya.ru/neuralsearch/api/get_fresh_message?lr=11091&suggest_reqid=620775595168845935386354600584464&msid={ResponseMessageId}&reqid={ResponseMessageId}"
        
        

        
        
        body = {
            "ResponseMessageId": ResponseMessageId
        }
        
        while True:
            response = cls.session.post(url, headers=cls.headers, json=body)
            print(response.text)
            data = response.json()
            print(data)
            # Check if the condition is met
            if data.get("IsFinalMarkdownText"):
                print("Final data received:", data)
                break
            else:
                print("Waiting for final data...")
            time.sleep(1)

a=yandexgpt()
b=a.send_request("мой промпт")
a.fetch_until_final(b["ResponseMessageId"])