Clustering SEO Keywords using OpenAI Batch API, Saving 50% in Cost

May 06, 2024 at 11:03:29 AM

TL;DR OpenAI's Batch API allows grouped requests at half the cost, ideal for non-immediate tasks. It's useful for large-scale SEO tasks like keyword clustering. The API works with all OpenAI’s models, including GPT-3.5-turbo-16k and GPT-4-turbo. A Python guide demonstrates how to use the Batch API for clustering SEO keywords, including preparing a batch file in JSONL format, uploading the file, and retrieving results.

Clustering SEO Keywords using OpenAI Batch API, Saving 50% in Cost

OpenAI's Batch API allows users to send grouped requests at half the cost, ideal for tasks that can wait up to 24 hours. It's useful when immediate responses aren't necessary or when rate limits hinder executing many queries quickly.

Batch API is beneficial for scenarios like clustering SEO Keywords, Product Feed Optimization, and Document Summarization. It works with all OpenAI’s models, including GPT-3.5-turbo-16k and GPT-4-turbo.

To use the Batch API, prepare a batch file in JSONL format. For example, to cluster over 150,000 keywords, split this into two requests due to the GPT-4 turbo model's context limit of 128k tokens. Create a list of requests, each formatted as a dictionary containing your SEO keywords.

requests = [
 {
 "custom_id": "keyword_cluster_1",
 "method": "POST",
 "url": "/v1/completions",
 "body": {
 "model": "gpt-4-turbo",
 "prompt": f"{prompt} ['SEO', 'optimization', 'Google ranking']",
 },
 },
 {
 "custom_id": "keyword_cluster_2",
 "method": "POST",
 "url": "/v1/completions",
 "body": {
 "model": "gpt-4-turbo",
 "prompt": f"{prompt} ['backlinks', 'page authority', 'domain score']",
 },
 },
]

Convert your data to a JSONL file and upload it using the file API to obtain a file ID.

import json
from openai import OpenAI
client = OpenAI()
with open('seo_keywords.jsonl', 'w') as file:
 for request in requests:
 file.write(json.dumps(request) + '\n')
with open('seo_keywords.jsonl', 'rb') as file:
 batch_input_file = client.files.create(file=file, purpose='batch')

Submit the batch with one line of code and check the status and retrieve results after 12 to 18 hours.

batch = client.batches.create(input_file_id=batch_input_file.id, endpoint="/v1/completions", completion_window="24h")
status = client.batches.retrieve(batch.id)
if status['status'] == 'completed':
 output = client.files.content(batch.output_file_id)
 print(output)

The Batch API is a cost-effective tool for managing large-scale SEO tasks like keyword clustering, saving time and reducing costs.

Q&A

Have more questions on this topic? Ask our AI assistant for in-depth insights.

The Only Digital Marketing Feed You'll Ever Need.

Stay informed your way. Tailored updates when and how you want them. 100% Free.

10,000+ Users

500+ Sources

1000+ Tools

Or

Related Posts

Google Ads Monthly Slides with AI Insights

Google Ads Monthly Slides with AI Insights

Featured
Markifact
Markifact

Verified Sponsor

Verified Sponsor

Markifact is a Verified Sponsor. Want to get featured here? Contact us.

Verified Sponsor
Google Lighthouse Audits Transition to Performance Insight Audits with Major Changes

Google Lighthouse Audits Transition to Performance Insight Audits with Major Changes

Chrome for Developers
Chrome for Developers

Official Source

Official Source

Chrome for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google corrects Google News crawler documentation error regarding publisher preferences

Google corrects Google News crawler documentation error regarding publisher preferences

Google for Developers
Google for Developers

Official Source

Official Source

Google for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source
ChatGPT Introduces Enhanced Shopping, WhatsApp Access and Improved Citations Trending ️‍🔥

ChatGPT Introduces Enhanced Shopping, WhatsApp Access and Improved Citations

OpenAI
OpenAI

Official Source

Official Source

OpenAI is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Apple updates Applebot documentation clarifying crawler differences and data usage

Apple updates Applebot documentation clarifying crawler differences and data usage

Apple
Apple

Official Source

Official Source

Apple is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google updates documentation for Google-Extended user agent and product token description

Google updates documentation for Google-Extended user agent and product token description

Google for Developers
Google for Developers

Official Source

Official Source

Google for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google updates Merchant Center documentation on energy efficiency and certification

Google updates Merchant Center documentation on energy efficiency and certification

Google for Developers
Google for Developers

Official Source

Official Source

Google for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google to Drop Support for Special Announcement Structured Data by July 2025

Google to Drop Support for Special Announcement Structured Data by July 2025

Google for Developers
Google for Developers

Official Source

Official Source

Google for Developers is a Official Source. The source has been verified by Swipe Insight team.

Official Source

Related Tools

Markifact logo

Markifact

Verified Tool

Verified Tool

Markifact is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Marketing Workflows Powered by AI

Featured
Marketing Auditor logo

Marketing Auditor

Verified Tool

Verified Tool

Marketing Auditor is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Automated audits for Google Ads and Analytics.

Get Featured Here

Showcase your tool in this list.

Contact Us
Ahrefs logo

Ahrefs

SEO tools to boost traffic and rank higher

SEO
Surfer SEO logo

Surfer SEO

SEO content creation and optimization made easy

SEO
Sitebulb logo

Sitebulb

Efficient website crawler for better SEO audits

SEO
Screpy logo

Screpy

AI-Powered SEO and Web Analysis Simplified

SEO
Blogify logo

Blogify

Convert multimedia to SEO-optimized blogs fast

SEO
Answer the Public logo

Answer the Public

Unlock Consumer Insights for Content Creation

SEO
SEO Writing AI logo

SEO Writing AI

AI-powered SEO content in 1 click

SEO
SEO Stuff logo

SEO Stuff

Affordable SEO tools without monthly fees

SEO

Get Featured Here

Showcase your tool in this list.

Contact Us