Instructions to use JJHou1/ConTra-4B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use JJHou1/ConTra-4B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="JJHou1/ConTra-4B") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("JJHou1/ConTra-4B") model = AutoModelForMultimodalLM.from_pretrained("JJHou1/ConTra-4B", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use JJHou1/ConTra-4B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "JJHou1/ConTra-4B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "JJHou1/ConTra-4B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/JJHou1/ConTra-4B
- SGLang
How to use JJHou1/ConTra-4B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "JJHou1/ConTra-4B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "JJHou1/ConTra-4B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "JJHou1/ConTra-4B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "JJHou1/ConTra-4B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use JJHou1/ConTra-4B with Docker Model Runner:
docker model run hf.co/JJHou1/ConTra-4B
Jiajun Hou ยท Jingbang Chen
CUHK-Shenzhen ยท SLAI
ConTra writes short contextual prefixes for document chunks. At indexing time, each prefix is prepended to its chunk and reused for both embedding retrieval and reranking.
Models ยท RealContextBench ยท Project page
Models and dataset
| Model | Base model |
|---|---|
| ConTra-0.8B | Qwen3.5-0.8B |
| ConTra-2B | Qwen3.5-2B |
| ConTra-4B | Qwen3.5-4B |
All three are Qwen3.5 generators post-trained with reinforcement learning to produce contextual prefixes. ConTra-2B has the highest average reranked score in this release.
RealContextBench contains five application collections and three NanoBEIR context conditions for text retrieval.
Using ConTra on documents
Generate one prefix per chunk when building your index. We recommend a context window of about 16K tokens. Use the full document when it fits. For longer documents, use nearby complete chunks in their original order and mark the target chunk separately.
This example uses ConTra-2B with PyTorch and Transformers on CUDA.
import torch
from transformers import AutoModelForImageTextToText, AutoTokenizer
repo = "JJHou1/ConTra-2B"
tokenizer = AutoTokenizer.from_pretrained(repo)
model = AutoModelForImageTextToText.from_pretrained(
repo, dtype=torch.bfloat16,
).to("cuda").eval()
chunks = [
"The City Library will reopen its East Branch on 3 June.",
"It will be open from 10 a.m. to 6 p.m. on weekdays.",
]
target = chunks[1]
document = "\n\n".join(
f'<CHUNK index="{index}">\n{chunk}\n</CHUNK>'
for index, chunk in enumerate(chunks)
)
system_prompt = (
"You are a contextualization model for retrieval-augmented generation (RAG).\n\n"
"Given a document and one target chunk from that document, generate a concise "
"contextual prefix containing only the necessary information that helps the chunk "
"remain understandable and retrievable outside its original document.\n\n"
"Do not repeat the target chunk, introduce unsupported information, or add "
"unnecessary details. If the chunk does not need additional context, output exactly "
"<NO_CONTEXT>.\n\n"
"Output only the contextual prefix or <NO_CONTEXT>."
)
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": (
f"<DOCUMENT>\n{document}\n</DOCUMENT>\n\n<TARGET_CHUNK>\n{target}\n</TARGET_CHUNK>"
)},
]
inputs = tokenizer.apply_chat_template(
messages, tokenize=True, add_generation_prompt=True, enable_thinking=False,
return_dict=True, return_tensors="pt",
).to("cuda")
with torch.inference_mode():
output = model.generate(**inputs, do_sample=False, max_new_tokens=768)
new_tokens = output[0, inputs["input_ids"].shape[1]:]
prefix = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
augmented_chunk = target
if prefix and not prefix.upper().startswith("<NO_CONTEXT>") and prefix != target:
augmented_chunk = f"{prefix}\n\n{target}"
print(augmented_chunk)
Keep each chunk's original ID and use the augmented text for both embedding and reranking.
Results
Reported macro-averaged nDCG@10 after top-15 reranking with Qwen3-Reranker-4B.
| Method | ConTEB | RealContextBench |
|---|---|---|
| Qwen3-Embedding-4B, original text | 0.6483 | 0.6373 |
| InSeNT | 0.6381 | 0.5291 |
| PPLX-context-0.6B | 0.7110 | 0.6083 |
| PPLX-context-4B | 0.7310 | 0.6542 |
| Voyage-context-3 | 0.7292 | 0.6636 |
| Voyage-context-4 | 0.7300 | 0.6608 |
| Qwen3.5-0.8B | 0.6531 | 0.6384 |
| Qwen3.5-2B | 0.7585 | 0.6456 |
| Qwen3.5-4B | 0.8110 | 0.6553 |
| Qwen3.5-9B | 0.8279 | 0.6632 |
| ConTra-0.8B | 0.7346 | 0.6471 |
| ConTra-2B | 0.8525 | 0.6692 |
| ConTra-4B | 0.8465 | 0.6669 |
ConTEB averages eight tasks. RealContextBench averages five application collections and three NanoBEIR conditions. Contextual encoders supply original chunks to the reranker. Prefix-based pipelines supply the prefix and original chunk.
- Downloads last month
- -