|
| 1 | +import time |
| 2 | +import subprocess |
| 3 | + |
| 4 | +import sys |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | +import ray # using Ray for overall ease of process management, parallel requests, and debugging. |
| 8 | +import openai # use the official client for correctness check |
| 9 | + |
| 10 | +MAX_SERVER_START_WAIT_S = 600 # wait for server to start for 60 seconds |
| 11 | +MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta" # any model with a chat template should work here |
| 12 | + |
| 13 | +pytestmark = pytest.mark.asyncio |
| 14 | + |
| 15 | + |
| 16 | +@ray.remote(num_gpus=1) |
| 17 | +class ServerRunner: |
| 18 | + |
| 19 | + def __init__(self, args): |
| 20 | + self.proc = subprocess.Popen( |
| 21 | + ["python3", "-m", "vllm.entrypoints.openai.api_server"] + args, |
| 22 | + stdout=sys.stdout, |
| 23 | + stderr=sys.stderr, |
| 24 | + ) |
| 25 | + self._wait_for_server() |
| 26 | + |
| 27 | + def ready(self): |
| 28 | + return True |
| 29 | + |
| 30 | + def _wait_for_server(self): |
| 31 | + # run health check |
| 32 | + start = time.time() |
| 33 | + while True: |
| 34 | + try: |
| 35 | + if requests.get( |
| 36 | + "http://localhost:8000/health").status_code == 200: |
| 37 | + break |
| 38 | + except Exception as err: |
| 39 | + if self.proc.poll() is not None: |
| 40 | + raise RuntimeError("Server exited unexpectedly.") from err |
| 41 | + |
| 42 | + time.sleep(0.5) |
| 43 | + if time.time() - start > MAX_SERVER_START_WAIT_S: |
| 44 | + raise RuntimeError( |
| 45 | + "Server failed to start in time.") from err |
| 46 | + |
| 47 | + def __del__(self): |
| 48 | + if hasattr(self, "proc"): |
| 49 | + self.proc.terminate() |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="session") |
| 53 | +def server(): |
| 54 | + ray.init() |
| 55 | + server_runner = ServerRunner.remote([ |
| 56 | + "--model", |
| 57 | + MODEL_NAME, |
| 58 | + "--dtype", |
| 59 | + "bfloat16", # use half precision for speed and memory savings in CI environment |
| 60 | + "--max-model-len", |
| 61 | + "8192" |
| 62 | + ]) |
| 63 | + ray.get(server_runner.ready.remote()) |
| 64 | + yield server_runner |
| 65 | + ray.shutdown() |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture(scope="session") |
| 69 | +def client(): |
| 70 | + client = openai.AsyncOpenAI( |
| 71 | + base_url="http://localhost:8000/v1", |
| 72 | + api_key="token-abc123", |
| 73 | + ) |
| 74 | + yield client |
| 75 | + |
| 76 | + |
| 77 | +async def test_single_completion(server, client: openai.AsyncOpenAI): |
| 78 | + completion = await client.completions.create(model=MODEL_NAME, |
| 79 | + prompt="Hello, my name is", |
| 80 | + max_tokens=5, |
| 81 | + temperature=0.0) |
| 82 | + |
| 83 | + assert completion.id is not None |
| 84 | + assert completion.choices is not None and len(completion.choices) == 1 |
| 85 | + assert completion.choices[0].text is not None and len( |
| 86 | + completion.choices[0].text) >= 5 |
| 87 | + assert completion.choices[0].finish_reason == "length" |
| 88 | + assert completion.usage == openai.types.CompletionUsage( |
| 89 | + completion_tokens=5, prompt_tokens=6, total_tokens=11) |
| 90 | + |
| 91 | + |
| 92 | +async def test_single_chat_session(server, client: openai.AsyncOpenAI): |
| 93 | + messages = [{ |
| 94 | + "role": "system", |
| 95 | + "content": "you are a helpful assistant" |
| 96 | + }, { |
| 97 | + "role": "user", |
| 98 | + "content": "what is 1+1?" |
| 99 | + }] |
| 100 | + |
| 101 | + # test single completion |
| 102 | + chat_completion = await client.chat.completions.create( |
| 103 | + model=MODEL_NAME, |
| 104 | + messages=messages, |
| 105 | + max_tokens=10, |
| 106 | + ) |
| 107 | + assert chat_completion.id is not None |
| 108 | + assert chat_completion.choices is not None and len( |
| 109 | + chat_completion.choices) == 1 |
| 110 | + assert chat_completion.choices[0].message is not None |
| 111 | + message = chat_completion.choices[0].message |
| 112 | + assert message.content is not None and len(message.content) >= 10 |
| 113 | + assert message.role == "assistant" |
| 114 | + messages.append({"role": "assistant", "content": message.content}) |
| 115 | + |
| 116 | + # test multi-turn dialogue |
| 117 | + messages.append({"role": "user", "content": "express your result in json"}) |
| 118 | + chat_completion = await client.chat.completions.create( |
| 119 | + model=MODEL_NAME, |
| 120 | + messages=messages, |
| 121 | + max_tokens=10, |
| 122 | + ) |
| 123 | + message = chat_completion.choices[0].message |
| 124 | + assert message.content is not None and len(message.content) >= 0 |
| 125 | + |
| 126 | + |
| 127 | +async def test_completion_streaming(server, client: openai.AsyncOpenAI): |
| 128 | + prompt = "What is an LLM?" |
| 129 | + |
| 130 | + single_completion = await client.completions.create( |
| 131 | + model=MODEL_NAME, |
| 132 | + prompt=prompt, |
| 133 | + max_tokens=5, |
| 134 | + temperature=0.0, |
| 135 | + ) |
| 136 | + single_output = single_completion.choices[0].text |
| 137 | + single_usage = single_completion.usage |
| 138 | + |
| 139 | + stream = await client.completions.create( |
| 140 | + model=MODEL_NAME, |
| 141 | + prompt=prompt, |
| 142 | + max_tokens=5, |
| 143 | + temperature=0.0, |
| 144 | + stream=True, |
| 145 | + ) |
| 146 | + chunks = [] |
| 147 | + async for chunk in stream: |
| 148 | + chunks.append(chunk.choices[0].text) |
| 149 | + assert chunk.choices[0].finish_reason == "length" |
| 150 | + assert chunk.usage == single_usage |
| 151 | + assert "".join(chunks) == single_output |
| 152 | + |
| 153 | + |
| 154 | +async def test_chat_streaming(server, client: openai.AsyncOpenAI): |
| 155 | + messages = [{ |
| 156 | + "role": "system", |
| 157 | + "content": "you are a helpful assistant" |
| 158 | + }, { |
| 159 | + "role": "user", |
| 160 | + "content": "what is 1+1?" |
| 161 | + }] |
| 162 | + |
| 163 | + # test single completion |
| 164 | + chat_completion = await client.chat.completions.create( |
| 165 | + model=MODEL_NAME, |
| 166 | + messages=messages, |
| 167 | + max_tokens=10, |
| 168 | + temperature=0.0, |
| 169 | + ) |
| 170 | + output = chat_completion.choices[0].message.content |
| 171 | + stop_reason = chat_completion.choices[0].finish_reason |
| 172 | + |
| 173 | + # test streaming |
| 174 | + stream = await client.chat.completions.create( |
| 175 | + model=MODEL_NAME, |
| 176 | + messages=messages, |
| 177 | + max_tokens=10, |
| 178 | + temperature=0.0, |
| 179 | + stream=True, |
| 180 | + ) |
| 181 | + chunks = [] |
| 182 | + async for chunk in stream: |
| 183 | + delta = chunk.choices[0].delta |
| 184 | + if delta.role: |
| 185 | + assert delta.role == "assistant" |
| 186 | + if delta.content: |
| 187 | + chunks.append(delta.content) |
| 188 | + assert chunk.choices[0].finish_reason == stop_reason |
| 189 | + assert "".join(chunks) == output |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + pytest.main([__file__]) |
0 commit comments