Prefer init-provided system instruction in Grok Realtime
Add system_instruction parameter to the Grok Realtime adapter's get_llm_invocation_params() and call _resolve_system_instruction() to prefer init-provided over context-provided system instructions and warn on conflicts. Previously context-provided took precedence. Update the Grok Realtime example to use settings.system_instruction instead of session_properties.instructions.
This commit is contained in:
@@ -172,23 +172,6 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
|
|||||||
session_properties = SessionProperties(
|
session_properties = SessionProperties(
|
||||||
# Voice options: Ara, Rex, Sal, Eve, Leo
|
# Voice options: Ara, Rex, Sal, Eve, Leo
|
||||||
voice="Ara",
|
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:
|
# Grok-specific built-in tools can be added here:
|
||||||
# tools=[
|
# tools=[
|
||||||
# WebSearchTool(), # Enable web search
|
# WebSearchTool(), # Enable web search
|
||||||
@@ -200,6 +183,22 @@ Always be helpful and proactive in offering assistance.""",
|
|||||||
llm = GrokRealtimeLLMService(
|
llm = GrokRealtimeLLMService(
|
||||||
api_key=os.getenv("GROK_API_KEY"),
|
api_key=os.getenv("GROK_API_KEY"),
|
||||||
settings=GrokRealtimeLLMService.Settings(
|
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,
|
session_properties=session_properties,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -50,18 +50,26 @@ class GrokRealtimeLLMAdapter(BaseLLMAdapter):
|
|||||||
"""Get the identifier used in LLMSpecificMessage instances for Grok Realtime."""
|
"""Get the identifier used in LLMSpecificMessage instances for Grok Realtime."""
|
||||||
return "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.
|
"""Get Grok Realtime-specific LLM invocation parameters from a universal LLM context.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
context: The LLM context containing messages, tools, etc.
|
context: The LLM context containing messages, tools, etc.
|
||||||
|
system_instruction: Optional system instruction from service settings.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dictionary of parameters for invoking Grok's Voice Agent API.
|
Dictionary of parameters for invoking Grok's Voice Agent API.
|
||||||
"""
|
"""
|
||||||
messages = self._from_universal_context_messages(self.get_messages(context))
|
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 {
|
return {
|
||||||
"system_instruction": messages.system_instruction,
|
"system_instruction": effective_system,
|
||||||
"messages": messages.messages,
|
"messages": messages.messages,
|
||||||
"tools": self.from_standard_tools(context.tools) or [],
|
"tools": self.from_standard_tools(context.tools) or [],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -607,11 +607,15 @@ class GrokRealtimeLLMService(LLMService):
|
|||||||
adapter: GrokRealtimeLLMAdapter = self.get_llm_adapter()
|
adapter: GrokRealtimeLLMAdapter = self.get_llm_adapter()
|
||||||
|
|
||||||
if self._context:
|
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"]:
|
if llm_invocation_params["tools"]:
|
||||||
settings.tools = 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"]:
|
if llm_invocation_params["system_instruction"]:
|
||||||
settings.instructions = llm_invocation_params["system_instruction"]
|
settings.instructions = llm_invocation_params["system_instruction"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user