llm
Grok LLM service implementation using OpenAI-compatible interface.
This module provides a service for interacting with Grok’s API through an OpenAI-compatible interface.
- class pipecat.services.xai.llm.GrokLLMSettings(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>, use_provider_thinking_default: bool | NotGiven = <factory>, max_completion_tokens: int | None | NotGiven | NotGiven = <factory>, reasoning_effort: Literal['none', 'low', 'medium', 'high', 'xhigh'] | None | ~pipecat.utils.types.NotGiven=<factory>)[source]
Bases:
OpenAILLMSettingsSettings for GrokLLMService.
- Parameters:
reasoning_effort – How much the model thinks before answering. One of “none”, “low”, “medium”, “high”, or “xhigh”; which values a model accepts varies (
grok-4.6rejects “none”,xhighneedsgrok-4.6or later, and the non-reasoning models reject the parameter altogether). When unset, xAI’s per-model default applies.
- class pipecat.services.xai.llm.GrokLLMService(*, api_key: str, base_url: str = 'https://api.x.ai/v1', model: str | None = None, settings: GrokLLMSettings | None = None, **kwargs)[source]
Bases:
OpenAILLMServiceA service for interacting with Grok’s API using the OpenAI-compatible interface.
This service extends OpenAILLMService to connect to Grok’s API endpoint while maintaining full compatibility with OpenAI’s interface and functionality.
- Settings
alias of
GrokLLMSettings
- __init__(*, api_key: str, base_url: str = 'https://api.x.ai/v1', model: str | None = None, settings: GrokLLMSettings | None = None, **kwargs)[source]
Initialize the GrokLLMService with API key and model.
- Parameters:
api_key – The API key for accessing Grok’s API.
base_url – The base URL for Grok API. Defaults to “https://api.x.ai/v1”.
model –
The model identifier to use. Defaults to “grok-4.20-non-reasoning”.
Deprecated since version 0.0.105: Use
settings=GrokLLMService.Settings(model=...)instead. Will be removed in 2.0.0.settings – Runtime-updatable settings. When provided alongside deprecated parameters,
settingsvalues take precedence.**kwargs – Additional keyword arguments passed to OpenAILLMService.
- create_client(api_key=None, base_url=None, **kwargs)[source]
Create OpenAI-compatible client for Grok API endpoint.
- Parameters:
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.
- build_chat_completion_params(params_from_context: OpenAILLMInvocationParams) dict[str, Any][source]
Build parameters for Grok chat completion request.
Extends the base OpenAI parameters with Grok’s reasoning effort control.
- 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.