> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 维基百科集成

> 使用 LangChain Python 与 Wikipedia 检索器集成。

> [Wikipedia](https://wikipedia.org/) 是一个多语言免费在线百科全书，由志愿者社区（称为 Wikipedians）通过开放协作和使用基于 Wiki 的编辑系统 MediaWiki 编写和维护。`Wikipedia` 是历史上最大且阅读最多的参考作品。

本笔记本展示了如何从 `wikipedia.org` 检索维基百科页面，并将其检索为下游使用的 [Document](https://reference.langchain.com/python/langchain-core/documents/base/Document) 格式。

### 集成详情

<ItemTable category="external_retrievers" item="WikipediaRetriever" />

## 设置

要启用单个工具的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

该集成位于 `langchain-community` 包中。我们还需要安装 `wikipedia` Python 包本身。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-community wikipedia
```

## 实例化

现在我们可以实例化我们的检索器：

`WikipediaRetriever` 参数包括：

* 可选 `lang`：默认="en"。用于在维基百科的特定语言部分进行搜索
* 可选 `load_max_docs`：默认=100。用于限制下载文档的数量。下载所有 100 个文档需要时间，因此实验时使用较小的数字。目前硬限制为 300。
* 可选 `load_all_available_meta`：默认=False。默认情况下仅下载最重要的字段：`Published`（文档发布/最后更新日期）、`title`、`Summary`。如果为 True，则下载其他字段。

`get_relevant_documents()` 有一个参数 `query`：用于在维基百科中查找文档的自由文本

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.retrievers import WikipediaRetriever

retriever = WikipediaRetriever()
```

## 用法

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docs = retriever.invoke("TOKYO GHOUL")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs[0].page_content[:400])
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Tokyo Ghoul (Japanese: 東京喰種（トーキョーグール）, Hepburn: Tōkyō Gūru) is a Japanese dark fantasy manga series written and illustrated by Sui Ishida. It was serialized in Shueisha's seinen manga magazine Weekly Young Jump from September 2011 to September 2014, with its chapters collected in 14 tankōbon volumes. The story is set in an alternate version of Tokyo where humans coexist with ghouls, beings who loo
```

***

## API 参考

有关所有 `WikipediaRetriever` 功能和配置的详细文档，请前往 [API reference](https://reference.langchain.com/python/langchain-community/retrievers/wikipedia/WikipediaRetriever)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\retrievers\wikipedia.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
