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

# Perplexity 搜索集成

> 使用 LangChain Python 与 Perplexity 搜索工具集成。

[Perplexity Search](https://docs.perplexity.ai/docs/search/quickstart) 是一个网络搜索 API，它返回按排名排序、带有来源归属的结果，专为大语言模型和智能体使用而设计。它为 [perplexity.ai](https://www.perplexity.ai/) 的答案引擎提供支持，并通过专用的 [Search API 端点](https://docs.perplexity.ai/api-reference/search-post) 暴露。

本页介绍如何将 Perplexity Search API 用作 LangChain 工具。

## 设置

### 安装

安装 LangChain Perplexity 集成包：

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

# 以及此笔记本的一些依赖项
pip install -qU langchain langchain-openai
```

### 凭证

您需要一个 Perplexity API 密钥才能使用此集成。请在 [Perplexity API 密钥仪表板](https://www.perplexity.ai/account/api/keys) 中创建一个。

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

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

如果您更喜欢使用 `PERPLEXITY_API_KEY` 这个名称，该集成也接受它。

## 使用 PerplexitySearchResults 工具

`PerplexitySearchResults` 是一个可以与 LangChain 智能体一起使用的工具，用于执行 Perplexity 搜索。调用它会返回一个包含搜索结果的 JSON 数组，每个结果包含 `title`、`url`、`snippet`、`date` 和 `last_updated` 字段。

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

# 初始化 PerplexitySearchResults 工具
search_tool = PerplexitySearchResults(max_results=5)

# 执行搜索查询
search_results = search_tool.invoke("When was the last time the New York Knicks won the NBA Championship?")

print("Search Results:", search_results)
```

### PerplexitySearchResults 的高级功能

该工具接受以下构造函数和调用时参数：

* `max_results` — 要返回的结果数量。
* `country` — 用于偏向结果的 ISO 国家代码（例如 `"US"`）。
* `search_domain_filter` — 要包含或排除的域名列表（最多 20 个）。在域名前加上 `-` 以排除它。参见 [域名过滤器文档](https://docs.perplexity.ai/docs/search/filters/domain-filter)。
* `search_recency_filter` — `"day"`、`"week"`、`"month"`、`"year"` 之一。参见 [日期和时间过滤器文档](https://docs.perplexity.ai/docs/search/filters/date-time-filters)。
* `search_after_date` / `search_before_date` — 格式为 `MM/DD/YYYY` 的日期字符串。

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

# 将结果限制在最近的时间范围和特定的域名集合内
search_tool = PerplexitySearchResults(
    max_results=10,
    country="US",
    search_recency_filter="week",
    search_domain_filter=["nytimes.com", "reuters.com", "-pinterest.com"],
)

results = search_tool.invoke("Latest AI research papers")
print(results)
```

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

# 将结果限制在特定的日期范围内
search_tool = PerplexitySearchResults(
    max_results=5,
    search_after_date="01/01/2025",
    search_before_date="06/30/2025",
)

results = search_tool.invoke("US Federal Reserve interest rate decisions")
print(results)
```

## 在智能体中使用

我们可以将 `PerplexitySearchResults` 工具与 LangGraph 智能体一起使用。这赋予了智能体动态搜索网络以获取有依据、有来源归属信息的能力。

首先，设置语言模型。您需要提供您的 OpenAI API 密钥：

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

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain_perplexity import PerplexitySearchResults


# 初始化语言模型
model = init_chat_model(model="gpt-5.5", temperature=0)

# 初始化 Perplexity 搜索工具
perplexity_search = PerplexitySearchResults(
    max_results=5,
    search_recency_filter="month",
)

# 创建智能体
agent = create_agent(model, [perplexity_search])

# 运行智能体
user_input = "What are the latest developments in quantum computing?"

for step in agent.stream(
    {"messages": [{"role": "user", "content": user_input}]},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()
```

***

## API 参考

有关 Perplexity Search API 及其所有选项的详细文档，请参阅 [Search API 参考](https://docs.perplexity.ai/api-reference/search-post) 和 [Perplexity API 文档](https://docs.perplexity.ai)。

***

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

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