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

通过Databricks调用Azure OpenAI GPT-4遇APITimeoutError求助

解决Databricks调用Azure OpenAI GPT-4时的APITimeoutError问题

使用以下代码调用Azure OpenAI GPT-4模型时触发APITimeoutError: Request timed out,此前代码运行正常,更换API密钥后问题仍存在:

import os
from openai import AzureOpenAI


client = AzureOpenAI(
  azure_endpoint = "https://az*****.openai.azure.com/", 
  api_key=OPEN_AI_KEY,  
  api_version="2024-02-15-preview"
)


message_text = [{"role":"system","content":"You are an AI assistant that helps people find information."}]

completion = client.chat.completions.create(
  model="gpt-4-1106-Preview", # model = "deployment_name"
  messages = message_text,
  temperature=0.7,
  max_tokens=800,
  top_p=0.95,
  frequency_penalty=0,
  presence_penalty=0,
  timeout = 120,
  stop=None
)

可按以下步骤排查定位问题:

一、验证Azure OpenAI服务本身可用性

  • 在Azure OpenAI Studio中测试对应部署的GPT-4模型,运行相同prompt。若Studio也超时/报错,说明是服务或部署问题:
    • 检查Azure OpenAI资源的配额使用情况,确认部署的实例配额未耗尽
    • 查看Azure状态页,确认对应区域的OpenAI服务无故障
  • 用curl在本地(或公网环境)直接调用API:
    curl https://az*****.openai.azure.com/openai/deployments/[你的部署名称]/chat/completions?api-version=2024-02-15-preview \
      -H "Content-Type: application/json" \
      -H "api-key: 你的API密钥" \
      -d '{
        "messages": [{"role":"system","content":"You are an AI assistant that helps people find information."}],
        "temperature": 0.7,
        "max_tokens": 800
      }'
    
    若本地调用也超时,问题在Azure OpenAI服务或网络路由;若本地正常,问题出在Databricks侧。

二、排查Databricks网络连通性

  • VNet注入集群:确认NSG出站规则允许访问Azure OpenAI的IP范围,或已配置Microsoft.CognitiveServices服务端点
    • 可通过Azure CLI获取OpenAI服务IP:az network list-service-tags --location 你的区域 --query "values[?properties.serviceName=='Microsoft.CognitiveServices/openAIServices']"
  • 公共集群:确认Databricks所在环境的防火墙/代理未拦截Azure OpenAI域名
  • 在Databricks中测试端点连通性:
    import requests
    
    try:
        response = requests.get("https://az*****.openai.azure.com/", timeout=30)
        print(f"HTTP状态码: {response.status_code}")
    except requests.exceptions.Timeout:
        print("无法连接到Azure OpenAI端点,网络超时")
    except Exception as e:
        print(f"连接错误: {str(e)}")
    
    若此测试超时,说明Databricks到OpenAI的网络通路异常;若返回200/404,说明网络正常,问题在API调用或服务端。

三、代码与配置检查

  • 确认model参数是Azure OpenAI的部署名称,而非模型官方名称(部署名称可能与模型名不同)
  • 调小max_tokens(如改为200),减少模型生成压力,测试是否仍超时
  • 尝试移除timeout=120或调整为60秒,避免与Databricks集群超时规则冲突
  • 核对API密钥权限:确保密钥所属资源有Cognitive Services OpenAI User权限,且密钥对应正确的Azure OpenAI资源

四、其他排查动作

  • 检查Databricks集群资源使用率,确认CPU/内存未过载
  • 查看Azure OpenAI资源的监控日志,排查请求是否到达服务端、是否存在处理异常
  • 重启Databricks集群,修复可能的网络会话异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 18:57:49