Skip to content

Commit

Permalink
update: formatting for perplexity.py
Browse files Browse the repository at this point in the history
  • Loading branch information
porcelaincode committed Feb 5, 2025
1 parent 90936e0 commit 67ef13c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/pipecat/services/perplexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
OpenAILLMService,
)


class PerplexityLLMService(OpenAILLMService):
"""
A service for interacting with Perplexity's API using an OpenAI-compatible interface.
This service connects to Perplexity's chat completions endpoint and processes the
aggregated conversation context similarly to GrokLLMService.
Args:
api_key (str): The API key for accessing Perplexity's API.
base_url (str, optional): The base URL for the Perplexity API. Defaults to "https://api.perplexity.ai".
Expand All @@ -31,6 +32,7 @@ class PerplexityLLMService(OpenAILLMService):
top_p (float, optional): Nucleus sampling parameter (default: 0.9).
**kwargs: Additional keyword arguments passed to OpenAILLMService.
"""

def __init__(
self,
*,
Expand Down Expand Up @@ -68,7 +70,7 @@ async def _process_context(self, context: OpenAILLMContext):
"""
Process the conversation context by sending it to Perplexity's API and
incorporating the assistant's reply into the context.
This method also accumulates token usage metrics from the Perplexity response.
"""
# Reset counters and mark processing start
Expand All @@ -95,12 +97,14 @@ async def _process_context(self, context: OpenAILLMContext):
"Content-Type": "application/json",
}
url = f"{self.base_url.rstrip('/')}/chat/completions"

async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as response:
if response.status != 200:
error_text = await response.text()
raise Exception(f"Perplexity API call failed (status {response.status}): {error_text}")
raise Exception(
f"Perplexity API call failed (status {response.status}): {error_text}"
)
data = await response.json()

# Extract the assistant's reply from the response
Expand All @@ -111,7 +115,9 @@ async def _process_context(self, context: OpenAILLMContext):
usage = data.get("usage", {})
self._prompt_tokens = usage.get("prompt_tokens", 0)
self._completion_tokens = usage.get("completion_tokens", 0)
self._total_tokens = usage.get("total_tokens", self._prompt_tokens + self._completion_tokens)
self._total_tokens = usage.get(
"total_tokens", self._prompt_tokens + self._completion_tokens
)

# Push the updated context as a frame
frame = OpenAILLMContextFrame(context)
Expand Down

0 comments on commit 67ef13c

Please sign in to comment.