langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
11.88k stars 1.99k forks source link

Unable to get langchain running with typescript/esbuild #322

Closed ayalpani closed 1 year ago

ayalpani commented 1 year ago

Hello!

langchain looks very promising and I would love to start using it. Unfortunately I am not able to get it running with the most basic typescript setup. Here is the repo to reproduce:

https://github.com/ayalpani/langchain-errors-demo

This is the output:

Screenshot 2023-03-14 at 16 30 54

I am using esbuild, Node 18.15.0, MacOS/Ventura

You can reproduce by running npm run build

Thanks for your kind support in advance!

ayalpani commented 1 year ago

One minute later...

here you ask to do:

import { OpenAI } from "langchain";

which is what I failed with. Then somehere else (lost it) you had a code sample with this import:

import { OpenAI } from "langchain/llms";

With this, it works. But why? What's the correct way of doing it?

nfcampos commented 1 year ago

@ayalpani hi, thanks for letting us know about this. I'm looking into this, this appears to be an issue due to esbuild including in the bundle some dependencies of our dependencies that aren't actually used. For now I'd suggest using the 2nd import you mentioned, and I'll update here once I have some resolution.

faizan-ali commented 1 year ago

I'm also seeing a similar error while using Langchain in a Next.js application (swc under the hood). This started happening on version 0.0.26 onwards - trying to find a good diff b/w .25 and .26 for a potential cause.

I'm trying to import from langchain/chains

import { VectorDBQAChain } from 'langchain/chains'

Error:

error - ../../node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.10/node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:0
Module not found: Can't resolve 'mock-aws-s3'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.10/node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
../../node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.10/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
../../node_modules/.pnpm/sqlite3@5.1.4/node_modules/sqlite3/lib/sqlite3-binding.js
../../node_modules/.pnpm/sqlite3@5.1.4/node_modules/sqlite3/lib/sqlite3.js
../../node_modules/.pnpm/typeorm@0.3.12_h3l5zcb3i6sggvllyfknv6gs44/node_modules/typeorm/platform/PlatformTools.js
../../node_modules/.pnpm/typeorm@0.3.12_h3l5zcb3i6sggvllyfknv6gs44/node_modules/typeorm/globals.js
../../node_modules/.pnpm/typeorm@0.3.12_h3l5zcb3i6sggvllyfknv6gs44/node_modules/typeorm/index.js
../../node_modules/.pnpm/typeorm@0.3.12_h3l5zcb3i6sggvllyfknv6gs44/node_modules/typeorm/index.mjs
../../node_modules/.pnpm/langchain@0.0.27_ncihua5ypz2qwoalor6utz2gcq/node_modules/langchain/dist/sql_db.js
../../node_modules/.pnpm/langchain@0.0.27_ncihua5ypz2qwoalor6utz2gcq/node_modules/langchain/dist/chains/sql_db/sql_db_chain.js
../../node_modules/.pnpm/langchain@0.0.27_ncihua5ypz2qwoalor6utz2gcq/node_modules/langchain/dist/chains/index.js
../../node_modules/.pnpm/langchain@0.0.27_ncihua5ypz2qwoalor6utz2gcq/node_modules/langchain/chains.js

Other details: Node 18.14.2, MacOS Ventura

faizan-ali commented 1 year ago

@nfcampos Let me know how I can help here, happy to spend some time digging if you can point me in the right direction :)

faizan-ali commented 1 year ago

Ah looks like my issue is resolved as of 0.0.35!

nfcampos commented 1 year ago

Great that your issue is now resolved @faizan-ali

theindianappguy commented 1 year ago

I am still facing this issue

./node_modules/@dqbd/tiktoken/tiktoken_bg.wasm Module parse failed: Unexpected character '' (1:0) The module seem to be a WebAssembly module, but module is not flagged as WebAssembly module for webpack. BREAKING CHANGE: Since webpack 5 WebAssembly is not enabled by default and flagged as experimental feature. You need to enable one of the WebAssembly experiments via 'experiments.asyncWebAssembly: true' (based on async modules) or 'experiments.syncWebAssembly: true' (like webpack 4, deprecated). For files that transpile to WebAssembly, make sure to set the module type in the 'module.rules' section of the config (e. g. 'type: "webassembly/async"'). (Source code omitted for this binary file)

nfcampos commented 1 year ago

@theindianappguy you can fix that by following the instructions here https://github.com/dqbd/tiktoken/tree/main/js#nextjs

ljs19923 commented 1 year ago

Hey @nfcampos I have the same error. Where are the instructions in your link?

ljs19923 commented 1 year ago

@theindianappguy did you find a solution?

marcelo-smartup commented 1 year ago

@theindianappguy did you find a solution?

Did you find one? I'm struggling with this also.

dhruvbhatia7 commented 1 year ago

Here's the solution: Add this code to your next.config.js

webpack(config) { config.experiments = { asyncWebAssembly: true, layers: true, }; return config; },

It is explained in the langChain doc - https://js.langchain.com/docs/getting-started/install