Instructions to use transformers-community/contrastive-search with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use transformers-community/contrastive-search with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="transformers-community/contrastive-search") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("transformers-community/contrastive-search") model = AutoModelForCausalLM.from_pretrained("transformers-community/contrastive-search") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.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=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use transformers-community/contrastive-search with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "transformers-community/contrastive-search" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "transformers-community/contrastive-search", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/transformers-community/contrastive-search
- SGLang
How to use transformers-community/contrastive-search 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 "transformers-community/contrastive-search" \ --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": "transformers-community/contrastive-search", "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 "transformers-community/contrastive-search" \ --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": "transformers-community/contrastive-search", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use transformers-community/contrastive-search with Docker Model Runner:
docker model run hf.co/transformers-community/contrastive-search
Description
Implementation of Contrastive Search, a decoding strategy that jointly optimizes model confidence and a degeneration penalty to produce fluent, coherent, and low-repetition text. At each step, the model considers the top-k candidate tokens and selects the one maximizing:
score(v) = (1 - alpha) * p(v | context) - alpha * max_cosine_similarity(h_v, H_context)
where alpha is the trade-off between confidence and the cosine-similarity-based penalty.
This strategy typically:
- Reduces repetition compared to greedy/beam search
- Preserves semantic coherence better than pure sampling
Base model
Qwen/Qwen2.5-0.5B-Instruct(example)
Model compatibility
- Decoder and encoder-decoder transformer models for causal LM
Additional Arguments
top_k(int): Number of candidate tokens to consider each step (e.g., 4)penalty_alpha(float): Weight of the degeneration penalty (e.g., 0.6)
Tips:
- Larger
top_kexplores more candidates but increases compute penalty_alphain [0.3, 0.8] often works well;0.0reduces to greedy
Output Type changes
(none) — returns the same structure as standard transformers generation
Example usage
from transformers import AutoModelForCausalLM, AutoTokenizer, infer_device
device = infer_device()
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto").to(device)
inputs = tokenizer(["DeepMind Company is"], return_tensors="pt").to(device)
# Contrastive search
gen_out = model.generate(
**inputs,
custom_generate="contrastive_search",
penalty_alpha=0.6,
top_k=4,
max_new_tokens=128,
trust_remote_code=True,
)
print(tokenizer.batch_decode(gen_out, skip_special_tokens=True))
- Downloads last month
- 30