Perplexity 搜索 是一个网络搜索 API,返回经过排序的、有来源标注的结果,专为 LLM 和代理设计。它为 perplexity.ai 的答案引擎提供支持,并通过专用的 搜索 API 端点.
本页介绍如何使用 Perplexity Search API 作为 LangChain 工具。
设置
安装
安装 LangChain Perplexity 集成包:
pip install -qU langchain-perplexity
# and some deps for this notebook
pip install -qU langchain langchain-openai
凭证
您需要一个 Perplexity API 密钥才能使用此集成。在 Perplexity API 密钥仪表板.
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 field.
from langchain_perplexity import PerplexitySearchResults
# Initialize the PerplexitySearchResults tool
search_tool = PerplexitySearchResults(max_results=5)
# Perform a search query
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 个)。在域名前添加-以排除它。请参阅 域名过滤器文档. - -
search_recency_filter— 之一"day","week","month","year"。请参阅 日期和时间过滤器文档. - -
search_after_date/search_before_date— ISO 8601 格式的日期字符串MM/DD/YYYYformat.
from langchain_perplexity import PerplexitySearchResults
# Restrict results to a recent timeframe and a specific set of domains
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)
from langchain_perplexity import PerplexitySearchResults
# Restrict results to a specific date range
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 密钥:
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API key:\n")
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain_perplexity import PerplexitySearchResults
# Initialize the language model
model = init_chat_model(model="gpt-5.5", temperature=0)
# Initialize the Perplexity search tool
perplexity_search = PerplexitySearchResults(
max_results=5,
search_recency_filter="month",
)
# Create the agent
agent = create_agent(model, [perplexity_search])
# Run the agent
user_input = "What are the latest developments in quantum computing?"
stream = agent.stream_events(
{"messages": [{"role": "user", "content": user_input}]},
version="v3",
)
for snapshot in stream.values:
snapshot["messages"][-1].pretty_print()
API 参考
有关 Perplexity Search API 及其所有选项的详细文档,请参阅 搜索 API 参考 和 Perplexity API 文档.