Views
No views yet
pip install "git+https://github.com/huggingface/optimum-intel.git" "torch==2.8" --extra-index-url https://download.pytorch.org/whl/cpu
import torch
from transformers import AutoTokenizer
from optimum.intel import OVModelForCausalLM
def format_instruction(instruction, query, doc):
prefix = '<|im_start|>system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be "yes" or "no".<|im_end|>\n<|im_start|>user\n'
suffix = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
if instruction is None:
instruction = (
"Given a web search query, retrieve relevant passages that answer the query"
)
output = f"{prefix}<Instruct>: {instruction}\n<Query>: {query}\n<Document>: {doc}{suffix}"
return output
model_id = "OpenVINO/Qwen3-Reranker-0.6B-seq-cls-fp16-ov"
model = OVModelForCausalLM.from_pretrained(model_id, use_cache=False, export=False)
tokenizer = AutoTokenizer.from_pretrained(model_id, padding_side="left")
# We recommend enabling flash_attention_2 for better acceleration and memory saving.
# model = AutoModelForSequenceClassification.from_pretrained("tomaarsen/Qwen3-Reranker-0.6B-seq-cls", torch_dtype=torch.float16, attn_implementation="flash_attention_2").cuda().eval()
max_length = 8192
task = "Given a web search query, retrieve relevant passages that answer the query"
queries = [
"Which planet is known as the Red Planet?",
"Which planet is known as the Red Planet?",
"Which planet is known as the Red Planet?",
"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 = [format_instruction(task, query, doc) for query, doc in zip(queries, documents)]
inputs = tokenizer(
pairs,
padding=True,
truncation=True,
max_length=max_length,
return_tensors="pt",
)
logits = model(**inputs).logits.squeeze()
print(logits.tolist())
# [-3.109282970428467, 7.120373725891113, -0.37874650955200195, 3.5416228771209717]
scores = logits.sigmoid()
print(scores.tolist())
# [0.04272596165537834, 0.9991921782493591, 0.406429260969162, 0.9718491435050964]1mkdir C:\models
2ovms.exe --rest_port 8000 --source_model OpenVINO/Qwen3-Reranker-0.6B-seq-cls-fp16-ov --model_repository_path C:\models1mkdir -p ${HOME}/models
2export GPU_ARGS=$(if ls /dev/dri/render* >/dev/null 2>&1; then echo "--device /dev/dri --group-add $(stat -c '%g' /dev/dri/render* | head -n1)"; fi)
3docker run -d ${GPU_ARGS} --rm --user $(id -u):$(id -g) -p 8000:8000 -v ${HOME}/models:/models openvino/model_server:latest-gpu --rest_port 8000 --model_repository_path /models --source_model OpenVINO/Qwen3-Reranker-0.6B-seq-cls-fp16-ovpip install requests "numpy<2"1import requests
2
3prefix = "<|im_start|>system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be \"yes\" or \"no\".<|im_end|>\n<|im_start|>user\n"
4suffix = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
5
6instruction = "Given a web search query, retrieve relevant passages that answer the query"
7query = "What are the key benefits of machine learning in business?"
8
9documents = [
10 "Machine learning benefits businesses with better decisions, task automation, customer personalization, and cost savings.",
11 "Quantum computing uses quantum bits for fast calculations in cryptography and simulations.",
12]
13
14query_formatted = f"{prefix}<Instruct>: {instruction}\n<Query>: {query}\n"
15doc_formatted = [f"<Document>: {doc}{suffix}" for doc in documents]
16
17response = requests.post(
18 "http://localhost:8000/v1/rerank",
19 json={
20 "model": "OpenVINO/Qwen3-Reranker-0.6B-seq-cls-fp16-ov",
21 "query": query_formatted,
22 "documents": doc_formatted,
23 }
24)
25
26print(response.json())