Docling
Docling は IBM Research のドキュメント処理エンジンで、PDF、DOCX、PPTX などのさまざまなドキュメント形式からテキストと構造を抽出します。OCR、表の抽出、レイアウト解析などの高度な機能を提供します。
この統合は、REST API を介して実行中の docling-serve インスタンスと通信し、公式 Docling Java ライブラリを使用して構築されています。
Maven 依存関係
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-document-parser-docling</artifactId>
<version>1.23.0-beta33</version>
</dependency>
このモジュールは docling-serve-api(インターフェース)に依存しており、docling-serve-client(リファレンス HTTP クライアント)をオプションのランタイム依存関係として含んでいます。
アプリケーション内のほかのライブラリが DoclingServeApi 実装を提供しない場合(Quarkus や Spring Boot の一部の統合は提供します)、リファレンスクライアントも明示的に追加してください。
<dependency>
<groupId>ai.docling</groupId>
<artifactId>docling-serve-client</artifactId>
<version>0.7.1</version>
</dependency>
langchain4j-document-parser-docling が導入する docling-serve-api と同じバージョンを使用してください(mvn dependency:tree で確認できます)。異なるバージョンを混在させると、実行時に DoclingServeApi.builder() が失敗する場合があります。
Quarkus や Spring Boot などのフレームワークは、独自の Docling 統合を提供しています。これらの特定の実装を組み込む方法については、Docling Java ドキュメントを参照してください。
使い方
docling-serve インスタンスを起動し(docling-serve ドキュメントを参照)、DoclingServeApi クライアントを構築してパーサーに渡します:
DoclingServeApi api = DoclingServeApi.builder()
.baseUrl("http://localhost:5001")
.build();
DoclingDocumentParser parser = DoclingDocumentParser.builder()
.doclingClient(api)
.build();
Document document = parser.parse(inputStream);
String text = document.text();
ファイル、ディレクトリ、URL、またはクラスパスからドキュメントを読み込み、
file_name、absolute_directory_path、url などの標準的なメタデータを
自動的に設定するには、langchain4j モジュールのローダーを
パーサーと併せて使用します:
Document fromFile = FileSystemDocumentLoader.loadDocument(Path.of("/tmp/report.pdf"), parser);
Document fromUrl = UrlDocumentLoader.load("https://example.com/report.pdf", parser);
Docling の操作の選択
Docling が実行する操作(変換、階層的チャンク分割、ハイブリッドチャンク分割)は、
ビルダーに渡される DocumentRequest
テンプレートによって制御されます。このテンプレートには、ドキュメントのソース以外のすべてが含まれます。
パーサーは、呼び出しのたびに、解析対象のドキュメントのソースをテンプレートの
新しいコピーに注入し、対応する Docling
エンドポイントにルーティングします。テンプレートが指定されていない場合は、プレーンな ConvertDocumentRequest が使用されます。
変換をカスタマイズするには、ConvertDocumentOptions を保持する
ConvertDocumentRequest を渡します:
ConvertDocumentOptions options = ConvertDocumentOptions.builder()
// configure options here
.build();
DoclingDocumentParser parser = DoclingDocumentParser.builder()
.doclingClient(api)
.documentRequest(ConvertDocumentRequest.builder()
.options(options)
.build())
.build();
非推奨の
Builder.options(ConvertDocumentOptions)ショートカットは引き続き利用可能で、 単にdocumentRequest(...)に委譲しますが、Docling の操作も選択できるよう、ConvertDocumentRequestを渡すことを推奨します。
ドキュメントを変換する代わりにチャンク分割するには、
HierarchicalChunkDocumentRequest または HybridChunkDocumentRequest を渡します:
DoclingDocumentParser parser = DoclingDocumentParser.builder()
.doclingClient(api)
.documentRequest(HybridChunkDocumentRequest.builder().build())
.build();
テンプレートに含まれるドキュメントソースは無視されます。パーサーが
解析対象のドキュメントのソースを注入します。テンプレートは in-body 操作を記述している必要があります。
BatchConvertDocumentRequest、または in-body ではない
ターゲット(例:ZipTarget や PresignedUrlTarget)を持つリクエストの場合、build() は
失敗します。