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

LangChain对接Azure OpenAI API返回冗余信息问题求助

解决Azure OpenAI + LangChain返回冗余内容的问题

针对你遇到的Azure OpenAI模型返回多余问答内容的问题,可以通过以下几种方式解决:

  • 强化提示词的终止指令
    修改提示模板,明确要求模型输出答案后立即停止,避免生成额外内容:

    prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
    Just answer the question asked. Do NOT provide additional questions or answers. Stop right after giving the answer.
    {context}
    
    Question: {question}
    Answer:"""
    PROMPT = PromptTemplate(
        template=prompt_template, input_variables=["context", "question"]
    )
    

    新增的Answer:引导模型聚焦于输出目标答案,同时明确停止指令。

  • 设置LLM的stop参数
    在初始化AzureOpenAI时,指定终止触发词,让模型生成到特定内容时停止。由于冗余内容以Question:开头,可将其设为终止词:

    llm = AzureOpenAI(model_name="gpt-35-turbo",
                      temperature=0,
                      openai_api_key=open_api_key,
                      openai_api_base=open_api_base,
                      openai_api_type=open_api_type,
                      openai_api_version=open_api_version,
                      deployment_name=openai_deployment,
                      stop=["Question:"]
                      )
    
  • 限制模型生成的最大令牌数
    通过max_tokens参数约束模型输出长度,避免无意义的冗余生成(数值根据实际答案长度调整):

    llm = AzureOpenAI(model_name="gpt-35-turbo",
                      temperature=0,
                      max_tokens=60,
                      openai_api_key=open_api_key,
                      openai_api_base=open_api_base,
                      openai_api_type=open_api_type,
                      openai_api_version=open_api_version,
                      deployment_name=openai_deployment)
    
  • 检查检索到的上下文内容
    启用RetrievalQA的return_source_documents=True参数,查看返回的源文档是否本身包含多余的问答对:

    qa_chain = RetrievalQA.from_chain_type(
           llm,
           retriever=retriever,
           chain_type="stuff",
           chain_type_kwargs=chain_type_kwargs,
           return_source_documents=True
    )
    result = qa_chain(user_input)
    print(result["source_documents"])
    

    如果上下文本身冗余,需对源文档进行预处理,过滤无关的问答段落。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:13:12