mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 09:01:06 +03:00
update retrievers and add mmr to other vector stores
This commit is contained in:
+61
-13
@@ -1,7 +1,7 @@
|
||||
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
|
||||
import { BaseRetriever } from 'langchain/schema/retriever'
|
||||
import { ContextualCompressionRetriever } from 'langchain/retrievers/contextual_compression'
|
||||
import { getCredentialData, getCredentialParam } from '../../../src'
|
||||
import { getCredentialData, getCredentialParam, handleEscapeCharacters } from '../../../src'
|
||||
import { CohereRerank } from './CohereRerank'
|
||||
import { VectorStoreRetriever } from 'langchain/vectorstores/base'
|
||||
|
||||
@@ -15,16 +15,16 @@ class CohereRerankRetriever_Retrievers implements INode {
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
inputs: INodeParams[]
|
||||
outputs: INodeOutputsValue[]
|
||||
credential: INodeParams
|
||||
badge: string
|
||||
outputs: INodeOutputsValue[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'Cohere Rerank Retriever'
|
||||
this.name = 'cohereRerankRetriever'
|
||||
this.version = 1.0
|
||||
this.type = 'Cohere Rerank Retriever'
|
||||
this.icon = 'compressionRetriever.svg'
|
||||
this.icon = 'Cohere.svg'
|
||||
this.category = 'Retrievers'
|
||||
this.badge = 'NEW'
|
||||
this.description = 'Cohere Rerank indexes the documents from most to least semantically relevant to the query.'
|
||||
@@ -37,7 +37,7 @@ class CohereRerankRetriever_Retrievers implements INode {
|
||||
}
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Base Retriever',
|
||||
label: 'Vector Store Retriever',
|
||||
name: 'baseRetriever',
|
||||
type: 'VectorStoreRetriever'
|
||||
},
|
||||
@@ -58,36 +58,84 @@ class CohereRerankRetriever_Retrievers implements INode {
|
||||
default: 'rerank-english-v2.0',
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'Query',
|
||||
name: 'query',
|
||||
type: 'string',
|
||||
description: 'Query to retrieve documents from retriever. If not specified, user question will be used',
|
||||
optional: true,
|
||||
acceptVariable: true
|
||||
},
|
||||
{
|
||||
label: 'Top K',
|
||||
name: 'topK',
|
||||
description: 'Number of top results to fetch. Default to the TopK of the Base Retriever',
|
||||
placeholder: '0',
|
||||
placeholder: '4',
|
||||
type: 'number',
|
||||
additionalParams: true,
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'Max Chunks Per Doc',
|
||||
name: 'maxChunksPerDoc',
|
||||
description: 'The maximum number of chunks to produce internally from a document. Default to 10',
|
||||
placeholder: '10',
|
||||
type: 'number',
|
||||
default: 0,
|
||||
additionalParams: true,
|
||||
optional: true
|
||||
}
|
||||
]
|
||||
this.outputs = [
|
||||
{
|
||||
label: 'Cohere Rerank Retriever',
|
||||
name: 'retriever',
|
||||
baseClasses: this.baseClasses
|
||||
},
|
||||
{
|
||||
label: 'Document',
|
||||
name: 'document',
|
||||
baseClasses: ['Document']
|
||||
},
|
||||
{
|
||||
label: 'Text',
|
||||
name: 'text',
|
||||
baseClasses: ['string', 'json']
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||
async init(nodeData: INodeData, input: string, options: ICommonObject): Promise<any> {
|
||||
const baseRetriever = nodeData.inputs?.baseRetriever as BaseRetriever
|
||||
const model = nodeData.inputs?.model as string
|
||||
const query = nodeData.inputs?.query as string
|
||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||
const cohereApiKey = getCredentialParam('cohereApiKey', credentialData, nodeData)
|
||||
const topK = nodeData.inputs?.topK as string
|
||||
let k = topK ? parseFloat(topK) : 4
|
||||
const k = topK ? parseFloat(topK) : (baseRetriever as VectorStoreRetriever).k ?? 4
|
||||
const maxChunksPerDoc = nodeData.inputs?.maxChunksPerDoc as string
|
||||
const max_chunks_per_doc = maxChunksPerDoc ? parseFloat(maxChunksPerDoc) : 10
|
||||
const output = nodeData.outputs?.output as string
|
||||
|
||||
if (k <= 0) {
|
||||
k = (baseRetriever as VectorStoreRetriever).k
|
||||
}
|
||||
const cohereCompressor = new CohereRerank(cohereApiKey, model, k, max_chunks_per_doc)
|
||||
|
||||
const cohereCompressor = new CohereRerank(cohereApiKey, model, k)
|
||||
return new ContextualCompressionRetriever({
|
||||
const retriever = new ContextualCompressionRetriever({
|
||||
baseCompressor: cohereCompressor,
|
||||
baseRetriever: baseRetriever
|
||||
})
|
||||
|
||||
if (output === 'retriever') return retriever
|
||||
else if (output === 'document') return await retriever.getRelevantDocuments(query ? query : input)
|
||||
else if (output === 'text') {
|
||||
let finaltext = ''
|
||||
|
||||
const docs = await retriever.getRelevantDocuments(query ? query : input)
|
||||
|
||||
for (const doc of docs) finaltext += `${doc.pageContent}\n`
|
||||
|
||||
return handleEscapeCharacters(finaltext, false)
|
||||
}
|
||||
|
||||
return retriever
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user