Skip to content

Conversation

@Psylockz
Copy link

This change adds an Azure Rerank Retriever node to Flowise, allowing reranking via the Azure Foundry endpoint.

Features

  • Adds a new AzureRerankRetriever node under Retrievers
  • Calls Azure AI Foundry endpoint
  • Returns reranked documents with relevance_score in metadata

Implementation details

  • New node implementation: packages/components/nodes/retrievers/AzureRerankRetriever/
  • Users can implement their own endpoint from Azure Foundry

Usage

Users can now add the Azure Rerank Retriever node to their flows and rerank documents through secure Azure Foundry infrastructure.

Closes #5555

Screenshots:
image
image
image

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • New Azure Rerank Retriever Node: Introduces a dedicated node for document reranking using Azure AI Foundry.
  • Azure AI Foundry Integration: Enables seamless connection and utilization of Azure AI Foundry's reranking capabilities.
  • Relevance Scoring: Reranked documents now include a 'relevance_score' in their metadata for improved result evaluation.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +44 to +56
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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This part of the code can be improved for robustness and clarity:

  1. Error Handling: Silently returning original documents on API failure is risky. It's better to throw an error to signal that reranking failed.
  2. Redundancy: The splice() call is redundant as the API is already instructed to return top_n: this.k results.
  3. Type Safety: The result from the API response is typed as any. 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}`)
        }
    }

Comment on lines +125 to +126
const azureApiKey = getCredentialParam('azureFoundryApiKey', credentialData, nodeData)
const azureEndpoint = getCredentialParam('azureFoundryEndpoint', credentialData, nodeData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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.')
}

Comment on lines +7 to +19
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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are a few opportunities to improve naming, typing, and immutability in the class properties and constructor, following common TypeScript best practices:

  1. Property names AzureAPIKey and AZURE_API_URL should be in camelCase (e.g., azureApiKey, azureApiUrl).
  2. The type of AzureAPIKey should be string, not any.
  3. 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.

Suggested change
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
}

Comment on lines +130 to +133
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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)

@Psylockz
Copy link
Author

@HenryHengZJ I would really appreciate a review :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rerankers from Azure

1 participant