Getting rate limit error even after having enough credits in account

I Have made a function to identify product name and its category. But i want to run that on 1.5 lakh items but after 700 items i got an error -

RateLimitError: Error code: 429 - {‘error’: {‘message’: ‘Rate limit reached for gpt-3.5-turbo in organization org-4yoElpWysEHGS0NRIdabJR2l on tokens per min (TPM): Limit 60000, Used 59716, Requested 801. Please try again in 517ms. Visit https://platform.openai.com/account/rate-limits to learn more.’, ‘type’: ‘tokens’, ‘param’: None, ‘code’: ‘rate_limit_exceeded’}}

I have used this function -

import pandas as pd
from multiprocessing import Pool
from openai import OpenAI
import logging

Configure logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(name)

Initialize OpenAI client

client = OpenAI(api_key="api-key)

Define function for checking product names

def check_for_product_name(sentence):
try:
response = client.chat.completions.create(
model=“gpt-3.5-turbo-0125”,
messages=[
{
“role”: “user”,
“content”: f"Determine if the following sentence contains a product name and product category:\n\nText:{sentence}\ncontains:\nProduct Name: \nProduct Category:"
},
{
“role”: “system”,
“content”: “You are an expert Product person, and knows how to identify if sentence contains a product name or not. And if doesnot contain product name then return none in product name and product category\n\nExample 1:\nDetermine if the following sentence contains a product name and product category:\n\nText:Under ₹699 | Combo packs | Amazon brands & more\ncontains:No\nProduct Name: none\nProduct Ctegory:none\n\nExample 2:\nDetermine if the following sentence contains a product name:\n\nText:4K TVs | Up to 24 months No Cost EMI\ncontains:No\nProduct Name: none\nProduct Ctegory:none\n\nExample 3:\nDetermine if the following sentence contains a product name:\n\nText:Mi 11 Lite 5G\ncontains:Yes\nProduct Name: Mi 11 Lite 5G\nProduct Ctegory: Smartphone\n\nExample 4:\nDetermine if the following sentence contains a product name:\n\nText:moto g54 5g\ncontains:Yes\nProduct Name: Moto G54\nProduct Ctegory: Smartphone/Cell phone\n\nExample 5:\nDetermine if the following sentence contains a product name:\n\nText:moto g54 5g\ncontains:Yes\nProduct Name: Acer Nitro VG271U M3\nProduct Ctegory: Monitor\n\nExample 6:\nDetermine if the following sentence contains a product name and product category:\n\nText:ZEBRONICS Zeb-Jaguar Wireless Mouse, 2.4GHz with USB Nano Receiver, High Precision Optical Tracking, 4 Buttons, Plug & Play, Ambidextrous, for PC/Mac/Laptop (Black+Grey)\ncontains:Yes\nProduct Name: ZEBRONICS Zeb-Jaguar Wireless Mouse\nProduct Ctegory: Computer Accessories/Mouse\n\nExample 7:\nDetermine if the following sentence contains a product name and product category:\n\nText:Up to 75% off | boAt\ncontains: No\nProduct Name: none\nProduct Ctegory: none\n\nExample 8:\nDetermine if the following sentence contains a product name and product category:\n\nText: Get the perfect screen size | TVs Starting ₹6,999\ncontains: No\nProduct Name: none\nProduct Ctegory: none\n\nExample 9:\nDetermine if the following sentence contains a product name and product category:\n\nText: Men’s Footwear: Bata, Hush Puppies and more\ncontains: No\nProduct Name: none\nProduct Category: none\n\nExample 10:\nDetermine if the following sentence contains a product name and product category:\n\nText: 12FOR COLLECTION MULTIPUPOSE Bathroom Countertop Organizer, 2-Tier Vanity Tray Corner Shelf, Cosmetic Makeup Counter Standing Storage, Detachable Organizer Shelf Holder Kitchen Spice Rack (Black)\ncontains: Yes\nProduct Name: 12FOR COLLECTION MULTIPUPOSE Bathroom Countertop Organizer\nProduct Category: Home & Kitchen/Bathroom Organizer”
}
],
temperature=0,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
except Exception as e:
logger.error(f"Error processing sentence: {sentence}. Error: {str(e)}")
return None

Define function to process each row

def process_row(i):
try:
sentence = str(df[‘text’][i])
logger.info(f"Processing sentence {i}: {sentence}“)
print(i)
time.sleep(1)
pr = check_for_product_name(sentence)
logger.info(f"Processed sentence {i}”)
return str(pr)
except Exception as e:
logger.error(f"Error processing row {i}: {str(e)}")
return None

if name == ‘main’:
try:
# Load DataFrame
df = pd.read_csv(“exterc.csv”)
df[‘output’]=0
# Initialize multiprocessing Pool
with Pool(processes=4) as pool:
# Use pool.map to parallelize execution
outputs = pool.map(process_row, range(len(df)))

    # Update DataFrame with outputs
    df['output'] = outputs

    # Write DataFrame to Excel file
    df.to_excel("amazon_data4.xlsx", index=False)
except Exception as e:
    logger.error(f"An error occurred: {str(e)}")

Answered here: Need help identifying products in sentences using OpenAI API - #15 by idonotwritecode

1 Like