You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用OpenAI GPT-3 API遇请求超时问题求助

OpenAI GPT-3 API 请求超时问题求助

调用OpenAI GPT-3 API时持续出现超时错误:

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

相关代码如下:

import time
import openai as ai

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)))

# 提取结果
df_test['GPT'] = df_test['GPT'].apply(lambda x: (x.split(':')[1]).replace(']',''))

已尝试修改参数但错误仍未解决,求可行解决方案。


可行解决方案

  • 添加请求重试机制:网络波动或API临时限流是超时常见原因,用重试机制自动重试失败请求,避免单次中断:

    from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
    import requests
    
    @retry(stop=stop_after_attempt(3),
          wait=wait_exponential(multiplier=1, min=2, max=10),
          retry=retry_if_exception_type((requests.exceptions.Timeout, requests.exceptions.ConnectionError)))
    def generate_gpt3_response(user_text, print_output=False):
        time.sleep(5)
        completions = ai.Completion.create(
            engine='text-davinci-003',
            temperature=0.5,
            prompt=user_text,
            max_tokens=150,
            n=1,
            stop=None,
            timeout=300  # 缩短超时阈值,避免无效等待
        )
        if print_output:
            print(completions)
        return completions.choices[0].text
    
  • 调整超时参数:当前read timeout=600过长,API正常响应无需这么久,建议在ai.Completion.create()中显式设置timeout=300(或更短的120),覆盖默认超时配置。

  • 优化请求方式:用apply逐行调用API会增加连接开销,改用循环遍历并记录失败项,同时控制请求频率:

    from tqdm import tqdm
    
    results = []
    for text in tqdm(df_test['Q20'].tolist()):
        try:
            res = generate_gpt3_response(text)
            results.append(res)
        except Exception as e:
            results.append(None)  # 失败项标记后统一处理
    df_test['GPT'] = results
    
  • 检查网络环境:确认本地网络可稳定访问api.openai.com,尝试切换网络(如手机热点)或排查代理、防火墙是否拦截HTTPS连接。

  • 更换模型引擎:text-davinci-003属于旧版模型,响应稳定性不如新版gpt-3.5-turbo-instruct,替换后可提升响应速度并降低成本:

    completions = ai.Completion.create(
        engine='gpt-3.5-turbo-instruct',  # 替换为新版指令模型
        temperature=0.5,
        prompt=user_text,
        max_tokens=150,
        n=1,
        stop=None,
        timeout=300
    )
    

内容的提问来源于stack exchange,提问作者opsv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 22:57:34