Skip to content

Commit 01f1fc9

Browse files
wukathcopybara-github
authored andcommitted
fix: Only append skills to system instruction if ListSkillsTool isn't available
This reduces the token usage of having skills appended to the instruction on every call Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 910904229
1 parent 3a1eadc commit 01f1fc9

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/google/adk/tools/skill_toolset.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,15 @@ async def process_llm_request(
900900
self, *, tool_context: ToolContext, llm_request: LlmRequest
901901
) -> None:
902902
"""Processes the outgoing LLM request to include available skills."""
903-
skills = self._list_skills()
904-
skills_xml = prompt.format_skills_as_xml(skills)
905-
instructions = []
906-
instructions.append(_DEFAULT_SKILL_SYSTEM_INSTRUCTION)
907-
instructions.append(skills_xml)
903+
instructions = [_DEFAULT_SKILL_SYSTEM_INSTRUCTION]
904+
905+
has_list_skills = any(isinstance(t, ListSkillsTool) for t in self._tools)
906+
907+
if not has_list_skills:
908+
skills = self._list_skills()
909+
skills_xml = prompt.format_skills_as_xml(skills)
910+
instructions.append(skills_xml)
911+
908912
llm_request.append_instructions(instructions)
909913

910914

tests/unittests/tools/test_skill_toolset.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ async def test_load_resource_process_llm_request_binary(
427427

428428

429429
@pytest.mark.asyncio
430-
async def test_process_llm_request(
430+
async def test_process_llm_request_with_list_skills_tool(
431431
mock_skill1, mock_skill2, tool_context_instance
432432
):
433433
toolset = skill_toolset.SkillToolset([mock_skill1, mock_skill2])
@@ -437,6 +437,29 @@ async def test_process_llm_request(
437437
tool_context=tool_context_instance, llm_request=llm_req
438438
)
439439

440+
llm_req.append_instructions.assert_called_once_with(
441+
[skill_toolset.DEFAULT_SKILL_SYSTEM_INSTRUCTION]
442+
)
443+
444+
445+
@pytest.mark.asyncio
446+
async def test_process_llm_request_without_list_skills_tool(
447+
mock_skill1, mock_skill2, tool_context_instance
448+
):
449+
toolset = skill_toolset.SkillToolset([mock_skill1, mock_skill2])
450+
# Manually remove ListSkillsTool from self._tools to simulate it not being available
451+
toolset._tools = [
452+
t
453+
for t in toolset._tools
454+
if not isinstance(t, skill_toolset.ListSkillsTool)
455+
]
456+
457+
llm_req = mock.create_autospec(llm_request_model.LlmRequest, instance=True)
458+
459+
await toolset.process_llm_request(
460+
tool_context=tool_context_instance, llm_request=llm_req
461+
)
462+
440463
llm_req.append_instructions.assert_called_once()
441464
args, _ = llm_req.append_instructions.call_args
442465
instructions = args[0]

0 commit comments

Comments
 (0)