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.PerplexityLLMService(*, api_key: str, base_url: str = 'https://api.perplexity.ai', model: str = 'sonar', **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).
- __init__(*, api_key: str, base_url: str = 'https://api.perplexity.ai', model: str = 'sonar', **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”.
**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.