-
-
Notifications
You must be signed in to change notification settings - Fork 23.5k
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
Open
Psylockz
wants to merge
14
commits into
FlowiseAI:main
Choose a base branch
from
Psylockz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba6ab63
Create AzureRerankerApi.credential.ts
Psylockz 54a6d91
Create test
Psylockz a675e90
Add files via upload
Psylockz e887f53
Delete packages/components/nodes/retrievers/AzureRerankRetriever/test
Psylockz fd781b3
feat: Add Azure Foundry Reranker integration
Psylockz 1ce5c79
Delete packages/components/nodes/retrievers/AzureRerankRetriever/0351…
Psylockz 94593f4
feat: Add Azure Reranker integration
Psylockz ceee2cf
feat: Add Azure Reranker integration
Psylockz d799420
feat: Add Azure Reranker integration
Psylockz 14313de
feat: Add Azure Reranker integration
Psylockz 1e947da
feat: Add Azure Reranker integration
Psylockz b3a5434
Merge branch 'FlowiseAI:main' into main
Psylockz e8ae029
Update AzureRerankRetriever.ts
Psylockz 0914b4f
Update AzureRerank.ts
Psylockz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
packages/components/credentials/AzureRerankerApi.credential.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { INodeParams, INodeCredential } from '../src/Interface' | ||
|
|
||
| class AzureRerankerApi implements INodeCredential { | ||
| label: string | ||
| name: string | ||
| version: number | ||
| description: string | ||
| inputs: INodeParams[] | ||
|
|
||
| constructor() { | ||
| this.label = 'Azure Foundry API' | ||
| this.name = 'azureFoundryApi' | ||
| this.version = 1.0 | ||
| this.description = | ||
| 'Refer to <a target="_blank" href="https://docs.microsoft.com/en-us/azure/ai-foundry/">Azure AI Foundry documentation</a> for setup instructions' | ||
| this.inputs = [ | ||
| { | ||
| label: 'Azure Foundry API Key', | ||
| name: 'azureFoundryApiKey', | ||
| type: 'password', | ||
| description: 'Your Azure AI Foundry API key' | ||
| }, | ||
| { | ||
| label: 'Azure Foundry Endpoint', | ||
| name: 'azureFoundryEndpoint', | ||
| type: 'string', | ||
| placeholder: 'https://your-foundry-instance.services.ai.azure.com/providers/cohere/v2/rerank', | ||
| description: 'Your Azure AI Foundry endpoint URL' | ||
| } | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| module.exports = { credClass: AzureRerankerApi } |
57 changes: 57 additions & 0 deletions
57
packages/components/nodes/retrievers/AzureRerankRetriever/AzureRerank.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import axios from 'axios' | ||
| import { Callbacks } from '@langchain/core/callbacks/manager' | ||
| import { Document } from '@langchain/core/documents' | ||
| import { BaseDocumentCompressor } from 'langchain/retrievers/document_compressors' | ||
|
|
||
| export class AzureRerank extends BaseDocumentCompressor { | ||
| 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 | ||
| } | ||
| async compressDocuments( | ||
| documents: Document<Record<string, any>>[], | ||
| query: string, | ||
| _?: Callbacks | undefined | ||
| ): Promise<Document<Record<string, any>>[]> { | ||
| // avoid empty api call | ||
| if (documents.length === 0) { | ||
| return [] | ||
| } | ||
| const config = { | ||
| headers: { | ||
| 'api-key': `${this.azureApiKey}`, | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json' | ||
| } | ||
| } | ||
| const data = { | ||
| model: this.model, | ||
| top_n: this.k, | ||
| max_chunks_per_doc: this.maxChunksPerDoc, | ||
| query: query, | ||
| return_documents: false, | ||
| documents: documents.map((doc) => doc.pageContent) | ||
| } | ||
| try { | ||
| let returnedDocs = await axios.post(this.azureApiUrl, 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) { | ||
| throw new Error(`Azure Rerank API call failed: ${error.message}`) | ||
| } | ||
Psylockz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
162 changes: 162 additions & 0 deletions
162
packages/components/nodes/retrievers/AzureRerankRetriever/AzureRerankRetriever.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { BaseRetriever } from '@langchain/core/retrievers' | ||
| import { VectorStoreRetriever } from '@langchain/core/vectorstores' | ||
| import { ContextualCompressionRetriever } from 'langchain/retrievers/contextual_compression' | ||
| import { AzureRerank } from './AzureRerank' | ||
| import { getCredentialData, getCredentialParam, handleEscapeCharacters } from '../../../src' | ||
| import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' | ||
|
|
||
| class AzureRerankRetriever_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 = 'Azure Rerank Retriever' | ||
| this.name = 'AzureRerankRetriever' | ||
| this.version = 1.0 | ||
| this.type = 'Azure Rerank Retriever' | ||
| this.icon = 'azurefoundry.svg' | ||
| this.category = 'Retrievers' | ||
| this.description = 'Azure 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: ['azureFoundryApi'] | ||
| } | ||
| this.inputs = [ | ||
| { | ||
| label: 'Vector Store Retriever', | ||
| name: 'baseRetriever', | ||
| type: 'VectorStoreRetriever' | ||
| }, | ||
| { | ||
| label: 'Model Name', | ||
| name: 'model', | ||
| type: 'options', | ||
| options: [ | ||
| { | ||
| label: 'rerank-v3.5', | ||
| name: 'rerank-v3.5' | ||
| }, | ||
| { | ||
| label: 'rerank-english-v3.0', | ||
| name: 'rerank-english-v3.0' | ||
| }, | ||
| { | ||
| label: 'rerank-multilingual-v3.0', | ||
| name: 'rerank-multilingual-v3.0' | ||
| }, | ||
| { | ||
| label: 'Cohere-rerank-v4.0-fast', | ||
| name: 'Cohere-rerank-v4.0-fast' | ||
| }, | ||
| { | ||
| label: 'Cohere-rerank-v4.0-pro', | ||
| name: 'Cohere-rerank-v4.0-pro' | ||
| } | ||
| ], | ||
| default: 'Cohere-rerank-v4.0-fast', | ||
| optional: true | ||
| }, | ||
Psylockz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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 K', | ||
| name: 'topK', | ||
| description: 'Number of top results to fetch. Default to the TopK of the Base Retriever', | ||
| placeholder: '4', | ||
| type: 'number', | ||
| additionalParams: true, | ||
| optional: true | ||
| }, | ||
| { | ||
| label: 'Max Chunks Per Doc', | ||
| name: 'maxChunksPerDoc', | ||
| description: 'The maximum number of chunks to produce internally from a document. Default to 10', | ||
| placeholder: '10', | ||
| type: 'number', | ||
| additionalParams: true, | ||
| optional: true | ||
| } | ||
| ] | ||
| this.outputs = [ | ||
| { | ||
| label: 'Azure 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 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.') | ||
| } | ||
| const topK = nodeData.inputs?.topK as string | ||
| const k = topK ? parseFloat(topK) : (baseRetriever as VectorStoreRetriever).k ?? 4 | ||
| const maxChunksPerDoc = nodeData.inputs?.maxChunksPerDoc as string | ||
| const maxChunksPerDocValue = maxChunksPerDoc ? parseFloat(maxChunksPerDoc) : 10 | ||
| const output = nodeData.outputs?.output as string | ||
|
|
||
| const azureCompressor = new AzureRerank(azureApiKey, azureEndpoint, model, k, maxChunksPerDocValue) | ||
|
|
||
| const retriever = new ContextualCompressionRetriever({ | ||
| baseCompressor: azureCompressor, | ||
| baseRetriever: baseRetriever | ||
| }) | ||
|
|
||
| if (output === 'retriever') return retriever | ||
| else if (output === 'document') return await retriever.getRelevantDocuments(query ? query : input) | ||
| else if (output === 'text') { | ||
| let finaltext = '' | ||
|
|
||
| const docs = await retriever.getRelevantDocuments(query ? query : input) | ||
|
|
||
| for (const doc of docs) finaltext += `${doc.pageContent}\n` | ||
|
|
||
| return handleEscapeCharacters(finaltext, false) | ||
Psylockz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return retriever | ||
| } | ||
| } | ||
|
|
||
| module.exports = { nodeClass: AzureRerankRetriever_Retrievers } | ||
1 change: 1 addition & 0 deletions
1
packages/components/nodes/retrievers/AzureRerankRetriever/azurefoundry.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.