chore: Adjust the LLM Request logging

1. function declarations is not necessary in the first tool
2. log the config

PiperOrigin-RevId: 816547534
This commit is contained in:
Xiang (Sean) Zhou
2025-10-07 23:07:18 -07:00
committed by Copybara-Service
parent 30212669ff
commit f2bed14c4b
2 changed files with 168 additions and 4 deletions
+36 -4
View File
@@ -351,10 +351,19 @@ def _build_function_declaration_log(
def _build_request_log(req: LlmRequest) -> str:
function_decls: list[types.FunctionDeclaration] = cast(
list[types.FunctionDeclaration],
req.config.tools[0].function_declarations if req.config.tools else [],
)
# Find which tool contains function_declarations
function_decls: list[types.FunctionDeclaration] = []
function_decl_tool_index: Optional[int] = None
if req.config.tools:
for idx, tool in enumerate(req.config.tools):
if tool.function_declarations:
function_decls = cast(
list[types.FunctionDeclaration], tool.function_declarations
)
function_decl_tool_index = idx
break
function_logs = (
[
_build_function_declaration_log(func_decl)
@@ -375,12 +384,35 @@ def _build_request_log(req: LlmRequest) -> str:
for content in req.contents
]
# Build exclusion dict for config logging
tools_exclusion = (
{function_decl_tool_index: {'function_declarations'}}
if function_decl_tool_index is not None
else True
)
try:
config_log = str(
req.config.model_dump(
exclude_none=True,
exclude={
'system_instruction': True,
'tools': tools_exclusion if req.config.tools else True,
},
)
)
except Exception:
config_log = repr(req.config)
return f"""
LLM Request:
-----------------------------------------------------------
System Instruction:
{req.config.system_instruction}
-----------------------------------------------------------
Config:
{config_log}
-----------------------------------------------------------
Contents:
{_NEW_LINE.join(contents_logs)}
-----------------------------------------------------------