Prefer init-provided system instruction in OpenAI Realtime
Add system_instruction parameter to the OpenAI 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.
This commit is contained in:
@@ -43,20 +43,26 @@ class OpenAIRealtimeLLMAdapter(BaseLLMAdapter):
|
|||||||
"""Get the identifier used in LLMSpecificMessage instances for OpenAI Realtime."""
|
"""Get the identifier used in LLMSpecificMessage instances for OpenAI Realtime."""
|
||||||
return "openai-realtime"
|
return "openai-realtime"
|
||||||
|
|
||||||
def get_llm_invocation_params(self, context: LLMContext) -> OpenAIRealtimeLLMInvocationParams:
|
def get_llm_invocation_params(
|
||||||
|
self, context: LLMContext, *, system_instruction: Optional[str] = None
|
||||||
|
) -> OpenAIRealtimeLLMInvocationParams:
|
||||||
"""Get OpenAI Realtime-specific LLM invocation parameters from a universal LLM context.
|
"""Get OpenAI Realtime-specific LLM invocation parameters from a universal LLM context.
|
||||||
|
|
||||||
This is a placeholder until support for universal LLMContext machinery is added for OpenAI Realtime.
|
|
||||||
|
|
||||||
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 OpenAI Realtime's API.
|
Dictionary of parameters for invoking OpenAI Realtime's 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,
|
||||||
# NOTE: LLMContext's tools are guaranteed to be a ToolsSchema (or NOT_GIVEN)
|
# NOTE: LLMContext's tools are guaranteed to be a ToolsSchema (or NOT_GIVEN)
|
||||||
"tools": self.from_standard_tools(context.tools) or [],
|
"tools": self.from_standard_tools(context.tools) or [],
|
||||||
|
|||||||
@@ -687,14 +687,16 @@ class OpenAIRealtimeLLMService(LLMService):
|
|||||||
adapter: OpenAIRealtimeLLMAdapter = self.get_llm_adapter()
|
adapter: OpenAIRealtimeLLMAdapter = 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
|
||||||
|
)
|
||||||
|
|
||||||
# tools given in the context override the tools in the session properties
|
# tools given in the context override the tools in the session properties
|
||||||
if llm_invocation_params["tools"]:
|
if llm_invocation_params["tools"]:
|
||||||
settings.tools = llm_invocation_params["tools"]
|
settings.tools = llm_invocation_params["tools"]
|
||||||
|
|
||||||
# instructions in the context come from an initial "system" message in the
|
# The adapter resolves conflicts between init-provided and
|
||||||
# messages list, and override instructions in the session properties
|
# 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