-
-
Notifications
You must be signed in to change notification settings - Fork 23.4k
feat: add Rerankers from Azure #5576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Add Azure Foundry Reranker integration
feat: Add Azure Foundry Reranker integration
…3-icon-service-AI-Studio.svg
Summary of ChangesHello @Psylockz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request integrates Azure AI Foundry's reranking functionality into Flowise by adding a new AzureRerankRetriever node. This allows users to enhance the relevance of retrieved documents by applying advanced semantic reranking, ensuring that the most pertinent information is prioritized and returned with a clear relevance score. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a new Azure Rerank Retriever node. The implementation is largely correct, but I've identified several areas for improvement to enhance robustness, code consistency, and type safety. My feedback for AzureRerank.ts focuses on improving property naming, strengthening type-safety, and replacing silent error handling with explicit error throwing to prevent unexpected behavior. For AzureRerankRetriever.ts, I've suggested adding credential validation to avoid potential runtime errors and correcting an inconsistent variable name to align with the project's coding style.
| try { | ||
| let returnedDocs = await axios.post(this.AZURE_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.splice(0, this.k) | ||
| } catch (error) { | ||
| return documents | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part of the code can be improved for robustness and clarity:
- Error Handling: Silently returning original documents on API failure is risky. It's better to throw an error to signal that reranking failed.
- Redundancy: The
splice()call is redundant as the API is already instructed to returntop_n: this.kresults. - Type Safety: The
resultfrom the API response is typed asany. Defining an interface improves type safety.
try {
const returnedDocs = await axios.post(this.AZURE_API_URL, data, config)
const finalResults: Document<Record<string, any>>[] = []
interface RerankResult {
index: number
relevance_score: number
}
returnedDocs.data.results.forEach((result: RerankResult) => {
const doc = documents[result.index]
doc.metadata.relevance_score = result.relevance_score
finalResults.push(doc)
})
return finalResults
} catch (error) {
throw new Error(`Azure Rerank API call failed: ${error.message}`)
}
}| const azureApiKey = getCredentialParam('azureFoundryApiKey', credentialData, nodeData) | ||
| const azureEndpoint = getCredentialParam('azureFoundryEndpoint', credentialData, nodeData) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The azureApiKey and azureEndpoint are retrieved from credentials, but there's no validation to ensure they exist. If they are missing, undefined will be passed to the AzureRerank constructor, likely causing a runtime error. It's safer to check for their presence and throw an informative error if they are not found.
| const azureApiKey = getCredentialParam('azureFoundryApiKey', credentialData, nodeData) | |
| const azureEndpoint = getCredentialParam('azureFoundryEndpoint', credentialData, nodeData) | |
| const azureApiKey = getCredentialParam('azureFoundryApiKey', credentialData, nodeData) | |
| if (!azureApiKey) { | |
| throw new Error('Azure Foundry API Key is missing in credentials.') | |
| } | |
| const azureEndpoint = getCredentialParam('azureFoundryEndpoint', credentialData, nodeData) | |
| if (!azureEndpoint) { | |
| throw new Error('Azure Foundry Endpoint is missing in credentials.') | |
| } |
| private AzureAPIKey: any | ||
| private AZURE_API_URL: string | ||
| private readonly model: string | ||
| private readonly k: number | ||
| private readonly maxChunksPerDoc: number | ||
| constructor(AzureAPIKey: string, AZURE_API_URL: string, model: string, k: number, maxChunksPerDoc: number) { | ||
| super() | ||
| this.AzureAPIKey = AzureAPIKey | ||
| this.AZURE_API_URL = AZURE_API_URL | ||
| this.model = model | ||
| this.k = k | ||
| this.maxChunksPerDoc = maxChunksPerDoc | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few opportunities to improve naming, typing, and immutability in the class properties and constructor, following common TypeScript best practices:
- Property names
AzureAPIKeyandAZURE_API_URLshould be incamelCase(e.g.,azureApiKey,azureApiUrl). - The type of
AzureAPIKeyshould bestring, notany. - Since all properties are initialized in the constructor and not modified later, they can be marked as
readonly.
Applying these changes will require updating the property access in the compressDocuments method as well.
| private AzureAPIKey: any | |
| private AZURE_API_URL: string | |
| private readonly model: string | |
| private readonly k: number | |
| private readonly maxChunksPerDoc: number | |
| constructor(AzureAPIKey: string, AZURE_API_URL: string, model: string, k: number, maxChunksPerDoc: number) { | |
| super() | |
| this.AzureAPIKey = AzureAPIKey | |
| this.AZURE_API_URL = AZURE_API_URL | |
| this.model = model | |
| this.k = k | |
| this.maxChunksPerDoc = maxChunksPerDoc | |
| } | |
| private readonly azureApiKey: string | |
| private readonly azureApiUrl: string | |
| private readonly model: string | |
| private readonly k: number | |
| private readonly maxChunksPerDoc: number | |
| constructor(azureApiKey: string, azureApiUrl: string, model: string, k: number, maxChunksPerDoc: number) { | |
| super() | |
| this.azureApiKey = azureApiKey | |
| this.azureApiUrl = azureApiUrl | |
| this.model = model | |
| this.k = k | |
| this.maxChunksPerDoc = maxChunksPerDoc | |
| } |
| const max_chunks_per_doc = maxChunksPerDoc ? parseFloat(maxChunksPerDoc) : 10 | ||
| const output = nodeData.outputs?.output as string | ||
|
|
||
| const azureCompressor = new AzureRerank(azureApiKey, azureEndpoint, model, k, max_chunks_per_doc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable max_chunks_per_doc uses snake_case, which is inconsistent with the TypeScript camelCase convention used in the rest of the file. For consistency, it should be renamed to maxChunksPerDocValue and used in the AzureRerank constructor.
| const max_chunks_per_doc = maxChunksPerDoc ? parseFloat(maxChunksPerDoc) : 10 | |
| const output = nodeData.outputs?.output as string | |
| const azureCompressor = new AzureRerank(azureApiKey, azureEndpoint, model, k, max_chunks_per_doc) | |
| const maxChunksPerDocValue = maxChunksPerDoc ? parseFloat(maxChunksPerDoc) : 10 | |
| const output = nodeData.outputs?.output as string | |
| const azureCompressor = new AzureRerank(azureApiKey, azureEndpoint, model, k, maxChunksPerDocValue) |
|
@HenryHengZJ I would really appreciate a review :) |
This change adds an Azure Rerank Retriever node to Flowise, allowing reranking via the Azure Foundry endpoint.
Features
AzureRerankRetrievernode under Retrieversrelevance_scorein metadataImplementation details
packages/components/nodes/retrievers/AzureRerankRetriever/Usage
Users can now add the Azure Rerank Retriever node to their flows and rerank documents through secure Azure Foundry infrastructure.
Closes #5555
Screenshots:


