OpenAI GPT-3 API错误:"请求超时"

8
我运行以下代码时,一直出现如下错误:

请求超时:HTTPSConnectionPool(host='api.openai.com', port=443):读取超时。(读取超时=600)

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(']',''))

我尝试修改参数,但错误仍然发生。

有人经历过相同的过程吗?

提前感谢。


热烈欢迎来到SO。请尽量使用正确的大写字母,例如在标题、句子开头或“I”字等处。这将使您的读者更舒适。请阅读“如何提问”和“最小可重现示例”,然后使用代码更新您的问题,展示您迄今为止的尝试。同时,请使用正确的术语。“AI”是一种营销用语。您可以寻找类似于模式匹配或机器学习的东西。 - buhtz
2个回答

3
如前面的回答中提到的,超时错误的原因可能是多种因素导致的。默认情况下,API 的配置为 600 秒。我找到的一个解决方法是通过使用 "request_timeout" 参数将响应时间减少到 10 或 15 秒,然后使用 while 循环迭代 API 调用。您还可以编程设置暂停时间以给服务器更多时间。请注意,在我的情况下,我正在使用 Azure 的 GPT API,因此该参数的名称可能在 CHAT-GPT 中有所不同。我建议检查该方法的参数,例如使用 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

是否有类似的选项适用于图像创建API?如果设置了request_timeout参数,它会抛出错误。 - undefined

2
根据官方OpenAI文档所述:

Timeout错误表示您的请求花费的时间太长,导致我们的服务器关闭了连接。这可能是由于网络问题、我们服务的负载过重或需要更多处理时间的复杂请求引起的。

如果遇到Timeout错误,请尝试以下步骤:

  • 等待几秒钟并重试您的请求。有时候,网络拥堵或我们服务的负载可能会减少,第二次尝试可能会成功。
  • 检查您的网络设置,并确保您拥有稳定且快速的互联网连接。您可能需要切换到不同的网络、使用有线连接或减少使用带宽的设备或应用程序数量。
  • 如果问题仍然存在,请查看我们的persistent errors下一步操作部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接