Skip to main content

llama-index llms perplexity integration

Project description

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/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_llms_perplexity-0.3.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_index_llms_perplexity-0.3.3-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_perplexity-0.3.3.tar.gz.

File metadata

  • Download URL: llama_index_llms_perplexity-0.3.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for llama_index_llms_perplexity-0.3.3.tar.gz
Algorithm Hash digest
SHA256 528983a977b1b8518b11447b38ea34191b650faa6a6da485b3ed7db9f6c24bb4
MD5 44ebcae26b1808951a0d946352559486
BLAKE2b-256 07b49153d76d4c155999d09aa49d402fbf33dde7b70ae5cae605bdf479c9422c

See more details on using hashes here.

File details

Details for the file llama_index_llms_perplexity-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_perplexity-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d5a55306683cda445136669fa646f5d30a6c739ec1ef164448f32f09ee10abf2
MD5 cb79e3df169b1be0ca9e52f3839e21ad
BLAKE2b-256 48c58283c813b2f4f79d9de18fde46b3b8e43d3722704086d2a7f0d7c19094b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page