Repository navigation
Expand file tree
/
Copy pathragas_progress.py
More file actions
77 lines (66 loc) · 2.51 KB
/
Copy pathragas_progress.py
File metadata and controls
77 lines (66 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""RAGAS 评分进度条:按样本完成数显示,而非样本×指标任务数。"""
from __future__ import annotations
from contextlib import contextmanager
from typing import Iterator
from ragas.async_utils import as_completed, process_futures
from ragas.executor import Executor
from tqdm.auto import tqdm
@contextmanager
def sample_progress_bar(
n_samples: int,
n_metrics: int,
*,
desc: str = "RAGAS 评分",
) -> Iterator[None]:
"""
临时替换 Executor._process_coroutines,将进度按样本聚合。
RAGAS 默认 total = n_samples × n_metrics(如 15×4=60),
此处改为 total = n_samples,每完成一条样本的全部指标 update 一次。
"""
if n_samples <= 0 or n_metrics <= 0:
yield
return
sample_bar = tqdm(
total=n_samples,
desc=desc,
unit="样本",
dynamic_ncols=True,
)
row_done = [0] * n_samples
original = Executor._process_coroutines
async def _process_coroutines(self, jobs, pbar, results, max_workers):
coroutines = [afunc(*args, **kwargs) for afunc, args, kwargs, _ in jobs]
job_names = [name for *_, name in jobs]
async for result in process_futures(
as_completed(coroutines, max_workers, cancel_check=self.is_cancelled)
):
if isinstance(result, Exception) and self.raise_exceptions:
raise result
results.append(result)
counter = result[0] if isinstance(result, tuple) else None
if counter is not None:
row = counter // n_metrics
metric_idx = counter % n_metrics
job_name = (
job_names[counter]
if counter < len(job_names)
else f"metric-{metric_idx}"
)
if 0 <= row < n_samples:
row_done[row] += 1
print(
f" [样本 {row + 1}/{n_samples}] 完成 {job_name}",
flush=True,
)
if row_done[row] == n_metrics:
sample_bar.update(1)
sample_bar.set_postfix(
已完成=f"{int(sample_bar.n)}/{n_samples}",
refresh=False,
)
Executor._process_coroutines = _process_coroutines # type: ignore[method-assign]
try:
yield
finally:
Executor._process_coroutines = original
sample_bar.close()