-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[Feat] CrossEncoder class accept prompt template parameters #3406
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?
[Feat] CrossEncoder class accept prompt template parameters #3406
Conversation
…ple to support dynamic prompt template and default configuration. Updated CrossEncoder class to accept prompt template parameters and apply them in prediction and ranking. Added test cases to verify the functionality of prompt template and the correctness of default configuration.
|
cc @tomaarsen |
|
Hello! This is really cool! I've been planning to add support for the "decoder-style" rerankers in a future version after v5.0. For context, Sentence Transformers v5.0 is scheduled for tomorrow, and in the interest of avoiding feature creep for v5.0, I'll have a look at this in more detail after tomorrow.
|
Thank you very much! Looking forward to it |
|
Is the version upgrade going smoothly? If you have time, can you evaluate my PR? @tomaarsen |
|
So far so good re. the release!
|
|
I'm looking into potentially reusing
|
It is indeed more reasonable to reuse |
|
P.s. the latest update is that there's no convenient way to handle the truncation, nor a commonly agreed upon truncation strategy. To be as model agnostic as possible, we'd have to support various different options, but it gets quite messy quite quickly. |
|
Hello @BetterAndBetterII, Apologies for the delay. Locally, I've been working on this problem some more. Particularly, my goal is to support not just https://huggingface.co/tomaarsen/Qwen3-Reranker-0.6B-seq-cls, but also https://huggingface.co/Qwen/Qwen3-Reranker-0.6B itself. It will require a full refactor of the CrossEncoder class to be more like the SentenceTransformer/SparseEncoder classes, i.e. with modules that are executed sequentially. This allows me to create separate modules wrapping That would then also include support for templating akin to what you proposed here.
|
|
a91ab2d from #3554 should introduce (chat) templating support to CrossEncoder models. The PR introduces a Even if passing text pairs and a prompt, the code can convert this to the required "message" format (also discussed in this thread: vllm-project/vllm#30550 (comment)), allowing the chat template to be used. Here are some example models that should work when that PR is checked out:
For example: from sentence_transformers import CrossEncoder
# Download from the 🤗 Hub
model = CrossEncoder("cross-encoder-testing/Qwen3-Reranker-0.6B-STv6")
# Get scores for pairs of texts
query = "Which planet is known as the Red Planet?"
documents = [
"Venus is often called Earth's twin because of its similar size and proximity.",
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet.",
]
pairs = [[query, doc] for doc in documents]
scores = model.predict(pairs)
print(scores)
# [-3.1092978 7.12039 -0.37875462 3.5416374 ]This allows me to reuse the Apologies for the long delay, these changes are built on a big refactor in #3554, but I think it's a more convenient solution that's more futureproof. I wanted to give you a heads up on the direction that we'll head for templating in CrossEncoders, as I figured you might be curious!
|
Summary
Best Practice
{ ... "sentence_transformers": { "version": "xxx", "prompt_template": "Instruct: {instruction}\nQuery: {query}\nDocument: {document}", "prompt_template_kwargs": { "instruction": "Given a query, find the most relevant document." } }, ... }model.predictlike normalMore customized usage
or
Test Cases:
prompt_template, passing or not should get different scoresprompt_template+prompt_template_kwargs, differentprompt_template_kwargsshould have different scoresprompt_templateand instruction configuration from config.json, differentprompt_template, instruction should have different scores, and no errors will be reportedTest Result:
Additional Example E2E Demo on Qwen3-Reranker-0.6B:
--Instruction and Correct template is very important to Qwen3-Reranker--
Misc:
which related to many issues requesting Qwen Reranker in serving vllm