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

如何在Langchain的AzureMLOnlineEndpoint中延长超时时间?

解决Langchain中AzureMLOnlineEndpoint设置timeout仍超时的问题

你在Langchain中使用AzureMLOnlineEndpoint调用LLM时,已设置timeout=120但仍出现读取操作超时错误,可通过以下几种方式解决:

1. 升级Langchain到最新版本

旧版本的Langchain可能存在timeout参数未正确传递到底层请求的bug,先执行升级:

pip install --upgrade langchain

升级后重新测试,看超时问题是否解决。

2. 调整timeout参数的设置方式

Langchain的AzureMLOnlineEndpoint底层依赖requests库,该库支持分别设置连接超时和读取超时(以元组形式传入),单一数值的timeout可能无法覆盖长耗时的读取场景。修改代码如下:

llm = AzureMLOnlineEndpoint(
    endpoint_api_key='your_api_key',
    endpoint_url='your_endpoint_url',
    model_kwargs={"temperature": 0.8, "max_tokens": 300},
    content_formatter=content_formatter,
    timeout=(10, 300)  # 10秒连接超时,300秒读取超时
)

也可以直接设置更大的单一超时值,比如timeout=300,根据实际需求调整。

3. 自定义AzureMLOnlineEndpoint类强制传递超时

如果上述方法无效,说明Langchain的封装未正确处理timeout参数,可通过子类化重写_call方法,手动控制请求的超时设置:

from langchain.llms.azureml_endpoint import AzureMLOnlineEndpoint
import requests

class CustomAzureMLOnlineEndpoint(AzureMLOnlineEndpoint):
    def _call(self, prompt: str, stop: list[str] | None = None) -> str:
        payload = self.content_formatter.format_request(prompt, self.model_kwargs)
        headers = {
            "Content-Type": self.content_formatter.content_type,
            "Authorization": f"Bearer {self.endpoint_api_key}",
        }
        # 显式设置请求超时
        response = requests.post(
            self.endpoint_url,
            headers=headers,
            data=payload,
            timeout=self.timeout
        )
        response.raise_for_status()
        return self.content_formatter.format_response(response.json())

# 使用自定义类初始化LLM
llm = CustomAzureMLOnlineEndpoint(
    endpoint_api_key='',
    endpoint_url='',
    model_kwargs={"temperature": 0.8, "max_tokens": 300},
    content_formatter=content_formatter,
    timeout=300
)

额外检查项

  • Azure ML端点侧超时设置:登录Azure门户,检查在线端点的部署配置,确保服务器端的超时时间大于客户端设置的timeout值,避免服务器提前断开连接。
  • 网络环境排查:如果是企业内网环境,确认是否存在代理、防火墙限制导致请求延迟过高,必要时联系网络管理员排查。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 09:54:52