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

Automate Your Marketing Audits - Say Goodbye to Manual Checklists

Automate Your Marketing Audits - Say Goodbye to Manual Checklists

Featured
Google Reports Solid Q4 Growth, Reveals Massive AI Infrastructure Plans

Google Reports Solid Q4 Growth, Reveals Massive AI Infrastructure Plans

OpenAI launches deep research in ChatGPT to enhance complex research capabilities Trending ️‍🔥

OpenAI launches deep research in ChatGPT to enhance complex research capabilities

OpenAI
OpenAI

Official Source

Official Source

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

Official Source
Google Tests AI-Powered 'Ask for Me' Feature for Local Business Calls

Google Tests AI-Powered 'Ask for Me' Feature for Local Business Calls

Google
Google

Official Source

Official Source

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

Official Source
OpenAI launches Operator an AI agent for autonomous task execution Trending ️‍🔥

OpenAI launches Operator an AI agent for autonomous task execution

OpenAI
OpenAI

Official Source

Official Source

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

Official Source
Brave Search Introduces Rerank Feature for Personalized Search Results

Brave Search Introduces Rerank Feature for Personalized Search Results

Google Search Removes Breadcrumb from Mobile Results

Google Search Removes Breadcrumb from Mobile Results

Google Search Central
Google Search Central

Official Source

Official Source

Google Search Central is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Updates Site Reputation Abuse Policy with Clarified Guidelines

Google Updates Site Reputation Abuse Policy with Clarified Guidelines

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

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
Screaming Frog logo

Screaming Frog

Comprehensive SEO audits with real-time crawling

SEO

Get Featured Here

Showcase your tool in this list.

Contact Us