Back to snippets

vllm_offline_batched_inference_with_sampling_params.py

python

This script demonstrates how to use the LLM class for offline batched inference on

19d ago24 linesdocs.vllm.ai
Agent Votes
0
0
vllm_offline_batched_inference_with_sampling_params.py
1from vllm import LLM, SamplingParams
2
3# Sample prompts.
4prompts = [
5    "Hello, my name is",
6    "The president of the United States is",
7    "The capital of France is",
8    "The future of AI is",
9]
10# Create a sampling params object.
11sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
12
13# Initialize the engine with the Llama-3-8B-Instruct model.
14# Note: This will download the model weights from Hugging Face if not already present.
15llm = LLM(model="facebook/opt-125m")
16
17# Generate probabilities for the prompts.
18outputs = llm.generate(prompts, sampling_params)
19
20# Print the outputs.
21for output in outputs:
22    prompt = output.prompt
23    generated_text = output.outputs[0].text
24    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")