あらゆる文書を LLM 向けの Markdown に変換
言語モデルは Markdown をうまく読み取れます。見出しで話題の始まりがわかり、リストはリストのまま、段落にはひとまとまりの内容が収まります。しかし、ユーザーがアップロードする文書はそうではありません。PDF では各ページにヘッダーとページ番号が繰り返され、行末で文が途切れます。スキャンにはテキスト自体がありません。そのままモデルに渡すと、RAG インデックスの各チャンクにノイズが入り、トークンの費用がかかるうえ、回答の質も下がります。
Transloadit で、こうした文書を整った Markdown に変換できるようになりました。PDF、スキャン、ページを撮影した写真、Word、PowerPoint ファイルを、1 コマンドで文書単位またはページ単位に変換できます。
1 コマンドで変換
まず、Transloadit CLI の認証を行います。次に実行します。
npx -y @transloadit/node assemblies create \
--template builtin/extract-text \
--field text_format=md \
-i report.pdf -o out/
builtin/extract-text は、既製の Builtin Templateです。🤖 /document/extractを使っており、Markdown は out/report-text.md に保存されます。
NASA の Artemis II プレスキットで試しました。ミッションの概要、日程、搭乗員の情報をまとめた 27 ページの文書です。約 5 秒で結果が返ってきました。以下は 3 ページ目の下半分です。
このページに対してコマンドが返した結果がこちらです。
### Mission Priorities
The Artemis II test flight will confirm the systems necessary to support astronauts in deep space exploration and prepare to establish a sustained presence on the Moon. There are five main priorities for Artemis II:
1. Crew: Demonstrate the ability of systems and teams to sustain the flight crew in the flight environment, and through their return to Earth.
2. Systems: Demonstrate systems and operations essential to a crewed lunar campaign. This ranges from ground systems to hardware in space, and operations spanning from development to launch, flight, and recovery.
3. Hardware and Data: Retrieve flight hardware and data, assessing performance for future missions.
4. Emergency Operations: Demonstrate emergency system capabilities and validate associated operations to the extent practical, such as abort operations and rescue procedures, as needed.
5. Data and Subsystems: Complete additional objectives to verify subsystems and validate data.
変換では、次の処理が行われています。
- 見出しを見出しとして出力。 本文より明らかに大きいテキストは Markdown の見出しになり、サイズに応じて見出しレベルが決まります。
- 段落をひと続きに復元。 PDF には印刷上の各行が別々に保存されています。Markdown では段落ごとに 1 行になるため、文が途中で分断されません。
- リストをリストのまま出力。 5 つの優先事項は番号付きリストとして返されます。
- ページ共通の装飾要素を除去。 フッターの「Artemis II Press Kit」とページ番号は PDF の各ページで繰り返されますが、Markdown には含まれません。
段落内の太字や斜体は引き継がれません。リスト内の太字のラベルも通常のテキストとして返されます。
Markdown は 4 種類ある形式の 1 つです。text_format には
txt、html、json も指定できます。
RAG 向けにページごとのファイルを出力
検索では通常、より小さな単位と、引用に使えるページ番号が必要です。2 つのフィールドを追加します。
npx -y @transloadit/node assemblies create \
--template builtin/extract-text \
--field text_format=md \
--field text_granularity=page \
--field text_method=native \
-i report.pdf -o out/
これで各ページが個別のファイルになります。
$ ls -v out/report/native_text/ | head -n 3
report-page-1.md
report-page-2.md
report-page-3.md
$ ls out/report/native_text/ | wc -l
27
見出しレベルは文書全体で判定されるため、セクション見出しは 3 ページ目でも 20 ページ目でも同じレベルになります。text_method=native は、PDF に保存されているテキストを読み取るよう Robot に指定します。ページ単位の出力には、この指定が必要です。スキャンした PDF をページ単位で処理するパイプラインは後述します。
スキャンと写真
スキャンはテキストを写した画像です。同じコマンドに JPEG、PNG、またはテキストを含まない PDF を渡すと、Transloadit が OCR を実行します。
npx -y @transloadit/node assemblies create \
--template builtin/extract-text \
--field text_format=md \
-i scan.jpg -o out/
NASA の 1969 年の Apollo 11 プレスキットにある、タイプライターで作成されたページで試しました。
APOLLO 11
The United States will launch a three-man spacecraft toward the Moon on July 16 with the goal of landing two astronaut-explorers on the lunar surface four days later.
If the mission--called Apollo 11 is successful, man will accomplish his long-time dream of walking on another celestial body.
OCR の出力は段落単位で返され、同じ段落に属する行は結合されます。完璧ではありません。よく見ると、「Apollo 11」の後の 2 つ目のダッシュが抜けています。
スキャンした PDF の中には、以前の OCR 処理によるテキストが残っているものもありますが、その品質は低いことがよくあります。--field text_method=ocr を追加すると、そのテキストを無視してページを読み取り直します。
Word と PowerPoint
Office ファイルも同じように処理できます。.docx または
.pptx を渡すと、Template がまず
🤖 /document/convertで PDF に変換してから、Markdown を抽出します。
npx -y @transloadit/node assemblies create \
--template builtin/extract-text \
--field text_format=md \
-i countdown.docx -o out/
NASA の Artemis I 打ち上げカウントダウンを解説する Word ファイルは、5 秒未満で整った段落として返ってきました。42 枚のスライドからなる 320 MB の PowerPoint 資料は 27 秒かかりました。スライド資料からは、タイトルを見出しとした各スライドのテキストが得られます。写真や図の説明は生成されません。
より大きなパイプライン
builtin/extract-text は一般的な用途をカバーします。より細かく制御したい場合は、Step を自分で記述します。次のパイプラインは、スキャンした PDF の各ページを個別の Markdown ファイルにします。🤖 /document/splitで PDF をページごとに分割し、/document/extract で各ページに OCR を実行します。scan-pages.json として保存してください。
{
"steps": {
":original": {
"robot": "/upload/handle"
},
"pages": {
"use": ":original",
"robot": "/document/split",
"split": true
},
"markdown": {
"use": "pages",
"robot": "/document/extract",
"extract": ["text"],
"text_method": "ocr",
"ocr_provider": "aws",
"text_format": "md",
"result": true
}
}
}
次に実行します。
npx -y @transloadit/node assemblies create --steps scan-pages.json -i scans.pdf -o out/
スキャンした 4 ページの処理には 10 秒かかりました。
$ find out -type f | sort
out/scans/markdown/scans_page_1-text.md
out/scans/markdown/scans_page_2-text.md
out/scans/markdown/scans_page_3-text.md
out/scans/markdown/scans_page_4-text.md
ここからは通常の Assembly の処理です。pages パラメーターを使って
/document/split でページを選択したり、🤖 /document/mergeで複数の PDF を先にまとめたりできます。また、ストレージ用 Robotを追加すれば、ご自身のバケットに、元のアップロードファイルと並べて Markdown を保存できます。
はじめに
手元の文書で最初のコマンドを実行するか、Template Editorで Step を試してください。🤖 /document/extract のドキュメントに全パラメーターを記載しています。
サンプル文書は、NASA の Artemis II プレスキットと Apollo 11 プレスキットです。ここでの使用は、NASA が Transloadit を推奨していることを意味するものではありません。
