Skip to content

Commit 9a1adcd

Browse files
committed
feat(decorators): add performance measurement for streaming functions
This commit introduces a decorator `measure_stream_performance` that tracks and prints performance metrics for streaming functions. It measures time to the first character, total execution time, and calculates output rates for characters and tokens. Additionally, the `run_llm_stream` function now includes detailed documentation about its parameters and return type. These enhancements aim to facilitate better performance monitoring and debugging for developers working with streaming outputs in language models.
1 parent 00d66ae commit 9a1adcd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

hdl/utils/decorators/llm.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
from functools import wraps
33

44
def measure_stream_performance(func):
5+
"""
6+
Measures the performance of a streaming function by tracking the time taken to output characters and the rate of character and token generation.
7+
8+
Args:
9+
func (callable): The streaming function to be measured. It should return a generator that yields dictionaries containing 'type' and 'content' keys.
10+
11+
Returns:
12+
callable: A wrapper function that performs the performance measurement and prints statistics related to the streaming output.
13+
14+
Statistics Printed:
15+
- Time to first character (in seconds)
16+
- Total time taken for the execution (in seconds)
17+
- Total number of characters output
18+
- Total number of tokens processed
19+
- Characters output per second
20+
- Tokens processed per second
21+
"""
522
@wraps(func)
623
def wrapper(*args, **kwargs):
724
# 开始计时
@@ -54,6 +71,18 @@ def run_llm_stream(
5471
prompt,
5572
**kwargs
5673
):
74+
"""
75+
Run a language model stream with the given parameters.
76+
77+
Args:
78+
llm (object): The language model object used to generate responses.
79+
client_id (str): The unique identifier for the client making the request.
80+
prompt (str): The input prompt to which the language model should respond.
81+
**kwargs: Additional keyword arguments to customize the request.
82+
83+
Returns:
84+
iterable: An iterable response stream from the language model.
85+
"""
5786
resp = llm.stream(
5887
client_id=client_id,
5988
prompt=prompt,

0 commit comments

Comments
 (0)