Add task-specific instructions via enum (T2C/C2C/C2T) with usage examples
Browse files
README.md
CHANGED
|
@@ -55,34 +55,55 @@ Reranking delta on CoREB v202603, using C2LLM-7B as the first-stage retriever:
|
|
| 55 |
|
| 56 |
## Usage
|
| 57 |
|
| 58 |
-
CoREB-Reranker follows the same usage pattern as Qwen3-Reranker.
|
| 59 |
|
| 60 |
```python
|
|
|
|
| 61 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
model_id = "hq-bench/coreb-code-reranker"
|
| 64 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 65 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
|
| 66 |
-
|
| 67 |
-
# Format as Qwen3-Reranker input
|
| 68 |
-
query = "binary search implementation"
|
| 69 |
-
document = "def binary_search(arr, target):\n lo, hi = 0, len(arr) - 1\n ..."
|
| 70 |
-
|
| 71 |
-
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'
|
| 72 |
-
suffix = "<|im_end|>\n<|im_start|>assistant\n"
|
| 73 |
-
instruct = "Given a code search query, does the following code snippet match the query intent?"
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 78 |
-
outputs = model(**inputs)
|
| 79 |
-
|
| 80 |
-
# Score is the logit difference between "yes" and "no" tokens
|
| 81 |
yes_id = tokenizer.convert_tokens_to_ids("yes")
|
| 82 |
no_id = tokenizer.convert_tokens_to_ids("no")
|
| 83 |
-
|
| 84 |
-
score
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
```
|
| 87 |
|
| 88 |
For batch reranking with the CoREB evaluation pipeline, see the [CoREB repository](https://github.com/hq-bench/coreb).
|
|
|
|
| 55 |
|
| 56 |
## Usage
|
| 57 |
|
| 58 |
+
CoREB-Reranker follows the same usage pattern as Qwen3-Reranker. The instruction is **task-specific** — use the appropriate one for your retrieval task:
|
| 59 |
|
| 60 |
```python
|
| 61 |
+
from enum import Enum
|
| 62 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 63 |
+
import torch
|
| 64 |
+
|
| 65 |
+
class Task(Enum):
|
| 66 |
+
TEXT_TO_CODE = "Given a natural language programming task, retrieve code that correctly solves or implements the task."
|
| 67 |
+
CODE_TO_CODE = "Given a code snippet, retrieve code that is semantically equivalent or solves the same task."
|
| 68 |
+
CODE_TO_TEXT = "Given a code snippet, retrieve the natural language description or problem statement that best matches the code."
|
| 69 |
|
| 70 |
model_id = "hq-bench/coreb-code-reranker"
|
| 71 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 72 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, trust_remote_code=True)
|
| 73 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
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'
|
| 76 |
+
SUFFIX = "<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
yes_id = tokenizer.convert_tokens_to_ids("yes")
|
| 78 |
no_id = tokenizer.convert_tokens_to_ids("no")
|
| 79 |
+
|
| 80 |
+
def score(query: str, document: str, task: Task) -> float:
|
| 81 |
+
prompt = f"{PREFIX}<Instruct>: {task.value}\n<Query>: {query}\n<Document>: {document}{SUFFIX}"
|
| 82 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=4096)
|
| 83 |
+
with torch.no_grad():
|
| 84 |
+
logits = model(**inputs).logits[0, -1, :]
|
| 85 |
+
return (logits[yes_id] - logits[no_id]).item()
|
| 86 |
+
|
| 87 |
+
# Text-to-Code: natural language query -> code
|
| 88 |
+
print(score(
|
| 89 |
+
query="binary search implementation",
|
| 90 |
+
document="def binary_search(arr, target):\n lo, hi = 0, len(arr) - 1\n ...",
|
| 91 |
+
task=Task.TEXT_TO_CODE,
|
| 92 |
+
))
|
| 93 |
+
|
| 94 |
+
# Code-to-Code: code -> semantically equivalent code
|
| 95 |
+
print(score(
|
| 96 |
+
query="def binary_search(arr, target): ...",
|
| 97 |
+
document="int binarySearch(int[] arr, int target) { ... }",
|
| 98 |
+
task=Task.CODE_TO_CODE,
|
| 99 |
+
))
|
| 100 |
+
|
| 101 |
+
# Code-to-Text: code -> problem description
|
| 102 |
+
print(score(
|
| 103 |
+
query="def binary_search(arr, target): ...",
|
| 104 |
+
document="Find the index of a target value in a sorted array using binary search.",
|
| 105 |
+
task=Task.CODE_TO_TEXT,
|
| 106 |
+
))
|
| 107 |
```
|
| 108 |
|
| 109 |
For batch reranking with the CoREB evaluation pipeline, see the [CoREB repository](https://github.com/hq-bench/coreb).
|