Skip to content

Commit 3f61b0b

Browse files
authored
Add TextReranker documentation (#2500)
Ticket: 170852
1 parent b557de0 commit 3f61b0b

File tree

11 files changed

+222
-2
lines changed

11 files changed

+222
-2
lines changed

samples/cpp/rag/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ Install [../../export-requirements.txt](../../export-requirements.txt) to conver
1212
pip install --upgrade-strategy eager -r ../../export-requirements.txt
1313
```
1414

15-
Then, run the export with Optimum CLI:
15+
To export text embedding model run Optimum CLI command:
1616

1717
```sh
1818
optimum-cli export openvino --trust-remote-code --model BAAI/bge-small-en-v1.5 BAAI/bge-small-en-v1.5
1919
```
2020

21+
To export text reranking model run Optimum CLI command:
22+
23+
```sh
24+
optimum-cli export openvino --trust-remote-code --model cross-encoder/ms-marco-MiniLM-L6-v2 cross-encoder/ms-marco-MiniLM-L6-v2
25+
```
26+
2127

2228
## Run
2329

samples/python/rag/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ Install [../../export-requirements.txt](../../export-requirements.txt) to conver
1212
pip install --upgrade-strategy eager -r ../../export-requirements.txt
1313
```
1414

15-
Then, run the export with Optimum CLI:
15+
To export text embedding model run Optimum CLI command:
1616

1717
```sh
1818
optimum-cli export openvino --trust-remote-code --model BAAI/bge-small-en-v1.5 BAAI/bge-small-en-v1.5
1919
```
2020

21+
To export text reranking model run Optimum CLI command:
22+
23+
```sh
24+
optimum-cli export openvino --trust-remote-code --model cross-encoder/ms-marco-MiniLM-L6-v2 cross-encoder/ms-marco-MiniLM-L6-v2
25+
```
26+
2127
Alternatively, do it in Python code:
2228

2329
```python
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import { TEXT_RERANK_MODELS } from './models';
3+
import { BaseModelsTable, LinksCell } from '../base-models-table';
4+
5+
export default function TextRerankModelsTable(): React.JSX.Element {
6+
const headers = ['Architecture', 'Example HuggingFace Models'];
7+
8+
const rows = TEXT_RERANK_MODELS.map(({ architecture, models }) => (
9+
<>
10+
<tr key={architecture}>
11+
<td rowSpan={models.length}>
12+
<code>{architecture}</code>
13+
</td>
14+
<LinksCell links={models[0].links} />
15+
</tr>
16+
</>
17+
));
18+
19+
return <BaseModelsTable headers={headers} rows={rows} />;
20+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
type TextRerankModelType = {
2+
architecture: string;
3+
models: Array<{
4+
links: string[];
5+
}>;
6+
};
7+
8+
export const TEXT_RERANK_MODELS: TextRerankModelType[] = [
9+
{
10+
architecture: 'BertForSequenceClassification',
11+
models: [
12+
{
13+
links: [
14+
'https://huggingface.co/cross-encoder/ms-marco-MiniLM-L2-v2',
15+
'https://huggingface.co/cross-encoder/ms-marco-MiniLM-L4-v2',
16+
'https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2',
17+
'https://huggingface.co/cross-encoder/ms-marco-MiniLM-L12-v2',
18+
'https://huggingface.co/cross-encoder/ms-marco-TinyBERT-L2-v2',
19+
'https://huggingface.co/tomaarsen/reranker-MiniLM-L12-gooaq-bce',
20+
],
21+
},
22+
],
23+
},
24+
{
25+
architecture: 'XLMRobertaForSequenceClassification',
26+
models: [
27+
{
28+
links: [
29+
'https://huggingface.co/BAAI/bge-reranker-v2-m3',
30+
'https://huggingface.co/BAAI/bge-reranker-base',
31+
],
32+
},
33+
],
34+
},
35+
{
36+
architecture: 'GemmaForCausalLM',
37+
models: [
38+
{
39+
links: ['https://huggingface.co/BAAI/bge-reranker-v2-gemma'],
40+
},
41+
],
42+
},
43+
{
44+
architecture: 'ModernBertForSequenceClassification',
45+
models: [
46+
{
47+
links: [
48+
'https://huggingface.co/tomaarsen/reranker-ModernBERT-base-gooaq-bce',
49+
'https://huggingface.co/tomaarsen/reranker-ModernBERT-large-gooaq-bce',
50+
'https://huggingface.co/Alibaba-NLP/gte-reranker-modernbert-base',
51+
],
52+
},
53+
],
54+
},
55+
{
56+
architecture: 'ModernBertForMaskedLM',
57+
models: [
58+
{
59+
links: ['https://huggingface.co/answerdotai/ModernBERT-base'],
60+
},
61+
],
62+
},
63+
];

site/docs/supported-models/index.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import VLMModelsTable from './_components/vlm-models-table';
44
import WhisperModelsTable from './_components/whisper-models-table';
55
import TextEmbeddingsModelsTable from './_components/text-embeddings-models-table';
66
import SpeechGenerationModelsTable from './_components/speech-generation-models-table';
7+
import TextRerankModelsTable from './_components/text-rerank-models-table';
78

89

910
# Supported Models
@@ -78,6 +79,14 @@ pip install timm einops
7879

7980
<SpeechGenerationModelsTable />
8081

82+
## Text Rerank Models
83+
84+
<TextRerankModelsTable />
85+
86+
:::info
87+
LoRA adapters are not supported.
88+
:::
89+
8190
:::info
8291

8392
Some models may require access request submission on the Hugging Face page to be downloaded.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import CodeBlock from '@theme/CodeBlock';
2+
3+
<CodeBlock language="cpp" showLineNumbers>
4+
{`#include "openvino/genai/rag/text_rerank_pipeline.hpp"
5+
6+
int main(int argc, char* argv[]) {
7+
std::vector<std::string> documents(argv + 3, argv + argc);
8+
std::string models_path = argv[1], query = argv[2];
9+
10+
ov::genai::TextRerankPipeline pipeline(models_path, "${props.device || 'CPU'}", ov::genai::top_n(3));
11+
12+
auto rerank_result = pipeline.rerank(query, documents);
13+
14+
std::cout << "Reranked documents:\\n";
15+
for (const auto& [index, score] : rerank_result) {
16+
std::cout << "Document " << index << " (score: " << score << "): " << documents[index] << '\\n';
17+
}
18+
}
19+
`}
20+
</CodeBlock>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import CodeBlock from '@theme/CodeBlock';
2+
3+
<CodeBlock language="python" showLineNumbers>
4+
{`import openvino_genai
5+
6+
pipeline = openvino_genai.TextRerankPipeline(model_path, "${props.device || 'CPU'}", top_n=3)
7+
8+
rerank_result = pipeline.rerank(query, documents)
9+
10+
print("Reranked documents:")
11+
for index, score in rerank_result:
12+
print(f"Document {index} (score: {score:.4f}): {documents[index]}")
13+
`}
14+
</CodeBlock>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import CodeExampleCPP from './_code_example_cpp.mdx';
2+
import CodeExamplePython from './_code_example_python.mdx';
3+
4+
## Run Model Using OpenVINO GenAI
5+
6+
The `TextRerankPipeline` enables you to reorder candidate documents or passages by semantic relevance to a query using a cross-encoder or reranker model. You can control how many top results are returned using the `top_n` parameter.
7+
8+
<LanguageTabs>
9+
<TabItemPython>
10+
<Tabs groupId="device">
11+
<TabItem label="CPU" value="cpu">
12+
<CodeExamplePython device="CPU" />
13+
</TabItem>
14+
<TabItem label="GPU" value="gpu">
15+
<CodeExamplePython device="GPU" />
16+
</TabItem>
17+
</Tabs>
18+
</TabItemPython>
19+
<TabItemCpp>
20+
<Tabs groupId="device">
21+
<TabItem label="CPU" value="cpu">
22+
<CodeExampleCPP device="CPU" />
23+
</TabItem>
24+
<TabItem label="GPU" value="gpu">
25+
<CodeExampleCPP device="GPU" />
26+
</TabItem>
27+
</Tabs>
28+
</TabItemCpp>
29+
</LanguageTabs>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
import OptimumCLI from '@site/src/components/OptimumCLI';
5+
import ConvertModelSection from '../_shared/_convert_model.mdx';
6+
import RunModelSection from './_sections/_run_model/index.mdx';
7+
8+
# Text Reranking
9+
10+
<ConvertModelSection>
11+
Download and convert a reranker model (e.g. [cross-encoder/ms-marco-MiniLM-L6-v2](https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2)) to OpenVINO format from Hugging Face:
12+
13+
<OptimumCLI model='cross-encoder/ms-marco-MiniLM-L6-v2' outputDir='cross-encoder/ms-marco-MiniLM-L6-v2' trustRemoteCode />
14+
15+
See all supported [Reranker Models](/docs/supported-models/#text-rerank-models).
16+
</ConvertModelSection>
17+
18+
<RunModelSection />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Button from '@site/src/components/Button';
2+
import { LanguageTabs, TabItemCpp, TabItemPython } from '@site/src/components/LanguageTabs';
3+
import UseCaseCard from './UseCaseCard';
4+
5+
import CodeExampleCpp from '@site/docs/use-cases/text-rerank/_sections/_run_model/_code_example_cpp.mdx';
6+
import CodeExamplePython from '@site/docs/use-cases/text-rerank/_sections/_run_model/_code_example_python.mdx';
7+
8+
export const TextRerank = () => (
9+
<UseCaseCard>
10+
<UseCaseCard.Title>Text Rerank for RAG</UseCaseCard.Title>
11+
<UseCaseCard.Description>
12+
Boost the relevance and accuracy of your Retrieval-Augmented Generation (RAG) workflows by
13+
reranking retrieved documents with the TextRerankPipeline.
14+
</UseCaseCard.Description>
15+
<UseCaseCard.Features>
16+
<li>Reorder search results by semantic relevance to the query</li>
17+
</UseCaseCard.Features>
18+
<UseCaseCard.Code>
19+
<LanguageTabs>
20+
<TabItemPython>
21+
<CodeExamplePython />
22+
</TabItemPython>
23+
<TabItemCpp>
24+
<CodeExampleCpp />
25+
</TabItemCpp>
26+
</LanguageTabs>
27+
</UseCaseCard.Code>
28+
<UseCaseCard.Actions>
29+
<Button label="Explore Use Case" link="docs/use-cases/text-rerank" variant="primary" />
30+
<Button label="View Code Samples" link="docs/samples" variant="primary" outline />
31+
</UseCaseCard.Actions>
32+
</UseCaseCard>
33+
);

0 commit comments

Comments
 (0)