langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

Broken OpenAI LLM implementation for node version 18

Open DarylRodrigo opened this issue 1 year ago • 3 comments

Issue

When running the vanilla OpenAI LLM function, it returns a 400 for node version 18.

To reproduce

The following code was used to create this error.

import { OpenAI } from "langchain/llms/openai";

export const run = async () => {
  const model = new OpenAI({ modelName: "gpt-3.5-turbo"});
  try {

    const resA = await model.call(
      "What would be a good company name a company that makes colorful socks?"
    );
    console.log({ resA });
    } catch (e: any) {
      console.log(e!.response!.data)
    }
};

run()

Error from the response body

{
  error: {
    message: 'you must provide a model parameter',
    type: 'invalid_request_error',
    param: null,
    code: null
  }
}

As seen, even though a model parameter is provided it still returns an error asking to provide one.

Additional notes

This issue is not present in node v19 and v20.

DarylRodrigo avatar Apr 27 '23 06:04 DarylRodrigo

I've been struggling with that one, thanks for the issue report.

JeremyFabrikapp avatar Apr 27 '23 23:04 JeremyFabrikapp

@DarylRodrigo I just run this code using v18.15.0 and it works. The only change to the code you provided was adding openAIApiKey to the constructor. Do you have any more details on this?

kwkr avatar Apr 28 '23 09:04 kwkr

@DarylRodrigo @JeremyFabrikapp I just ran the exact code snippet above using node v18.16.0 and it works. Can you confirm the exact version of Node you're using? And any more details about your setup

nfcampos avatar Apr 28 '23 10:04 nfcampos

@nfcampos it seems like the issue doesn't occur on every 18.x version, i've tried the followings and so far found only one incompatibility :

  • Node v18.0.0 (npm v8.6.0) ✅
  • Node v18.1.0 (npm v8.8.0) ❌
  • Node v18.4.0 (npm v8.12.1) ✅
  • Node v18.16.0 (npm v9.5.1) ✅

I'm using NVM. I can push a fix to .nvmrc by setting specifically 18.16.0 rather than v18 or 20.

JeremyFabrikapp avatar May 02 '23 00:05 JeremyFabrikapp

Using Node v18.16.0 and still getting same issue

Error: Request failed with status code 400 and body {
    "error": {
        "message": "you must provide a model parameter",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

jacobcwright avatar May 09 '23 05:05 jacobcwright

@dqbd another one to look into, I cannot reproduce this

nfcampos avatar May 09 '23 09:05 nfcampos

@jacobcwright can you confirm it's working on your side with node 20.x ? Also are you using a static Node install or nvm ?

JeremyFabrikapp avatar May 09 '23 09:05 JeremyFabrikapp

I'm using nvm It does indeed work with Node v20.1.0 @JeremyFabrikapp

jacobcwright avatar May 09 '23 17:05 jacobcwright

The issue stems from fetch implementation found in Node 18.1.0 adding an additional suffix to the Content-Type when passing a string as a body. The Content-Type header will thus be application/json, text/plain;charset=UTF-8 instead of application/json.

The issue seems to be gone in LangchainJS 0.0.71 and later, where the fetch adapter is not used when NodeJS is detected #1144 and the issue goes away when using other NodeJS 18.x versions. Consider upgrading either of them to fix this issue.

Closing this issue for now.


Note: if the issue occurs again, it might make sense to patch axios-fetch-adapter to convert body into an Uint8Array instead, this will fix the issue even in NodeJS 18.1.0, but I'm not sure if this might break other environments as well.

dqbd avatar May 11 '23 11:05 dqbd

Thanks @dqbd for the coverage!

JeremyFabrikapp avatar May 11 '23 12:05 JeremyFabrikapp