Get (document, similarity score) pairs from retriever #4595
-
Checked other resources
Commit to Help
Example Codepublic static createConversationalRetrievalChain(
args: ConversationalRetrievalChainParams,
onDocumentsRetrieved: (documents: Document[]) => void,
): RunnableSequence {
const { llm, retriever } = args;
const condenseQuestionTemplate = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`;
const CONDENSE_QUESTION_PROMPT = PromptTemplate.fromTemplate(
condenseQuestionTemplate
);
const answerTemplate = `Answer the question with as detailed as possible based only on the following context:
{context}
Question: {question}
`;
const ANSWER_PROMPT = PromptTemplate.fromTemplate(answerTemplate);
const formatChatHistory = (chatHistory: [string, string][]) => {
const formattedDialogueTurns = chatHistory.map(
(dialogueTurn) => `Human: ${dialogueTurn[0]}\nAssistant: ${dialogueTurn[1]}`
);
return formattedDialogueTurns.join("\n");
};
const standaloneQuestionChain = RunnableSequence.from([
{
question: (input: ConversationalRetrievalQAChainInput) => input.question,
chat_history: (input: ConversationalRetrievalQAChainInput) =>
formatChatHistory(input.chat_history),
},
CONDENSE_QUESTION_PROMPT,
llm,
new StringOutputParser(),
]);
const formatDocumentsAsStringAndStore = async (documents: Document[]) => {
// Store or log documents for debugging
onDocumentsRetrieved(documents);
return formatDocumentsAsString(documents);
};
const answerChain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsStringAndStore),
question: new RunnablePassthrough(),
},
ANSWER_PROMPT,
llm,
]);
const conversationalRetrievalQAChain = standaloneQuestionChain.pipe(answerChain);
return conversationalRetrievalQAChain as RunnableSequence;
} DescriptionNeed guidance on getting the (doc, similarity score) pairs from this LCEL setup. I have a callback System Infonode v19.8.1 |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
🤖 Hey @logancyang, great to see you diving into another challenge with langchainjs! Let's tackle this one together. 🚀 Based on the code you've provided, it seems you're trying to retrieve both the documents and their similarity scores from the retriever. However, the current implementation of the To achieve this, you would need to modify or extend the retriever to also return the similarity scores. Here's a general approach:
class ScoreThresholdRetrieverWithScores extends ScoreThresholdRetriever {
async getRelevantDocumentsWithScores(query: string): Promise<Array<{document: Document, score: number}>> {
// Your implementation here
}
}
Please note that this is a general approach and the actual implementation would depend on the internals of the For more information on the structure of the I hope this helps! If you have any more questions or need further clarification, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
-
@dosu-bot Assuming that I implemented this |
Beta Was this translation helpful? Give feedback.
-
Hey @logancyang, this is something we've been looking into a bit. The tricky thing is that we don't want to store scores as metadata on the documents themselves since those can be resaved to a store later. If you're ok with that happening, you could create your own custom retriever: https://js.langchain.com/docs/modules/data_connection/retrievers/custom Or you could just wrap the vectorstore directly in something like a |
Beta Was this translation helpful? Give feedback.
-
@dosu-bot actually, how do I add console.log to show the output of
|
Beta Was this translation helpful? Give feedback.
-
@dosu-bot how come I do not see the console log from the 2nd snippet you suggested, but can see them from the 1st snippet successfully? This is the snippet from which I do not see the message logged
|
Beta Was this translation helpful? Give feedback.
Hey @logancyang, this is something we've been looking into a bit. The tricky thing is that we don't want to store scores as metadata on the documents themselves since those can be resaved to a store later.
If you're ok with that happening, you could create your own custom retriever:
https://js.langchain.com/docs/modules/data_connection/retrievers/custom
Or you could just wrap the vectorstore directly in something like a
RunnableLambda
and return tuples.