75 Downloads Updated 3 weeks ago
ollama run saihantaki/bge-reranker-v2-gemma:q8_0
This is a reranker, not a chat or embedding model. The auto-generated CLI / cURL / Python / JavaScript tabs above don’t apply:
ollama runand/api/chatgive empty output. Use/api/generatewithrawandlogprobs, as shown below.
BAAI/bge-reranker-v2-gemma is a multilingual LLM-based reranker (cross-encoder) built on Gemma 1 (google/gemma-2b, 2.5B parameters). Given a query and a passage, it returns a relevance score. Higher means more relevant.
logprobs / top_logprobs to the API, and this model’s score is read from them. Older versions ignore the fields and you get no score. Tested on Ollama 0.40.15.ollama ≥ 0.6.1, the first release with logprobs (tested with 0.6.3)./api/generate request per query/passage pair, with this exact prompt and raw: true:
A: {query}
B: {passage}
Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'.
logprobs: true, top_logprobs: 5, num_predict: 1.
score = (logprob("<unused0>") - logprob("<unused1>")) / 0.5
Both tokens are always in top_logprobs. The result equals the original model’s raw “Yes” logit, which is the same number FlagEmbedding’s compute_score returns.sigmoid(score) gives a 0–1 relevance, the same as FlagEmbedding’s normalize=True.ollama run can’t return logprobs, so use this shell function:
rerank() {
local instr="Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'."
jq -n --arg q "$1" --arg p "$2" --arg i "$instr" --arg m "${MODEL:-<your-username>/bge-reranker-v2-gemma}" \
'{model: $m, raw: true, stream: false, prompt: "A: \($q)\nB: \($p)\n\($i)",
logprobs: true, top_logprobs: 5, options: {num_predict: 1, temperature: 0}}' |
curl -s "${OLLAMA_HOST:-http://localhost:11434}/api/generate" -d @- |
jq '.logprobs[0].top_logprobs | map({(.token): .logprob}) | add
| (.["<unused0>"] - .["<unused1>"]) / 0.5'
}
rerank "what is panda?" "The giant panda (Ailuropoda melanoleuca) is a bear species endemic to China."
# 9.12
rerank "what is panda?" "Paris is the capital of France."
# -0.34
curl http://localhost:11434/api/generate -d '{
"model": "<your-username>/bge-reranker-v2-gemma",
"prompt": "A: what is panda?\nB: The giant panda is a bear species endemic to China.\nGiven a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either '"'"'Yes'"'"' or '"'"'No'"'"'.",
"raw": true,
"stream": false,
"logprobs": true,
"top_logprobs": 5,
"options": {"num_predict": 1, "temperature": 0}
}'
Relevant part of the response:
"logprobs": [{
"top_logprobs": [
{"token": "<unused0>", "logprob": -0.0258},
{"token": "<unused1>", "logprob": -4.0818},
...
]
}]
That gives score = (-0.0258 - -4.0818) / 0.5 = 8.11.
import ollama # pip install "ollama>=0.6.1"
MODEL = "<your-username>/bge-reranker-v2-gemma"
INSTRUCTION = ("Given a query A and a passage B, determine whether the passage contains an answer "
"to the query by providing a prediction of either 'Yes' or 'No'.")
def score(query: str, passage: str) -> float:
r = ollama.generate(
model=MODEL,
prompt=f"A: {query}\nB: {passage}\n{INSTRUCTION}",
raw=True, logprobs=True, top_logprobs=5,
options={"num_predict": 1, "temperature": 0},
)
top = {t.token: t.logprob for t in r.logprobs[0].top_logprobs}
return (top["<unused0>"] - top["<unused1>"]) / 0.5
query = "what is panda?"
passages = [
"hi",
"The giant panda (Ailuropoda melanoleuca) is a bear species endemic to China.",
"পান্ডা চীনের একটি ভাল্লুক প্রজাতি।",
"Paris is the capital of France.",
]
for s, p in sorted(((score(query, p), p) for p in passages), reverse=True):
print(f"{s:8.3f} {p}")
9.220 The giant panda (Ailuropoda melanoleuca) is a bear species endemic to China.
8.545 পান্ডা চীনের একটি ভাল্লুক প্রজাতি।
-0.293 Paris is the capital of France.
-0.731 hi
OLLAMA_NUM_PARALLEL on the server to raise throughput.<unused0> or <unused1> in queries or passages; strip or escape it first.temperature doesn’t affect the score (logprobs are computed before sampling). It’s set to 0 only to make the generated token deterministic.The GGUF was converted from the original weights with llama.cpp. Two unused vocabulary rows were modified so the score can be read through Ollama:
E[<unused0>] = 1.1 * E[<bos>] + 0.25 * E[Yes]
E[<unused1>] = 1.1 * E[<bos>] - 0.25 * E[Yes]
Why this was needed:
- The reranker’s score is the raw “Yes” logit. A logprob API can’t expose that directly, because softmax only preserves differences between logits.
- The original model’s top token is <bos>, a control token for which Ollama returns no logprobs.
Gemma ties its output layer to the embedding table, so the logprob difference of these two tokens is exactly 0.5 × score. No other weights were changed. The quantized tags keep the embedding table in f16 (--token-embedding-type f16), because quantizing it distorts the patched rows.
The model card for BAAI/bge-reranker-v2-gemma lists Apache-2.0. The model is a fine-tune of google/gemma-2b, so the Gemma Terms of Use also apply. Credit for the model goes to BAAI (FlagEmbedding).