langchain-ai / langchainjs

πŸ¦œπŸ”— Build context-aware reasoning applications πŸ¦œπŸ”—
https://js.langchain.com/docs/
MIT License
12.29k stars 2.08k forks source link

const {OpenAIEmbeddings} = require("langchain/embeddings/openai") #3240

Closed Santy1422 closed 10 months ago

Santy1422 commented 10 months ago

Hello, I've been here for 3 days with the error in that function, I tried importing the library anyway and it doesn't work.It used to work before

` import { pineIndex } from "./init"; import { OPENAI_API_KEY } from "../../config/env"; import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; const {OpenAIEmbeddings} = require("langchain/embeddings/openai")

export default async (docs, company: string) => { for (const doc of docs) { console.log(Procesando doc: ${doc.metadata.pdf_originalname}); const pdfFileName = doc.metadata.pdf_originalname; const text = doc.pageContent;

const textSplitter = new RecursiveCharacterTextSplitter({
  chunkSize: 1000,
});
console.log('Separando texto en chunks...');

const chunks = await textSplitter.createDocuments([text]);
console.log("chunks  ", chunks)
console.log(`Text split into ${chunks.length} chunks`);
console.log(
  `Calling OpenAI's Embedding endpoint documents with ${chunks.length} text chunks ...`
);
const embeddingsArrays = await new OpenAIEmbeddings({ openAIApiKey: OPENAI_API_KEY }).embedDocuments(
  chunks.map((chunk) => chunk.pageContent.replace(/\n/g, " "))
);
console.log('Finished embedding documents');
console.log(
  `Creating ${chunks.length} vectors array with id, values, and metadata...`
);

const batchSize = 100;
let batch: any = [];

for (let idx = 0; idx < chunks.length; idx++) {
  try {
    const chunk = chunks[idx];
    const vector = {
      id: `${pdfFileName}_${idx}`,
      values: embeddingsArrays[idx],
      metadata: {
        ...chunk.metadata,
        loc: JSON.stringify(chunk.metadata.loc),
        pageContent: chunk.pageContent,
      },
    };
    batch.push(vector);
    console.log('Embedding dimension:', embeddingsArrays[0].length);
    console.log('Pinecone index dimension:', batchSize);
    if (batch.length === batchSize || idx === chunks.length - 1) {
      // Upsert vectors to the namespace
      await pineIndex.upsert({
        upsertRequest: {
          vectors: batch,
          namespace: company,
        },
      });
      // Empty the batch
      batch = [];
    }
  } catch (error) {
    console.error('Error upserting vectors:', error);
  }
}
console.log(`Pinecone index updated with ${chunks.length} vectors`);

} }; `

Packet Json { "name": "whatsapp-botgpt", "version": "1.0.0", "description": "Chat de whatsapp bot", "main": "index.js", "directories": { "doc": "docs", "test": "test" }, "engines": { "node": "v18.17.1" }, "scripts": { "test": "jest --detectOpenHandles", "postinstall": "npm run tsc", "tsc": "tsc", "dev": "nodemon ./src/index.ts", "start": "npm run tsc && NODE_ENV=PRODUCTION node build/index.js", "lint": "eslint src/*/.js --fix", "whatsapp": "npm run tsc && NODE_ENV=PRODUCTION node build/Whatsapp-MicroService/index.js" }, "keywords": [], "author": "", "license": "ISC", "bugs": { "url": "https://github.com/Santy1422/WhatsappBot/issues" }, "homepage": "https://github.com/Santy1422/WhatsappBot", "devDependencies": { "@types/bcrypt": "^5.0.0", "@types/express": "^4.17.17", "@types/jest": "^29.5.2", "@types/multer": "^1.4.7", "@types/node": "^20.4.1", "@types/validator": "^13.7.17", "@types/xml2js": "^0.4.11", "@typescript-eslint/parser": "^5.61.0", "eslint": "^7.9.0", "eslint-config-airbnb": "^18.2.0", "eslint-config-airbnb-base": "^14.2.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-import": "^2.22.0", "eslint-plugin-prettier": "^3.1.4", "jest": "^26.4.2", "nodemon": "^2.0.4", "prettier": "^2.1.2", "ts-node": "^10.9.1", "ts-node-dev": "^2.0.0", "typescript": "^5.1.6" }, "dependencies": { "@pinecone-database/pinecone": "^0.1.6", "async": "^3.2.4", "axios": "^1.3.0", "axios-rate-limit": "^1.3.0", "bcrypt": "^5.1.0", "body-parser": "^1.20.2", "cors": "^2.8.5", "dotenv": "^8.6.0", "express": "^4.18.2", "express-jwt": "^8.3.0", "express-openid-connect": "^2.11.0", "express-rate-limit": "^6.7.0", "firebase-admin": "^11.9.0", "fs": "^0.0.1-security", "jsonwebtoken": "^9.0.0", "jspdf": "^2.5.1", "jwks-rsa": "^3.0.1", "langchain": "0.0.144", "mongodb": "^5.2.0", "mongoose": "^6.11.3", "morgan": "^1.10.0", "multer": "^1.4.5-lts.1", "openai": "^4.0.0", "path": "^0.12.7", "pdf-parse": "^1.1.1", "qrcode-terminal": "^0.12.0", "socket.io-client": "^4.7.2", "stripe": "^13.4.0", "uuid": "^9.0.1", "validator": "^13.7.0" }, "peerDependencies": { "mongodb": "^5.2.0" }, "prettier": { "printWidth": 900, "semi": true, "singleQuote": true, "trailingComma": "es5" } }

dosubot[bot] commented 10 months ago

πŸ€–

Hello again @Santy1422!

Nice to see you here. I remember our last interaction. I'm currently looking into the issue you're facing and will get back to you with a comprehensive response shortly.

dosubot[bot] commented 10 months ago

πŸ€–

Hello @Santy1422! Good to see you again.

Based on the error you're encountering, it seems like the OpenAIEmbeddings class is not being exported from the openai.ts file in the langchain package. This is a common issue that has been reported by other users as well, and you can find similar solved issues in the langchainjs repository here, here, and here.

To resolve this issue, you need to add an export statement for the OpenAIEmbeddings class in the openai.ts file. Here's how you can do it:

export class OpenAIEmbeddings
  extends Embeddings
  implements OpenAIEmbeddingsParams, AzureOpenAIInput
{
  // ... rest of the class definition
}

After adding the export keyword before the class definition, you should be able to import OpenAIEmbeddings from langchain/embeddings/openai without any issues.

This solution is based on the information provided in the langchainjs codebase, specifically the openai.ts file.

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

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.

Santy1422 commented 10 months ago

i fixed "langchain": "git+https://github.com/Santy1422/lanchain-embeding.fix.git#main",