langchainjs: Network Error in `ConversationChain`

I’m getting the following axios error when calling my ConversationChain

Error

"Error: Network Error\n    at createError (webpack-internal:///(api)/./node_modules/langchain/dist/util/axios-fetch-adapter.js:316:19)\n    at getResponse (webpack-internal:///(api)/./node_modules/langchain/dist/util/axios-fetch-adapter.js:197:16)\n    at async fetchAdapter (webpack-internal:///(api)/./node_modules/langchain/dist/util/axios-fetch-adapter.js:174:18)"

Implementation

const history = messages.map(({ agent, message }) =>
    agent === "ai" ? new AIChatMessage(message) : new HumanChatMessage(message)
  );

  const memory = new BufferMemory({
    memoryKey: "history",
    chatHistory: new ChatMessageHistory(history),
    returnMessages: true,
  });

  const llm = new ChatOpenAI({
    temperature: 0,
  });

  const prompt = ChatPromptTemplate.fromPromptMessages([
    SystemMessagePromptTemplate.fromTemplate(
      DEFAULT_PROMPT_TEMPLATE
    ),
    new MessagesPlaceholder("history"),
    HumanMessagePromptTemplate.fromTemplate("{message}"),
  ]);

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

  try {
    response.status(200).json({
      success: true,
      data: await chain.call({
        message,
      }),
      agent: "ai",
    });
  } catch (error) {
    console.log(error);
    response.status(500).json({ success: false, error });
  }

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 33 (5 by maintainers)

Most upvoted comments

I don’t see you passing the OPENAI_API_KEY anywhere, might that be the issue? Also, I’d recommend updating to 0.0.52

Upgrading to 0.0.52 made it worse, getting Network Errors on all requests now. Perhaps something with the axios setup and NextJS?

The raw query works each time using cURL.

You can see the changes here: https://github.com/homanp/langchain-ui/blob/c60f1b14a0560e2f9f01379c8ae9003f8185876c/pages/api/v1/chatbots/[chatbotId]/index.js

I also can send my request with cURL and even Chrome web console. But not work with pure Node fetch request. The Node 18 seems like never run fetch with network proxy (until I use proxychains to force it) in my case. By the way, my computer system is MacOS with M1 chip. I guess it’s some kind of issue of Node 18 fetch or M1 ?

We actually test with NextJs before each release inside this repo, and I can confirm openai requests work in frontend components, api routes in serverless mode and api routes in edge mode. https://github.com/hwchase17/langchainjs/tree/main/test-exports-vercel/src/pages/api

I can also link you to this project which is using both of those (and the author tells me it works both locally and when deployed to vercel and fly.io) https://github.com/PineappleExpress808/lex-gpt/tree/main/pages/api

Do you want to compare your projects to this, and let me know what’s different about yours? As it stands I cannot reproduce this issue, therefore can’t help