-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add Perplexity LLM plugin #5610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jliounis
wants to merge
1
commit into
livekit:main
Choose a base branch
from
jliounis:add-perplexity-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| ## 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. | ||
46 changes: 46 additions & 0 deletions
46
livekit-plugins/livekit-plugins-perplexity/livekit/plugins/perplexity/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
86 changes: 86 additions & 0 deletions
86
livekit-plugins/livekit-plugins-perplexity/livekit/plugins/perplexity/llm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
3 changes: 3 additions & 0 deletions
3
livekit-plugins/livekit-plugins-perplexity/livekit/plugins/perplexity/log.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import logging | ||
|
|
||
| logger = logging.getLogger("livekit.plugins.perplexity") |
3 changes: 3 additions & 0 deletions
3
livekit-plugins/livekit-plugins-perplexity/livekit/plugins/perplexity/models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from typing import Literal | ||
|
|
||
| PerplexityChatModels = Literal["sonar-pro"] |
Empty file.
15 changes: 15 additions & 0 deletions
15
livekit-plugins/livekit-plugins-perplexity/livekit/plugins/perplexity/version.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can update the docs on our end to reflect this plugin