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

使用LangChain连接时NextJS AI SDK无法对接Azure OpenAI

Vercel AI SDK + NextJS 对接 LangChain + Azure OpenAI 问题排查与修复

我正在使用Vercel AI SDK搭配NextJS,尝试通过LangChain对接Azure OpenAI。目前直接对接Azure OpenAI(不通过LangChain)可以正常工作,但借助LangChain实现数据与聊天处理功能时遇到了问题。以下是我的route.ts代码:

import { NextRequest } from "next/server";
import { Message as VercelChatMessage, StreamingTextResponse } from "ai";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { BytesOutputParser } from "langchain/schema/output_parser";
import { PromptTemplate } from "langchain/prompts";

export const runtime = "edge";

const formatMessage = (message: VercelChatMessage) => {
  return `${message.role}: ${message.content}`;
};

const TEMPLATE = `You are a pirate named Patchy. All responses must be extremely verbose and in pirate dialect.
 
Current conversation:
{chat_history}
 
User: {input}
AI:`;

export async function POST(req: NextRequest) {
  const body = await req.json();
  const messages = body.messages ?? [];
  const formattedPreviousMessages = messages.slice(0, -1).map(formatMessage);
  const currentMessageContent = messages[messages.length - 1].content;

  const prompt = PromptTemplate.fromTemplate(TEMPLATE);

  const model = new ChatOpenAI({
    temperature: 0.8,
    azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
    azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
    azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
    azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
    azureOpenAIBasePath: process.env.AZURE_OPENAI_BASE_PATH,
  });

  const outputParser = new BytesOutputParser();

  const chain = prompt.pipe(model).pipe(outputParser);

  const stream = await chain.stream({
    chat_history: formattedPreviousMessages.join("\n"),
    input: currentMessageContent,
  });

  return new StreamingTextResponse(stream);
}

常见问题与修复方案

  • 环境变量配置错误:确认所有Azure OpenAI相关环境变量(AZURE_OPENAI_API_KEY、AZURE_OPENAI_API_INSTANCE_NAME等)拼写正确,且已在Vercel或本地开发环境中配置。注意azureOpenAIBasePath格式应为https://<实例名称>.openai.azure.com/,末尾斜杠不能省略。
  • LangChain参数不匹配:检查langchain包版本,建议升级到最新稳定版。确认azureOpenAIApiDeploymentName是Azure门户中自定义的部署名称,而非模型本身名称(比如模型名是gpt-35-turbo,部署名可能是my-gpt-deployment)。
  • 流式解析器适配问题:当前使用的BytesOutputParser与Vercel的StreamingTextResponse适配性差,建议替换为StringOutputParser:
    import { StringOutputParser } from "langchain/schema/output_parser";
    const outputParser = new StringOutputParser();
    
  • Edge Runtime兼容性问题:部分LangChain依赖可能不支持Edge Runtime,可临时将runtime改为nodejs测试,排查是否为环境兼容问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 12:58:24