> ## 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.

# Nia 工具包集成

> 使用 LangChain Python 与 Nia 搜索和索引 API 进行集成。

[Nia](https://trynia.ai) 是一个搜索和索引 API，持续提供来自文档、研究论文、数据集、代码库等的上下文——这样代理永远不会依赖过时的数据。可扩展、便宜 5 倍且可靠。

## 概述

### 集成详情

| 类                                                             | 包                                                          | 可序列化 | [JS 支持](https://js.langchain.com/docs/integrations/tools/) |                                           版本                                           |
| :------------------------------------------------------------ | :--------------------------------------------------------- | :--: | :--------------------------------------------------------: | :------------------------------------------------------------------------------------: |
| [`NiaToolkit`](https://github.com/nozomio-labs/nia-langchain) | [`langchain-nia`](https://pypi.org/project/langchain-nia/) |   ❌  |                              ❌                             | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain-nia?style=flat-square\&label=%20) |

### 工具功能

| [返回工件](/oss/python/langchain/tools) | 原生异步 | 工具包 | 工具数量 |   定价   |
| :---------------------------------: | :--: | :-: | :--: | :----: |
|                  ❌                  |   ✅  |  ✅  |  20  | 提供免费层级 |

## 设置

集成位于 `langchain-nia` 包中。

<CodeGroup>
  ```python pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install -U langchain-nia
  ```

  ```python uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langchain-nia
  ```
</CodeGroup>

### 凭据

在 [trynia.ai](https://trynia.ai) 注册以获取 API 密钥。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if not os.environ.get("NIA_API_KEY"):
    os.environ["NIA_API_KEY"] = getpass.getpass("Nia API key:\n")
```

设置 LangSmith 也很有帮助（但不是必需的），以获得一流的观测/<Tooltip tip="记录模型执行的每一步以调试和改进它">追踪</Tooltip>。要启用自动追踪，请设置您的 [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"
```

## 实例化

### 使用工具包

`NiaToolkit` 提供所有 20 个 Nia 工具和共享的 API 包装器。使用 `include_*` 标志控制哪些工具组可用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nia import NiaToolkit

toolkit = NiaToolkit(
    include_search=True,        # NiaSearch, NiaWebSearch, NiaDeepResearch, NiaUniversalSearch, NiaAdvisor
    include_sources=True,       # NiaIndex, NiaSourceList, NiaSourceSubscribe, NiaSourceSync, NiaRead, NiaGrep, NiaExplore
    include_github=True,        # NiaGitHubSearch, NiaGitHubRead, NiaGitHubGlob, NiaGitHubTree
    include_contexts=True,      # NiaContextSave, NiaContextSearch
    include_dependencies=True,  # NiaDependencySubscribe, NiaDependencyAnalyze
)
tools = toolkit.get_tools()
```

### 使用独立工具

您也可以直接使用工具：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nia import NiaSearch

tool = NiaSearch()
```

## 调用

### 跨索引源搜索

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nia import NiaSearch

tool = NiaSearch()
tool.invoke({"query": "how to use React hooks"})
```

### 搜索网络

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nia import NiaWebSearch

tool = NiaWebSearch()
tool.invoke({"query": "latest Python release", "num_results": 5})
```

### 从索引源读取文件

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nia import NiaRead

tool = NiaRead()
tool.invoke({"source_id": "your-source-id", "path": "README.md"})
```

### 在代理内部

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nia import NiaToolkit

toolkit = NiaToolkit(include_search=True, include_sources=False, include_github=False, include_contexts=False, include_dependencies=False)
tools = toolkit.get_tools()

# pip install -qU "langchain[anthropic]"
from langchain.agents import create_agent

agent = create_agent(
    model="claude-sonnet-4-6",
    tools=tools,
)

agent.invoke(
    {"messages": [{"role": "user", "content": "Search for React hooks best practices"}]}
)
```

## 可用工具

### 搜索工具

* **NiaSearch** - 跨索引仓库、文档、数据集等进行语义搜索
* **NiaWebSearch** - 带类别过滤和日期范围的网页搜索
* **NiaDeepResearch** - 多步骤综合研究
* **NiaUniversalSearch** - 同时搜索所有源
* **NiaAdvisor** - 针对索引文档分析代码

### 源管理工具

* **NiaIndex** - 索引新源（仓库、文档、论文、数据集）
* **NiaSourceList** - 列出索引源并支持过滤
* **NiaSourceSubscribe** - 订阅预索引的公共源
* **NiaSourceSync** - 重新同步源以拉取最新更改
* **NiaRead** - 从索引源读取文件或页面
* **NiaGrep** - 在索引源内进行正则搜索
* **NiaExplore** - 浏览索引源的文件树

### GitHub 工具

* **NiaGitHubSearch** - 在 GitHub 仓库中搜索代码
* **NiaGitHubRead** - 从 GitHub 仓库读取文件
* **NiaGitHubGlob** - 查找匹配通配符模式的文件
* **NiaGitHubTree** - 浏览仓库文件树结构

### 上下文和内存工具

* **NiaContextSave** - 保存上下文以供跨代理共享
* **NiaContextSearch** - 对已保存的上下文进行语义搜索

### 依赖工具

* **NiaDependencySubscribe** - 自动订阅项目依赖的文档
* **NiaDependencyAnalyze** - 预览清单中将索引的内容

## API 参考

有关所有 Nia 工具和配置的详细文档，请参阅 [langchain-nia GitHub 仓库](https://github.com/nozomio-labs/nia-langchain)。

***

<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\tools\nia.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>
