43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import json
|
|||
|
|
|
|||
|
|
from compute.engines.llama_factory.eval_runner import _load_dataset
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _write(tmp_path, name: str, text: str) -> str:
|
|||
|
|
path = tmp_path / name
|
|||
|
|
path.write_text(text, encoding="utf-8")
|
|||
|
|
return str(path)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_load_jsonl_multiline(tmp_path) -> None:
|
|||
|
|
path = _write(
|
|||
|
|
tmp_path,
|
|||
|
|
"eval.jsonl",
|
|||
|
|
'{"question": "q1", "answer": "a1"}\n{"question": "q2", "answer": "a2"}\n',
|
|||
|
|
)
|
|||
|
|
assert _load_dataset(path) == [
|
|||
|
|
{"question": "q1", "answer": "a1"},
|
|||
|
|
{"question": "q2", "answer": "a2"},
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_load_json_array(tmp_path) -> None:
|
|||
|
|
path = _write(
|
|||
|
|
tmp_path,
|
|||
|
|
"eval.json",
|
|||
|
|
json.dumps([{"question": "x", "answer": "y"}]),
|
|||
|
|
)
|
|||
|
|
assert _load_dataset(path) == [{"question": "x", "answer": "y"}]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_load_jsonl_with_bom_and_embedded_array(tmp_path) -> None:
|
|||
|
|
"""jsonl 带 BOM 且单行内嵌 JSON 数组,都应正常加载。"""
|
|||
|
|
path = _write(
|
|||
|
|
tmp_path,
|
|||
|
|
"eval.jsonl",
|
|||
|
|
"" + json.dumps([{"question": "a", "answer": "b"}, {"question": "c", "answer": "d"}]),
|
|||
|
|
)
|
|||
|
|
assert len(_load_dataset(path)) == 2
|