LLMMetadataExtractor

LLMMetadataExtractor

대규모 언어 모델을 사용해 문서에서 메타데이터를 추출하는 컴포넌트예요. LLM에 프롬프트를 제공하면 메타데이터를 생성해요. 인덱싱 파이프라인에서 PreProcessor 뒤에 두면 돼요.

출처: LLMMetadataExtractor

본문

개요

LLMMetadataExtractor의 추출은 LLM과 프롬프트에 의존해 메타데이터를 뽑아내요. 초기화 때 LLM(Haystack Generator)과 메타데이터 추출 과정을 설명하는 프롬프트를 기대해요.

프롬프트에는 document라는 이름의 변수가 정확히 하나 있어야 해요. 이 변수는 문서 목록의 단일 문서를 가리켜요. 문서 내용에 접근하려면 프롬프트에서 {{ document.content }}를 쓰면 돼요. 프롬프트에 변수가 없거나 변수가 둘 이상이거나 다른 이름의 변수가 있으면, 컴포넌트는 초기화 때 ValueError를 발생시켜요.

실행 시점에는 문서 목록을 기대하며, 목록의 각 문서마다 LLM을 실행해 문서에서 메타데이터를 추출해요. 추출된 메타데이터는 문서의 metadata 필드에 추가돼요.

LLM이 어떤 문서에서 메타데이터 추출에 실패하면 그 문서는 failed_documents 목록에 추가돼요. 실패한 문서의 메타데이터에는 metadata_extraction_error와 metadata_extraction_response 키가 담겨요.

이 문서들은 프롬프트에 metadata_extraction_response와 metadata_extraction_error를 사용해 다른 추출기로 다시 실행해 메타데이터를 뽑을 수 있어요.

chat_generator는 JSON 객체를 반환하도록 설정된 어떤 Haystack Chat Generator든 받아들여요. 예:

사용법

LLMMetadataExtractor로 명명된 엔티티를 추출해 문서의 메타데이터에 추가하는 예시예요.

먼저 필수 import:

from haystack import Document
from haystack.components.extractors.llm_metadata_extractor import LLMMetadataExtractor
from haystack.components.generators.chat import OpenAIChatGenerator

그리고 문서를 정의해요.

docs = [
    Document(
        content="deepset was founded in 2018 in Berlin, and is known for its Haystack framework",
    ),
    Document(
        content="Hugging Face is a company founded in New York, USA and is known for its Transformers library",
    ),
]

이제 문서에서 명명된 엔티티를 추출하는 프롬프트를 만들어요.

NER_PROMPT = """
 -Goal-
 Given text and a list of entity types, identify all entities of those types from the text.

 -Steps-
 1. Identify all entities. For each identified entity, extract the following information:
 - entity_name: Name of the entity, capitalized
 - entity_type: One of the following types: [organization, product, service, industry]
 Format each entity as a JSON like: {"entity": <entity_name>, "entity_type": <entity_type>}

 2. Return output in a single list with all the entities identified in steps 1.

 -Examples-
 #####################
 Example 1:
 entity_types: [organization, person, partnership, financial metric, product, service, industry, investment strategy, market trend]
 text: Another area of strength is our co-brand issuance. Visa is the primary network partner for eight of the top
 10 co-brand partnerships in the US today and we are pleased that Visa has finalized a multi-year extension of
 our successful credit co-branded partnership with Alaska Airlines, a portfolio that benefits from a loyal customer
 base and high cross-border usage.
 We have also had significant co-brand momentum in CEMEA. First, we launched a new co-brand card in partnership
 with Qatar Airways, British Airways and the National Bank of Kuwait. Second, we expanded our strong global
 Marriott relationship to launch Qatar's first hospitality co-branded card with Qatar Islamic Bank. Across the
 United Arab Emirates, we now have exclusive agreements with all the leading airlines marked by a recent
 agreement with Emirates Skywards.
 And we also signed an inaugural Airline co-brand agreement in Morocco with Royal Air Maroc. Now newer digital
 issuers are equally
 ------------------------
 output:
 {"entities": [{"entity": "Visa", "entity_type": "company"}, {"entity": "Alaska Airlines", "entity_type": "company"}, {"entity": "Qatar Airways", "entity_type": "company"}, {"entity": "British Airways", "entity_type": "company"}, {"entity": "National Bank of Kuwait", "entity_type": "company"}, {"entity": "Marriott", "entity_type": "company"}, {"entity": "Qatar Islamic Bank", "entity_type": "company"}, {"entity": "Emirates Skywards", "entity_type": "company"}, {"entity": "Royal Air Maroc", "entity_type": "company"}]}
 ############################
 -Real Data-
 #####################
 entity_types: [company, organization, person, country, product, service]
 text: {{ document.content }}
 #####################
 output:
 """

이제 LLMMetadataExtractor로 문서에서 명명된 엔티티를 추출하는 간단한 인덱싱 파이프라인을 정의해요.

chat_generator = OpenAIChatGenerator(
    generation_kwargs={
        "max_completion_tokens": 500,
        "seed": 0,
        "response_format": {"type": "json_object"},
    },
    max_retries=1,
    timeout=60.0,
)

extractor = LLMMetadataExtractor(
    prompt=NER_PROMPT,
    chat_generator=chat_generator,
    expected_keys=["entities"],
    raise_on_failure=False,
)

extractor.run(documents=docs)
# >> {'documents': [
# >> Document(id=.., content: 'deepset was founded in 2018 in Berlin, and is known for its Haystack framework',
# >> meta: {'entities': [{'entity': 'deepset', 'entity_type': 'company'},
# >> {'entity': 'Haystack', 'entity_type': 'product'}]}),
# >> Document(id=.., content: 'Hugging Face is a company founded in New York, USA and is known for its Transformers library',
# >> meta: {'entities': [
# >> {'entity': 'Hugging Face', 'entity_type': 'company'}, {'entity': 'USA', 'entity_type': 'country'},
# >> {'entity': 'Transformers Library', 'entity_type': 'product'}
# >> ]})
# >> ],
# >> 'failed_documents': []
# >> }

더 알아보기 (Learn more)