Skip to content

Commit b94b34d

Browse files
authored
Merge pull request FlowiseAI#1012 from FlowiseAI/feature/SimilarityThresholdRetriever
Feature/Similarity Threshold Retriever
2 parents fe9c63a + 6f9618a commit b94b34d

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { VectorStore } from 'langchain/vectorstores/base'
2+
import { INode, INodeData, INodeParams, INodeOutputsValue } from '../../../src/Interface'
3+
import { handleEscapeCharacters } from '../../../src'
4+
import { ScoreThresholdRetriever } from 'langchain/retrievers/score_threshold'
5+
6+
class SimilarityThresholdRetriever_Retrievers implements INode {
7+
label: string
8+
name: string
9+
version: number
10+
description: string
11+
type: string
12+
icon: string
13+
category: string
14+
baseClasses: string[]
15+
inputs: INodeParams[]
16+
outputs: INodeOutputsValue[]
17+
18+
constructor() {
19+
this.label = 'Similarity Score Threshold Retriever'
20+
this.name = 'similarityThresholdRetriever'
21+
this.version = 1.0
22+
this.type = 'SimilarityThresholdRetriever'
23+
this.icon = 'similaritythreshold.svg'
24+
this.category = 'Retrievers'
25+
this.description = 'Return results based on the minimum similarity percentage'
26+
this.baseClasses = [this.type, 'BaseRetriever']
27+
this.inputs = [
28+
{
29+
label: 'Vector Store',
30+
name: 'vectorStore',
31+
type: 'VectorStore'
32+
},
33+
{
34+
label: 'Minimum Similarity Score (%)',
35+
name: 'minSimilarityScore',
36+
description: 'Finds results with at least this similarity score',
37+
type: 'number',
38+
default: 80,
39+
step: 1
40+
},
41+
{
42+
label: 'Max K',
43+
name: 'maxK',
44+
description: `The maximum number of results to fetch`,
45+
type: 'number',
46+
default: 20,
47+
step: 1
48+
},
49+
{
50+
label: 'K Increment',
51+
name: 'kIncrement',
52+
description: `How much to increase K by each time. It'll fetch N results, then N + kIncrement, then N + kIncrement * 2, etc.`,
53+
type: 'number',
54+
default: 2,
55+
step: 1
56+
}
57+
]
58+
this.outputs = [
59+
{
60+
label: 'Similarity Threshold Retriever',
61+
name: 'retriever',
62+
baseClasses: this.baseClasses
63+
},
64+
{
65+
label: 'Document',
66+
name: 'document',
67+
baseClasses: ['Document']
68+
},
69+
{
70+
label: 'Text',
71+
name: 'text',
72+
baseClasses: ['string', 'json']
73+
}
74+
]
75+
}
76+
77+
async init(nodeData: INodeData, input: string): Promise<any> {
78+
const vectorStore = nodeData.inputs?.vectorStore as VectorStore
79+
const minSimilarityScore = nodeData.inputs?.minSimilarityScore as number
80+
const maxK = nodeData.inputs?.maxK as string
81+
const kIncrement = nodeData.inputs?.kIncrement as string
82+
83+
const output = nodeData.outputs?.output as string
84+
85+
const retriever = ScoreThresholdRetriever.fromVectorStore(vectorStore, {
86+
minSimilarityScore: minSimilarityScore ? minSimilarityScore / 100 : 0.9,
87+
maxK: maxK ? parseInt(maxK, 10) : 100,
88+
kIncrement: kIncrement ? parseInt(kIncrement, 10) : 2
89+
})
90+
91+
if (output === 'retriever') return retriever
92+
else if (output === 'document') return await retriever.getRelevantDocuments(input)
93+
else if (output === 'text') {
94+
let finaltext = ''
95+
96+
const docs = await retriever.getRelevantDocuments(input)
97+
98+
for (const doc of docs) finaltext += `${doc.pageContent}\n`
99+
100+
return handleEscapeCharacters(finaltext, false)
101+
}
102+
103+
return retriever
104+
}
105+
}
106+
107+
module.exports = { nodeClass: SimilarityThresholdRetriever_Retrievers }

0 commit comments

Comments
 (0)