Add Baidu Qianfan rerank retriever#6439
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the Baidu Qianfan Rerank Retriever component, which includes credential configuration, unit tests, and an SVG icon. The retriever integrates with the Baidu Qianfan Rerank API to reorder documents based on semantic relevance. The feedback recommends validating the presence of the API key and parsing topN as an integer to prevent silent failures. Additionally, it is suggested to log errors in BaiduQianfanRerank instead of swallowing them silently, and to use handleDocumentLoaderOutput to format the retriever's output and reduce code duplication.
| const qianfanApiKey = | ||
| getCredentialParam('qianfanApiKey', credentialData, nodeData) || getCredentialParam('qianfanAccessKey', credentialData, nodeData) | ||
| const k = topN ? parseFloat(topN) : (baseRetriever as VectorStoreRetriever).k ?? 4 |
There was a problem hiding this comment.
If both qianfanApiKey and qianfanAccessKey are missing, the node will proceed with an empty API key, leading to a failed API call that silently returns the original documents. We should throw an error if no API key is provided. Additionally, topN represents a count of documents and should be parsed as an integer using parseInt(topN, 10) rather than parseFloat(topN) to avoid potential decimal values.
const qianfanApiKey =
getCredentialParam('qianfanApiKey', credentialData, nodeData) || getCredentialParam('qianfanAccessKey', credentialData, nodeData)
if (!qianfanApiKey) {
throw new Error('Baidu Qianfan API Key is missing. Please configure it in the credential.')
}
const k = topN ? parseInt(topN, 10) : (baseRetriever as VectorStoreRetriever).k ?? 4References
- When a feature requires a specific configuration (e.g., an API key for a sandboxed environment), it is preferable to throw an error if the configuration is missing rather than silently falling back to a different implementation.
| } catch (error) { | ||
| return documents | ||
| } |
There was a problem hiding this comment.
Swallowing all errors silently makes debugging extremely difficult if the API key is invalid, rate limits are hit, or the model name is incorrect. Consider logging the error to console.error before returning the fallback documents so that users and administrators can troubleshoot issues.
} catch (error) {
console.error('Baidu Qianfan Rerank Error:', error)
return documents
}| if (output === 'retriever') return retriever | ||
| if (output === 'document') return await retriever.invoke(query ? query : input) | ||
| 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) | ||
| } |
There was a problem hiding this comment.
To reduce code duplication and leverage existing utility functions, we can use handleDocumentLoaderOutput to format the retriever's output. Note: You will also need to import handleDocumentLoaderOutput from ../../../src/utils instead of handleEscapeCharacters.
if (output === 'retriever') return retriever
const docs = await retriever.invoke(query ? query : input)
return handleDocumentLoaderOutput(docs, output)|
Resolved the actionable review items in
For the other two suggestions:
|
Summary
This PR adds a Baidu Qianfan Rerank Retriever node for reranking vector store retrieval results with Qianfan rerank models.
Changes
Baidu Qianfan Rerank Retrieverunder Retrievers.Baidu Qianfan API Keycredential for Qianfan bearer API keys./v2/rerank.bce-reranker-baseas the default rerank model.relevance_scoremetadata to reranked documents.Why
Flowise already supports Baidu Qianfan chat and embeddings. Adding Qianfan rerank support completes a common RAG workflow for Baidu users: retrieve candidates from a vector store, then rerank them with a dedicated cross-encoder reranker before sending context downstream.
Verification
node packages/components/node_modules/jest/bin/jest.js --config packages/components/jest.config.js BaiduQianfanRerank.test.ts BaiduQianfanRerankRetriever.test.tsgit diff --check origin/main...HEAD/v2/rerank