I have open ai key for gpt4o mini and i am doing request.get in a class instance. It is giving 429 error

Divyansh Rajput 0 Reputation points
2024-08-02T11:38:26.2466667+00:00

I think the issue is with max_tokens where if i am giving it greater than 100 it is giving 429 error.
Following my code:

import requests
api_base = xxxx
api_key = xxxx
api_version = xxxx

# Define the URL for the request
url = xxx

# Define the headers
headers = {
    "Content-Type": "application/json",
    "api-key": api_key
        }
        
class OpenAI:
    def __init__(self):
        pass

    def generate_text(self, input_text, model_param=None, do_sample=True):
        
        if model_param is None:
            model_param = {}

        if do_sample:
            payload = {
                "messages": [
                    {"role": "user", "content": input_text}
                ],
                "temperature": model_param.get("temperature", 0.7),
                "max_tokens": model_param.get("max_length", 1000),
                "top_p": model_param.get("top_p", 0.95),
                "top_k": model_param.get("top_k", 50)
            }
        else:
            payload = {
                "messages": [
                    {"role": "user", "content": input_text}
                ],
                "max_tokens": model_param.get("max_length", 100)
            }
        
        # Send the POST request
        response = requests.post(url, headers=headers, json=payload)
        
        # Check if the request was successful
        if response.status_code == 200:
            result = response.json()
            # Return the 'content' field from the response
            return result['choices'][0]['message']['content'].strip()
        else:
            # Handle the error 
            response.raise_for_status()


open_ai = OpenAI()
input_text = "Who are you?" 
model_param = {"temperature": 0.8}
response_text = open_ai.generate_text(input_text=input_text, model_param=model_param)
print(response_text)

Error:
HTTPError: 429 Client Error: Too Many Requests for url: https://nddevplatforma2586613460.cognitiveservices.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2023-03-15-preview
Here My curl is working is working but above python code is showing 429 error:

curl --location xxx \
--header 'Content-Type: application/json' \
--header 'api-key: xxx' \
--data '{
  "messages": [
    {"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
    {"role": "user", "content": "Who were the founders of Microsoft?"}
  ]
}'
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,101 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.