langchain-ai / langchainjs

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

error TS2304: Cannot find name 'TransformStream', 'WritableStreamDefaultWriter'... #2991

Closed akudo7 closed 1 year ago

akudo7 commented 1 year ago

Description:

I am encountering compilation errors with porting my VSCode extension from @azure/openai to langchain. Here are the specific errors:

node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:89 - error TS2304: Cannot find name 'TransformStream'. node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:90 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'. node_modules/langchain/dist/chat_models/base.d.ts:32 - error TS2304: Cannot find name 'TransformStream'. node_modules/langchain/dist/util/stream.d.ts:1 - error TS2304: Cannot find name 'ReadableStream'. node_modules/langchain/dist/util/stream.d.ts:2 - error TS2304: Cannot find name 'ReadableStreamDefaultReader'. node_modules/langchain/dist/util/stream.d.ts:4 - error TS2304: Cannot find name 'ReadableStreamReadResult'. node_modules/langchain/dist/util/stream.d.ts:10 - error TS2304: Cannot find name 'ReadableStream'.

Additional Information:

Package Manager: Yarn TypeScript Version: [^5.1.3] langchain Version: [^0.0.169] Node.js Version: [v18.16.0] Operating System: [macOS 14.0]

package.json:

  "scripts": {
    "vscode:prepublish": "yarn run compile",
    "compile": "tsc -p ./",
    "watch": "tsc -watch -p ./",
    "pretest": "yarn run compile && yarn run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js"
  },
  "devDependencies": {
    "@types/glob": "^8.1.0",
    "@types/mocha": "^10.0.1",
    "@types/uuid": "^9.0.4",
    "@types/vscode": "^1.79.0",
    "@typescript-eslint/eslint-plugin": "^5.59.8",
    "@typescript-eslint/parser": "^5.59.8",
    "@vscode/test-electron": "^2.3.2",
    "eslint": "^8.41.0",
    "glob": "^8.1.0",
    "mocha": "^10.2.0",
    "typescript": "^5.1.3"
  },
  "dependencies": {
    "@notionhq/client": "^2.2.13",
    "@types/fs-extra": "^11.0.1",
    "@types/mustache": "^4.2.2",
    "langchain": "^0.0.169",
    "mustache": "^4.2.0",
    "uuid": "^9.0.1"
  }

Thank you.

dosubot[bot] commented 1 year ago

🤖

Hello,

The errors you're encountering are related to the use of web APIs ('TransformStream', 'WritableStreamDefaultWriter', and 'ReadableStream') that are not available in the Node.js environment by default.

These APIs are used in the LangChainJS codebase, and while TypeScript includes type definitions for these APIs (as specified in the "lib" field of the tsconfig.json file), they are not available at runtime in a Node.js environment.

To resolve this issue, you can use a polyfill for these APIs in Node.js. The 'web-streams-polyfill' package is a popular choice. You can install it using Yarn (since you're using Yarn as your package manager):

yarn add web-streams-polyfill

Then, you can import and use it in your code:

import { ReadableStream, WritableStream, TransformStream } from 'web-streams-polyfill/ponyfill';

// Now you can use ReadableStream, WritableStream, and TransformStream in your Node.js code.

This solution is based on a similar issue encountered in the LangChainJS repository: Readablestream not defined

Please try this solution and let me know if it resolves your issue or if you need further assistance.

Best regards.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

akudo7 commented 1 year ago

@dosu-bot

Thanks for reply. Tried some patterns as below are not working. Any ideas?

yarn add web-streams-polyfill
// No1
// import { ReadableStream, WritableStream, TransformStream } from 'web-streams-polyfill/ponyfill';

// No2
//const { ReadableStream, WritableStream, TransformStream } = require('web-streams-polyfill/ponyfill');

// No3
// import { ReadableStream, WritableStream, TransformStream } from 'web-streams-polyfill/ponyfill';
//(global as any).TransformStream = TransformStream;

// No4
//const { ReadableStream, WritableStream, TransformStream } = require('web-streams-polyfill/ponyfill');
//(global as any).TransformStream = TransformStream;

import { AzureOpenAIInput, ChatOpenAI, ChatOpenAICallOptions } from 'langchain/chat_models/openai';
import { HumanMessage, SystemMessage, AIMessage } from "langchain/schema";

Thanks

dosubot[bot] commented 1 year ago

🤖

Hello,

Based on the issues you've described, it seems like you're encountering problems due to the ReadableStream, WritableStream, and TransformStream APIs not being available in Node.js. These are web APIs and are not natively available in Node.js.

The web-streams-polyfill package provides a polyfill for these APIs, but they are not automatically applied to the global scope. You need to explicitly assign the polyfilled versions of these APIs to the global scope in Node.js.

Here's how you can do it:

import { ReadableStream, WritableStream, TransformStream } from 'web-streams-polyfill/ponyfill/es2018';

global.ReadableStream = ReadableStream;
global.WritableStream = WritableStream;
global.TransformStream = TransformStream;

You should add this code at the top of your main file, or in a setup file that's run before your tests. This will ensure that when your code references ReadableStream, WritableStream, and TransformStream, it will be using the polyfilled versions provided by web-streams-polyfill.

This solution is based on a similar issue encountered in the langchainjs repository, which you can find here.

Please note that this solution assumes that you're running your code in a Node.js environment. If you're running your code in a different environment, the solution might be different.

If you continue to encounter issues after trying this solution, please provide the specific error messages you're seeing, and I'll be happy to help you further.

Best, Dosu

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

akudo7 commented 1 year ago

@dosu-bot

Again, thanks for reply and kindly support!

Tried to add your advice as blow.

import { ReadableStream, WritableStream, TransformStream } from 'web-streams-polyfill/ponyfill/es2018';
(global as any).TransformStream = TransformStream;
(global as any).ReadableStream = ReadableStream;
(global as any).WritableStream = WritableStream;

Here is the errors

% yarn compile
yarn run v1.22.19
warning package.json: No license field
$ tsc -p ./
node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:89:32 - error TS2304: Cannot find name 'TransformStream'.

89     protected transformStream: TransformStream;
                                  ~~~~~~~~~~~~~~~

node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:90:13 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

90     writer: WritableStreamDefaultWriter;
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/chat_models/base.d.ts:32:64 - error TS2304: Cannot find name 'TransformStream'.

32 export declare function createChatMessageChunkEncoderStream(): TransformStream<BaseMessageChunk, any>;
                                                                  ~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:1:56 - error TS2304: Cannot find name 'ReadableStream'.

1 export declare class IterableReadableStream<T> extends ReadableStream<T> {
                                                         ~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:2:13 - error TS2304: Cannot find name 'ReadableStreamDefaultReader'.

2     reader: ReadableStreamDefaultReader<T>;
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:4:21 - error TS2304: Cannot find name 'ReadableStreamReadResult'.

4     next(): Promise<ReadableStreamReadResult<T>>;
                      ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:10:42 - error TS2304: Cannot find name 'ReadableStream'.

10     static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
                                            ~~~~~~~~~~~~~~

Found 7 errors in 3 files.

Errors  Files
     2  node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:89
     1  node_modules/langchain/dist/chat_models/base.d.ts:32
     4  node_modules/langchain/dist/util/stream.d.ts:1
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Let me expose the smallest reproducible later.

Again, thanks for your help. You are my only hope!

dosubot[bot] commented 1 year ago

🤖

Hello,

The issue you're encountering is related to the use of web streams in a Node.js environment. The 'TransformStream', 'WritableStreamDefaultWriter', and 'ReadableStream' are part of the web streams API, which is not natively supported in Node.js.

The 'web-streams-polyfill' package is meant to provide this functionality in environments where it's not natively supported, but it seems it's not working as expected in your case.

To resolve this issue, you can try using the 'stream' module provided by Node.js instead. Here's how you can modify your code:

import { ChatOpenAI } from "langchain/chat_models/openai";
import { HumanMessage } from "langchain/schema";
import { Writable } from 'stream';

// Create a writable stream
const outStream = new Writable({
  write(chunk, encoding, callback) {
    process.stdout.write(chunk);
    callback();
  }
});

const chat = new ChatOpenAI({
  maxTokens: 25,
  streaming: true,
  callbacks: [
    {
      handleLLMNewToken(token: string) {
        outStream.write(token);
      },
    },
  ],
});

const response = await chat.call([new HumanMessage("Tell me a joke.")]);

console.log(response);

In this code, we're using the 'Writable' class from the 'stream' module to create a writable stream. The 'write' method is called whenever data is written to the stream. In this case, it writes the data to the standard output and then calls the callback function to signal that it's ready to receive more data. This should resolve the compilation errors related to 'TransformStream', 'WritableStreamDefaultWriter', and 'ReadableStream' that you're encountering while porting your VSCode extension from @azure/openai to langchainjs.

You can find similar examples in the langchainjs repository:

Please try this solution and let me know if it resolves your issue. If you have any other questions or need further clarification, feel free to ask.

Best regards.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

akudo7 commented 1 year ago

@dosu-bot

Thanks for reply. Here is my reproducible as the simplest. It's just in case tho. https://bitbucket.org/kudoa/hello-langchain/src/main/

extension.ts

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

import { ReadableStream, WritableStream, TransformStream } from 'web-streams-polyfill/ponyfill/es2018';
(global as any).TransformStream = TransformStream;
(global as any).ReadableStream = ReadableStream;
(global as any).WritableStream = WritableStream;

import { AzureOpenAIInput, ChatOpenAI, ChatOpenAICallOptions } from 'langchain/chat_models/openai';
import { HumanMessage, SystemMessage, AIMessage } from "langchain/schema";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "hello-langchain" is now active!');

    const client = new ChatOpenAI(
        {
          azureOpenAIApiVersion: '2023-09-01-preview',
          azureOpenAIApiKey: '...',
          azureOpenAIApiDeploymentName: '...',
          azureOpenAIApiInstanceName: '...',
          temperature: 1.0,
          maxTokens: 2048,
          topP: 1,
          frequencyPenalty: 0.0,
          presencePenalty: 0.0,
        }
      );
      if (!client) {
        throw new Error("Failed to initializethe OpenAIClient");
      }

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = vscode.commands.registerCommand('hello-langchain.helloWorld', () => {
        // The code you place here will be executed every time your command is executed
        // Display a message box to the user
        vscode.window.showInformationMessage('Hello World from hello-langchain!');
    });

    context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
export function deactivate() {}

package.json

  "scripts": {
    "vscode:prepublish": "yarn run compile",
    "compile": "tsc -p ./",
    "watch": "tsc -watch -p ./",
    "pretest": "yarn run compile && yarn run lint",
    "lint": "eslint src --ext ts",
    "test": "node ./out/test/runTest.js"
  },
  "devDependencies": {
    "@types/glob": "^8.1.0",
    "@types/mocha": "^10.0.1",
    "@types/node": "20.2.5",
    "@types/vscode": "^1.83.0",
    "@typescript-eslint/eslint-plugin": "^5.59.8",
    "@typescript-eslint/parser": "^5.59.8",
    "@vscode/test-electron": "^2.3.2",
    "eslint": "^8.41.0",
    "glob": "^8.1.0",
    "mocha": "^10.2.0",
    "typescript": "^5.1.3"
  },
  "dependencies": {
    "add": "^2.0.6",
    "langchain": "^0.0.169",
    "web-streams-polyfill": "^3.2.1",
    "yarn": "^1.22.19"
  }

Thanks

dosubot[bot] commented 1 year ago

🤖

Hello,

From the error messages you've shared, it seems like the TypeScript compiler is unable to find the definitions for 'TransformStream', 'WritableStreamDefaultWriter', and 'ReadableStream'. These are standard interfaces for working with streaming data in JavaScript, and they are expected to be globally available in modern browsers. However, Node.js uses a different set of streaming interfaces, which is why you're trying to polyfill them using the 'web-streams-polyfill' package.

The way you're importing and assigning these classes to the global object seems correct. However, the TypeScript compiler might not be aware that these classes are now available globally. To fix this, you can try adding type declarations for these classes in a global.d.ts file in your project:

declare global {
  const TransformStream: typeof import('web-streams-polyfill/ponyfill/es2018').TransformStream;
  const ReadableStream: typeof import('web-streams-polyfill/ponyfill/es2018').ReadableStream;
  const WritableStream: typeof import('web-streams-polyfill/ponyfill/es2018').WritableStream;
}

This tells TypeScript that these classes are available globally and what their types are.

Regarding the specific usage of these classes in the langchainjs codebase, I'm afraid I don't have that information. However, these classes are typically used for handling streaming data, so they might be used in langchainjs for streaming responses from language models, for example.

As for the specific requirements or dependencies for these classes in the langchainjs codebase, the script create-entrypoints.js specifies two lists of entrypoints that have special requirements. However, these lists seem to be more related to the structure of the langchainjs codebase and its compatibility with different environments, rather than the usage of the 'TransformStream', 'WritableStreamDefaultWriter', and 'ReadableStream' classes specifically.

I hope this helps! Let me know if you have any other questions.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

akudo7 commented 1 year ago

@dosu-bot

Thanks for your reply and helpful advice.

It works for compile errors, but it shows some errors below at the runtime.

[12:07:49 AM] Starting compilation in watch mode...

node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:89:32 - error TS2749: 'TransformStream' refers to a value, but is being used as a type here. Did you mean 'typeof TransformStream'?

89     protected transformStream: TransformStream;
                                  ~~~~~~~~~~~~~~~

node_modules/langchain/dist/callbacks/handlers/log_stream.d.ts:90:13 - error TS2304: Cannot find name 'WritableStreamDefaultWriter'.

90     writer: WritableStreamDefaultWriter;
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/chat_models/base.d.ts:32:64 - error TS2749: 'TransformStream' refers to a value, but is being used as a type here. Did you mean 'typeof TransformStream'?

32 export declare function createChatMessageChunkEncoderStream(): TransformStream<BaseMessageChunk, any>;
                                                                  ~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/async_caller.d.ts:44:38 - error TS2304: Cannot find name 'fetch'.

44     fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
                                        ~~~~~

node_modules/langchain/dist/util/async_caller.d.ts:44:65 - error TS2304: Cannot find name 'fetch'.

44     fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
                                                                   ~~~~~

node_modules/langchain/dist/util/stream.d.ts:2:13 - error TS2304: Cannot find name 'ReadableStreamDefaultReader'.

2     reader: ReadableStreamDefaultReader<T>;
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:4:21 - error TS2304: Cannot find name 'ReadableStreamReadResult'.

4     next(): Promise<ReadableStreamReadResult<T>>;
                      ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:9:5 - error TS2425: Class 'ReadableStream<T>' defines instance member property '[Symbol.asyncIterator]', but extended class 'IterableReadableStream<T>' defines it as instance member function.

9     [Symbol.asyncIterator](): this;
      ~~~~~~~~~~~~~~~~~~~~~~

node_modules/langchain/dist/util/stream.d.ts:10:42 - error TS2749: 'ReadableStream' refers to a value, but is being used as a type here. Did you mean 'typeof ReadableStream'?

10     static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
                                            ~~~~~~~~~~~~~~

node_modules/langsmith/dist/utils/async_caller.d.ts:36:38 - error TS2304: Cannot find name 'fetch'.

36     fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
                                        ~~~~~

node_modules/langsmith/dist/utils/async_caller.d.ts:36:65 - error TS2304: Cannot find name 'fetch'.

36     fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
                                                                   ~~~~~

[12:07:51 AM] Found 11 errors. Watching for file changes.

Thanks!

dosubot[bot] commented 1 year ago

🤖

Hello,

Based on the errors you're encountering, it seems like the issue is related to the TypeScript compiler not recognizing the types for the web streams and fetch APIs. This is likely because these APIs are part of the web platform, and not included in the standard TypeScript lib files that are meant for a Node.js environment.

The web-streams-polyfill package you're using provides the runtime implementation for these APIs, but it doesn't include TypeScript type definitions. Therefore, you'll need to install the @types/web-streams-polyfill package to provide the TypeScript compiler with the necessary type information.

You can install it using the following command:

npm install --save-dev @types/web-streams-polyfill

After installing the types, you should update your TypeScript configuration to include the web-streams-polyfill types. You can do this by adding "web-streams-polyfill" to the types array in your tsconfig.json file:

{
  "compilerOptions": {
    "types": ["web-streams-polyfill", ...other types]
  }
}

For the fetch API, you can use the node-fetch package which provides a Node.js compatible implementation and includes TypeScript type definitions. You can install it using the following command:

npm install --save node-fetch

Then, you can import it in your code like this:

import fetch from 'node-fetch';
(global as any).fetch = fetch;

I hope this helps! Let me know if you have any other questions.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

jacoblee93 commented 1 year ago

Hey @akudo7, what environment are you in?

For Node, we require 18 or later. You can use the following instructions for Node 16 (though note that they may not work in the future, and we recommend updating):

https://js.langchain.com/docs/get_started/installation#unsupported-nodejs-16

Loong1512 commented 1 year ago

@akudo7 I had the same problem as you and I made sure my node version was greater than 18.

I finally solved the problem by modifying the npm path in vscode adding "typescript.npm": "/home/long/.nvm/versions/node/v21.1.0/bin/npm" to settings.json. you can use which -a npm to get your npm path

The problem seems to be that no matter what your node version is, vscode extension debug env is node 16, this issue is discussed in this link https://github.com/microsoft/vscode-discussions/discussions/587

jacoblee93 commented 1 year ago

Oh dear, thank you for digging in. Now that 16 is EOL, hopefully MS will fix this soon!

akudo7 commented 8 months ago

I decided to use the LangChain on the Flowise. So calling Flowise API in the VSCode extension is a workaround for me. Thanks.