Search code examples
javascriptopenai-apilangchain

How can I limit sourceDocs in langchain + pinecone response in JS?


I'm using langchain with pinecode, it gives me 4 sourceDocs but I want only most relevant 1 sourceDoc. I'm using javascript and don't know where to put top_k in code. Here's my code

export const makeChain = (vectorStore) => {
  const model = new OpenAI({
    temperature: 0
    modelName: 'gpt-3.5-turbo', 
  });

  const chain = ConversationalRetrievalQAChain.fromLLM(
    model,
    vectorStore.asRetriever(),
    {
      qaTemplate: QA_PROMPT,
      questionGeneratorTemplate: CONDENSE_PROMPT,
      returnSourceDocuments: true,
    }
  );
  return chain;
};
/* create vectorStore*/
    const vectorStore = await PineconeStore.fromExistingIndex(
      new OpenAIEmbeddings({}),
      {
        pineconeIndex: index,
        textKey: 'text',
        namespace: PINECONE_NAME_SPACE, //namespace comes from your config folder
      }
    );

    //create chain
    const chain = makeChain(vectorStore);
    //Ask a question using chat history
    const response = await chain.call({
      question: sanitizedQuestion,
      chat_history: history || [],
    });
    ```
    

Solution

  • The first parameter of the vectorStore.asRetriever() method is top_k, like vectorStore.asRetriever(1).