Feature/Add multi query retriever (#2676)

add multi query retriever
This commit is contained in:
Henry Heng
2024-06-19 14:52:58 +01:00
committed by GitHub
parent b5ead0745b
commit 21743656a8
2 changed files with 84 additions and 0 deletions
@@ -0,0 +1,83 @@
import { PromptTemplate } from '@langchain/core/prompts'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { MultiQueryRetriever } from 'langchain/retrievers/multi_query'
const defaultPrompt = `You are an AI language model assistant. Your task is
to generate 3 different versions of the given user
question to retrieve relevant documents from a vector database.
By generating multiple perspectives on the user question,
your goal is to help the user overcome some of the limitations
of distance-based similarity search.
Provide these alternative questions separated by newlines between XML tags. For example:
<questions>
Question 1
Question 2
Question 3
</questions>
Original question: {question}`
class MultiQueryRetriever_Retrievers implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
constructor() {
this.label = 'Multi Query Retriever'
this.name = 'multiQueryRetriever'
this.version = 1.0
this.type = 'MultiQueryRetriever'
this.icon = 'multiQueryRetriever.svg'
this.category = 'Retrievers'
this.description = 'Generate multiple queries from different perspectives for a given user input query'
this.baseClasses = [this.type, 'BaseRetriever']
this.inputs = [
{
label: 'Vector Store',
name: 'vectorStore',
type: 'VectorStore'
},
{
label: 'Language Model',
name: 'model',
type: 'BaseLanguageModel'
},
{
label: 'Prompt',
name: 'modelPrompt',
description:
'Prompt for the language model to generate alternative questions. Use {question} to refer to the original question',
type: 'string',
rows: 4,
default: defaultPrompt
}
]
}
async init(nodeData: INodeData, input: string): Promise<any> {
const model = nodeData.inputs?.model
const vectorStore = nodeData.inputs?.vectorStore
let prompt = nodeData.inputs?.modelPrompt || (defaultPrompt as string)
prompt = prompt.replaceAll('{question}', input)
const retriever = MultiQueryRetriever.fromLLM({
llm: model,
retriever: vectorStore.asRetriever(),
verbose: process.env.DEBUG === 'true',
// @ts-ignore
prompt: PromptTemplate.fromTemplate(prompt)
})
return retriever
}
}
module.exports = { nodeClass: MultiQueryRetriever_Retrievers }
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-relation-one-to-many"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M3 5m0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v10a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z" /><path d="M7 10h1v4" /><path d="M14 14v-4l3 4v-4" /><path d="M11 10.5l0 .01" /><path d="M11 13.5l0 .01" /></svg>

After

Width:  |  Height:  |  Size: 524 B