Text Generation
Transformers
Safetensors
qwen3
custom_generate
conversational
text-generation-inference
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", device_map="auto") 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 Settings
- 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
Fix cache format
Browse filesTransformers now only uses the Cache classes
custom_generate/generate.py
CHANGED
|
@@ -282,10 +282,8 @@ def _contrastive_search(
|
|
| 282 |
f"{model.__class__.__name__} does not support caching and therefore **can't** be used "
|
| 283 |
"for contrastive search."
|
| 284 |
)
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
or past_key_values[0][0].shape[0] != batch_size
|
| 288 |
-
):
|
| 289 |
raise ValueError(
|
| 290 |
f"{model.__class__.__name__} does not have a standard cache format and therefore **can't** be "
|
| 291 |
"used for contrastive search without further modifications."
|
|
|
|
| 282 |
f"{model.__class__.__name__} does not support caching and therefore **can't** be used "
|
| 283 |
"for contrastive search."
|
| 284 |
)
|
| 285 |
+
# We now only use Cache classes, but a few models have custom cache class, so we use this check instead of an instance check
|
| 286 |
+
elif not hasattr(past_key_values, "update"):
|
|
|
|
|
|
|
| 287 |
raise ValueError(
|
| 288 |
f"{model.__class__.__name__} does not have a standard cache format and therefore **can't** be "
|
| 289 |
"used for contrastive search without further modifications."
|