基类:EventBaseReader
SmartPDFLoader 利用嵌套布局信息(如章节、段落、列表和表格)智能地分割PDF文档,以实现LLM上下文窗口的最佳利用。
参数:
| 名称 |
类型 |
描述 |
默认 |
llmsherpa_api_url
|
str
|
托管 llmsherpa PDF 解析器的服务地址
|
None
|
workflows/handler.py 中的源代码llama_index/readers/smart_pdf_loader/base.py
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 | class SmartPDFLoader(BaseReader):
"""
SmartPDFLoader uses nested layout information such as sections, paragraphs, lists and tables to smartly chunk PDFs for optimal usage of LLM context window.
Args:
llmsherpa_api_url (str): Address of the service hosting llmsherpa PDF parser
"""
def __init__(
self, *args: Any, llmsherpa_api_url: str = None, **kwargs: Any
) -> None:
super().__init__(*args, **kwargs)
from llmsherpa.readers import LayoutPDFReader
self.pdf_reader = LayoutPDFReader(llmsherpa_api_url)
def load_data(
self, pdf_path_or_url: str, extra_info: Optional[Dict] = None
) -> List[Document]:
"""
Load data and extract table from PDF file.
Args:
pdf_path_or_url (str): A url or file path pointing to the PDF
Returns:
List[Document]: List of documents.
"""
results = []
doc = self.pdf_reader.read_pdf(str(pdf_path_or_url))
for chunk in doc.chunks():
document = Document(
text=chunk.to_context_text(),
extra_info={**extra_info, "chunk_type": chunk.tag}
if extra_info
else {"chunk_type": chunk.tag},
)
results.append(document)
return results
|
load_data
load_data(pdf_path_or_url: str, extra_info: Optional[Dict] = None) -> List[文档]
从PDF文件加载数据并提取表格。
参数:
| 名称 |
类型 |
描述 |
默认 |
pdf_path_or_url
|
str
|
|
required
|
返回:
workflows/handler.py 中的源代码llama_index/readers/smart_pdf_loader/base.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 | def load_data(
self, pdf_path_or_url: str, extra_info: Optional[Dict] = None
) -> List[Document]:
"""
Load data and extract table from PDF file.
Args:
pdf_path_or_url (str): A url or file path pointing to the PDF
Returns:
List[Document]: List of documents.
"""
results = []
doc = self.pdf_reader.read_pdf(str(pdf_path_or_url))
for chunk in doc.chunks():
document = Document(
text=chunk.to_context_text(),
extra_info={**extra_info, "chunk_type": chunk.tag}
if extra_info
else {"chunk_type": chunk.tag},
)
results.append(document)
return results
|