Feature: Add Jina AI Rerank Retriever (#3898)

This commit is contained in:
Nguyễn Đức Hùng 2025-01-20 19:33:42 +07:00 committed by GitHub
parent 5c9f17814b
commit 9c2203be62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 94 40">
<path fill="#EB6161" d="M6.12 39.92c3.38 0 6.12-2.74 6.12-6.12s-2.74-6.12-6.12-6.12S0 30.42 0 33.8s2.74 6.12 6.12 6.12z"/>
<path fill="#009191" fill-rule="evenodd" d="M25.4 14.52c.8 0 1.44.64 1.44 1.44l-.08 11.72c0 6.68-5.36 12.12-12.04 12.24h-.2v-12.2h.04L14.6 16c0-.8.64-1.44 1.44-1.44h9.36v-.04zm18.8 0c.8 0 1.44.64 1.44 1.44v16.4c0 .8-.64 1.44-1.44 1.44h-9.36c-.8 0-1.44-.64-1.44-1.44v-16.4c0-.8.64-1.44 1.44-1.44h9.36zm14.72-.04h.2c6 .08 10.88 4.92 11.04 10.92v6.92c0 .8-.64 1.44-1.44 1.44H53.6c-.8 0-1.44-.64-1.44-1.44v-16.4c0-.8.64-1.44 1.44-1.44h5.32zM83.68 33.6c-5.04-.32-9.08-4.52-9.08-9.64 0-5.32 4.32-9.64 9.64-9.64 5.12 0 9.32 4 9.64 9.08v8.76c0 .8-.64 1.44-1.44 1.44h-8.76z" clip-rule="evenodd"/>
<path fill="#FBCB67" d="M39.499 12.24c3.38 0 6.12-2.74 6.12-6.12S42.879 0 39.499 0s-6.12 2.74-6.12 6.12 2.74 6.12 6.12 6.12z"/>
</svg>

After

Width:  |  Height:  |  Size: 913 B

View File

@ -0,0 +1,51 @@
import { Callbacks } from '@langchain/core/callbacks/manager'
import { Document } from '@langchain/core/documents'
import axios from 'axios'
import { BaseDocumentCompressor } from 'langchain/retrievers/document_compressors'
export class JinaRerank extends BaseDocumentCompressor {
private jinaAPIKey: string
private readonly JINA_RERANK_API_URL = 'https://api.jina.ai/v1/rerank'
private model: string = 'jina-reranker-v2-base-multilingual'
private readonly topN: number
constructor(jinaAPIKey: string, model: string, topN: number) {
super()
this.jinaAPIKey = jinaAPIKey
this.model = model
this.topN = topN
}
async compressDocuments(
documents: Document<Record<string, any>>[],
query: string,
_?: Callbacks | undefined
): Promise<Document<Record<string, any>>[]> {
if (documents.length === 0) {
return []
}
const config = {
headers: {
Authorization: `Bearer ${this.jinaAPIKey}`,
'Content-Type': 'application/json'
}
}
const data = {
model: this.model,
query: query,
documents: documents.map((doc) => doc.pageContent),
top_n: this.topN
}
try {
let returnedDocs = await axios.post(this.JINA_RERANK_API_URL, data, config)
const finalResults: Document<Record<string, any>>[] = []
returnedDocs.data.results.forEach((result: any) => {
const doc = documents[result.index]
doc.metadata.relevance_score = result.relevance_score
finalResults.push(doc)
})
return finalResults
} catch (error) {
return documents
}
}
}

View File

@ -0,0 +1,129 @@
import { BaseRetriever } from '@langchain/core/retrievers'
import { ContextualCompressionRetriever } from 'langchain/retrievers/contextual_compression'
import { getCredentialData, getCredentialParam, handleEscapeCharacters } from '../../../src'
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { JinaRerank } from './JinaRerank'
class JinaRerankRetriever_Retrievers implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
badge: string
outputs: INodeOutputsValue[]
constructor() {
this.label = 'Jina AI Rerank Retriever'
this.name = 'JinaRerankRetriever'
this.version = 1.0
this.type = 'JinaRerankRetriever'
this.icon = 'JinaAI.svg'
this.category = 'Retrievers'
this.description = 'Jina AI Rerank indexes the documents from most to least semantically relevant to the query.'
this.baseClasses = [this.type, 'BaseRetriever']
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['jinaAIApi']
}
this.inputs = [
{
label: 'Vector Store Retriever',
name: 'baseRetriever',
type: 'VectorStoreRetriever'
},
{
label: 'Model Name',
name: 'model',
type: 'options',
options: [
{
label: 'jina-reranker-v2-base-multilingual',
name: 'jina-reranker-v2-base-multilingual'
},
{
label: 'jina-colbert-v2',
name: 'jina-colbert-v2'
}
],
default: 'jina-reranker-v2-base-multilingual',
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 N',
name: 'topN',
description: 'Number of top results to fetch. Default to 4',
placeholder: '4',
default: 4,
type: 'number',
additionalParams: true,
optional: true
}
]
this.outputs = [
{
label: 'Jina AI Rerank Retriever',
name: 'retriever',
baseClasses: this.baseClasses
},
{
label: 'Document',
name: 'document',
description: 'Array of document objects containing metadata and pageContent',
baseClasses: ['Document', 'json']
},
{
label: 'Text',
name: 'text',
description: 'Concatenated string from pageContent of documents',
baseClasses: ['string', 'json']
}
]
}
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 jinaApiKey = getCredentialParam('jinaAIAPIKey', credentialData, nodeData)
const topN = nodeData.inputs?.topN ? parseFloat(nodeData.inputs?.topN as string) : 4
const output = nodeData.outputs?.output as string
const jinaCompressor = new JinaRerank(jinaApiKey, model, topN)
const retriever = new ContextualCompressionRetriever({
baseCompressor: jinaCompressor,
baseRetriever: baseRetriever
})
if (output === 'retriever') return retriever
else if (output === 'document') return await retriever.invoke(query ? query : input)
else if (output === 'text') {
const docs = await retriever.invoke(query ? query : input)
let finaltext = ''
for (const doc of docs) finaltext += `${doc.pageContent}\n`
return handleEscapeCharacters(finaltext, false)
}
return retriever
}
}
module.exports = { nodeClass: JinaRerankRetriever_Retrievers }