Rerank Documents
AI-generatedOverview
This node re-ranks a list of documents based on their relevance to a given query using a large language model (LLM). It sends the query and documents to an external API, which returns relevance scores for each document. The node then filters and sorts these documents according to user-defined criteria.
Common scenarios where this node is useful include:
- Improving search results by prioritizing the most relevant documents.
- Filtering large sets of text data to highlight top matches.
- Enhancing recommendation systems by ranking items based on textual similarity to a query.
For example, if you have a collection of product descriptions and want to find the most relevant ones for a customer query, this node can rank those descriptions accordingly.
Properties
| Name | Meaning |
|---|---|
| Model | The name of the LLM model used to perform the reranking (e.g., "rerank-v3.5"). |
| Query | The search query string to compare against the documents. |
| Rerank Mode | How to return results: either "Top N" results or all results above a "Relevance Threshold". |
| Top N | (Shown if Rerank Mode = Top N) The number of top documents to return. |
| Relevance Threshold | (Shown if Rerank Mode = Relevance Threshold) The minimum relevance score a document must meet to be included. |
| Documents Field Name | The name of the input field containing the array of documents to be reranked. |
Output
The node outputs a JSON array with one item per input item. Each output item contains:
results: An array of objects, each with:document: The original document object from the input.relevance: The relevance score assigned by the model.
billed_units: A numeric value indicating the amount of billing units consumed by the API call (useful for cost tracking).
No binary data is output by this node.
Example output JSON structure:
[
{
"results": [
{
"document": { /* original document */ },
"relevance": 0.87
},
{
"document": { /* original document */ },
"relevance": 0.75
}
],
"billed_units": 1
}
]
Dependencies
- Requires an API key credential for accessing an external OpenAI-compatible API endpoint.
- The base URL for the API can be customized via credentials but defaults to
https://api.openai.com. - The node makes HTTP POST requests to the
/v2/rerankendpoint of the API.
Troubleshooting
- Input documents not an array: If the specified documents field does not contain an array, the node skips processing that item. Ensure the documents field name is correct and points to an array.
- API authentication errors: Make sure the API key credential is valid and has access to the reranking endpoint.
- Empty or invalid query: Providing an empty query may result in irrelevant or empty results.
- Incorrect rerank mode parameters: When using "Top N", ensure the number is positive; when using "Relevance Threshold", ensure the threshold is between 0 and 1.
- Network or API errors: The node ignores HTTP status errors internally but may fail silently or return empty results if the API is unreachable or returns invalid responses.
Links and References
- OpenAI API Documentation (for general understanding of API usage)
- n8n Documentation (for how to configure credentials and use custom nodes)