main
py 73 lines 2.3 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """This file demonstrates using the `LLMEngine`
4 for processing prompts with various sampling parameters.
5 """
6
7 import argparse
8
9 from vllm import EngineArgs, LLMEngine, RequestOutput, SamplingParams
10 from vllm.utils.argparse_utils import FlexibleArgumentParser
11
12
13 def create_test_prompts() -> list[tuple[str, SamplingParams]]:
14 """Create a list of test prompts with their sampling parameters."""
15 return [
16 (
17 "A robot may not injure a human being",
18 SamplingParams(temperature=0.0, logprobs=1, prompt_logprobs=1),
19 ),
20 (
21 "To be or not to be,",
22 SamplingParams(temperature=0.8, top_k=5, presence_penalty=0.2),
23 ),
24 (
25 "What is the meaning of life?",
26 SamplingParams(n=2, temperature=0.8, top_p=0.95, frequency_penalty=0.1),
27 ),
28 ]
29
30
31 def process_requests(engine: LLMEngine, test_prompts: list[tuple[str, SamplingParams]]):
32 """Continuously process a list of prompts and handle the outputs."""
33 request_id = 0
34
35 print("-" * 50)
36 while test_prompts or engine.has_unfinished_requests():
37 if test_prompts:
38 prompt, sampling_params = test_prompts.pop(0)
39 engine.add_request(str(request_id), prompt, sampling_params)
40 request_id += 1
41
42 request_outputs: list[RequestOutput] = engine.step()
43
44 for request_output in request_outputs:
45 if request_output.finished:
46 print(request_output)
47 print("-" * 50)
48
49
50 def initialize_engine(args: argparse.Namespace) -> LLMEngine:
51 """Initialize the LLMEngine from the command line arguments."""
52 engine_args = EngineArgs.from_cli_args(args)
53 return LLMEngine.from_engine_args(engine_args)
54
55
56 def parse_args():
57 parser = FlexibleArgumentParser(
58 description="Demo on using the LLMEngine class directly"
59 )
60 parser = EngineArgs.add_cli_args(parser)
61 return parser.parse_args()
62
63
64 def main(args: argparse.Namespace):
65 """Main function that sets up and runs the prompt processing."""
66 engine = initialize_engine(args)
67 test_prompts = create_test_prompts()
68 process_requests(engine, test_prompts)
69
70
71 if __name__ == "__main__":
72 args = parse_args()
73 main(args)