diff --git a/examples/foundational/51-grok-realtime.py b/examples/foundational/51-grok-realtime.py index a233b85bc..8784359f0 100644 --- a/examples/foundational/51-grok-realtime.py +++ b/examples/foundational/51-grok-realtime.py @@ -172,23 +172,6 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): session_properties = SessionProperties( # Voice options: Ara, Rex, Sal, Eve, Leo voice="Ara", - # System instructions - instructions="""You are a helpful and friendly AI assistant powered by Grok. - -You have access to several tools: -- Weather information -- Current time -- Restaurant recommendations -- Web search (built-in) -- X/Twitter search (built-in) - -Your voice and personality should be warm and engaging. Keep your responses -concise and conversational since this is a voice interaction. - -If the user asks about current events or news, use web search. -If they ask about what people are saying on social media, use X search. - -Always be helpful and proactive in offering assistance.""", # Grok-specific built-in tools can be added here: # tools=[ # WebSearchTool(), # Enable web search @@ -200,6 +183,22 @@ Always be helpful and proactive in offering assistance.""", llm = GrokRealtimeLLMService( api_key=os.getenv("GROK_API_KEY"), settings=GrokRealtimeLLMService.Settings( + system_instruction="""You are a helpful and friendly AI assistant powered by Grok. + + You have access to several tools: + - Weather information + - Current time + - Restaurant recommendations + - Web search (built-in) + - X/Twitter search (built-in) + + Your voice and personality should be warm and engaging. Keep your responses + concise and conversational since this is a voice interaction. + + If the user asks about current events or news, use web search. + If they ask about what people are saying on social media, use X search. + + Always be helpful and proactive in offering assistance.""", session_properties=session_properties, ), ) diff --git a/src/pipecat/adapters/services/grok_realtime_adapter.py b/src/pipecat/adapters/services/grok_realtime_adapter.py index 07b1de843..b95efb62c 100644 --- a/src/pipecat/adapters/services/grok_realtime_adapter.py +++ b/src/pipecat/adapters/services/grok_realtime_adapter.py @@ -50,18 +50,26 @@ class GrokRealtimeLLMAdapter(BaseLLMAdapter): """Get the identifier used in LLMSpecificMessage instances for Grok Realtime.""" return "grok-realtime" - def get_llm_invocation_params(self, context: LLMContext) -> GrokRealtimeLLMInvocationParams: + def get_llm_invocation_params( + self, context: LLMContext, *, system_instruction: Optional[str] = None + ) -> GrokRealtimeLLMInvocationParams: """Get Grok Realtime-specific LLM invocation parameters from a universal LLM context. Args: context: The LLM context containing messages, tools, etc. + system_instruction: Optional system instruction from service settings. Returns: Dictionary of parameters for invoking Grok's Voice Agent API. """ messages = self._from_universal_context_messages(self.get_messages(context)) + effective_system = self._resolve_system_instruction( + messages.system_instruction, + system_instruction, + discard_context_system=True, + ) return { - "system_instruction": messages.system_instruction, + "system_instruction": effective_system, "messages": messages.messages, "tools": self.from_standard_tools(context.tools) or [], } diff --git a/src/pipecat/services/grok/realtime/llm.py b/src/pipecat/services/grok/realtime/llm.py index 6e37d21d2..1317e7269 100644 --- a/src/pipecat/services/grok/realtime/llm.py +++ b/src/pipecat/services/grok/realtime/llm.py @@ -607,11 +607,15 @@ class GrokRealtimeLLMService(LLMService): adapter: GrokRealtimeLLMAdapter = self.get_llm_adapter() if self._context: - llm_invocation_params = adapter.get_llm_invocation_params(self._context) + llm_invocation_params = adapter.get_llm_invocation_params( + self._context, system_instruction=self._settings.system_instruction + ) if llm_invocation_params["tools"]: settings.tools = llm_invocation_params["tools"] + # The adapter resolves conflicts between init-provided and + # context-provided system instructions (preferring init-provided). if llm_invocation_params["system_instruction"]: settings.instructions = llm_invocation_params["system_instruction"]