llama-index-llms-perplexity


Namellama-index-llms-perplexity JSON
Version 0.4.0 PyPI version JSON
download
home_pageNone
Summaryllama-index llms perplexity integration
upload_time2025-07-30 21:29:25
maintainerNone
docs_urlNone
authorNone
requires_python<4.0,>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```bash
%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](https://docs.perplexity.ai/home) to get started. You can follow the steps outlined [here](https://docs.perplexity.ai/guides/getting-started) to generate your API key.

Import the necessary libraries and set your Perplexity API key:

```python
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:

```python
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:

```python
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:

```python
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:

```python
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:

```python
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:

```python
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/

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "llama-index-llms-perplexity",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Your Name <you@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/9f/9b/da47ff15974f00bbdc3f72957f01daddc0919b0552decfa51c8f4b06e597/llama_index_llms_perplexity-0.4.0.tar.gz",
    "platform": null,
    "description": "# LlamaIndex Llms Integration: Perplexity\n\nThe 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\u2014as well as streaming responses.\n\n## Installation\n\nTo install the required packages, run:\n\n```bash\n%pip install llama-index-llms-perplexity\n!pip install llama-index\n```\n\n## Setup\n\n### Import Libraries and Configure API Key\n\nPlease refer to the official Perplexity [API documentation](https://docs.perplexity.ai/home) to get started. You can follow the steps outlined [here](https://docs.perplexity.ai/guides/getting-started) to generate your API key.\n\nImport the necessary libraries and set your Perplexity API key:\n\n```python\nfrom llama_index.llms.perplexity import Perplexity\n\npplx_api_key = \"your-perplexity-api-key\"  # Replace with your actual API key\n```\n\n### Initialize the Perplexity LLM\n\nCreate an instance of the Perplexity LLM with your API key and desired model settings:\n\n```python\nllm = Perplexity(api_key=pplx_api_key, model=\"sonar-pro\", temperature=0.2)\n```\n\n## Chat Example\n\n### Sending a Chat Message\n\nYou can send a chat message using the `chat` method. Here\u2019s how to do that:\n\n```python\nfrom llama_index.core.llms import ChatMessage\n\nmessages_dict = [\n    {\"role\": \"system\", \"content\": \"Be precise and concise.\"},\n    {\n        \"role\": \"user\",\n        \"content\": \"What is the weather like in San Francisco today?\",\n    },\n]\n\nmessages = [ChatMessage(**msg) for msg in messages_dict]\n\n# Obtain a response from the model\nresponse = llm.chat(messages)\nprint(response)\n```\n\n### Async Chat\n\nFor asynchronous conversation processing, use the `achat` method to send messages and await the response:\n\n```python\nresponse = await llm.achat(messages)\nprint(response)\n```\n\n### Stream Chat\n\nFor cases where you want to receive a response token by token in real time, use the `stream_chat` method:\n\n```python\nresp = llm.stream_chat(messages)\nfor r in resp:\n    print(r.delta, end=\"\")\n```\n\n### Async Stream Chat\n\nSimilarly, for asynchronous streaming, the `astream_chat` method provides a way to process response deltas asynchronously:\n\n```python\nresp = await llm.astream_chat(messages)\nasync for delta in resp:\n    print(delta.delta, end=\"\")\n```\n\n### Tool calling\n\nPerplexity 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\u2019s configured with the updated default model (\"sonar-pro\") and the enable_search_classifier parameter enabled.\n\nBelow is an example of how to define and register the tool:\n\n```python\nfrom llama_index.core.tools import FunctionTool\nfrom llama_index.llms.perplexity import Perplexity\nfrom llama_index.core.llms import ChatMessage\n\n\ndef query_perplexity(query: str) -> str:\n    \"\"\"\n    Queries the Perplexity API via the LlamaIndex integration.\n\n    This function instantiates a Perplexity LLM with updated default settings\n    (using model \"sonar-pro\" and enabling search classifier so that the API can\n    intelligently decide if a search is needed), wraps the query into a ChatMessage,\n    and returns the generated response content.\n    \"\"\"\n    pplx_api_key = (\n        \"your-perplexity-api-key\"  # Replace with your actual API key\n    )\n\n    llm = Perplexity(\n        api_key=pplx_api_key,\n        model=\"sonar-pro\",\n        temperature=0.7,\n        enable_search_classifier=True,  # This will determine if the search component is necessary in this particular context\n    )\n\n    messages = [ChatMessage(role=\"user\", content=query)]\n    response = llm.chat(messages)\n    return response.message.content\n\n\n# Create the tool from the query_perplexity function\nquery_perplexity_tool = FunctionTool.from_defaults(fn=query_perplexity)\n```\n\n### LLM Implementation example\n\nhttps://docs.llamaindex.ai/en/stable/examples/llm/perplexity/\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "llama-index llms perplexity integration",
    "version": "0.4.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "dc21944b11404dc14382960e42f931bc24020e15a1b90ed1e6097b202d5897fb",
                "md5": "cf6fffb18e6c8f900a138e6c0d1d076d",
                "sha256": "ea0f5413e19bdcc5eea17e76c044d0e557e8965db6b314259afd6fc9fd55ad46"
            },
            "downloads": -1,
            "filename": "llama_index_llms_perplexity-0.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cf6fffb18e6c8f900a138e6c0d1d076d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 6951,
            "upload_time": "2025-07-30T21:29:24",
            "upload_time_iso_8601": "2025-07-30T21:29:24.487005Z",
            "url": "https://files.pythonhosted.org/packages/dc/21/944b11404dc14382960e42f931bc24020e15a1b90ed1e6097b202d5897fb/llama_index_llms_perplexity-0.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9f9bda47ff15974f00bbdc3f72957f01daddc0919b0552decfa51c8f4b06e597",
                "md5": "3417a926c18f7ae171d73e93a48ef5fb",
                "sha256": "dc0c8075367eef5a8eba0861582dbc24ca4681c9a5550dfa344022fb1c1c2fb0"
            },
            "downloads": -1,
            "filename": "llama_index_llms_perplexity-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3417a926c18f7ae171d73e93a48ef5fb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 7198,
            "upload_time": "2025-07-30T21:29:25",
            "upload_time_iso_8601": "2025-07-30T21:29:25.193549Z",
            "url": "https://files.pythonhosted.org/packages/9f/9b/da47ff15974f00bbdc3f72957f01daddc0919b0552decfa51c8f4b06e597/llama_index_llms_perplexity-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-30 21:29:25",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "llama-index-llms-perplexity"
}
        
Elapsed time: 3.54476s