llm
Perplexity LLM service implementation.
This module provides a service for interacting with Perplexity’s API using an OpenAI-compatible interface. It handles Perplexity’s unique token usage reporting patterns while maintaining compatibility with the Pipecat framework.
- class pipecat.services.perplexity.llm.PerplexityLLMSettings(model: str | None | _NotGiven = <factory>, extra: dict[str, Any]=<factory>, system_instruction: str | None | _NotGiven = <factory>, temperature: float | None | _NotGiven | NotGiven = <factory>, max_tokens: int | None | _NotGiven | NotGiven = <factory>, top_p: float | None | _NotGiven | NotGiven = <factory>, top_k: int | None | _NotGiven = <factory>, frequency_penalty: float | None | _NotGiven | NotGiven = <factory>, presence_penalty: float | None | _NotGiven | NotGiven = <factory>, seed: int | None | _NotGiven | NotGiven = <factory>, filter_incomplete_user_turns: bool | None | _NotGiven = <factory>, user_turn_completion_config: UserTurnCompletionConfig | None | _NotGiven = <factory>, max_completion_tokens: int | _NotGiven | NotGiven = <factory>)[source]
Bases:
OpenAILLMSettingsSettings for PerplexityLLMService.
- class pipecat.services.perplexity.llm.PerplexityLLMService(*, api_key: str, base_url: str = 'https://api.perplexity.ai', model: str | None = None, settings: PerplexityLLMSettings | None = None, **kwargs)[source]
Bases:
OpenAILLMServiceA service for interacting with Perplexity’s API.
This service extends OpenAILLMService to work with Perplexity’s API while maintaining compatibility with the OpenAI-style interface. It specifically handles the difference in token usage reporting between Perplexity (incremental) and OpenAI (final summary).
- adapter_class
alias of
PerplexityLLMAdapter
- supports_developer_role = False
Whether this service’s API supports the “developer” message role.
OpenAI’s native API supports it, but some OpenAI-compatible services (e.g. Cerebras) do not. Subclasses that don’t support it should set this to
False, which causes the adapter to convert “developer” messages to “user” messages before sending them to the API.
- Settings
alias of
PerplexityLLMSettings
- __init__(*, api_key: str, base_url: str = 'https://api.perplexity.ai', model: str | None = None, settings: PerplexityLLMSettings | None = None, **kwargs)[source]
Initialize the Perplexity LLM service.
- Parameters:
api_key – The API key for accessing Perplexity’s API.
base_url – The base URL for Perplexity’s API. Defaults to “https://api.perplexity.ai”.
model –
The model identifier to use. Defaults to “sonar”.
Deprecated since version 0.0.105: Use
settings=PerplexityLLMService.Settings(model=...)instead.settings – Runtime-updatable settings. When provided alongside deprecated parameters,
settingsvalues take precedence.**kwargs – Additional keyword arguments passed to OpenAILLMService.
- build_chat_completion_params(params_from_context: OpenAILLMInvocationParams) dict[source]
Build parameters for Perplexity chat completion request.
Perplexity uses a subset of OpenAI parameters and doesn’t support tools.
- Parameters:
params_from_context – Parameters, derived from the LLM context, to use for the chat completion. Contains messages, tools, and tool choice.
- Returns:
Dictionary of parameters for the chat completion request.
- async start_llm_usage_metrics(tokens: LLMTokenUsage)[source]
Accumulate token usage metrics during processing.
Perplexity reports token usage incrementally during streaming, unlike OpenAI which provides a final summary. We accumulate the counts and report the total at the end of processing.
- Parameters:
tokens – Token usage information to accumulate.