add multi retrieval qa chain

This commit is contained in:
Henry
2023-05-25 13:50:11 +01:00
parent 2802ee0142
commit 769b4ea567
7 changed files with 1023 additions and 1 deletions
@@ -0,0 +1,68 @@
import { BaseLanguageModel } from 'langchain/base_language'
import { INode, INodeData, INodeParams, VectorStoreRetriever } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { MultiRetrievalQAChain } from 'langchain/chains'
class MultiRetrievalQAChain_Chains implements INode {
label: string
name: string
type: string
icon: string
category: string
baseClasses: string[]
description: string
inputs: INodeParams[]
constructor() {
this.label = 'Multi Retrieval QA Chain'
this.name = 'multiRetrievalQAChain'
this.type = 'MultiRetrievalQAChain'
this.icon = 'chain.svg'
this.category = 'Chains'
this.description = 'QA Chain that automatically picks an appropriate vector store from multiple retrievers'
this.baseClasses = [this.type, ...getBaseClasses(MultiRetrievalQAChain)]
this.inputs = [
{
label: 'Language Model',
name: 'model',
type: 'BaseLanguageModel'
},
{
label: 'Vector Store Retriever',
name: 'vectorStoreRetriever',
type: 'VectorStoreRetriever',
list: true
}
]
}
async init(nodeData: INodeData): Promise<any> {
const model = nodeData.inputs?.model as BaseLanguageModel
const vectorStoreRetriever = nodeData.inputs?.vectorStoreRetriever as VectorStoreRetriever[]
const retrieverNames = []
const retrieverDescriptions = []
const retrievers = []
for (const vs of vectorStoreRetriever) {
retrieverNames.push(vs.name)
retrieverDescriptions.push(vs.description)
retrievers.push(vs.vectorStore.asRetriever())
}
const chain = MultiRetrievalQAChain.fromRetrievers(model, retrieverNames, retrieverDescriptions, retrievers, undefined, {
verbose: process.env.DEBUG === 'true' ? true : false
} as any)
return chain
}
async run(nodeData: INodeData, input: string): Promise<string> {
const chain = nodeData.instance as MultiRetrievalQAChain
const res = await chain.call({ input })
return res?.text
}
}
module.exports = { nodeClass: MultiRetrievalQAChain_Chains }
@@ -0,0 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-dna" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M14.828 14.828a4 4 0 1 0 -5.656 -5.656a4 4 0 0 0 5.656 5.656z"></path>
<path d="M9.172 20.485a4 4 0 1 0 -5.657 -5.657"></path>
<path d="M14.828 3.515a4 4 0 0 0 5.657 5.657"></path>
</svg>

After

Width:  |  Height:  |  Size: 489 B

@@ -0,0 +1,61 @@
import { VectorStore } from 'langchain/vectorstores/base'
import { INode, INodeData, INodeParams, VectorStoreRetriever, VectorStoreRetrieverInput } from '../../../src/Interface'
class VectorStoreRetriever_Retrievers implements INode {
label: string
name: string
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
constructor() {
this.label = 'Vector Store Retriever'
this.name = 'vectorStoreRetriever'
this.type = 'VectorStoreRetriever'
this.icon = 'vectorretriever.svg'
this.category = 'Retrievers'
this.description = 'Store vector store as retriever to be later queried by MultiRetrievalQAChain'
this.baseClasses = [this.type]
this.inputs = [
{
label: 'Vector Store',
name: 'vectorStore',
type: 'VectorStore'
},
{
label: 'Retriever Name',
name: 'name',
type: 'string',
placeholder: 'netflix movies'
},
{
label: 'Retriever Description',
name: 'description',
type: 'string',
rows: 3,
description: 'Description of when to use the vector store retriever',
placeholder: 'Good for answering questions about netflix movies'
}
]
}
async init(nodeData: INodeData): Promise<any> {
const name = nodeData.inputs?.name as string
const description = nodeData.inputs?.description as string
const vectorStore = nodeData.inputs?.vectorStore as VectorStore
const obj = {
name,
description,
vectorStore
} as VectorStoreRetrieverInput
const retriever = new VectorStoreRetriever(obj)
return retriever
}
}
module.exports = { nodeClass: VectorStoreRetriever_Retrievers }
@@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-database-export" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M4 6c0 1.657 3.582 3 8 3s8 -1.343 8 -3s-3.582 -3 -8 -3s-8 1.343 -8 3"></path>
<path d="M4 6v6c0 1.657 3.582 3 8 3c1.118 0 2.183 -.086 3.15 -.241"></path>
<path d="M20 12v-6"></path>
<path d="M4 12v6c0 1.657 3.582 3 8 3c.157 0 .312 -.002 .466 -.005"></path>
<path d="M16 19h6"></path>
<path d="M19 16l3 3l-3 3"></path>
</svg>

After

Width:  |  Height:  |  Size: 647 B

+19
View File
@@ -95,6 +95,7 @@ export interface IMessage {
*/
import { PromptTemplate as LangchainPromptTemplate, PromptTemplateInput } from 'langchain/prompts'
import { VectorStore } from 'langchain/vectorstores/base'
export class PromptTemplate extends LangchainPromptTemplate {
promptValues: ICommonObject
@@ -124,3 +125,21 @@ export class PromptRetriever {
this.systemMessage = `${fields.systemMessage}\n${fixedTemplate}`
}
}
export interface VectorStoreRetrieverInput {
name: string
description: string
vectorStore: VectorStore
}
export class VectorStoreRetriever {
name: string
description: string
vectorStore: VectorStore
constructor(fields: VectorStoreRetrieverInput) {
this.name = fields.name
this.description = fields.description
this.vectorStore = fields.vectorStore
}
}