Always getting "undefined" for the source document

169 Views Asked by At

I'm using langchainJS with openAI for my local documents, and I wrote the below code, the source of the answer I get is always undefined:

import { OpenAI } from "langchain/llms/openai";
import { ConversationalRetrievalQAChain } from "langchain/chains";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { Chroma } from "langchain/vectorstores/chroma";
import { ChatOpenAI } from "langchain/chat_models/openai";
import readline from 'readline';

const rl = readline.createInterface({
    input: process.stdin,
});

/* Initialize the LLM to use to answer the question */
const model = new OpenAI({
    openAIApiKey: Bun.env.OPENAI_API_KEY,
    streaming: true,
    callbacks: [
        {
            handleLLMNewToken(token) {
                process.stdout.write(token.replace(/^\n/, ""));
            },
        },
    ],
});

const GPTchat = new ChatOpenAI({
    openAIApiKey: Bun.env.OPENAI_API_KEY,
    modelName: "gpt-3.5-turbo-0613",
    temperature: 0.9,
});

/* Load the vector database of the embedings */
const vectorStore = await Chroma.fromExistingCollection(
    new OpenAIEmbeddings(),
    { collectionName: "data" }
);

/* Create the chain */
const chain = ConversationalRetrievalQAChain.fromLLM(
    model,
    vectorStore.asRetriever(),
);

async function converse(prompt, chatHistory, senderId) {
    const followUpRes = await chain.call({
        question: prompt,
        chat_history: chatHistory,
        search: false,
        returnSourceDocuments: true
    });
    chatHistory = `${prompt}${followUpRes.text}`;
    console.log(followUpRes);
    return { text: followUpRes.text, source: followUpRes.source };
}

let firstRun = true;
let chatHistory = [];

async function chat() {
    process.stdout.write(firstRun ? "User: " : "\nUser: ");
    firstRun=false;
    const prompt = await new Promise((resolve) => rl.question("", resolve));
    process.stdout.write("AI GPT: ");
    const { text, source } = await converse(prompt, chatHistory, 1);
    if (source) {
        console.log(`\nSource: ${source}`);
    }
    chat(chatHistory);
}

chat();

Sample output:

hajsf@DESKTOP-JS1NVNB:~/wa$ bun qa.js
[0.02ms] ".env"
User: Hi
AI GPT:  Hi there! What can I help you with?{
  text: " Hi there! What can I help you with?",
  __run: undefined
}

User: When can I take my annual leave
AI GPT:  You must complete 12 months of service to be entitled to take your annual leave and you must submit your request in the ESS Portal to your department manager at least 3 months earlier.{
  text: " You must complete 12 months of service to be entitled to take your annual leave and you must submit your request in the ESS Portal to your department manager at least 3 months earlier.",
  __run: undefined
}
0

There are 0 best solutions below