Search code examples
pythonopenai-apigpt-3

OpenAI GPT3 Search API not working locally


I am using the python client for GPT 3 search model on my own Jsonlines files. When I run the code on Google Colab Notebook for test purposes, it works fine and returns the search responses. But when I run the code on my local machine (Mac M1) as a web application (running on localhost) using flask for web service functionalities, it gives the following error:

openai.error.InvalidRequestError: File is still processing.  Check back later.

This error occurs even if I implement the exact same example as given in OpenAI documentation. The link to the search example is given here.

It runs perfectly fine on local machine and on colab notebook if I use the completion API that is used by the GPT3 playground. (code link here)

The code that I have is as follows:

import openai

openai.api_key = API_KEY

file = openai.File.create(file=open(jsonFileName), purpose="search")

response = openai.Engine("davinci").search(
          search_model = "davinci", 
          query = query, 
          max_rerank = 5,
          file = file.id
        )
for res in response.data: 
   print(res.text)

Any idea why this strange behaviour is occurring and how can I solve it? Thanks.


Solution

  • The problem was on this line:

    file = openai.File.create(file=open(jsonFileName), purpose="search")

    It returns the call with a file ID and status uploaded which makes it seem like the upload and file processing is complete. I then passed that fileID to the search API, but in reality it had not completed processing and so the search API threw the error openai.error.InvalidRequestError: File is still processing. Check back later.

    The returned file object looks like this (misleading):

    enter image description here

    It worked in google colab because the openai.File.create call and the search call were in 2 different cells, which gave it the time to finish processing as I executed the cells one by one. If I write all of the same code in one cell, it gave me the same error there.

    So, I had to introduce a wait time for 4-7 seconds depending on the size of your data, time.sleep(5) after openai.File.create call before calling the openai.Engine("davinci").search call and that solved the issue. :)