|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import openai |
| 4 | +import pytest |
| 5 | +import pytest_asyncio |
| 6 | + |
| 7 | +from ...utils import RemoteOpenAIServer |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope="module") |
| 11 | +def chat_server_with_force_include_usage(request): # noqa: F811 |
| 12 | + args = [ |
| 13 | + # use half precision for speed and memory savings in CI environment |
| 14 | + "--dtype", |
| 15 | + "bfloat16", |
| 16 | + "--max-model-len", |
| 17 | + "128", |
| 18 | + "--enforce-eager", |
| 19 | + "--max-num-seqs", |
| 20 | + "1", |
| 21 | + "--enable-force-include-usage", |
| 22 | + "--port", |
| 23 | + "55857", |
| 24 | + "--gpu-memory-utilization", |
| 25 | + "0.2", |
| 26 | + ] |
| 27 | + |
| 28 | + with RemoteOpenAIServer("Qwen/Qwen3-0.6B", args, auto_port=False) as remote_server: |
| 29 | + yield remote_server |
| 30 | + |
| 31 | + |
| 32 | +@pytest_asyncio.fixture |
| 33 | +async def chat_client_with_force_include_usage(chat_server_with_force_include_usage): |
| 34 | + async with chat_server_with_force_include_usage.get_async_client() as async_client: |
| 35 | + yield async_client |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_chat_with_enable_force_include_usage( |
| 40 | + chat_client_with_force_include_usage: openai.AsyncOpenAI, |
| 41 | +): |
| 42 | + messages = [ |
| 43 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 44 | + {"role": "user", "content": "What is the capital of France?"}, |
| 45 | + ] |
| 46 | + |
| 47 | + stream = await chat_client_with_force_include_usage.chat.completions.create( |
| 48 | + model="Qwen/Qwen3-0.6B", |
| 49 | + messages=messages, |
| 50 | + max_completion_tokens=10, |
| 51 | + extra_body=dict(min_tokens=10), |
| 52 | + temperature=0.0, |
| 53 | + stream=True, |
| 54 | + ) |
| 55 | + last_completion_tokens = 0 |
| 56 | + async for chunk in stream: |
| 57 | + if not len(chunk.choices): |
| 58 | + assert chunk.usage.prompt_tokens >= 0 |
| 59 | + assert ( |
| 60 | + last_completion_tokens == 0 |
| 61 | + or chunk.usage.completion_tokens > last_completion_tokens |
| 62 | + or ( |
| 63 | + not chunk.choices |
| 64 | + and chunk.usage.completion_tokens == last_completion_tokens |
| 65 | + ) |
| 66 | + ) |
| 67 | + assert chunk.usage.total_tokens == ( |
| 68 | + chunk.usage.prompt_tokens + chunk.usage.completion_tokens |
| 69 | + ) |
| 70 | + else: |
| 71 | + assert chunk.usage is None |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture(scope="module") |
| 75 | +def transcription_server_with_force_include_usage(): |
| 76 | + args = [ |
| 77 | + # use half precision for speed and memory savings in CI environment |
| 78 | + "--dtype", |
| 79 | + "bfloat16", |
| 80 | + "--max-num-seqs", |
| 81 | + "1", |
| 82 | + "--enforce-eager", |
| 83 | + "--enable-force-include-usage", |
| 84 | + "--gpu-memory-utilization", |
| 85 | + "0.2", |
| 86 | + ] |
| 87 | + |
| 88 | + with RemoteOpenAIServer("openai/whisper-large-v3-turbo", args) as remote_server: |
| 89 | + yield remote_server |
| 90 | + |
| 91 | + |
| 92 | +@pytest_asyncio.fixture |
| 93 | +async def transcription_client_with_force_include_usage( |
| 94 | + transcription_server_with_force_include_usage, |
| 95 | +): |
| 96 | + async with ( |
| 97 | + transcription_server_with_force_include_usage.get_async_client() as async_client |
| 98 | + ): |
| 99 | + yield async_client |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.asyncio |
| 103 | +async def test_transcription_with_enable_force_include_usage( |
| 104 | + transcription_client_with_force_include_usage, winning_call |
| 105 | +): |
| 106 | + res = ( |
| 107 | + await transcription_client_with_force_include_usage.audio.transcriptions.create( |
| 108 | + model="openai/whisper-large-v3-turbo", |
| 109 | + file=winning_call, |
| 110 | + language="en", |
| 111 | + temperature=0.0, |
| 112 | + stream=True, |
| 113 | + timeout=30, |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + async for chunk in res: |
| 118 | + if not len(chunk.choices): |
| 119 | + # final usage sent |
| 120 | + usage = chunk.usage |
| 121 | + assert isinstance(usage, dict) |
| 122 | + assert usage["prompt_tokens"] > 0 |
| 123 | + assert usage["completion_tokens"] > 0 |
| 124 | + assert usage["total_tokens"] > 0 |
| 125 | + else: |
| 126 | + assert not hasattr(chunk, "usage") |
0 commit comments