Update GrokLLMService docstrings

This commit is contained in:
Mark Backman
2025-06-26 10:39:46 -04:00
parent 9b64d2c325
commit 166c8e8e82

View File

@@ -4,6 +4,13 @@
# SPDX-License-Identifier: BSD 2-Clause License # SPDX-License-Identifier: BSD 2-Clause License
# #
"""Grok LLM service implementation using OpenAI-compatible interface.
This module provides a service for interacting with Grok's API through an
OpenAI-compatible interface, including specialized token usage tracking
and context aggregation functionality.
"""
from dataclasses import dataclass from dataclasses import dataclass
from loguru import logger from loguru import logger
@@ -23,13 +30,33 @@ from pipecat.services.openai.llm import (
@dataclass @dataclass
class GrokContextAggregatorPair: class GrokContextAggregatorPair:
"""Pair of context aggregators for user and assistant interactions.
Provides a convenient container for managing both user and assistant
context aggregators together for Grok LLM interactions.
Parameters:
_user: The user context aggregator instance.
_assistant: The assistant context aggregator instance.
"""
_user: OpenAIUserContextAggregator _user: OpenAIUserContextAggregator
_assistant: OpenAIAssistantContextAggregator _assistant: OpenAIAssistantContextAggregator
def user(self) -> OpenAIUserContextAggregator: def user(self) -> OpenAIUserContextAggregator:
"""Get the user context aggregator.
Returns:
The user context aggregator instance.
"""
return self._user return self._user
def assistant(self) -> OpenAIAssistantContextAggregator: def assistant(self) -> OpenAIAssistantContextAggregator:
"""Get the assistant context aggregator.
Returns:
The assistant context aggregator instance.
"""
return self._assistant return self._assistant
@@ -38,12 +65,14 @@ class GrokLLMService(OpenAILLMService):
This service extends OpenAILLMService to connect to Grok's API endpoint while This service extends OpenAILLMService to connect to Grok's API endpoint while
maintaining full compatibility with OpenAI's interface and functionality. maintaining full compatibility with OpenAI's interface and functionality.
Includes specialized token usage tracking that accumulates metrics during
processing and reports final totals.
Args: Args:
api_key (str): The API key for accessing Grok's API api_key: The API key for accessing Grok's API.
base_url (str, optional): The base URL for Grok API. Defaults to "https://api.x.ai/v1" base_url: The base URL for Grok API. Defaults to "https://api.x.ai/v1".
model (str, optional): The model identifier to use. Defaults to "grok-3-beta" model: The model identifier to use. Defaults to "grok-3-beta".
**kwargs: Additional keyword arguments passed to OpenAILLMService **kwargs: Additional keyword arguments passed to OpenAILLMService.
""" """
def __init__( def __init__(
@@ -63,7 +92,16 @@ class GrokLLMService(OpenAILLMService):
self._is_processing = False self._is_processing = False
def create_client(self, api_key=None, base_url=None, **kwargs): def create_client(self, api_key=None, base_url=None, **kwargs):
"""Create OpenAI-compatible client for Grok API endpoint.""" """Create OpenAI-compatible client for Grok API endpoint.
Args:
api_key: The API key to use. If None, uses instance default.
base_url: The base URL to use. If None, uses instance default.
**kwargs: Additional arguments passed to client creation.
Returns:
The configured client instance for Grok API.
"""
logger.debug(f"Creating Grok client with api {base_url}") logger.debug(f"Creating Grok client with api {base_url}")
return super().create_client(api_key, base_url, **kwargs) return super().create_client(api_key, base_url, **kwargs)
@@ -75,8 +113,8 @@ class GrokLLMService(OpenAILLMService):
them once at the end of processing. them once at the end of processing.
Args: Args:
context (OpenAILLMContext): The context to process, containing messages context: The context to process, containing messages and other
and other information needed for the LLM interaction. information needed for the LLM interaction.
""" """
# Reset all counters and flags at the start of processing # Reset all counters and flags at the start of processing
self._prompt_tokens = 0 self._prompt_tokens = 0
@@ -107,8 +145,8 @@ class GrokLLMService(OpenAILLMService):
The final accumulated totals are reported at the end of processing. The final accumulated totals are reported at the end of processing.
Args: Args:
tokens (LLMTokenUsage): The token usage metrics for the current chunk tokens: The token usage metrics for the current chunk of processing,
of processing, containing prompt_tokens and completion_tokens counts. containing prompt_tokens and completion_tokens counts.
""" """
# Only accumulate metrics during active processing # Only accumulate metrics during active processing
if not self._is_processing: if not self._is_processing:
@@ -130,22 +168,20 @@ class GrokLLMService(OpenAILLMService):
user_params: LLMUserAggregatorParams = LLMUserAggregatorParams(), user_params: LLMUserAggregatorParams = LLMUserAggregatorParams(),
assistant_params: LLMAssistantAggregatorParams = LLMAssistantAggregatorParams(), assistant_params: LLMAssistantAggregatorParams = LLMAssistantAggregatorParams(),
) -> GrokContextAggregatorPair: ) -> GrokContextAggregatorPair:
"""Create an instance of GrokContextAggregatorPair from an """Create an instance of GrokContextAggregatorPair from an OpenAILLMContext.
OpenAILLMContext. Constructor keyword arguments for both the user and
assistant aggregators can be provided. Constructor keyword arguments for both the user and assistant aggregators
can be provided.
Args: Args:
context (OpenAILLMContext): The LLM context. context: The LLM context to create aggregators for.
user_params (LLMUserAggregatorParams, optional): User aggregator user_params: Parameters for configuring the user aggregator.
parameters. assistant_params: Parameters for configuring the assistant aggregator.
assistant_params (LLMAssistantAggregatorParams, optional): User
aggregator parameters.
Returns: Returns:
GrokContextAggregatorPair: A pair of context aggregators, one for GrokContextAggregatorPair: A pair of context aggregators, one for
the user and one for the assistant, encapsulated in an the user and one for the assistant, encapsulated in an
GrokContextAggregatorPair. GrokContextAggregatorPair.
""" """
context.set_llm_adapter(self.get_llm_adapter()) context.set_llm_adapter(self.get_llm_adapter())