mayooear / gpt4-pdf-chatbot-langchain

GPT4 & LangChain Chatbot for large PDF docs
https://www.youtube.com/watch?v=ih9PBGVVOO4
14.94k stars 3.02k forks source link

Error when trying to transition from GPT-4 to PaLM #423

Closed F4brizio closed 9 months ago

F4brizio commented 1 year ago

I'm trying to implement PaLM as a replacement for ChatGPT, but so far, I haven't had any luck.

My code:

chat-bot.ts

import type { NextApiRequest, NextApiResponse } from 'next';
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
import { GooglePaLMEmbeddings } from 'langchain/embeddings/googlepalm';
import { PineconeStore } from 'langchain/vectorstores/pinecone';
import { AIMessage, HumanMessage } from 'langchain/schema';
import { makeChain } from '@/utils/makechain-palm';
import { pinecone } from '@/utils/pinecone-client-palm';
import { PINECONE_INDEX_NAME, PINECONE_NAME_SPACE } from '@/config/pinecone-palm';

...

  const sanitizedQuestion = question.trim().replaceAll('\n', ' ');
      const index = pinecone.Index(PINECONE_INDEX_NAME);
      const vectorStore = await PineconeStore.fromExistingIndex(
          new GooglePaLMEmbeddings({
              apiKey: process.env.GOOGLE_PALM_API_KEY,
          }),
          {
              pineconeIndex: index,
              textKey: 'text',
              namespace: PINECONE_NAME_SPACE, //namespace comes from your config folder
          },
      );
      const pastMessages = history.map((message: string, i: number) => {
          if (i % 2 === 0) {
              console.log('HumanMessage', message, 'i', i);
              return new HumanMessage(message); // Assuming HumanMessage is the correct type
          } else {
              console.log('AIMessage', message, 'i', i);
              return new AIMessage(message); // Assuming AIMessage is the correct type
          }
      });
      const chain = makeChain(vectorStore);
      try {
          const response = await chain.call({
              question: sanitizedQuestion,
              chat_history: pastMessages || [],
          });
          console.log('response', response);
          res.status(200).json(response);

      } catch (error: any) {
          console.log('error', error);
          res.status(500).json({error: error.message || "Something went wrong"});
      }
  }

makechain-palm.ts

import {ChatGooglePaLM} from "langchain/chat_models/googlepalm";
import {PineconeStore} from 'langchain/vectorstores/pinecone';
import {ConversationalRetrievalQAChain} from 'langchain/chains';

let CONDENSE_TEMPLATE = ...
let QA_TEMPLATE = ...

export const makeChain = (vectorstore: PineconeStore) => {
    console.log('process.env.GOOGLE_PALM_API_KEY', process.env.GOOGLE_PALM_API_KEY);
    const modelPaLM = new ChatGooglePaLM({
        apiKey: process.env.GOOGLE_PALM_API_KEY,
        temperature: 0,
        modelName: "models/chat-bison-001"
    });
    return ConversationalRetrievalQAChain.fromLLM(
      modelPaLM,
      vectorstore.asRetriever(),
      {
          qaTemplate: QA_TEMPLATE,
          questionGeneratorTemplate: CONDENSE_TEMPLATE,
          returnSourceDocuments: true,
      },
  );
};

pinecone-client-palm.ts

import {Pinecone} from "@pinecone-database/pinecone";

if (!process.env.PINECONE_PALM_ENVIRONMENT || !process.env.PINECONE_PALM_API_KEY) {
  throw new Error('Pinecone environment or api key vars missing');
}

async function initPinecone() {
  try {
    return new Pinecone({
      environment: process.env.PINECONE_PALM_ENVIRONMENT ?? '',
      apiKey: process.env.PINECONE_PALM_API_KEY ?? '',
    });
  } catch (error) {
    console.log('error', error);
    throw new Error('Failed to initialize Pinecone Client');
  }
}

export const pinecone = await initPinecone();

package.json

  "dependencies": {
    "@google-ai/generativelanguage": "^0.2.1",
    "@radix-ui/react-accordion": "^1.1.1",
    "@pinecone-database/pinecone": "^0.0.14",
    "clsx": "^1.2.1",
    "dotenv": "^16.3.1",
    "google-auth-library": "^8.9.0",
    "pdf-parse": "^1.1.1",
    "next": "13.5.6",
    "react": "^18",
    "react-markdown": "^8.0.5",
    "lucide-react": "^0.125.0",
    "react-dom": "^18",
    "tailwind-merge": "^1.10.0",
    "langchain": "^0.0.151"
  },

I have the following error:

error TypeError: Cannot read properties of undefined (reading 'text')
    at NoOpOutputParser.parseResult (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/schema/output_parser.js:43:42)
    at NoOpOutputParser.parseResultWithPrompt (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/schema/output_parser.js:17:21)
    at LLMChain._getFinalOutput (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/llm_chain.js:93:55)
    at LLMChain._call (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/llm_chain.js:123:42)
    at async LLMChain.call (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/base.js:101:28)
    at async StuffDocumentsChain._call (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/combine_docs_chain.js:59:24)
    at async StuffDocumentsChain.call (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/base.js:101:28)
    at async ConversationalRetrievalQAChain._call (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/conversational_retrieval_chain.js:143:24)
    at async ConversationalRetrievalQAChain.call (file:///C:/Users/ASUS/Desktop/tmp-dev/chatbot/node_modules/langchain/dist/chains/base.js:101:28)
    at async handler (webpack-internal:///(api)/./pages/api/chat-bot.ts:51:26)

Any suggestions?

dosubot[bot] commented 1 year ago

🤖

Hello @F4brizio,

I'm here to assist you with your issue. I'm a bot designed to help solve bugs, answer questions, and guide you in becoming a contributor. While we wait for a human maintainer, let's see if we can figure this out together.

The error you're encountering, "TypeError: Cannot read properties of undefined (reading 'text')", typically occurs when you're trying to access a property of an object that hasn't been defined. In your case, it seems like this error might be related to the vectorStore object.

Here are a few things you could try:

  1. Check if the PineconeStore.fromExistingIndex method is returning a properly initialized vectorStore object. If the vectorStore object is undefined, then trying to access its text property would throw this error.

  2. Verify if pinecone.Index(PINECONE_INDEX_NAME) is returning a valid index. If the index object is undefined, then the PineconeStore.fromExistingIndex method might fail to initialize the vectorStore object properly.

  3. Ensure that OpenAIEmbeddings({}) is returning a valid embeddings object. If the embeddings object is undefined, then the PineconeStore.fromExistingIndex method might fail to initialize the vectorStore object properly.

To debug this issue, you could add some console.log statements to check the values of index, vectorStore, and embeddings:

try {
  const index = pinecone.Index(PINECONE_INDEX_NAME);
  console.log('index', index);

  const embeddings = new OpenAIEmbeddings({});
  console.log('embeddings', embeddings);

  const vectorStore = await PineconeStore.fromExistingIndex(
    embeddings,
    {
      pineconeIndex: index,
      textKey: 'text',
      namespace: PINECONE_NAME_SPACE,
    },
  );
  console.log('vectorStore', vectorStore);

  // rest of your code...
} catch (error: any) {
  console.log('error', error);
  res.status(500).json({ error: error.message || 'Something went wrong' });
}

This will help you identify which object is undefined and causing the error.

If you're still encountering issues after trying these steps, please provide more information about the context in which this error is occurring. This could include additional code snippets, error messages, or descriptions of what you're trying to achieve.

I hope this helps! Let me know how it goes.

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.

dosubot[bot] commented 9 months ago

Hi, @F4brizio,

I'm helping the gpt4-pdf-chatbot-langchain team manage their backlog and am marking this issue as stale. The issue involved a TypeError when transitioning from GPT-4 to PaLM in a chatbot implementation, specifically when trying to access the 'text' property, resulting in a "Cannot read properties of undefined" error. Dosubot provided suggestions for resolving the issue, including checking the initialization of the vectorStore object and adding console.log statements to debug the problem. The issue has been resolved by implementing these suggestions.

Could you please confirm if this issue is still relevant to the latest version of the gpt4-pdf-chatbot-langchain repository? If it is, please let the gpt4-pdf-chatbot-langchain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you!

mpakaris commented 4 weeks ago

the newest pinecone does not require environment variable