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

调用Azure OpenAI的gpt-3.5-turbo-0125时上下文长度不符问题咨询

问题描述

调用Azure OpenAI的gpt-3.5-turbo-0125模型时触发上下文长度错误,该模型官方文档显示最大上下文长度为16385 tokens,但实际报错提示最大仅为4096 tokens。

使用的代码如下:

openai.api_key = "*****"
openai.api_base = "https://lge-chatgpt-002.openai.azure.com/"
openai.api_type = 'azure'
openai.api_version = '2023-09-01-preview'
MODEL = 'gpt-3.5-turbo-0125'

response = openai.ChatCompletion.create(  # type: ignore
                        engine = "gpt-35-turbo",
                        model = MODEL,
                        messages = messages,
                        temperature=0,
                        max_tokens=max_tokens,
                        stop= None
                    )

收到的错误提示:

This model's maximum context length is 4096 tokens. However, you requested 4274 tokens (3774 in the messages, 500 in the completion). Please reduce the length of the messages or completion.

错误原因

核心问题在于Azure OpenAI中你指定的engine(部署名称)对应的是旧版4096上下文长度的gpt-35-turbo部署,而非16385上下文的gpt-3.5-turbo-0125:

  • 在Azure OpenAI体系中,engine参数对应你在Azure门户创建的部署名称,而非模型名称。你代码里写的engine = "gpt-35-turbo",说明当前调用的是旧版4k上下文的模型部署,即便代码中指定了model = "gpt-3.5-turbo-0125",Azure仍会优先采用engine对应的部署配置。
  • 要使用16385上下文的gpt-3.5-turbo-0125,需先在Azure OpenAI门户中创建基于该模型的部署,获取对应的部署名称,再将代码中的engine参数替换为这个新的部署名称。
  • 补充:Azure OpenAI调用中,指定engine后model参数并非必需,实际生效的是engine关联的部署配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 12:03:15