Indexify와 Mistral로 PDF 요약하기

Indexify와 Mistral로 PDF 요약하기 (PDF Summarization with Indexify and Mistral)

Indexify와 Mistral의 LLM을 사용해 PDF 요약 파이프라인을 만드는 방법을 배우는 문서예요. 수천 개의 PDF 문서를 받아들여 Mistral로 요약하는 파이프라인을 구축할 수 있습니다.

출처: 문서

본문

이 쿡북에서는 Indexify와 Mistral의 LLM을 사용해 PDF 요약 파이프라인을 만드는 방법을 알아볼게요. 문서가 끝나면 수천 개의 PDF 문서를 받아들이고 Mistral로 요약하는 파이프라인을 갖게 됩니다.

소개 (Introduction)

요약 파이프라인은 두 단계로 구성돼요:

  1. PDF에서 텍스트 추출: 미리 구축된 추출기 tensorlake/pdfextractor를 사용해요.
  2. Mistral로 요약: Mistral을 요약에 사용해요.

사전 요구사항 (Prerequisites)

시작 전에 다음을 확인하세요:

  • Python 3.9 이상으로 가상 환경 생성
python3.9 -m venv ve
source ve/bin/activate
  • pip (Python 패키지 관리자)
  • Mistral API 키
  • Python과 커맨드라인 인터페이스에 대한 기본 지식

설정 (Setup)

Indexify 설치

먼저 공식 설치 스크립트로 터미널에서 Indexify를 설치해요.

curl https://getindexify.ai | sh

Indexify 서버를 시작해요.

./indexify server -d

이것은 애플리케이션에 ingestion과 retrieval API를 노출하는 장기 실행 서버를 시작해요.

필요한 추출기 설치

새 터미널에서 필요한 추출기를 설치해요.

pip install indexify-extractor-sdk
indexify-extractor download tensorlake/pdfextractor
indexify-extractor download tensorlake/mistral

추출기가 다운로드되면 시작할 수 있어요.

indexify-extractor join-server

추출 그래프 생성 (Creating the Extraction Graph)

추출 그래프는 요약 파이프라인을 통한 데이터 흐름을 정의해요. 먼저 PDF에서 텍스트를 추출한 다음, 그 텍스트를 Mistral로 보내 요약하는 그래프를 만들게요.

from indexify import IndexifyClient, ExtractionGraph

client = IndexifyClient()

extraction_graph_spec = """
name: 'pdf_summarizer'
extraction_policies:
 - extractor: 'tensorlake/pdfextractor'
 name: 'pdf_to_text'
 - extractor: 'tensorlake/mistral'
 name: 'text_to_summary'
 input_params:
 model_name: 'mistral-large-latest'
 key: 'YOUR_MISTRAL_API_KEY'
 system_prompt: 'Summarize the following text in a concise manner, highlighting the key points:'
 content_source: 'pdf_to_text'
"""

extraction_graph = ExtractionGraph.from_yaml(extraction_graph_spec)
client.create_extraction_graph(extraction_graph)

'YOUR_MISTRAL_API_KEY'를 실제 Mistral API 키로 바꾸세요.

요약 파이프라인 구현 (Implementing the Summarization Pipeline)

이제 추출 그래프를 설정했으니, 파일을 업로드하고 파이프라인이 요약을 생성하게 할 수 있어요.

import os
import requests
from indexify import IndexifyClient

def download_pdf(url, save_path):
    response = requests.get(url)
    with open(save_path, 'wb') as f:
        f.write(response.content)
    print(f"PDF downloaded and saved to {save_path}")

def summarize_pdf(pdf_path):
    client = IndexifyClient()
    
    # Upload the PDF file
    content_id = client.upload_file("pdf_summarizer", pdf_path)
    
    # Wait for the extraction to complete
    client.wait_for_extraction(content_id)
    
    # Retrieve the summarized content
    summary = client.get_extracted_content(
        content_id=content_id,
        graph_name="pdf_summarizer",
        policy_name="text_to_summary"
    )
    
    return summary[0]['content'].decode('utf-8')
pdf_url = "https://arxiv.org/pdf/2310.06825.pdf"
pdf_path = "reference_document.pdf"

# Download the PDF
download_pdf(pdf_url, pdf_path)

# Summarize the PDF
summary = summarize_pdf(pdf_path)
print("Summary of the PDF:")
print(summary)

커스터마이즈와 고급 사용법 (Customization and Advanced Usage)

추출 그래프의 system_prompt을 수정해 요약 과정을 커스터마이즈할 수 있어요. 예시:

불릿 포인트 요약을 생성하려면:

system_prompt: 'Summarize the following text as a list of bullet points:'

문서의 특정 측면에 집중하려면:

system_prompt: 'Summarize the main arguments and supporting evidence from the following text:'

또한 model_name 파라미터를 바꿔 다양한 Mistral 모델로 실험해, 특정 사용 사례에 맞는 속도와 정확도의 최상의 균형을 찾을 수 있어요.

결론 (Conclusion)

예시가 단순해 보일 수 있지만 Indexify를 사용하면 몇 가지 독특한 이점이 있어요.

  • 확장 가능하고 고가용성: Indexify 서버는 클라우드에 배포해 업로드되는 수천 개의 PDF를 처리할 수 있고, 파이프라인에서 어떤 단계가 실패해도 다른 머신에서 자동으로 재시도합니다.
  • 유연성: 사용 중인 문서에서 여기서 사용한 [PDF 추출 모델]이 동작하지 않으면 다른 [PDF 추출 모델]을 사용할 수 있어요.

다음 단계 (Next Steps)

  • Indexify에 대해 더 알아보기: [https://docs.getindexify.ai]
  • Indexify와 Mistral을 사용해 [PDF 문서에서 엔티티 추출]하는 방법 배우기

더 알아보기 (Learn more)