Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions livekit-plugins/livekit-plugins-perplexity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Perplexity plugin for LiveKit Agents

Support for [Perplexity](https://www.perplexity.ai/) LLMs via the OpenAI-compatible
chat completions endpoint at `https://api.perplexity.ai`.

See [https://docs.livekit.io/agents/integrations/llm/](https://docs.livekit.io/agents/integrations/llm/) for more information.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
See [https://docs.livekit.io/agents/integrations/llm/](https://docs.livekit.io/agents/integrations/llm/) for more information.
See [https://docs.livekit.io/agents/models/llm/perplexity/](https://docs.livekit.io/agents/models/llm/perplexity/) for more information.

we can update the docs on our end to reflect this plugin


## Installation

```bash
pip install livekit-plugins-perplexity
```

## Pre-requisites

You'll need an API key from Perplexity. It can be passed directly or set as the
`PERPLEXITY_API_KEY` environment variable.

## Usage

```python
from livekit.plugins import perplexity

llm = perplexity.LLM(
model="sonar-pro",
# api_key picked up from PERPLEXITY_API_KEY if omitted
)
```

The plugin reuses the OpenAI plugin's chat completions transport with
`base_url="https://api.perplexity.ai"` and forwards an `X-Pplx-Integration`
attribution header on every outgoing request.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Perplexity plugin for LiveKit Agents

Wraps Perplexity's OpenAI-compatible chat completions endpoint at
``https://api.perplexity.ai`` so it can be used as a drop-in LLM for LiveKit
voice agents.
"""

from livekit.agents import Plugin

from .llm import LLM
from .log import logger
from .models import PerplexityChatModels
from .version import __version__

__all__ = ["LLM", "PerplexityChatModels", "logger", "__version__"]


class PerplexityPlugin(Plugin):
def __init__(self) -> None:
super().__init__(__name__, __version__, __package__, logger)


Plugin.register_plugin(PerplexityPlugin())

# Cleanup docs of unexported modules
_module = dir()
NOT_IN_ALL = [m for m in _module if m not in __all__]

__pdoc__ = {}

for n in NOT_IN_ALL:
__pdoc__[n] = False
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os

import httpx
import openai

from livekit.agents.llm import ToolChoice
from livekit.agents.types import (
NOT_GIVEN,
NotGivenOr,
)
from livekit.agents.utils import is_given
from livekit.plugins.openai import LLM as OpenAILLM

from .models import PerplexityChatModels
from .version import __version__

PERPLEXITY_BASE_URL = "https://api.perplexity.ai"
_ATTRIBUTION_HEADER = {"X-Pplx-Integration": f"livekit-agents/{__version__}"}


class LLM(OpenAILLM):
def __init__(
self,
*,
model: str | PerplexityChatModels = "sonar-pro",
api_key: NotGivenOr[str] = NOT_GIVEN,
base_url: NotGivenOr[str] = PERPLEXITY_BASE_URL,
client: openai.AsyncClient | None = None,
user: NotGivenOr[str] = NOT_GIVEN,
temperature: NotGivenOr[float] = NOT_GIVEN,
parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN,
tool_choice: NotGivenOr[ToolChoice] = NOT_GIVEN,
top_p: NotGivenOr[float] = NOT_GIVEN,
timeout: httpx.Timeout | None = None,
):
"""
Create a new instance of Perplexity LLM.

``api_key`` must be set to your Perplexity API key, either using the argument or by
setting the ``PERPLEXITY_API_KEY`` environmental variable.
"""
api_key = api_key if is_given(api_key) else os.environ.get("PERPLEXITY_API_KEY", "")
if not api_key:
raise ValueError(
"PERPLEXITY_API_KEY is required, either as argument or set "
"PERPLEXITY_API_KEY environmental variable"
)

super().__init__(
model=model,
api_key=api_key,
base_url=base_url,
client=client,
user=user,
temperature=temperature,
parallel_tool_calls=parallel_tool_calls,
tool_choice=tool_choice,
top_p=top_p,
timeout=timeout,
extra_headers=_ATTRIBUTION_HEADER,
_strict_tool_schema=False,
)

@property
def model(self) -> str:
return self._opts.model

@property
def provider(self) -> str:
return "Perplexity"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging

logger = logging.getLogger("livekit.plugins.perplexity")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from typing import Literal

PerplexityChatModels = Literal["sonar-pro"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "1.5.6"
44 changes: 44 additions & 0 deletions livekit-plugins/livekit-plugins-perplexity/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "livekit-plugins-perplexity"
dynamic = ["version"]
description = "Perplexity LLM plugin for LiveKit Agents"
readme = "README.md"
license = "Apache-2.0"
requires-python = ">=3.10.0"
authors = [{ name = "LiveKit", email = "hello@livekit.io" }]
keywords = ["voice", "ai", "realtime", "audio", "video", "livekit", "perplexity"]
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Topic :: Multimedia :: Sound/Audio",
"Topic :: Multimedia :: Video",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3 :: Only",
]
dependencies = [
"livekit-agents[openai]>=1.5.6",
]

[project.urls]
Documentation = "https://docs.livekit.io"
Website = "https://livekit.io/"
Source = "https://github.com/livekit/agents"

[tool.hatch.version]
path = "livekit/plugins/perplexity/version.py"

[tool.hatch.build.targets.wheel]
packages = ["livekit"]

[tool.hatch.build.targets.sdist]
include = ["/livekit"]

[tool.uv]
exclude-newer = "7 days"
exclude-newer-package = { livekit = "0 days", livekit-agents = "0 days" }
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ livekit-plugins-neuphonic = { workspace = true }
livekit-plugins-nltk = { workspace = true }
livekit-plugins-nvidia = { workspace = true }
livekit-plugins-openai = { workspace = true }
livekit-plugins-perplexity = { workspace = true }
livekit-plugins-resemble = { workspace = true }
livekit-plugins-rime = { workspace = true }
livekit-plugins-runway = { workspace = true }
Expand Down
35 changes: 35 additions & 0 deletions tests/test_plugin_perplexity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations

import pytest

from livekit.plugins.perplexity import LLM, __version__
from livekit.plugins.perplexity.llm import PERPLEXITY_BASE_URL


def test_default_model_and_base_url(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PERPLEXITY_API_KEY", "test-key")
llm = LLM()
assert llm.model == "sonar-pro"
assert PERPLEXITY_BASE_URL == "https://api.perplexity.ai"
# AsyncClient stores the configured base URL on _base_url.
assert str(llm._client.base_url).startswith("https://api.perplexity.ai")


def test_attribution_header_attached(monkeypatch: pytest.MonkeyPatch) -> None:
"""X-Pplx-Integration must be attached on every outgoing chat request."""
monkeypatch.setenv("PERPLEXITY_API_KEY", "test-key")
llm = LLM()
extra_headers = llm._opts.extra_headers
assert extra_headers is not None
assert extra_headers["X-Pplx-Integration"] == f"livekit-agents/{__version__}"


def test_missing_api_key_raises(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("PERPLEXITY_API_KEY", raising=False)
with pytest.raises(ValueError, match="PERPLEXITY_API_KEY"):
LLM()


def test_provider_name(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PERPLEXITY_API_KEY", "test-key")
assert LLM().provider == "Perplexity"