Prefer init-provided system instruction in Nova Sonic
Add system_instruction parameter to the Nova Sonic 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. Remove the service-side fallback logic, as the adapter now handles resolution.
This commit is contained in:
@@ -72,20 +72,26 @@ class AWSNovaSonicLLMAdapter(BaseLLMAdapter[AWSNovaSonicLLMInvocationParams]):
|
|||||||
"""Get the identifier used in LLMSpecificMessage instances for AWS Nova Sonic."""
|
"""Get the identifier used in LLMSpecificMessage instances for AWS Nova Sonic."""
|
||||||
return "aws-nova-sonic"
|
return "aws-nova-sonic"
|
||||||
|
|
||||||
def get_llm_invocation_params(self, context: LLMContext) -> AWSNovaSonicLLMInvocationParams:
|
def get_llm_invocation_params(
|
||||||
|
self, context: LLMContext, *, system_instruction: Optional[str] = None
|
||||||
|
) -> AWSNovaSonicLLMInvocationParams:
|
||||||
"""Get AWS Nova Sonic-specific LLM invocation parameters from a universal LLM context.
|
"""Get AWS Nova Sonic-specific LLM invocation parameters from a universal LLM context.
|
||||||
|
|
||||||
This is a placeholder until support for universal LLMContext machinery is added for AWS Nova Sonic.
|
|
||||||
|
|
||||||
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 AWS Nova Sonic's LLM API.
|
Dictionary of parameters for invoking AWS Nova Sonic's LLM 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 [],
|
||||||
|
|||||||
@@ -629,7 +629,9 @@ class AWSNovaSonicLLMService(LLMService):
|
|||||||
|
|
||||||
# Read context
|
# Read context
|
||||||
adapter: AWSNovaSonicLLMAdapter = self.get_llm_adapter()
|
adapter: AWSNovaSonicLLMAdapter = self.get_llm_adapter()
|
||||||
llm_connection_params = adapter.get_llm_invocation_params(self._context)
|
llm_connection_params = adapter.get_llm_invocation_params(
|
||||||
|
self._context, system_instruction=self._settings.system_instruction
|
||||||
|
)
|
||||||
|
|
||||||
# Send prompt start event, specifying tools.
|
# Send prompt start event, specifying tools.
|
||||||
# Tools from context take priority over self._tools.
|
# Tools from context take priority over self._tools.
|
||||||
@@ -642,12 +644,9 @@ class AWSNovaSonicLLMService(LLMService):
|
|||||||
await self._send_prompt_start_event(tools)
|
await self._send_prompt_start_event(tools)
|
||||||
|
|
||||||
# Send system instruction.
|
# Send system instruction.
|
||||||
# Instruction from context takes priority over self._settings.system_instruction.
|
# The adapter resolves conflicts between init-provided and
|
||||||
system_instruction = (
|
# context-provided system instructions (preferring init-provided).
|
||||||
llm_connection_params["system_instruction"]
|
system_instruction = llm_connection_params["system_instruction"]
|
||||||
if llm_connection_params["system_instruction"]
|
|
||||||
else self._settings.system_instruction
|
|
||||||
)
|
|
||||||
logger.debug(f"Using system instruction: {system_instruction}")
|
logger.debug(f"Using system instruction: {system_instruction}")
|
||||||
if system_instruction:
|
if system_instruction:
|
||||||
await self._send_text_event(text=system_instruction, role=Role.SYSTEM)
|
await self._send_text_event(text=system_instruction, role=Role.SYSTEM)
|
||||||
|
|||||||
Reference in New Issue
Block a user