Optimizing Bulk Tag Generation for Thousands of Blog Topics Using AI: Seeking Efficient Solutions

Hello everyone,
We have a use case in our company where we want to use AI. It’s about the following, we have about 5000 different topics for blog posts or magazines like:

“Food & Beverage - All about sugar”
“Food & Beverage - Hail the potato!”
"Health - Foot Care for Diabetes
“Home & Garden - Garden: Preserving the harvest”

…and so on…

Our goal is to create search tags for each topic, which we can then store in each article. Our previous approach was to create a file with about 2000 tags, e.g. single words like “sugar”, “wheat christmas”, “tomatoes” and so on with about 2000 tags in it. We then went and created embeddings for our headings like “Food & Beverage - All about sugar” and used cosine similarity to calculate these against the embeddings of the 2000 tags like “sugar”, “christmas”, “tomatoes” and then had the 10 most similar ones output. This actually works quite well, but sometimes there are some outliers in the generated top 10 tags due to embeddings.

That’s why we now want to send our 5000 different headings to the Chat Completion Api so that it gives us exact tags for each heading. This worked best in our tests and the tags were very good.

Now my question is, is there a way how I can send 5000 headlines at once to the chat completion to get the generated tags from the AI? Because if I send them one by one then I am making 5000 requests to the API which would take forever. I also tried aysnc but again I can only send a maximum of 80 at a time which doesn’t really get me anywhere. How can this be solved?

My Code for async:

# Read the Excel file
df = pd.read_excel(file_path)

df['Query'] = df.apply(lambda row: f"{row['Branche']} - {row['Thema']}", axis=1)
queries = df['Query'].tolist()
print(queries)
test_queries = queries[100:1500]

task_description = """
Task: You need to provide the top 10 single-word tags in German for the given subject.

Instructions:

    Deliver exactly 10 tags.
    Each tag should be a single word.
    The tags should be highly relevant and specific to the subject.

Example:
If the subject is "Künstliche Intelligenz":

    KI
    Maschinelleslernen
    Algorithmen
    Datenanalyse
    Automatisierung
    NeuronaleNetze
    DeepLearning
    Technologie
    Innovation
    Zukunft

Query: [Your subject here]
"""

async def get_completion(query):
    async with aiohttp.ClientSession() as session:
        async with session.post('https://api.openai.com/v1/chat/completions', json={
            'model': 'gpt-4',
            'messages': [
                {'role': 'system', 'content': task_description},
                {'role': 'user', 'content': query}
            ],
        }, headers={
            'Authorization': f'Bearer {openai.api_key}',
            'Content-Type': 'application/json'
        }) as response:
            result = await response.json()
            return result['choices'][0]['message']['content']

async def main():
    start_time = time.time()  # Start time
    tasks = [get_completion(query) for query in test_queries]
    responses = await asyncio.gather(*tasks)
    end_time = time.time()  # End time
    print(f"Total time taken: {end_time - start_time:.2f} seconds")
    with open("output_keywords.txt", "w", encoding="utf-8") as file:
        for i, response in enumerate(responses):
            
            file.write(f"{response}\n")
         
    
   

asyncio.run(main())

Welcome to the Forum!

Have you considered using the batch endpoint for this task?
https://platform.openai.com/docs/guides/batch/overview

Hey :slight_smile:

Thanks for your reply! Unfortunately our employees who would use the tool would need the tags back after a few minutes, they cannot wait a day. But thanks for the help!

2 Likes

Batch processing can handle up to 5,000 requests at a time.
The time required depends on the volume of system and user messages. So, it does not necessarily take a full day.

Why not try batch processing once to see how long it takes?

Also, if you are using chat completion, the higher the tier, the more requests you can send in a minute.

https://platform.openai.com/docs/guides/rate-limits/tier-5-rate-limits

1 Like

Yes, the batch request works and its faster then 24 hours you are right. But still the people who work with our tool need it fast like 1-10 min.
But thanks anyway :slight_smile:

1 Like