import time

import requests
def send_request(msg):
    # url = 'https://api.openai.com/v1/chat/completions'
    # headers = {
    #     'Content-Type': 'application/json',
    #     'Authorization': 'Bearer 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'
    # }
    # data = {
    #     "model": "gpt-3.5-turbo",
    #     "messages": [{
    #         "role": "user",
    #         "content": msg
    #     }]
    # }
    # res = requests.post(url=url, headers=headers, data=data)

    import os
    import openai
    openai.api_key = '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'

    res = None

    while not res:

        s = f'send request >>> {msg}'
        print(s)

        try:

            res = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": msg
                    }
                ]
            )
        except Exception as e:
            msg = f'!!! --- OpenAI send_request fail = {str(e)} > sleep 60sec'
            print(msg)
            time.sleep(60)
            # send_request(msg)

        if res and 'OpenAI account' in res['choices'][0]['message']['content']:
            msg = f"!!! --- OpenAI send_request fail = {str(res['choices'][0]['message']['content'])} > sleep 60sec"
            print(msg)
            res = None
            time.sleep(60)




    return res