main
py 102 lines 3.32 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from vllm import LLM, EngineArgs
5 from vllm.outputs import RequestOutput
6 from vllm.utils.argparse_utils import FlexibleArgumentParser
7
8
9 def create_parser():
10 parser = FlexibleArgumentParser()
11 # Add engine args
12 EngineArgs.add_cli_args(parser)
13 parser.set_defaults(model="meta-llama/Llama-3.2-1B-Instruct")
14 # Add sampling params
15 sampling_group = parser.add_argument_group("Sampling parameters")
16 sampling_group.add_argument("--max-tokens", type=int)
17 sampling_group.add_argument("--temperature", type=float)
18 sampling_group.add_argument("--top-p", type=float)
19 sampling_group.add_argument("--top-k", type=int)
20 # Add example params
21 parser.add_argument("--chat-template-path", type=str)
22
23 return parser
24
25
26 def main(args: dict):
27 # Pop arguments not used by LLM
28 max_tokens = args.pop("max_tokens")
29 temperature = args.pop("temperature")
30 top_p = args.pop("top_p")
31 top_k = args.pop("top_k")
32 chat_template_path = args.pop("chat_template_path")
33
34 # Create an LLM
35 llm = LLM(**args)
36
37 # Create sampling params object
38 sampling_params = llm.get_default_sampling_params()
39 if max_tokens is not None:
40 sampling_params.max_tokens = max_tokens
41 if temperature is not None:
42 sampling_params.temperature = temperature
43 if top_p is not None:
44 sampling_params.top_p = top_p
45 if top_k is not None:
46 sampling_params.top_k = top_k
47
48 def print_outputs(outputs: list[RequestOutput], prompts: list):
49 assert len(outputs) == len(prompts)
50 print("\nGenerated Outputs:\n" + "-" * 80)
51 for i, output in enumerate(outputs):
52 generated_text = output.outputs[0].text
53 print(f"Prompt: {prompts[i]!r}\n")
54 print(f"Generated text: {generated_text!r}")
55 print("-" * 80)
56
57 print("=" * 80)
58
59 # In this script, we demonstrate how to pass input to the chat method:
60 conversation = [
61 {"role": "system", "content": "You are a helpful assistant"},
62 {"role": "user", "content": "Hello"},
63 {"role": "assistant", "content": "Hello! How can I assist you today?"},
64 {
65 "role": "user",
66 "content": "Write an essay about the importance of higher education.",
67 },
68 ]
69 outputs = llm.chat(conversation, sampling_params, use_tqdm=False)
70 print_outputs(
71 outputs,
72 [
73 conversation,
74 ],
75 )
76
77 # You can run batch inference with llm.chat API
78 conversations = [conversation for _ in range(10)]
79
80 # We turn on tqdm progress bar to verify it's indeed running batch inference
81 outputs = llm.chat(conversations, sampling_params, use_tqdm=True)
82 print_outputs(outputs, conversations)
83
84 # A chat template can be optionally supplied.
85 # If not, the model will use its default chat template.
86 if chat_template_path is not None:
87 with open(chat_template_path) as f:
88 chat_template = f.read()
89
90 outputs = llm.chat(
91 conversations,
92 sampling_params,
93 use_tqdm=False,
94 chat_template=chat_template,
95 )
96 print_outputs(outputs, conversations)
97
98
99 if __name__ == "__main__":
100 parser = create_parser()
101 args: dict = vars(parser.parse_args())
102 main(args)