| name | vllm-llm |
| description | Text model inference (Llama, Qwen, Mistral, GPT, etc.) |
| triggers | ["When user wants to run text LLM inference","When user needs to configure inference parameters (temperature, top_p, etc.)","When user wants to use chat vs text completion","When user needs to handle long text contexts"] |
vllm-llm
Overview
vLLM supports 130+ text-based language models. This skill covers model selection, inference parameter tuning, and common text generation tasks.
Prerequisites
- vLLM installed
- Sufficient GPU memory for target model
- HuggingFace token (for gated models)
Main Workflow
Step 1: Select and Load Model
from vllm import LLM, SamplingParams
model_id = "meta-llama/Llama-2-7b-chat-hf"
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
model_id = "Qwen/Qwen2-7B-Instruct"
model_id = "mistralai/Mistral-7B-Instruct-v0.2"
llm = LLM(model=model_id)
Step 2: Configure Sampling Parameters
sampling_params = SamplingParams(
temperature=0.7,
top_p=0.95,
top_k=40,
max_tokens=256,
repetition_penalty=1.1
)
code_params = SamplingParams(
temperature=0.0,
max_tokens=512
)
creative_params = SamplingParams(
temperature=1.0,
top_p=0.9,
max_tokens=1024
)
Step 3: Chat Completion Format
messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Write a Python function to calculate factorial."}
]
prompt = llm.get_tokenizer().apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
output = llm.generate(prompt, sampling_params)
print(output[0].outputs[0].text)
Step 4: Text Completion Format
prompt = """Translate the following to French:
Hello, how are you?
Translation:"""
output = llm.generate(prompt, sampling_params)
print(output[0].outputs[0].text)
Common Patterns
Pattern 1: Classification Task
prompt = """Classify the sentiment of this text as positive, negative, or neutral:
Text: "The movie was absolutely fantastic!"
Sentiment:"""
classification_params = SamplingParams(
temperature=0.0,
max_tokens=10,
stop=["\n"]
)
output = llm.generate(prompt, classification_params)
sentiment = output[0].outputs[0].text.strip()
Pattern 2: Structured Output (JSON)
import json
prompt = """Extract information from this text and return as JSON:
Text: "John Doe is 30 years old and works as an engineer in New York."
JSON:{"name": "John Doe", "age": 30, "job": "engineer", "location": "New York"}"""
json_params = SamplingParams(
temperature=0.0,
max_tokens=200,
stop=["}\n"]
)
output = llm.generate(prompt, json_params)
try:
result = json.loads(output[0].outputs[0].text)
except json.JSONDecodeError:
result = None
Pattern 3: Summarization
long_text = """[Your long article here...]"""
prompt = f"Summarize the following text in 3 sentences:\n\n{long_text}\n\nSummary:"
summary_params = SamplingParams(
temperature=0.3,
max_tokens=200
)
output = llm.generate(prompt, summary_params)
summary = output[0].outputs[0].text
Pattern 4: Long Context Handling
long_context_models = [
"Qwen/Qwen2-7B-Instruct",
"mistralai/Mistral-7B-Instruct-v0.2",
"gradientai/Llama-3-8B-Instruct-Gradient-1048k"
]
llm = LLM(
model="model-name",
max_model_len=32768,
rope_scaling={
"type": "dynamic",
"factor": 4.0
}
)
Pattern 5: Multi-turn Conversation
conversation = []
while True:
user_input = input("User: ")
if user_input.lower() == "exit":
break
conversation.append({"role": "user", "content": user_input})
prompt = llm.get_tokenizer().apply_chat_template(
conversation,
tokenize=False,
add_generation_prompt=True
)
output = llm.generate(prompt, SamplingParams(max_tokens=200))
response = output[0].outputs[0].text
print(f"Assistant: {response}")
conversation.append({"role": "assistant", "content": response})
Troubleshooting
Problem: Outputs truncated mid-sentence
Solution:
sampling_params = SamplingParams(max_tokens=1024)
sampling_params = SamplingParams(
max_tokens=512,
stop=["\n\n", "###"]
)
Problem: Repetitive outputs
Solution:
sampling_params = SamplingParams(
temperature=0.8,
repetition_penalty=1.2,
top_p=0.95,
frequency_penalty=0.5
)
Problem: Context too long error
Solution:
max_len = llm.llm_engine.model_config.max_model_len
print(f"Max context: {max_len}")
tokenizer = llm.get_tokenizer()
tokens = tokenizer.encode(long_text, max_length=max_len-100, truncation=True)
truncated = tokenizer.decode(tokens)
References