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

Azure OpenAI中gpt-35-turbo-16k报错,gpt-35-turbo正常?原因解析

问题场景

使用Azure OpenAI、LangChain和Streamlit构建应用时,初始化gpt-35-turbo-16k模型报错:

# 原报错代码片段
LLM = AzureOpenAI(max_tokens=1500, deployment_name="gpt-35-turbo-16k", model="gpt-35-turbo-16k")

错误提示:

InvalidRequestError: The completion operation does not work with the specified model, gpt-35-turbo-16k. Please choose different model and try again.

但将模型改为gpt-35-turbo后代码可正常运行。

核心原因

错误本质是模型类型与调用接口不匹配:

  • LangChain的AzureOpenAI类默认调用OpenAI的「补全(Completion)」接口,这类接口仅支持传统文本补全模型(如text-davinci-003)。
  • gpt-35-turbo-16k属于聊天模型,Azure OpenAI对该模型严格限制仅能通过「聊天补全(Chat Completion)」接口访问。
  • 旧版4k上下文的gpt-35-turbo在部分部署中可能兼容补全接口,但这是非标准用法,16k版本不再支持这种兼容。
解决方案

改用LangChain的AzureChatOpenAI类调用聊天补全接口,适配gpt-35-turbo-16k模型,调整后的完整代码:

from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())

import streamlit as st
from langchain.chat_models import AzureChatOpenAI
from langchain.prompts import ChatPromptTemplate

# 初始化聊天模型(替换原AzureOpenAI类)
LLM = AzureChatOpenAI(
    max_tokens=1500,
    deployment_name="gpt-35-turbo-16k",
    model="gpt-35-turbo-16k"
)

# 调整为聊天专用的prompt模板
prompt_template = ChatPromptTemplate.from_messages([
    ("system", "If you don't know the answer, just say that you don't know, don't try to make up an answer."),
    ("human", "{question}")
])

st.title('Experiment AzureOpenAI :)')
user_question = st.text_input('Your query here please')

if user_question:
    # 生成结构化聊天消息
    prompt = prompt_template.format_messages(question=user_question)
    response = LLM(prompt)
    # 提取聊天响应内容
    st.write(response.content)
    st.write('done')
补充说明

OpenAI模型分为两类接口:

  • 补全接口:针对纯文本补全场景,输入单一段文本prompt。
  • 聊天补全接口:针对对话场景,输入系统消息、用户消息等结构化消息序列。

所有turbo系列模型(包括4k和16k版本)官方推荐使用聊天补全接口,16k版本更是强制要求,这是避免错误的关键。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 17:55:54