以编程方式使用文档

>Perplexity 是搜索互联网最强大的方式 > ,提供无限 Pro Search、升级的 AI 模型、无限文件上传, > 图像生成和 API 积分。 > > 你可以查看 可用模型列表.

安装和设置

安装 Perplexity x LangChain 集成包:

pip install langchain-perplexity
uv add langchain-perplexity

从以下位置获取你的 API 密钥 Perplexity API 密钥仪表板 并将其设置为 PERPLEXITY_API_KEY (or PPLX_API_KEY) 环境变量。参见 Perplexity 入门指南 了解更多详情。

聊天模型

查看各种 Perplexity 使用示例.

from langchain_perplexity import ChatPerplexity

ChatPerplexity 也可以指向 Perplexity Agent API 通过传递 use_responses_api=True (或通过传递 tools=[{"type": "web_search"}],它会自动启用)。参见 Agent API 支持 聊天页面上的详细信息和示例。

检索器

你可以使用 PerplexitySearchRetrieverPerplexity Search API as Document 对象获取网络搜索结果。

参见 使用示例.

from langchain_perplexity import PerplexitySearchRetriever

工具

你可以将 Perplexity 用作代理工具,让你的代理访问 Perplexity Search API。

参见 使用示例.

PerplexitySearchResults

一种查询 Perplexity Search API 并返回 JSON 结果数组的工具(标题、URL、摘要、日期、最后更新)。

from langchain_perplexity import PerplexitySearchResults

嵌入模型

你可以使用 PerplexityEmbeddings 通过 Perplexity Embeddings API.

生成嵌入。参见 使用示例.

from langchain_perplexity import PerplexityEmbeddings

Deep Agents Code

Deep Agents Code.

中将 Perplexity 作为一级提供商使用:

uv tool install 'deepagents-code[perplexity]'

然后设置 PERPLEXITY_API_KEY 并在交互式 perplexity: 切换器或配置中使用 perplexity:sonar-pro前缀引用 Perplexity 模型(例如 /model )。

组件参考

抽象导入路径描述
ChatPerplexity聊天模型from langchain_perplexity import ChatPerplexity封装 Perplexity API 用于基础聊天补全的聊天模型。
PerplexitySearchRetriever检索器from langchain_perplexity import PerplexitySearchRetriever返回来自 Perplexity Search API 的对象的检索器。
PerplexitySearchResults工具from langchain_perplexity import PerplexitySearchResults返回 Perplexity Search API 结果作为 JSON 数组供代理使用的工具。
PerplexityEmbeddings嵌入from langchain_perplexity import PerplexityEmbeddings封装 Perplexity Embeddings API 的嵌入模型。