File size: 3,819 Bytes
ba3347a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# import requests
# import time
# import os

# MAX_RETRIES = 3

# def ask_groq(messages_list):
#     headers = {
#         "Authorization": f"Bearer {GROQ_API_KEY}",
#         "Content-Type": "application/json"
#     }
#     data = {
#         "model": "llama-3.3-70b-versatile",
#         "messages": messages_list
#     }

#     for attempt in range(MAX_RETRIES):
#         try:
#             response = requests.post(GROQ_ENDPOINT, headers=headers, json=data, timeout=30)

#             if response.status_code == 429:
#                 retry_after = response.headers.get("Retry-After")
#                 if retry_after:
#                     try:
#                         wait_time = int(retry_after)
#                     except ValueError:
#                         wait_time = 2 ** attempt  # fallback if malformed
#                 else:
#                     wait_time = 2 ** attempt  # fallback if header missing

#                 print(f"๐Ÿ•’ Rate limit hit (429). Retrying in {wait_time} seconds...")
#                 time.sleep(wait_time)
#                 continue

#             response.raise_for_status()
#             return response.json()['choices'][0]['message']['content']

#         except requests.exceptions.RequestException as e:
#             if attempt == MAX_RETRIES - 1:
#                 print(f"Request failed after {MAX_RETRIES} attempts: {e}")
#                 return "โŒ Unable to connect to the AI after multiple attempts. Please try again later."
#             else:
#                 wait_time = 2 ** attempt
#                 print(f"โš ๏ธ Request error. Retrying in {wait_time} seconds...")
#                 time.sleep(wait_time)

#         except KeyError:
#             return "โš ๏ธ Received unexpected response from AI. Please try again."

#     return "โŒ Failed to get a response after multiple attempts."

import httpx  # Use httpx for async requests
import asyncio
import os

GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"

MAX_RETRIES = 3

async def ask_groq(messages_list):
    headers = {
        "Authorization": f"Bearer {GROQ_API_KEY}",
        "Content-Type": "application/json"
    }
    data = {
        "model": "llama-3.3-70b-versatile",
        "messages": messages_list
    }

    async with httpx.AsyncClient(timeout=30.0) as client:
        for attempt in range(MAX_RETRIES):
            try:
                response = await client.post(GROQ_ENDPOINT, headers=headers, json=data)

                if response.status_code == 429:
                    retry_after = response.headers.get("Retry-After")
                    wait_time = int(retry_after) if retry_after and retry_after.isdigit() else 2 ** attempt
                    print(f"๐Ÿ•’ Rate limit hit (429). Retrying in {wait_time} seconds...")
                    await asyncio.sleep(wait_time)
                    continue

                response.raise_for_status()
                return response.json()['choices'][0]['message']['content']

            except httpx.RequestError as e:
                if attempt == MAX_RETRIES - 1:
                    print(f"Request failed after {MAX_RETRIES} attempts: {e}")
                    return "โŒ Unable to connect to the AI after multiple attempts. Please try again later."
                else:
                    wait_time = 2 ** attempt
                    print(f"โš ๏ธ Request error. Retrying in {wait_time} seconds...")
                    await asyncio.sleep(wait_time)

            except (KeyError, IndexError):
                return "โš ๏ธ Received unexpected response from AI. Please try again."

    return "โŒ Failed to get a response after multiple attempts."