Skip to main content
Open In ColabOpen on GitHub

ArxivRetriever

arXiv 是一个开放获取的存档,包含物理学、数学、计算机科学、定量生物学、定量金融、统计学、电气工程和系统科学以及经济学领域的200万篇学术文章。

本笔记本展示了如何从Arxiv.org检索科学文章并将其转换为下游使用的Document格式。

有关所有ArxivRetriever功能和配置的详细文档,请访问API参考

集成详情

检索器来源
ArxivRetriever学术文章在 arxiv.orglangchain_community

设置

如果你想从单个查询中获取自动追踪,你也可以通过取消注释以下内容来设置你的 LangSmith API 密钥:

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

安装

这个检索器位于langchain-community包中。我们还需要arxiv依赖项:

%pip install -qU langchain-community arxiv

实例化

ArxivRetriever 参数包括:

  • 可选的 load_max_docs: 默认=100。用于限制下载的文档数量。下载所有100个文档需要时间,因此实验时请使用较小的数字。目前有一个300的硬性限制。
  • 可选的 load_all_available_meta: 默认=False。默认情况下,只下载最重要的字段:Published(文档发布/最后更新的日期),Title(标题),Authors(作者),Summary(摘要)。如果为True,则还会下载其他字段。
  • get_full_documents: boolean, 默认值为 False。决定是否获取文档的全文。

详情请参见API参考

from langchain_community.retrievers import ArxivRetriever

retriever = ArxivRetriever(
load_max_docs=2,
get_ful_documents=True,
)
API Reference:ArxivRetriever

用法

ArxivRetriever 支持通过文章标识符进行检索:

docs = retriever.invoke("1605.08386")
docs[0].metadata  # meta-information of the Document
{'Entry ID': 'http://arxiv.org/abs/1605.08386v1',
'Published': datetime.date(2016, 5, 26),
'Title': 'Heat-bath random walks with Markov bases',
'Authors': 'Caprice Stanley, Tobias Windisch'}
docs[0].page_content[:400]  # a content of the Document
'Graphs on lattice points are studied whose edges come from a finite set of\nallowed moves of arbitrary length. We show that the diameter of these graphs on\nfibers of a fixed integer matrix can be bounded from above by a constant. We\nthen study the mixing behaviour of heat-bath random walks on these graphs. We\nalso state explicit conditions on the set of moves so that the heat-bath random\nwalk, a ge'

ArxivRetriever 还支持基于自然语言文本的检索:

docs = retriever.invoke("What is the ImageBind model?")
docs[0].metadata
{'Entry ID': 'http://arxiv.org/abs/2305.05665v2',
'Published': datetime.date(2023, 5, 31),
'Title': 'ImageBind: One Embedding Space To Bind Them All',
'Authors': 'Rohit Girdhar, Alaaeldin El-Nouby, Zhuang Liu, Mannat Singh, Kalyan Vasudev Alwala, Armand Joulin, Ishan Misra'}

在链中使用

与其他检索器一样,ArxivRetriever可以通过chains集成到LLM应用中。

我们将需要一个LLM或聊天模型:

pip install -qU langchain-openai
import getpass
import os

if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

prompt = ChatPromptTemplate.from_template(
"""Answer the question based only on the context provided.

Context: {context}

Question: {question}"""
)


def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)


chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
chain.invoke("What is the ImageBind model?")
'The ImageBind model is an approach to learn a joint embedding across six different modalities - images, text, audio, depth, thermal, and IMU data. It shows that only image-paired data is sufficient to bind the modalities together and can leverage large scale vision-language models for zero-shot capabilities and emergent applications such as cross-modal retrieval, composing modalities with arithmetic, cross-modal detection and generation.'

API参考

有关所有ArxivRetriever功能和配置的详细文档,请访问API参考


这个页面有帮助吗?