Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : Groq TPM Limit Handling #602

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/llm/groq_client.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
from groq import Groq as _Groq

import time
from src.config import Config

import requests.exceptions

class Groq:
def __init__(self):
config = Config()
api_key = config.get_groq_api_key()
self.client = _Groq(api_key=api_key)

def inference(self, model_id: str, prompt: str) -> str:
chat_completion = self.client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt.strip(),
}
],
model=model_id,
temperature=0
)
def inference(self, model_id: str, prompt: str,retries:int=0) -> str:
try:
chat_completion = self.client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt.strip(),
}
],
model=model_id,
temperature=0,
)
return chat_completion.choices[0].message.content
except requests.exceptions.HTTPError as e:
if e.response.status_code == 429:
if retries <= 10:
retry_after = e.response.headers['retry-after']
print(f"Rate limit exceeded, waiting for {int(retry_after)+1} seconds, then retrying...")
time.sleep(int(retry_after)+1)

return chat_completion.choices[0].message.content
return self.inference(model_id, prompt,retries+1)
else:
raise RuntimeError(e.response.text)
else:
raise RuntimeError(e.response.text)