MetaFieldGroupingRanker

MetaFieldGroupingRanker

문서를 메타데이터 키 기준으로 그룹 지어 재정렬해주는 랭커예요. 쿼리 파이프라인에서 Retriever처럼 문서 목록을 반환하는 컴포넌트 뒤에 두면 돼요.

출처: MetaFieldGroupingRanker

본문

개요

MetaFieldGroupingRanker 컴포넌트는 기본 메타데이터 키 group_by로 문서를 그룹 짓고, 선택적 2차 키 subgroup_by로 하위 그룹을 만들어요. 각 그룹이나 하위 그룹 안에서 컴포넌트는 메타데이터 키 sort_docs_by로 문서를 정렬할 수도 있어요.

출력은 group_by와 subgroup_by 값 순서로 정렬된 평평한 문서 목록이에요. 그룹이 없는 문서는 목록 끝에 배치돼요.

이 컴포넌트는 이후 LLM 처리의 효율성과 성능을 높이는 데 도움을 줘요.

사용법

단독 사용:

from haystack.components.rankers import MetaFieldGroupingRanker
from haystack import Document

docs = [
    Document(
        content="JavaScript is popular",
        meta={"group": "42", "split_id": 7, "subgroup": "subB"},
    ),
    Document(
        content="Python is popular",
        meta={"group": "42", "split_id": 4, "subgroup": "subB"},
    ),
    Document(
        content="A chromosome is DNA",
        meta={"group": "314", "split_id": 2, "subgroup": "subC"},
    ),
    Document(
        content="An octopus has three hearts",
        meta={"group": "11", "split_id": 2, "subgroup": "subD"},
    ),
    Document(
        content="Java is popular",
        meta={"group": "42", "split_id": 3, "subgroup": "subB"},
    ),
]

ranker = MetaFieldGroupingRanker(
    group_by="group",
    subgroup_by="subgroup",
    sort_docs_by="split_id",
)
result = ranker.run(documents=docs)
print(result["documents"])

파이프라인 안에서:

다음 파이프라인은 MetaFieldGroupingRanker로 문서를 특정 메타 필드 기준으로 정리하면서 페이지 번호로 정렬하고, 정리된 문서를 채팅 메시지로 포맷해 OpenAIChatGenerator에 전달해 내용의 구조화된 설명을 만듭니다.

from haystack import Pipeline
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.rankers import MetaFieldGroupingRanker
from haystack.dataclasses import Document, ChatMessage

docs = [
    Document(
        content="Chapter 1: Introduction to Python",
        meta={"chapter": "1", "section": "intro", "page": 1},
    ),
    Document(
        content="Chapter 2: Basic Data Types",
        meta={"chapter": "2", "section": "basics", "page": 15},
    ),
    Document(
        content="Chapter 1: Python Installation",
        meta={"chapter": "1", "section": "setup", "page": 5},
    ),
]

ranker = MetaFieldGroupingRanker(
    group_by="chapter",
    subgroup_by="section",
    sort_docs_by="page",
)

chat_generator = OpenAIChatGenerator(
    generation_kwargs={"max_completion_tokens": 500},
)

# First run the ranker
ranked_result = ranker.run(documents=docs)
ranked_docs = ranked_result["documents"]

# Create chat messages with the ranked documents
messages = [
    ChatMessage.from_system("You are a helpful programming tutor."),
    ChatMessage.from_user(
        f"Here are the course documents in order:\n"
        + "\n".join([f"- {doc.content}" for doc in ranked_docs])
        + "\n\nBased on these documents, explain the structure of this Python course.",
    ),
]

# Create and run pipeline for just the chat generator
pipeline = Pipeline()
pipeline.add_component("chat_generator", chat_generator)

result = pipeline.run(data={"chat_generator": {"messages": messages}})

print(result["chat_generator"]["replies"][0])

더 알아보기 (Learn more)