困惑度¶
Perplexity的Sonar API提供了一种解决方案,将实时、基于网络的搜索与高级推理和深度研究能力相结合。
何时使用:
- 当您的应用需要直接从网络获取及时、相关的数据时,例如动态内容更新或当前事件追踪。
- 适用于需要支持复杂用户查询并集成推理和深度研究的产品,例如数字助手或高级搜索引擎。
在开始之前,请确保已安装 llama_index
In [ ]:
Copied!
%pip install llama-index-llms-perplexity
%pip install llama-index-llms-perplexity
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
初始设置¶
截至2025年4月12日,LLaMa Index中的Perplexity LLM类支持以下模型:
| 模型 | 上下文长度 | 模型类型 |
|---|---|---|
sonar-deep-research |
128k | Chat Completion |
sonar-reasoning-pro |
128k | Chat Completion |
sonar-reasoning |
128k | Chat Completion |
sonar-pro |
200k | Chat Completion |
sonar |
128k | Chat Completion |
r1-1776 |
128k | Chat Completion |
sonar-pro的最大输出令牌限制为8k。- 推理模型输出思维链式响应。
r1-1776是一个离线聊天模型,不使用 Perplexity 搜索子系统。
In [ ]:
Copied!
import getpass
import os
if "PPLX_API_KEY" not in os.environ:
os.environ["PPLX_API_KEY"] = getpass.getpass(
"Enter your Perplexity API key: "
)
import getpass
import os
if "PPLX_API_KEY" not in os.environ:
os.environ["PPLX_API_KEY"] = getpass.getpass(
"输入您的Perplexity API密钥: "
)
In [ ]:
Copied!
from llama_index.llms.perplexity import Perplexity
PPLX_API_KEY = __import__("os").environ.get("PPLX_API_KEY")
llm = Perplexity(api_key=PPLX_API_KEY, model="sonar-pro", temperature=0.2)
from llama_index.llms.perplexity import Perplexity
PPLX_API_KEY = __import__("os").environ.get("PPLX_API_KEY")
llm = Perplexity(api_key=PPLX_API_KEY, model="sonar-pro", temperature=0.2)
In [ ]:
Copied!
# Import the ChatMessage class from the llama_index library.
from llama_index.core.llms import ChatMessage
# Create a list of dictionaries where each dictionary represents a chat message.
# Each dictionary contains a 'role' key (e.g., system or user) and a 'content' key with the corresponding message.
messages_dict = [
{"role": "system", "content": "Be precise and concise."},
{
"role": "user",
"content": "Tell me the latest news about the US Stock Market.",
},
]
# Convert each dictionary in the list to a ChatMessage object using unpacking (**msg) in a list comprehension.
messages = [ChatMessage(**msg) for msg in messages_dict]
# Print the list of ChatMessage objects to verify the conversion.
print(messages)
# 从llama_index库中导入ChatMessage类。
from llama_index.core.llms import ChatMessage
# 创建一个字典列表,每个字典代表一条聊天消息。
# 每个字典包含'role'键(例如system或user)和对应的'content'键消息内容。
messages_dict = [
{"role": "system", "content": "保持精确简洁。"},
{
"role": "user",
"content": "告诉我关于美国股市的最新消息。",
},
]
# 使用列表推导式和字典解包(**msg)将列表中的每个字典转换为ChatMessage对象。
messages = [ChatMessage(**msg) for msg in messages_dict]
# 打印ChatMessage对象列表以验证转换结果。
print(messages)
[ChatMessage(role=<MessageRole.SYSTEM: 'system'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text='Be precise and concise.')]), ChatMessage(role=<MessageRole.USER: 'user'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text='Tell me the latest news about the US Stock Market.')])]
聊天¶
In [ ]:
Copied!
response = llm.chat(messages)
print(response)
response = llm.chat(messages)
print(response)
assistant: The latest update on the U.S. stock market indicates a strong performance recently. A significant 10% rally occurred on Wednesday, which contributed substantially to market gains. Additionally, the market closed strongly on Friday, with a 2% increase, ending near the intraday high. This reflects robust momentum, particularly in mega and large-cap growth stocks[1].
异步聊天¶
对于异步对话处理,使用achat方法发送消息并等待响应:
In [ ]:
Copied!
# Asynchronously send the list of chat messages to the LLM using the 'achat' method.
# This method returns a ChatResponse object containing the model's answer.
response = await llm.achat(messages)
print(response)
# 使用'achat'方法异步发送聊天消息列表给LLM。
# 该方法返回一个包含模型回答的ChatResponse对象。
response = await llm.achat(messages)
print(response)
assistant: The U.S. stock market has recently experienced significant gains. A major rally on Wednesday resulted in a 10% surge, contributing substantially to the market's overall upside. Additionally, the market closed strongly on Friday, with a 2% increase, ending near the intraday high. This performance highlights robust momentum, particularly in mega-cap and large-cap growth stocks[1].
流式聊天¶
如果您希望实时逐令牌接收响应,请使用stream_chat方法:
In [ ]:
Copied!
# Call the stream_chat method on the LLM instance, which returns a generator or iterable
# for streaming the chat response one delta (token or chunk) at a time.
response = llm.stream_chat(messages)
# Iterate over each streaming response chunk.
for r in response:
# Print the delta (the new chunk of generated text) without adding a newline.
print(r.delta, end="")
# 在LLM实例上调用stream_chat方法,该方法返回一个生成器或可迭代对象
# 用于逐个delta(令牌或块)流式传输聊天响应。
response = llm.stream_chat(messages)
# 遍历每个流式响应块。
for r in response:
# 打印delta(新生成的文本块)而不换行。
print(r.delta, end="")
The latest news about the U.S. stock market indicates a strong performance recently. The New York Stock Exchange (NYSE) experienced a significant rally, with a 10% surge on Wednesday, followed by a 2% gain on Friday. This upward momentum brought the market near its intraday high, driven by strength in mega-cap and large-cap growth stocks[1].
异步流式聊天¶
类似地,对于异步流式处理,astream_chat方法提供了异步处理响应增量的方式:
In [ ]:
Copied!
# Asynchronously call the astream_chat method on the LLM instance,
# which returns an asynchronous generator that yields response chunks.
resp = await llm.astream_chat(messages)
# Asynchronously iterate over each response chunk from the generator.
# For each chunk (delta), print the chunk's text content.
async for delta in resp:
print(delta.delta, end="")
# 异步调用LLM实例上的astream_chat方法,
# 该方法返回一个异步生成器,用于生成响应块。
resp = await llm.astream_chat(messages)
# 异步遍历生成器中的每个响应块。
# 对于每个块(delta),打印该块的文本内容。
async for delta in resp:
print(delta.delta, end="")
The latest updates on the U.S. stock market indicate significant positive momentum. The New York Stock Exchange (NYSE) experienced a strong rally, with a notable 10% surge on Wednesday. This was followed by a 2% gain on Friday, closing near the intraday high. The market's performance has been driven by mega and large-cap growth stocks, contributing to the overall upside[1].
工具调用¶
Perplexity模型可以轻松封装成llamaindex工具,以便在数据处理或对话工作流中调用。该工具利用Perplexity提供的实时生成式搜索功能,默认配置使用最新模型("sonar-pro")并启用了enable_search_classifier参数。
以下是如何定义和注册该工具的示例:
In [ ]:
Copied!
from llama_index.core.tools import FunctionTool
from llama_index.llms.perplexity import Perplexity
from llama_index.core.llms import ChatMessage
def query_perplexity(query: str) -> str:
"""
Queries the Perplexity API via the LlamaIndex integration.
This function instantiates a Perplexity LLM with updated default settings
(using model "sonar-pro" and enabling search classifier so that the API can
intelligently decide if a search is needed), wraps the query into a ChatMessage,
and returns the generated response content.
"""
pplx_api_key = (
"your-perplexity-api-key" # Replace with your actual API key
)
llm = Perplexity(
api_key=pplx_api_key,
model="sonar-pro",
temperature=0.7,
enable_search_classifier=True, # This will determine if the search component is necessary in this particular context
)
messages = [ChatMessage(role="user", content=query)]
response = llm.chat(messages)
return response.message.content
# Create the tool from the query_perplexity function
query_perplexity_tool = FunctionTool.from_defaults(fn=query_perplexity)
from llama_index.core.tools import FunctionTool
from llama_index.llms.perplexity import Perplexity
from llama_index.core.llms import ChatMessage
def query_perplexity(query: str) -> str:
"""
通过LlamaIndex集成查询Perplexity API。
此函数使用更新后的默认设置实例化一个Perplexity LLM(使用"sonar-pro"模型并启用搜索分类器,使API能智能判断是否需要搜索),将查询包装成ChatMessage,并返回生成的响应内容。
"""
pplx_api_key = (
"your-perplexity-api-key" # 替换为你的实际API密钥
)
llm = Perplexity(
api_key=pplx_api_key,
model="sonar-pro",
temperature=0.7,
enable_search_classifier=True, # 这将判断在当前特定上下文中是否需要搜索组件
)
messages = [ChatMessage(role="user", content=query)]
response = llm.chat(messages)
return response.message.content
# 从query_perplexity函数创建工具
query_perplexity_tool = FunctionTool.from_defaults(fn=query_perplexity)