Skip to content

Commit ebd5a77

Browse files
authored
feat: add usage to TranscriptionResponse (text and json response_format) (vllm-project#23576)
Signed-off-by: Guillaume Calmettes <gcalmettes@scaleway.com>
1 parent 384dd1b commit ebd5a77

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

tests/entrypoints/openai/test_transcription_validation.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ async def test_basic_audio(mary_had_lamb, model_name):
6969
language="en",
7070
response_format="text",
7171
temperature=0.0)
72-
out = json.loads(transcription)['text']
73-
assert "Mary had a little lamb," in out
72+
out = json.loads(transcription)
73+
out_text = out['text']
74+
out_usage = out['usage']
75+
assert "Mary had a little lamb," in out_text
76+
assert out_usage["seconds"] == 16, out_usage["seconds"]
7477

7578

7679
@pytest.mark.asyncio
@@ -116,9 +119,12 @@ async def test_long_audio_request(mary_had_lamb, client):
116119
language="en",
117120
response_format="text",
118121
temperature=0.0)
119-
out = json.loads(transcription)['text']
120-
counts = out.count("Mary had a little lamb")
122+
out = json.loads(transcription)
123+
out_text = out['text']
124+
out_usage = out['usage']
125+
counts = out_text.count("Mary had a little lamb")
121126
assert counts == 10, counts
127+
assert out_usage["seconds"] == 161, out_usage["seconds"]
122128

123129

124130
@pytest.mark.asyncio

vllm/entrypoints/openai/protocol.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,9 +2232,15 @@ def validate_transcription_request(cls, data):
22322232

22332233

22342234
# Transcription response objects
2235+
class TranscriptionUsageAudio(OpenAIBaseModel):
2236+
type: Literal["duration"] = "duration"
2237+
seconds: int
2238+
2239+
22352240
class TranscriptionResponse(OpenAIBaseModel):
22362241
text: str
22372242
"""The transcribed text."""
2243+
usage: TranscriptionUsageAudio
22382244

22392245

22402246
class TranscriptionWord(OpenAIBaseModel):

vllm/entrypoints/openai/speech_to_text.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,22 @@ async def _create_speech_to_text(
200200
for result_generator in list_result_generator:
201201
async for op in result_generator:
202202
text += op.outputs[0].text
203-
return cast(T, response_class(text=text))
203+
204+
if self.task_type == "transcribe":
205+
# add usage in TranscriptionResponse.
206+
usage = {
207+
"type": "duration",
208+
# rounded up as per openAI specs
209+
"seconds": int(math.ceil(duration_s)),
210+
}
211+
final_response = cast(T, response_class(text=text,
212+
usage=usage))
213+
else:
214+
# no usage in response for translation task
215+
final_response = cast(
216+
T, response_class(text=text)) # type: ignore[call-arg]
217+
218+
return final_response
204219
except asyncio.CancelledError:
205220
return self.create_error_response("Client disconnected")
206221
except ValueError as e:

0 commit comments

Comments
 (0)