Pinecone vectorstore is not working as expected with langchain

Hello, I’m using the Pinecone vector store. However, it’s not working as expected and not retrieving the matching documents from the store, even though I have the matching query documents in the store.

import { StreamingTextResponse, LangChainAdapter } from 'ai';
import { ChatOpenAI } from '@langchain/openai';
import {
  RunnablePassthrough,
  RunnableSequence,
} from '@langchain/core/runnables';
import { formatDocumentsAsString } from 'langchain/util/document';

import {
  ANSWER_GENERATION_CHAIN_PROMPT,
  EMBEDDING_MODEL,
  STANDALONE_QUESTION_PROMPT,
} from '@/constants/chat';
import { formatMessage } from '@/lib/utils';
import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';

import { tools } from './engine/tools';
import { StringOutputParser } from '@langchain/core/output_parsers';
import { PineconeStore } from '@langchain/pinecone';

export const dynamic = 'force-dynamic';

export async function POST(req: Request) {
  try {
    const body = await req.json();
    const { messages, nameSpace } = body;

    const formattedPreviousMessages = messages.slice(0, -1).map(formatMessage);

    const currentMessageContent = messages[messages.length - 1].content;

    const vectorStore = await PineconeStore.fromExistingIndex(EMBEDDING_MODEL, {
      pineconeConfig: {
        config: {
          apiKey: process.env.PINECONE_API_KEY!,
        },
        indexName: process.env.PINECONE_INDEX!,
        namespace: nameSpace,
      },
      onFailedAttempt: (error) => {
        console.error('Failed attempt', error);
      },
    });

    // Standalone question chain to retrieve to best documents
    const standaloneQuestionChain = RunnableSequence.from([
      RunnablePassthrough.assign({
        conversation_history: (input) => input.chat_history,
        question: (input) => input.question,
      }),
      STANDALONE_QUESTION_PROMPT,
      new ChatOpenAI({
        apiKey: process.env.OPENAI_API_KEY!,
        model: 'gpt-4o-mini',
        temperature: 0,
        verbose: true,
      }),
      new StringOutputParser(),
    ]);

    // Document retrieval chain
    const retriever = vectorStore.asRetriever();
    const documentRetrievalChain = RunnableSequence.from([
      // get the question from the input
      (input) => input.question,

      // retrieve the document from the vector store using the question
      retriever,

      // format the retrieved document as a string
      formatDocumentsAsString,
    ]);

    // Create the LLM model
    const llmModel = new ChatOpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
      model: 'gpt-4o-mini',
      // set the temperature to 0 to get deterministic results
      temperature: 0,
      streaming: true,
      verbose: true,
    });

    // Create the OpenAI tools agent
    const openAIToolsAgent = await createOpenAIToolsAgent({
      llm: llmModel,
      tools,
      prompt: ANSWER_GENERATION_CHAIN_PROMPT,
    });

    // Create the agent executor
    const openAIToolsAgentExecutor = new AgentExecutor({
      agent: openAIToolsAgent,
      tools,
    });

    const retrievalChain = RunnableSequence.from([
      RunnablePassthrough.assign({
        question: standaloneQuestionChain,
        chat_history: (input) => input.chat_history,
        original_message: (input) => input.question,
      }),
      {
        context: documentRetrievalChain,
        chat_history: (input) => input.chat_history,
        question: (input) => input.original_message,
      },
      openAIToolsAgentExecutor,
      {
        content: (input) => {
          return input.output;
        },
      },
    ]);

    // Execute the retrieval chain
    const stream = await retrievalChain.stream({
      question: currentMessageContent,
      chat_history: formattedPreviousMessages,
    });

    const aiStream = LangChainAdapter.toAIStream(stream);

    // Respond with the stream
    return new StreamingTextResponse(aiStream);
  } catch (e: any) {
    return Response.json({ error: e.message }, { status: e.status ?? 500 });
  }
}

Query: What are stars and forks count?

Thanks for the response, the issue has been resolved.

had to pass the namespace in PineconeStore.fromExistingIndex.

1 Like