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

Node.js中LangChain设置自定义OpenAI模型无效的问题求助

Node.js下LangChain设置gpt-3.5-turbo模型的正确方式

你当前使用的OpenAI类(来自langchain/llms)是针对OpenAI的补全类模型(如text-davinci)设计的,而gpt-3.5-turbo属于聊天类模型,需要使用专门的ChatOpenAI类来初始化,才能正确调用对应接口。

修改后的完整代码

const { ChatOpenAI } = require("langchain/chat_models");
const { ConversationChain } = require("langchain/chains");
const { BufferMemory } = require("langchain/memory");

// 使用ChatOpenAI初始化聊天模型
const model = new ChatOpenAI({ 
  modelName: "gpt-3.5-turbo", 
  openAIApiKey: "###", 
  temperature: 0.9 
});
const memory = new BufferMemory();

const chain = new ConversationChain({ llm: model, memory: memory });

async function x(){
  const res = await chain.call({ input: "Hello this is xyz!" });
  const res2 = await chain.call({ input: "Hello what was my name?" });
  console.log(res);
  console.log(res2);
}

x();

关键调整说明

  • 替换导入模块:从langchain/chat_models导入ChatOpenAI,而非langchain/llms中的OpenAI
  • 模型参数命名:聊天模型使用modelName指定模型名称(对应Python版本的model_name),而非原代码中的model参数
  • 其余逻辑无需改动:ConversationChain和BufferMemory可以直接兼容ChatOpenAI实例,对话记忆功能正常生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:12:04