Skip to main content
BibTeX 是一种文件格式和参考文献管理系统,通常与 LaTeX 排版系统结合使用。它是一种用于组织和存储学术及研究文档中书目信息的方式。
BibTeX 文件的扩展名为 .bib,由纯文本条目组成,代表对各种出版物的引用,例如书籍、文章、会议论文、学位论文等。每个 BibTeX 条目都遵循特定的结构,并包含不同书目详情的字段,如作者姓名、出版标题、期刊或书名、出版年份、页码等。 BibTeX 文件还可以存储文档路径,例如可以检索的 .pdf 文件。

安装

首先,您需要安装 bibtexparserPyMuPDF
pip install -qU  bibtexparser pymupdf

示例

BibtexLoader 的参数如下:
  • file_path.bib bibtex 文件的路径
  • 可选 max_docs:默认为 None,即无限制。用于限制检索的文档数量。
  • 可选 max_content_chars:默认为 4000。用于限制单个文档中的字符数。
  • 可选 load_extra_meta:默认为 False。默认情况下,只加载 bibtex 条目中最重要的字段:Published(出版年份)、TitleAuthorsSummaryJournalKeywordsURL。若设为 True,还会尝试加载 entry_idnotedoilinks 字段。
  • 可选 file_pattern:默认为 r'[^:]+\.pdf'。用于在 file 条目中查找文件的正则表达式模式。默认模式支持 Zotero 风格的 bibtex 格式和纯文件路径。
from langchain_community.document_loaders import BibtexLoader
# Create a dummy bibtex file and download a pdf.
import urllib.request

urllib.request.urlretrieve(
    "https://www.fourmilab.ch/etexts/einstein/specrel/specrel.pdf", "einstein1905.pdf"
)

bibtex_text = """
    @article{einstein1915,
        title={Die Feldgleichungen der Gravitation},
        abstract={Die Grundgleichungen der Gravitation, die ich hier entwickeln werde, wurden von mir in einer Abhandlung: ,,Die formale Grundlage der allgemeinen Relativit{\"a}tstheorie`` in den Sitzungsberichten der Preu{\ss}ischen Akademie der Wissenschaften 1915 ver{\"o}ffentlicht.},
        author={Einstein, Albert},
        journal={Sitzungsberichte der K{\"o}niglich Preu{\ss}ischen Akademie der Wissenschaften},
        volume={1915},
        number={1},
        pages={844--847},
        year={1915},
        doi={10.1002/andp.19163540702},
        link={https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.19163540702},
        file={einstein1905.pdf}
    }
    """
# save bibtex_text to biblio.bib file
with open("./biblio.bib", "w") as file:
    file.write(bibtex_text)
docs = BibtexLoader("./biblio.bib").load()
docs[0].metadata
{'id': 'einstein1915',
 'published_year': '1915',
 'title': 'Die Feldgleichungen der Gravitation',
 'publication': 'Sitzungsberichte der K{"o}niglich Preu{\\ss}ischen Akademie der Wissenschaften',
 'authors': 'Einstein, Albert',
 'abstract': 'Die Grundgleichungen der Gravitation, die ich hier entwickeln werde, wurden von mir in einer Abhandlung: ,,Die formale Grundlage der allgemeinen Relativit{"a}tstheorie`` in den Sitzungsberichten der Preu{\\ss}ischen Akademie der Wissenschaften 1915 ver{"o}ffentlicht.',
 'url': 'https://doi.org/10.1002/andp.19163540702'}
print(docs[0].page_content[:400])  # all pages of the pdf content
ON THE ELECTRODYNAMICS OF MOVING
BODIES
By A. EINSTEIN
June 30, 1905
It is known that Maxwell's electrodynamics—as usually understood at the
present time—when applied to moving bodies, leads to asymmetries which do
not appear to be inherent in the phenomena. Take, for example, the recipro-
cal electrodynamic action of a magnet and a conductor. The observable phe-
nomenon here depends only on the r