Skip to Content
RAG with Python クックブック (Japanese Edition)
book

RAG with Python クックブック (Japanese Edition)

by Dominik Polzer
May 2026
Intermediate
378 pages
5h 34m
Japanese
O'Reilly Media, Inc.
Content preview from RAG with Python クックブック (Japanese Edition)

第4章 データ 準備

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

RAGシステムに長文を入力する際、前処理によって文書は小さなチャンクに分割される。各チャンクは、その意味的意味を捉えた埋め込みベクトルに変換される。検索時には、システムはこれらのベクトル間の距離を計算して類似性を測定し、関連性の高いテキストを見つける。

この手法は、各チャンクが正確に1つの情報を含み、独立して理解できる場合に最も効果を発揮する。重要な課題は、周囲のコンテキストに依存せずに単独で成立するチャンクを準備することだ。堅牢なパイプラインは、生テキストをクリーニングし、適切な点で分割して意味のあるチャンクを生成する。

図4-1は、 における一般的な前処理テクニックを示している:

テキストの準備

略語を置き換え、テキストをクリーニングする。

メタデータの収集

ページ番号、ソース、著者を保存する。

テキストの分割

文字単位、再帰的、意味的、またはエージェントによるチャンキングを適用する。

目標は、周囲のコンテキストを必要としない、明確で曖昧さのないチャンクを生成することだ。

Diagram illustrating the simplified RAG indexing pipeline, showing stages of processing, chunking, and embedding model.
図4-1. データ処理テクニックを含む、簡略化されたRAGインデックス作成 パイプライン

各チャンクに有用なメタデータを保存することで、検索時のフィルタリングが可能になり、プロセスの高速化と精度向上につながる。

本書のGitHubリポジトリで、この章のすべてのコード例を確認できる。

4.1 メタデータフィルタリングを可能にするメタデータの追加

課題

ベクトルストア内のテキストチャンクと共にメタデータを保存し、検索時にメタデータによるフィルタリングを適用できるようにしたい。

解決策

ドキュメントから既存のメタデータを抽出し、追加のフィールドを計算する。特殊なユースケースでは、オプションとして、ドキュメントの内容から導出されたLLM生成メタデータを使用して、ドキュメントと共に保存されるメタデータを充実させる。以下の手順に従う:

  1. 文書に既に保存されているメタデータ(著者、タイトル、作成日)を抽出する。

  2. 計算されたメタデータフィールド(ファイルの場所、サイズ、ページ数、テキストの長さ)を追加する。

  3. (オプション)LLMを用いてドキュメントのテキストを分析し、コンテンツに基づくメタデータを生成する。

  4. メタデータをテキストチャンクと共に保存する。

図4-2はこのワークフローを示している。

Diagram illustrating a four-step metadata extraction workflow involving text analysis, cloud processing, language model application, and metadata generation with examples of extracted metadata fields.
図4-2. メタデータフィールドの抽出 と生成

手順を実行するには、要件のライブラリをインストールする:

pip install PyPDF2 openai pydantic

次に、PDFをインポートし、そのメタデータフィールドを辞書に格納する。これらのフィールドの網羅性は、PDFを作成したソフトウェアに大きく依存する。多くのPDFには、著者、タイトル、主題、作成者、作成日、および変更日が含まれている。以下にコードを示す: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python Distilled ―プログラミング言語Pythonのエッセンス

Python Distilled ―プログラミング言語Pythonのエッセンス

David M. Beazley, 鈴木 駿
Python によるテキスト分析の応用

Python によるテキスト分析の応用

Benjamin Bengfort, Rebecca Bilbro, Tony Ojeda
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 0642572383589