langchain-ai / langchainjs

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

[ERROR] Langchain in typescript can't find a bunch of modules! #602

Closed adifyr closed 1 year ago

adifyr commented 1 year ago

Hi. I installed langchain for my Node Typescript project. When I try to run the sample, I get the following errors in the terminal:

node_modules/langchain/dist/cache.d.ts:1:38 - error TS2307: Cannot find module 'redis' or its corresponding type declarations.

1 import type { RedisClientType } from "redis";
                                       ~~~~~~~

node_modules/langchain/dist/embeddings/cohere.d.ts:54:31 - error TS2307: Cannot find module 'cohere-ai' or its corresponding type declarations.

54         cohere: typeof import("cohere-ai");
                                 ~~~~~~~~~~~

node_modules/langchain/dist/llms/cohere.d.ts:21:31 - error TS2307: Cannot find module 'cohere-ai' or its corresponding type declarations.

21         cohere: typeof import("cohere-ai");
                                 ~~~~~~~~~~~

node_modules/langchain/dist/llms/hf.d.ts:29:36 - error TS2307: Cannot find module '@huggingface/inference' or its corresponding type declarations.

29         HfInference: typeof import("@huggingface/inference").HfInference;
                                      ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/llms/openai-chat.d.ts:2:15 - error TS2614: Module '"../util/axios-fetch-adapter.js"' has no exported member 'StreamingAxiosConfiguration'. Did you mean to use 'import StreamingAxiosConfiguration from "../util/axios-fetch-adapter.js"' instead?

2 import type { StreamingAxiosConfiguration } from "../util/axios-fetch-adapter.js";
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/llms/openai.d.ts:2:15 - error TS2614: Module '"../util/axios-fetch-adapter.js"' has no exported member 'StreamingAxiosConfiguration'. Did you mean to use 'import StreamingAxiosConfiguration from "../util/axios-fetch-adapter.js"' instead?

2 import type { StreamingAxiosConfiguration } from "../util/axios-fetch-adapter.js";
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/llms/replicate.d.ts:17:34 - error TS2307: Cannot find module 'replicate' or its corresponding type declarations.

17         Replicate: typeof import("replicate").Replicate;
                                    ~~~~~~~~~~~

node_modules/langchain/dist/sql_db.d.ts:1:67 - error TS2307: Cannot find module 'typeorm' or its corresponding type declarations.

1 import type { DataSource as DataSourceT, DataSourceOptions } from "typeorm";
                                                                    ~~~~~~~~~

node_modules/langchain/dist/text_splitter.d.ts:1:32 - error TS2307: Cannot find module '@dqbd/tiktoken' or its corresponding type declarations.

1 import type * as tiktoken from "@dqbd/tiktoken";
                                 ~~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/sql_utils.d.ts:1:52 - error TS2307: Cannot find module 'typeorm' or its corresponding type declarations.

1 import type { DataSource, DataSourceOptions } from "typeorm";
                                                     ~~~~~~~~~

node_modules/langchain/dist/vectorstores/chroma.d.ts:1:52 - error TS2307: Cannot find module 'chromadb' or its corresponding type declarations.

1 import type { ChromaClient as ChromaClientT } from "chromadb";
                                                     ~~~~~~~~~~

node_modules/langchain/dist/vectorstores/hnswlib.d.ts:1:69 - error TS2307: Cannot find module 'hnswlib-node' or its corresponding type declarations.

1 import type { HierarchicalNSW as HierarchicalNSWT, SpaceName } from "hnswlib-node";
                                                                      ~~~~~~~~~~~~~~

node_modules/langchain/dist/vectorstores/pinecone.d.ts:1:42 - error TS2307: Cannot find module '@pinecone-database/pinecone/dist/pinecone-generated-ts-fetch' or its corresponding type declarations.

1 import type { VectorOperationsApi } from "@pinecone-database/pinecone/dist/pinecone-generated-ts-fetch";
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/vectorstores/supabase.d.ts:1:37 - error TS2307: Cannot find module '@supabase/supabase-js' or its corresponding type declarations.

1 import type { SupabaseClient } from "@supabase/supabase-js";
                                      ~~~~~~~~~~~~~~~~~~~~~~~

Found 14 errors in 14 files.

Errors  Files
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/cache.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/embeddings/cohere.d.ts:54
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/llms/cohere.d.ts:21
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/llms/hf.d.ts:29
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/llms/openai-chat.d.ts:2
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/llms/openai.d.ts:2
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/llms/replicate.d.ts:17
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/sql_db.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/text_splitter.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/util/sql_utils.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/vectorstores/chroma.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/vectorstores/hnswlib.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/vectorstores/pinecone.d.ts:1
     1  ../../../../../users/adityam/documents/code/node-sandbox/node_modules/langchain/dist/vectorstores/supabase.d.ts:1

I did do npm install -S langchain. And I set all the parameters in the tsconfig.json & package.json to the recommended values.

Here is my package.json:

{
  "name": "sandbox",
  "version": "1.0.0",
  "description": "NodeJS sandbox.",
  "main": "dist/index.js",
  "type": "module",
  "scripts": {
    "start": "node dist/index.js",
    "build": "tsc && npm run start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "langchain": "^0.0.47"
  },
  "devDependencies": {
    "@types/node": "^18.15.11"
  }
}

And here is my tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "moduleResolution": "nodenext"
  },
  "include": [
    "./src/**/*",
    "./assets/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

What's going on? Will I have to install all these modules separately?

nfcampos commented 1 year ago

Hi, can you confirm which version of typescript you're using, I can't see it in your package.json

adifyr commented 1 year ago

I am using Typescript Version 4.8.2. Do I need to upgrade?

Also, here is my code in index.ts. As you can see, I'm just doing the basic state_of_the_union example:

import {OpenAI} from "langchain/llms";
import {RetrievalQAChain} from "langchain/chains";
import {readFileSync} from "fs";
import {RecursiveCharacterTextSplitter} from "langchain/text_splitter";
import {HNSWLib} from "langchain/vectorstores";
import {OpenAIEmbeddings} from "langchain/embeddings";

async function qaBot(query: string) {
    const model = new OpenAI({});
    const source = readFileSync("../assets/state_of_the_union.txt", "utf8");
    const textSplitter = new RecursiveCharacterTextSplitter({chunkSize: 1000});
    const docs = await textSplitter.createDocuments([source]);
    const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
    const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever());
    const res = await chain.call({query});
    console.log(res);
}

qaBot("What did the president say about Justice Breyer?");
nfcampos commented 1 year ago

You can make it work by adding "skipLibCheck": true to your tsconfig.json, see https://github.com/nfcampos/langchain-issue-602

dorpex commented 1 year ago

i have the same issue and i want to use this repo for production, i cannot use skipLibCheck, any other idea?

nfcampos commented 1 year ago

@dorpex which version of langchain are you using?

ghost commented 1 year ago

Any solution to this issue?

nfcampos commented 1 year ago

@PublicTrades can you share the exact error you're getting along with a code snippet that is generating that error

ianmcfall commented 1 year ago

Stumbled passed this issue today. I found for some reason my package.json was had "langchain": "^0.0.39" installed for some reason (old library doesn't know what the latest modules are, before it's time). Since I was using npm I ran npm uninstall langchain and then reinstalled with npm install -S langchain. This installed "langchain": "^0.0.97", Then my lost module party was over. Hope this helps.

ghost commented 1 year ago

Thanks @ianmcfall , Good work my friend, you are a time saver. Can't thank you enough.

blockspector commented 1 year ago

@ianmcfall Thank you this solved my issue! Kudos to you!

ChrisChiasson commented 1 year ago

This is still happening in the latest version 133 and is not solved for me with uninstall, reinstall (with or without -S), or wiping the npm cache. The missing packages are cohere-ai, @huggingface/inference, and replicate. If we look in the package.json file line 646 https://www.npmjs.com/package/langchain/v/0.0.133?activeTab=code

"cohere-ai": "^5.0.2",

we see that cohere is listed as a DEV dependency. I don't think langchain's dev dependencies are installed for npm i langchain (the command given on the langchain homepage on npm). This is probably why people are hitting errors. I suppose to get around the problem the packages could be added to our own package.json files.

I am hitting the same errors where it is declaring types as what the OP showed. I have no idea how many packages will be needed to be installed to prevent the type errors.

clashofphish commented 1 year ago

I'm having the same issue as @ChrisChiasson with the same package (cohere-ai) and langchain version 0.0.134

ChrisChiasson commented 1 year ago

I'm having the same issue as @ChrisChiasson with the same package (cohere-ai) and langchain version 0.0.134

BTW, adding the nominated packages should fix it @clashofphish

renestavnes commented 10 months ago

I'm having the same problem...

Can't find packages like AIMessage and HumanMessage.

Has anyone found a solution?

praveenaj commented 9 months ago

I was getting the same error but after installing the exact version "langchain": "^0.0.97" given by @ianmcfall I was able to succeed! Make sure you have uninstalled the manually installed cohere-ai when you do this