"torchvision/vscode:/vscode.git/clone" did not exist on "c36cb6d28333d3a3ce89974c062c807ce3e0a1ff"
async_io_api.py 931 Bytes
Newer Older
1
2
"""
Usage:
Lianmin Zheng's avatar
Lianmin Zheng committed
3

4
5
python3 async_io.py
"""
zhyncs's avatar
zhyncs committed
6

Ying Sheng's avatar
Ying Sheng committed
7
import asyncio
zhyncs's avatar
zhyncs committed
8

Ying Sheng's avatar
Ying Sheng committed
9
10
11
12
13
14
15
16
17
18
19
from sglang import Runtime


async def generate(
    engine,
    prompt,
    sampling_params,
):
    tokenizer = engine.get_tokenizer()

    messages = [
zhyncs's avatar
zhyncs committed
20
21
22
23
        {
            "role": "system",
            "content": "You will be given question answer tasks.",
        },
Ying Sheng's avatar
Ying Sheng committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
        {"role": "user", "content": prompt},
    ]

    prompt = tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=True
    )

    stream = engine.add_request(prompt, sampling_params)

    async for output in stream:
        print(output, end="", flush=True)
    print()


if __name__ == "__main__":
    runtime = Runtime(model_path="meta-llama/Llama-2-7b-chat-hf")
40
41
    print("--- runtime ready ---\n")

Ying Sheng's avatar
Ying Sheng committed
42
43
44
    prompt = "Who is Alan Turing?"
    sampling_params = {"max_new_tokens": 128}
    asyncio.run(generate(runtime, prompt, sampling_params))
zhyncs's avatar
zhyncs committed
45

Ying Sheng's avatar
Ying Sheng committed
46
    runtime.shutdown()