langchain-ai / langchainjs

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

ReferenceError: ReadableStream is not defined #2166

Closed alexkreidler closed 11 months ago

alexkreidler commented 11 months ago
ReferenceError: ReadableStream is not defined
    at file:///home/alex/toky/backend/v4/node_modules/.pnpm/langchain@0.0.122_axios@1.4.0_cheerio@1.0.0-rc.12_playwright@1.36.2_puppeteer@21.0.1/node_modules/langchain/dist/util/stream.js:41:45
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)
jacoblee93 commented 11 months ago

Can you let me know what version of Node/what environment you're using?

ojpetar commented 11 months ago

I am running into the same issue - node 18.15.0

jacoblee93 commented 11 months ago

Is there any other information you can give? Maybe what method you're calling?

ReadableStream has been defined since Node 18.0.0:

https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility

jimmy-vo commented 11 months ago

@jacoblee93 thanks for pointing out the node version. In my case updating from node:16-slim to node:18-slim make it work

alexkreidler commented 11 months ago

@jacoblee93 I'm running Node v18.14.0 with:

"langchain": "^0.0.122",
"openai": "^3.3.0",

Here's my code: (sorry for the sparse first report)

import { ChatOpenAI } from "langchain/chat_models/openai";
import { config } from "dotenv";
import findConfig from "find-config";

config({ path: findConfig(".env") ?? ".env" });

export const MODEL = new ChatOpenAI(
  { temperature: 0 },
  { apiKey: process.env.OPENAI_API_KEY, basePath: process.env.OPENAI_API_BASE }
);

const simpleChatCall = async (prompt: string) => (await MODEL.call([
  new HumanMessage(prompt)
])).content

function generateSearchQueries(question: string): string {
  return `
        Write 4 google search queries to search online that form an objective opinion from the following: "${question}"
        You must respond with a list of strings in the following format: ["query 1", "query 2", "query 3", "query 4"]
    `;
}

console.log(await simpleChatCall(generateSearchQueries("langchain")))
justinlevi commented 11 months ago

Seeing the same issue on node v 18.16.0

chatbot git:(main)✗  🚀 node --version            
v18.16.0
 chatbot git:(main)✗  🚀  cd /Users/justinwinter/projects/decoupled-days-demos/chatbot ; /usr/bin/env 'NODE_OPTIONS=--require "/Applications/Visual Studio Code.app/Contents/Resou
rces/app/extensions/ms-vscode.js-debug/src/bootloader.js" --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/var/folders/b1/m_45cj4x321b0c7m7x0p1vl40000gn/T/
node-cdp.65742-ea7db1c1-91.sock","deferredMode":false,"waitForDebugger":"","execPath":"/Users/justinwinter/.nvm/versions/node/v16.20.0/bin/node","onlyEntrypoint":false,"autoAttac
hMode":"always","fileCallback":"/var/folders/b1/m_45cj4x321b0c7m7x0p1vl40000gn/T/node-debug-callback-85b21ebf99f35705"}' /Users/justinwinter/.nvm/versions/node/v16.20.0/bin/node 
-r ts-node/register -r tsconfig-paths/register ./ingest/ingest.ts 
Debugger attached.
/Users/justinwinter/projects/decoupled-days-demos/chatbot/node_modules/langchain/dist/util/stream.cjs:45
class IterableReadableStream extends ReadableStream {
                                     ^
ReferenceError: ReadableStream is not defined
    at Object.<anonymous> (/Users/justinwinter/projects/decoupled-days-demos/chatbot/node_modules/langchain/dist/util/stream.cjs:45:38)
    at Module._compile (node:internal/modules/cjs/loader:1196:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1250:10)
    at Object.require.extensions.<computed> [as .js] (/Users/justinwinter/projects/decoupled-days-demos/chatbot/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (/Users/justinwinter/projects/decoupled-days-demos/chatbot/node_modules/langchain/dist/schema/runnable.cjs:6:21)
    at Module._compile (node:internal/modules/cjs/loader:1196:14)
Waiting for the debugger to disconnect...
 chatbot git:(main)✗  🚀 

here's my code:

 import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { Document } from "langchain/document";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { PromptTemplate } from "langchain/prompts";
import { LLMChain } from "langchain/chains";
import { OpenAI } from "langchain/llms/openai";

// JSON API Export from Drupal
import composetheweb from "./composetheweb.json";

const llm = new OpenAI({ openAIApiKey: process.env.OPENAI_API_KEY!, temperature: 0 });

const prompt = PromptTemplate.fromTemplate(`
You are a content tagging bot for a food blog.
You are given a recipe and you need to tag it with the appropriate tags.

Recipe: {{recipe}}

Tags:
`)

const chain = new LLMChain({ llm, prompt })
const generateTagsFromContent = async (content: string) => {
  return chain.call({ recipe: content })
}

export const run = async () => {

  const docsPromises = composetheweb.data
    .filter((doc) => doc.type === 'node--recipe' || doc.type === 'node--article')
    .map(async (doc) => {
      if (doc.type === 'node--recipe') {
        const pageContent =  
        `
        Title: ${doc.attributes.title}, 
        Difficulty: ${doc.attributes.field_difficulty}, 
        Ingredients: ${doc.attributes.field_ingredients}, 
        Recipe: ${doc.attributes.field_recipe_instruction?.value.replace(/<[^>]+>/g, "").replace(/\n/g, "")}, 
        Summary: ${doc.attributes.field_summary?.value.replace(/<[^>]+>/g, "").replace(/\n/g, "")}
        `

        return new Document({
          metadata: {
            tags: await generateTagsFromContent(pageContent),
            source: doc.links.self.href
          },
          pageContent
        })
      }
      const pageContent = `Title: ${doc.attributes.title} - ${doc.attributes.body!.value.replace(/<[^>]+>/g, "").replace(/\n/g, "")}`
      return new Document({ metadata: { source: doc.links.self.href }, pageContent})
    })

  // Wait for all promises to resolve
  const docs: Document[] = await Promise.all(docsPromises);

  console.log("Creating vector store...");
  /* Create the vectorstore */
  const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
  await vectorStore.save("data");
};

(async () => {
  await run();
  console.log("done");
})();
jacoblee93 commented 11 months ago

@justinlevi I see this in your stack trace:

"execPath":"/Users/justinwinter/.nvm/versions/node/v16.20.0/bin/node"

It looks like your local CLI version is different from whatever you're running things with - maybe something in your nvm config?

jacoblee93 commented 11 months ago

I'm fairly confident that something like the above comment is the result of everyone's issue here - can folks triple check?

justinlevi commented 11 months ago

Ugh. 100% that was the issue. My debug terminal was on node 16. Thanks

On Thu, Aug 10, 2023 at 6:40 PM Jacob Lee @.***> wrote:

I'm fairly confident that this is the result of everyone's issue here - can folks triple check?

— Reply to this email directly, view it on GitHub https://github.com/hwchase17/langchainjs/issues/2166#issuecomment-1674016108, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIQ6JLSSPJY2VVATSQRNLDXUVPNPANCNFSM6AAAAAA3EXHQAU . You are receiving this because you were mentioned.Message ID: @.***>

jacoblee93 commented 11 months ago

No problem! Glad it's resolved.

lucas-felinto commented 10 months ago

@jacoblee93 any workaround to run in with node 16? I'm using a twilio serverless service but the engine does no supports node 18 yet. Any advice will be appreciated

jacoblee93 commented 10 months ago

Hey @lucas-felinto, you can try the instructions here:

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

lucas-felinto commented 10 months ago

@jacoblee93 forgot to mention that I've tried but without success (sorry about it), receiving "ReadableStream is not defined" error, but I believe it is related to the serverless environment

jacoblee93 commented 10 months ago

I'm adding additional polyfill instructions in #2387. You'll see these instructions under: https://js.langchain.com/docs/get_started/installation#unsupported-nodejs-16

greysonthao commented 9 months ago

@justinlevi I see this in your stack trace:

"execPath":"/Users/justinwinter/.nvm/versions/node/v16.20.0/bin/node"

It looks like your local CLI version is different from whatever you're running things with - maybe something in your nvm config?

This was the case for me. I was using node 18 in my terminal but in the terminal in my IDE, I was running Node 16. I changed it to Node 18 and viola, it worked!

jun-low commented 9 months ago

Same case and update to Node v18.18.2 works 🚀