LlamaIndex Llms Integration: Perplexity
The Perplexity integration for LlamaIndex allows you to tap into real-time generative search powered by the Perplexity API. This integration supports synchronous and asynchronous chat completions—as well as streaming responses.
Installation
To install the required packages, run:
%pip install llama-index-llms-perplexity
!pip install llama-index
Setup
Import Libraries and Configure API Key
Please refer to the official Perplexity API documentation to get started. You can follow the steps outlined here to generate your API key.
Import the necessary libraries and set your Perplexity API key:
from llama_index.llms.perplexity import Perplexity
pplx_api_key = "your-perplexity-api-key" # Replace with your actual API key
Initialize the Perplexity LLM
Create an instance of the Perplexity LLM with your API key and desired model settings:
llm = Perplexity(api_key=pplx_api_key, model="sonar-pro", temperature=0.2)
Chat Example
Sending a Chat Message
You can send a chat message using the chat method. Here’s how to do that:
from llama_index.core.llms import ChatMessage
messages_dict = [
{"role": "system", "content": "Be precise and concise."},
{
"role": "user",
"content": "What is the weather like in San Francisco today?",
},
]
messages = [ChatMessage(**msg) for msg in messages_dict]
# Obtain a response from the model
response = llm.chat(messages)
print(response)
Async Chat
For asynchronous conversation processing, use the achat method to send messages and await the response:
response = await llm.achat(messages)
print(response)
Stream Chat
For cases where you want to receive a response token by token in real time, use the stream_chat method:
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
Async Stream Chat
Similarly, for asynchronous streaming, the astream_chat method provides a way to process response deltas asynchronously:
resp = await llm.astream_chat(messages)
async for delta in resp:
print(delta.delta, end="")
Tool calling
Perplexity models can easily be wrapped into a llamaindex tool so that it can be called as part of your data processing or conversational workflows. This tool uses real-time generative search powered by Perplexity, and it’s configured with the updated default model ("sonar-pro") and the enable_search_classifier parameter enabled.
Below is an example of how to define and register the tool:
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)
LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/perplexity/
Metadata
Release files for llama-index-llms-perplexity 0.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llama_index_llms_perplexity-0.6.1.tar.gz | 7.3 kB | Without provenance | Details for llama_index_llms_perplexity-0.6.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llama_index_llms_perplexity-0.6.1-py3-none-any.whl | Python 3 | none | any | Details for llama_index_llms_perplexity-0.6.1-py3-none-any.whl |
Total release size: 14.3 kB
Release files / llama_index_llms_perplexity-0.6.1.tar.gz
| Download URL | llama_index_llms_perplexity-0.6.1.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7d2e09a9bb3feaa2140721abafabca6b4ccdcc6365060df00ecb0dafe19df51c
|
|
BLAKE2b-256 checksum How to use checksums |
96381b492275c4048685125d84dd9dd05109544437a5da3b0f9781eeff1397d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llama_index_llms_perplexity-0.6.1-py3-none-any.whl
| Download URL | llama_index_llms_perplexity-0.6.1-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cd951ef985654d84b2a650d320808600c2aaed14cba3c5c39bacea5250dff42f
|
|
BLAKE2b-256 checksum How to use checksums |
73789366e7ccddf892cf1300a651490e0c1a614f7d298d4391739d53c712c289
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for llama-index-llms-perplexity 0.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.