mirror of
https://github.com/langgenius/dify.git
synced 2026-04-15 18:06:36 +08:00
fix: support qa_preview shape in IndexProcessor preview formatting (#34151)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Crazywoola <100913391+crazywoola@users.noreply.github.com>
This commit is contained in:
parent
beda78e911
commit
09ee8ea1f5
@ -35,7 +35,10 @@ class IndexProcessor:
|
|||||||
if "parent_mode" in preview:
|
if "parent_mode" in preview:
|
||||||
data.parent_mode = preview["parent_mode"]
|
data.parent_mode = preview["parent_mode"]
|
||||||
|
|
||||||
for item in preview["preview"]:
|
# Different index processors return different preview shapes:
|
||||||
|
# - paragraph/parent-child processors: {"preview": [...]}
|
||||||
|
# - QA processor: {"qa_preview": [...]} (no "preview" key)
|
||||||
|
for item in preview.get("preview", []):
|
||||||
if "content" in item and "child_chunks" in item:
|
if "content" in item and "child_chunks" in item:
|
||||||
data.preview.append(
|
data.preview.append(
|
||||||
PreviewItem(content=item["content"], child_chunks=item["child_chunks"], summary=None)
|
PreviewItem(content=item["content"], child_chunks=item["child_chunks"], summary=None)
|
||||||
@ -44,6 +47,10 @@ class IndexProcessor:
|
|||||||
data.qa_preview.append(QaPreview(question=item["question"], answer=item["answer"]))
|
data.qa_preview.append(QaPreview(question=item["question"], answer=item["answer"]))
|
||||||
elif "content" in item:
|
elif "content" in item:
|
||||||
data.preview.append(PreviewItem(content=item["content"], child_chunks=None, summary=None))
|
data.preview.append(PreviewItem(content=item["content"], child_chunks=None, summary=None))
|
||||||
|
|
||||||
|
for item in preview.get("qa_preview", []):
|
||||||
|
if "question" in item and "answer" in item:
|
||||||
|
data.qa_preview.append(QaPreview(question=item["question"], answer=item["answer"]))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def index_and_clean(
|
def index_and_clean(
|
||||||
|
|||||||
@ -0,0 +1,15 @@
|
|||||||
|
from core.rag.index_processor.index_processor import IndexProcessor
|
||||||
|
|
||||||
|
|
||||||
|
class TestIndexProcessor:
|
||||||
|
def test_format_preview_supports_qa_preview_shape(self) -> None:
|
||||||
|
preview = IndexProcessor().format_preview(
|
||||||
|
"qa_model",
|
||||||
|
{"qa_chunks": [{"question": "Q1", "answer": "A1"}]},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert preview.chunk_structure == "qa_model"
|
||||||
|
assert preview.total_segments == 1
|
||||||
|
assert len(preview.qa_preview) == 1
|
||||||
|
assert preview.qa_preview[0].question == "Q1"
|
||||||
|
assert preview.qa_preview[0].answer == "A1"
|
||||||
Loading…
Reference in New Issue
Block a user