Add continuous_usage_stats support for streaming responses (#12241)
This commit is contained in:
@@ -109,6 +109,7 @@ class UsageInfo(BaseModel):
|
||||
|
||||
class StreamOptions(BaseModel):
|
||||
include_usage: Optional[bool] = False
|
||||
continuous_usage_stats: Optional[bool] = False
|
||||
|
||||
|
||||
class JsonSchemaResponseFormat(BaseModel):
|
||||
|
||||
@@ -535,6 +535,17 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
choices=[choice_data],
|
||||
model=request.model,
|
||||
)
|
||||
|
||||
# Add usage stats if continuous_usage_stats is enabled
|
||||
if (
|
||||
request.stream_options
|
||||
and request.stream_options.continuous_usage_stats
|
||||
):
|
||||
chunk.usage = UsageProcessor.calculate_token_usage(
|
||||
prompt_tokens=prompt_tokens.get(index, 0),
|
||||
completion_tokens=completion_tokens.get(index, 0),
|
||||
)
|
||||
|
||||
yield f"data: {chunk.model_dump_json()}\n\n"
|
||||
|
||||
# Handle tool calls
|
||||
@@ -579,6 +590,17 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
choices=[choice_data],
|
||||
model=request.model,
|
||||
)
|
||||
|
||||
# Add usage stats if continuous_usage_stats is enabled
|
||||
if (
|
||||
request.stream_options
|
||||
and request.stream_options.continuous_usage_stats
|
||||
):
|
||||
chunk.usage = UsageProcessor.calculate_token_usage(
|
||||
prompt_tokens=prompt_tokens.get(index, 0),
|
||||
completion_tokens=completion_tokens.get(index, 0),
|
||||
)
|
||||
|
||||
yield f"data: {chunk.model_dump_json()}\n\n"
|
||||
|
||||
# Send finish_reason chunks for each index that completed
|
||||
@@ -1056,6 +1078,16 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
choices=[choice_data],
|
||||
model=request.model,
|
||||
)
|
||||
|
||||
# Add usage stats if continuous_usage_stats is enabled
|
||||
if request.stream_options and request.stream_options.continuous_usage_stats:
|
||||
prompt_tokens = content["meta_info"].get("prompt_tokens", 0)
|
||||
completion_tokens = content["meta_info"].get("completion_tokens", 0)
|
||||
chunk.usage = UsageProcessor.calculate_token_usage(
|
||||
prompt_tokens=prompt_tokens,
|
||||
completion_tokens=completion_tokens,
|
||||
)
|
||||
|
||||
yield f"data: {chunk.model_dump_json()}\n\n"
|
||||
|
||||
# Yield tool calls
|
||||
@@ -1096,6 +1128,16 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
choices=[choice_data],
|
||||
model=request.model,
|
||||
)
|
||||
|
||||
# Add usage stats if continuous_usage_stats is enabled
|
||||
if request.stream_options and request.stream_options.continuous_usage_stats:
|
||||
prompt_tokens = content["meta_info"].get("prompt_tokens", 0)
|
||||
completion_tokens = content["meta_info"].get("completion_tokens", 0)
|
||||
chunk.usage = UsageProcessor.calculate_token_usage(
|
||||
prompt_tokens=prompt_tokens,
|
||||
completion_tokens=completion_tokens,
|
||||
)
|
||||
|
||||
yield f"data: {chunk.model_dump_json()}\n\n"
|
||||
|
||||
def _check_for_unstreamed_tool_args(
|
||||
|
||||
@@ -272,6 +272,16 @@ class OpenAIServingCompletion(OpenAIServingBase):
|
||||
model=request.model,
|
||||
)
|
||||
|
||||
# Add usage stats if continuous_usage_stats is enabled
|
||||
if (
|
||||
request.stream_options
|
||||
and request.stream_options.continuous_usage_stats
|
||||
):
|
||||
chunk.usage = UsageProcessor.calculate_token_usage(
|
||||
prompt_tokens=prompt_tokens.get(index, 0),
|
||||
completion_tokens=completion_tokens.get(index, 0),
|
||||
)
|
||||
|
||||
yield f"data: {chunk.model_dump_json()}\n\n"
|
||||
|
||||
if request.return_hidden_states and hidden_states:
|
||||
|
||||
Reference in New Issue
Block a user