OpenAI GPT-3 API error: "Request timed out"
Asked Answered
Z

2

9

I keep get an error as below

Request timed out: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=600)

when I run the code below

def generate_gpt3_response(user_text, print_output=False):
    """
    Query OpenAI GPT-3 for the specific key and get back a response
    :type user_text: str the user's text to query for
    :type print_output: boolean whether or not to print the raw output JSON
    """
    time.sleep(5)
    completions = ai.Completion.create(
        engine='text-davinci-003',  # Determines the quality, speed, and cost.
        temperature=0.5,            # Level of creativity in the response
        prompt=user_text,           # What the user typed in
        max_tokens=150,             # Maximum tokens in the prompt AND response
        n=1,                        # The number of completions to generate
        stop=None,                  # An optional setting to control response generation
    )

    # Displaying the output can be helpful if things go wrong
    if print_output:
        print(completions)

    # Return the first choice's text
    return completions.choices[0].text
df_test['GPT'] = df_test['Q20'].apply(lambda x: \
              generate_gpt3_response\
              ("I am giving you the answer of respondents \
                in the format [Q20], \
                give me the Broader topics like customer service, technology, satisfaction\
                or the related high level topics in one word in the \
                format[Topic: your primary topic] for the text '{}' ".format(x)))

# result
df_test['GPT'] = df_test['GPT'].apply(lambda x: (x.split(':')[1]).replace(']',''))

I tried modifiying the parameters, but the error still occurs.

Anyone experienced the same process?

Thanks in advance.

Zecchino answered 20/3, 2023 at 7:43 Comment(0)
G
4

As stated in the official OpenAI documentation:

A Timeout error indicates that your request took too long to complete and our server closed the connection. This could be due to a network issue, a heavy load on our services, or a complex request that requires more processing time.

If you encounter a Timeout error, please try the following steps:

  • Wait a few seconds and retry your request. Sometimes, the network congestion or the load on our services may be reduced and your request may succeed on the second attempt.
  • Check your network settings and make sure you have a stable and fast internet connection. You may need to switch to a different network, use a wired connection, or reduce the number of devices or applications using your bandwidth.
  • If the issue persists, check out our persistent errors next steps section.
Goldenrod answered 20/3, 2023 at 9:29 Comment(0)
D
4

as mentioned in the previous response, the reasons for the timeout error can be due to various factors. By default, the API is configured to 600 seconds. One solution that has worked for me is to decrease the response time by using the "request_timeout" parameter to 10 or 15 seconds and then iterate the API call using a while loop. You can also program a pause to give more time to the server. Note that in my case, I'm using the GPT API from Azure, so the name of the parameter may differ in CHAT-GPT. I recommend checking the parameters of the method, such as using dir(openai.ChatCompletion()).

import openai
import json 
import time
 
retries = 3    
while retries > 0:    
     try: 
         response = openai.ChatCompletion.create(  
             engine="xxxxxxxxxxxx", 
             messages=[{"role":"system","content":"xxxxxxxxxxx"},  
                          {"role":"user","content":"xxxxxxxxxxxxx: {}! ".format(coment)}],  
             temperature=0.1,  
             max_tokens=4000,
             request_timeout=15,  
             top_p=0.1,  
             frequency_penalty=0.1,  
             presence_penalty=0.1,  
             stop=None) 

         data = json.loads(response['choices'][0]['message']['content'])     
         return data  
    except Exception as e:    
         if e: 
             print(e)   
             print('Timeout error, retrying...')    
             retries -= 1    
             time.sleep(5)    
         else:    
             raise e    
 print('API is not responding, moving on...')   
 bad_api = "x"  
 return bad_api
Dasha answered 2/8, 2023 at 19:37 Comment(2)
Is there any similar option for the image create API? It throws error if request_timeout parameter is set.Whorish
Note that sending three retries will cost you three times more money.Sprit

© 2022 - 2024 — McMap. All rights reserved.