main
py 69 lines 1.9 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """This script demonstrates how to extend the context length
4 of a Qwen model using the YARN method (rope_parameters)
5 and run a simple chat example.
6
7 Usage:
8 python examples/features/context_extension/context_extension_offline.py
9 """
10
11 from vllm import LLM, RequestOutput, SamplingParams
12
13
14 def create_llm():
15 rope_theta = 1000000
16 original_max_position_embeddings = 32768
17 factor = 4.0
18
19 # Use yarn to extend context
20 hf_overrides = {
21 "rope_parameters": {
22 "rope_theta": rope_theta,
23 "rope_type": "yarn",
24 "factor": factor,
25 "original_max_position_embeddings": original_max_position_embeddings,
26 },
27 "max_model_len": int(original_max_position_embeddings * factor),
28 }
29
30 llm = LLM(model="Qwen/Qwen3-0.6B", hf_overrides=hf_overrides)
31 return llm
32
33
34 def run_llm_chat(llm):
35 sampling_params = SamplingParams(
36 temperature=0.8,
37 top_p=0.95,
38 max_tokens=128,
39 )
40
41 conversation = [
42 {"role": "system", "content": "You are a helpful assistant"},
43 {"role": "user", "content": "Hello"},
44 {"role": "assistant", "content": "Hello! How can I assist you today?"},
45 ]
46 outputs = llm.chat(conversation, sampling_params, use_tqdm=False)
47 return outputs, [
48 conversation,
49 ]
50
51
52 def print_outputs(outputs: list[RequestOutput], conversations: list):
53 print("\nGenerated Outputs:\n" + "-" * 80)
54 for i, output in enumerate(outputs):
55 prompt = conversations[i]
56 generated_text = output.outputs[0].text
57 print(f"Prompt: {prompt!r}\n")
58 print(f"Generated text: {generated_text!r}")
59 print("-" * 80)
60
61
62 def main():
63 llm = create_llm()
64 outputs, conversations = run_llm_chat(llm)
65 print_outputs(outputs, conversations)
66
67
68 if __name__ == "__main__":
69 main()