如何在LangChain中移除gpt-35-turbo不支持的logprobs等参数?
解决LangChain调用Azure OpenAI gpt-35-turbo的参数错误问题
问题根源在于你使用了针对文本补全模型的OpenAI/AzureOpenAI类,而gpt-35-turbo属于聊天模型,两类模型的API接口参数不兼容——前者默认会携带logprobs、best_of等后者不支持的参数,触发报错。
解决方案:使用聊天模型专属的LangChain类
直接替换为AzureChatOpenAI(Azure环境专用),该类针对聊天模型设计,不会传递那些不兼容的参数。
修改后的测试代码:
from langchain.chains.llm import LLMChain from langchain.chat_models import AzureChatOpenAI from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate # 初始化聊天模型 llm = AzureChatOpenAI(temperature=0, deployment_name=deployment_name) # 构建聊天格式的prompt template = "You are a helpful assistant that translates English to French. Translate this sentence from English to French: {text}" human_prompt = HumanMessagePromptTemplate.from_template(template) chat_prompt = ChatPromptTemplate.from_messages([human_prompt]) # 创建链并执行 llm_chain = LLMChain(llm=llm, prompt=chat_prompt) response = llm_chain.generate([ {"text": "I love AI"}, {"text": "I love the ocean"}, ]) for g in response.generations: print(g[0].text)
简化调用的修改版本:
deployment_name = "my-deployment-name" from langchain.chat_models import AzureChatOpenAI llm = AzureChatOpenAI(deployment_name=deployment_name, temperature=0) # 直接调用时传入聊天消息格式的内容 response = llm.predict("Tell me a joke") print(response)
关键说明
- 聊天模型(gpt-35-turbo、gpt-4)必须使用
ChatOpenAI/AzureChatOpenAI类,而非针对文本补全模型的OpenAI/AzureOpenAI类。 - 聊天模型的prompt需要遵循消息结构(系统消息、人类消息等),可通过
ChatPromptTemplate快速构建。
内容的提问来源于stack exchange,提问作者Sarah
相关产品推荐
相关产品推荐

