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

# 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 - Version](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` 通过共享的 API 包装器提供所有 20 个 Nia 工具。使用 `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** - 查找匹配 glob 模式的文件
* **NiaGitHubTree** - 浏览仓库文件树结构

### 上下文和记忆工具

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

### 依赖工具

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

## API 参考

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

***

<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/tools/nia.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
