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

# Wikipedia 集成

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

> [Wikipedia](https://wikipedia.org/) 是一个由志愿者社区（称为维基人）通过开放协作和使用名为 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"。用于在 Wikipedia 的特定语言部分进行搜索
* 可选的 `load_max_docs`：默认值=100。用于限制下载的文档数量。下载所有 100 个文档需要时间，因此在实验时请使用较小的数字。目前硬性限制为 300。
* 可选的 `load_all_available_meta`：默认值=False。默认情况下，只下载最重要的字段：`Published`（文档发布/最后更新的日期）、`title`、`Summary`。如果为 True，则也会下载其他字段。

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

```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 参考](https://reference.langchain.com/python/langchain-community/retrievers/wikipedia/WikipediaRetriever)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/retrievers/wikipedia.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
