Anthropic-совместимый эндпоинт Z.AI; схему просим текстом (structured output у GLM нет), pydantic-валидация + один ретрай с фидбеком ошибки. zai_compare.py: GLM извлекает «Принцип» по сырому ASR → сравнение с эталоном по типам карточек, битым таймкодам, покрытию 9 golden-фактов. Выход GLM в data/eval/zai/ — базу знаний не трогает. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
"""Z.AI-извлечение: сборка запроса и разбор ответа — без API."""
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
|
||
from rag import corpus
|
||
from rag.extract import zai
|
||
|
||
|
||
@pytest.fixture()
|
||
def lecture(tmp_path: Path) -> corpus.Lecture:
|
||
seg = tmp_path / "segments.txt"
|
||
seg.write_text("[00:01] сегмент про контур 80 кГц\n", encoding="utf-8")
|
||
return corpus.Lecture(id="тест", fragments=(
|
||
corpus.Transcript(id="тест__фр1", source="mass", path=seg),
|
||
))
|
||
|
||
|
||
VALID_JSON = ('{"lecture_id": "тест", "cards": [{"type": "принцип", "title": "т",'
|
||
'"statement": "с", "layer": "радиотехника",'
|
||
'"sources": [{"video": "тест__фр1", "timecode": "00:01", "quote": "контур"}]}],'
|
||
'"asr_findings": []}')
|
||
|
||
|
||
def test_request_contains_schema_and_rules(lecture):
|
||
kw = zai.build_request_kwargs(lecture)
|
||
assert "json-схеме" in kw["system"].lower()
|
||
assert "ExtractionResult" in kw["system"] or "cards" in kw["system"]
|
||
assert "НИЧЕГО НЕ ВЫДУМЫВАТЬ" in kw["system"]
|
||
assert "=== ФРАГМЕНТ: тест__фр1 ===" in kw["messages"][0]["content"]
|
||
|
||
|
||
def test_parse_plain_json():
|
||
result = zai.parse_result(VALID_JSON)
|
||
assert result.lecture_id == "тест"
|
||
assert len(result.cards) == 1
|
||
|
||
|
||
def test_parse_fenced_json():
|
||
fenced = f"```json\n{VALID_JSON}\n```"
|
||
assert zai.parse_result(fenced).lecture_id == "тест"
|
||
|
||
|
||
def test_parse_garbage_raises():
|
||
with pytest.raises(Exception):
|
||
zai.parse_result("вот ваш JSON: непонятно что")
|