Imagine you have 50000 data points that you have to process using OpenAI’s GPT-4o-mini api , what would be the possible solutions that we can derive ?
One that comes to the top of the mind would be to setup a loop , If we utilise this method , it has too many drawback’s
If your code is deployed on a server , it will keep on executing for 50K requests and morever 50000 data points meaning , it would be a lot of tokens to proess which may incur a hefty amount, you can refer to the price chart given by OpenAI.
If while processing the requests with above method , your code throws any error , then further execution would be stopped .
Note : If you setup the pipeline for 50K requests during a night time , hoping processing would be completed by the time you wake up , it may go other way and you may wake up with just 5000 requests completed , which may be really annoying .
To avoid all of the above and plus get a 50% off on each request you make , there would be a 50% of total cost reduction for your entire data , which sounds like a good deal .
Yes you can achieve this by Batch processing service that OpenAI provides you with a 50% cost reduction and also with seperate pool of higher rate limits.
So lets dive into this how can we implement the batch processing .
Setup
Loading Data Here you can have a custom implementation for fetching data from your database .
I have utilised simple IMDB dataset .
Creating a Batch File The batch file, in the jsonl format, should contain one line (json object) per request. Each request is defined as such:
Note: the request ID should be unique per batch. This is what you can use to match results to the initial input files, as requests will not be returned in the same order.
Here comes the main part where you can directly load the json data into the memory.
Above code will ensure that you don’t have to save the data in any json file , most of the implementation on the internet would tell to save the data into a .json file .
But this will help to run this batch processing code even on server and you can actually automate your batch processing with the help of cron jobs and triggers.
Let’s move onto the next step .
Uploading the in Memory Json Object
Creating the batch job
You can save the above data into your data base and you can utilise a cron job to check the status of the batch job completion .
Checking if the batch has been completed or not
You can either utilise the above code or setup a cron job on cloud services to check if the batch processing has been completed or not .
Retrieving results
Now you can extract the output json data by directly decoding it into the memory , so that this can be utilised to setup a pipeline on the server.
Here res (List) contains the output json of each request stored as seperate element .
Now you can traverse through this res(List) and extract your outputs , input , output tokens and the custom id which can help you to identify each processed data point .
Now once you have decoded the outputs and prompt and completion tokens , you can store this data into your database .
Batch processing is a reliable approach to handle large amounts of data, which enables the organisations to automate workflows, streamline data processing, and manage costs more effectively .
Stay tuned ! As I would be uploading a part 2 of the blog on how you can setup a entire automated batch processing pipeline utilising supabase , OpenAI batching service , supabase CRUD and supabase cron job.
View All Blogs
Connect with Hushh
Say something to reach out to us
Connect with hushh
Say something to reach out to us