75 3 weeks ago

ollama run saihantaki/bge-reranker-v2-gemma:q8_0

Details

3 weeks ago

f28655a10d5b · 3.2GB

gemma
·
2.51B
·
Q8_0
{ "num_ctx": 8192, "num_predict": 1, "temperature": 0 }
{{ .Prompt }}

Readme

bge-reranker-v2-gemma

This is a reranker, not a chat or embedding model. The auto-generated CLI / cURL / Python / JavaScript tabs above don’t apply: ollama run and /api/chat give empty output. Use /api/generate with raw and logprobs, 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.

Requirements

  • Ollama v0.12.11 or newer. That’s the release that added 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.
  • For the Python example: ollama ≥ 0.6.1, the first release with logprobs (tested with 0.6.3).

How scoring works

  1. Send one /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'.
    
  2. Request logprobs: true, top_logprobs: 5, num_predict: 1.
  3. Compute the score:
    
    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.
  4. Optional: sigmoid(score) gives a 0–1 relevance, the same as FlagEmbedding’s normalize=True.

CLI (bash + curl + jq)

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

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.

Python

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

Tips

  • One request per pair. For many candidates, send requests concurrently (e.g. a thread pool), and set OLLAMA_NUM_PARALLEL on the server to raise throughput.
  • Length: the original model was trained with pairs up to about 1024 tokens (query ≤ 768). Longer passages work (context is 8192), but quality beyond the training length isn’t guaranteed. Chunk long documents.
  • Don’t put the literal text <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.

What was changed from the original model

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.

License

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).