add logging of prefix of tool call and tool response

This commit is contained in:
hjc-puro 2025-11-07 14:43:44 -05:00
parent 2d8f6c46f1
commit 0c618482c4
2 changed files with 34 additions and 20 deletions

View file

@ -164,7 +164,8 @@ def _process_single_prompt(
enabled_toolsets=selected_toolsets, enabled_toolsets=selected_toolsets,
save_trajectories=False, # We handle saving ourselves save_trajectories=False, # We handle saving ourselves
verbose_logging=config.get("verbose", False), verbose_logging=config.get("verbose", False),
ephemeral_system_prompt=config.get("ephemeral_system_prompt") ephemeral_system_prompt=config.get("ephemeral_system_prompt"),
log_prefix_chars=config.get("log_prefix_chars", 100)
) )
# Run the agent with task_id to ensure each task gets its own isolated VM # Run the agent with task_id to ensure each task gets its own isolated VM
@ -323,11 +324,12 @@ class BatchRunner:
model: str = "claude-opus-4-20250514", model: str = "claude-opus-4-20250514",
num_workers: int = 4, num_workers: int = 4,
verbose: bool = False, verbose: bool = False,
ephemeral_system_prompt: str = None ephemeral_system_prompt: str = None,
log_prefix_chars: int = 100,
): ):
""" """
Initialize the batch runner. Initialize the batch runner.
Args: Args:
dataset_file (str): Path to the dataset JSONL file with 'prompt' field dataset_file (str): Path to the dataset JSONL file with 'prompt' field
batch_size (int): Number of prompts per batch batch_size (int): Number of prompts per batch
@ -340,6 +342,7 @@ class BatchRunner:
num_workers (int): Number of parallel workers num_workers (int): Number of parallel workers
verbose (bool): Enable verbose logging verbose (bool): Enable verbose logging
ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional) ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional)
log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses (default: 20)
""" """
self.dataset_file = Path(dataset_file) self.dataset_file = Path(dataset_file)
self.batch_size = batch_size self.batch_size = batch_size
@ -352,6 +355,7 @@ class BatchRunner:
self.num_workers = num_workers self.num_workers = num_workers
self.verbose = verbose self.verbose = verbose
self.ephemeral_system_prompt = ephemeral_system_prompt self.ephemeral_system_prompt = ephemeral_system_prompt
self.log_prefix_chars = log_prefix_chars
# Validate distribution # Validate distribution
if not validate_distribution(distribution): if not validate_distribution(distribution):
@ -507,7 +511,8 @@ class BatchRunner:
"base_url": self.base_url, "base_url": self.base_url,
"api_key": self.api_key, "api_key": self.api_key,
"verbose": self.verbose, "verbose": self.verbose,
"ephemeral_system_prompt": self.ephemeral_system_prompt "ephemeral_system_prompt": self.ephemeral_system_prompt,
"log_prefix_chars": self.log_prefix_chars
} }
# Get completed prompts set # Get completed prompts set
@ -650,11 +655,12 @@ def main(
resume: bool = False, resume: bool = False,
verbose: bool = False, verbose: bool = False,
list_distributions: bool = False, list_distributions: bool = False,
ephemeral_system_prompt: str = None ephemeral_system_prompt: str = None,
log_prefix_chars: int = 100,
): ):
""" """
Run batch processing of agent prompts from a dataset. Run batch processing of agent prompts from a dataset.
Args: Args:
dataset_file (str): Path to JSONL file with 'prompt' field in each entry dataset_file (str): Path to JSONL file with 'prompt' field in each entry
batch_size (int): Number of prompts per batch batch_size (int): Number of prompts per batch
@ -669,6 +675,7 @@ def main(
verbose (bool): Enable verbose logging (default: False) verbose (bool): Enable verbose logging (default: False)
list_distributions (bool): List available toolset distributions and exit list_distributions (bool): List available toolset distributions and exit
ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional) ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional)
log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses (default: 20)
Examples: Examples:
# Basic usage # Basic usage
@ -729,9 +736,10 @@ def main(
model=model, model=model,
num_workers=num_workers, num_workers=num_workers,
verbose=verbose, verbose=verbose,
ephemeral_system_prompt=ephemeral_system_prompt ephemeral_system_prompt=ephemeral_system_prompt,
log_prefix_chars=log_prefix_chars
) )
runner.run(resume=resume) runner.run(resume=resume)
except Exception as e: except Exception as e:

View file

@ -65,7 +65,8 @@ class AIAgent:
disabled_toolsets: List[str] = None, disabled_toolsets: List[str] = None,
save_trajectories: bool = False, save_trajectories: bool = False,
verbose_logging: bool = False, verbose_logging: bool = False,
ephemeral_system_prompt: str = None ephemeral_system_prompt: str = None,
log_prefix_chars: int = 100,
): ):
""" """
Initialize the AI Agent. Initialize the AI Agent.
@ -81,6 +82,7 @@ class AIAgent:
save_trajectories (bool): Whether to save conversation trajectories to JSONL files (default: False) save_trajectories (bool): Whether to save conversation trajectories to JSONL files (default: False)
verbose_logging (bool): Enable verbose logging for debugging (default: False) verbose_logging (bool): Enable verbose logging for debugging (default: False)
ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional) ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional)
log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses (default: 20)
""" """
self.model = model self.model = model
self.max_iterations = max_iterations self.max_iterations = max_iterations
@ -88,6 +90,7 @@ class AIAgent:
self.save_trajectories = save_trajectories self.save_trajectories = save_trajectories
self.verbose_logging = verbose_logging self.verbose_logging = verbose_logging
self.ephemeral_system_prompt = ephemeral_system_prompt self.ephemeral_system_prompt = ephemeral_system_prompt
self.log_prefix_chars = log_prefix_chars
# Store toolset filtering options # Store toolset filtering options
self.enabled_toolsets = enabled_toolsets self.enabled_toolsets = enabled_toolsets
@ -474,9 +477,9 @@ class AIAgent:
print(f"❌ Invalid JSON in tool call arguments: {e}") print(f"❌ Invalid JSON in tool call arguments: {e}")
function_args = {} function_args = {}
# Preview tool call arguments (first 20 chars) # Preview tool call arguments
args_str = json.dumps(function_args, ensure_ascii=False) args_str = json.dumps(function_args, ensure_ascii=False)
args_preview = args_str[:20] + "..." if len(args_str) > 20 else args_str args_preview = args_str[:self.log_prefix_chars] + "..." if len(args_str) > self.log_prefix_chars else args_str
print(f" 📞 Tool {i}: {function_name}({list(function_args.keys())}) - {args_preview}") print(f" 📞 Tool {i}: {function_name}({list(function_args.keys())}) - {args_preview}")
tool_start_time = time.time() tool_start_time = time.time()
@ -498,8 +501,8 @@ class AIAgent:
"tool_call_id": tool_call.id "tool_call_id": tool_call.id
}) })
# Preview tool response (first 20 chars) # Preview tool response
response_preview = function_result[:20] + "..." if len(function_result) > 20 else function_result response_preview = function_result[:self.log_prefix_chars] + "..." if len(function_result) > self.log_prefix_chars else function_result
print(f" ✅ Tool {i} completed in {tool_duration:.2f}s - {response_preview}") print(f" ✅ Tool {i} completed in {tool_duration:.2f}s - {response_preview}")
# Delay between tool calls # Delay between tool calls
@ -582,7 +585,7 @@ class AIAgent:
def main( def main(
query: str = None, query: str = None,
model: str = "claude-opus-4-20250514", model: str = "claude-opus-4-20250514",
api_key: str = None, api_key: str = None,
base_url: str = "https://api.anthropic.com/v1/", base_url: str = "https://api.anthropic.com/v1/",
max_turns: int = 10, max_turns: int = 10,
@ -590,25 +593,27 @@ def main(
disabled_toolsets: str = None, disabled_toolsets: str = None,
list_tools: bool = False, list_tools: bool = False,
save_trajectories: bool = False, save_trajectories: bool = False,
verbose: bool = False verbose: bool = False,
log_prefix_chars: int = 20
): ):
""" """
Main function for running the agent directly. Main function for running the agent directly.
Args: Args:
query (str): Natural language query for the agent. Defaults to Python 3.13 example. query (str): Natural language query for the agent. Defaults to Python 3.13 example.
model (str): Model name to use. Defaults to claude-opus-4-20250514. model (str): Model name to use. Defaults to claude-opus-4-20250514.
api_key (str): API key for authentication. Uses ANTHROPIC_API_KEY env var if not provided. api_key (str): API key for authentication. Uses ANTHROPIC_API_KEY env var if not provided.
base_url (str): Base URL for the model API. Defaults to https://api.anthropic.com/v1/ base_url (str): Base URL for the model API. Defaults to https://api.anthropic.com/v1/
max_turns (int): Maximum number of API call iterations. Defaults to 10. max_turns (int): Maximum number of API call iterations. Defaults to 10.
enabled_toolsets (str): Comma-separated list of toolsets to enable. Supports predefined enabled_toolsets (str): Comma-separated list of toolsets to enable. Supports predefined
toolsets (e.g., "research", "development", "safe"). toolsets (e.g., "research", "development", "safe").
Multiple toolsets can be combined: "web,vision" Multiple toolsets can be combined: "web,vision"
disabled_toolsets (str): Comma-separated list of toolsets to disable (e.g., "terminal") disabled_toolsets (str): Comma-separated list of toolsets to disable (e.g., "terminal")
list_tools (bool): Just list available tools and exit list_tools (bool): Just list available tools and exit
save_trajectories (bool): Save conversation trajectories to JSONL files. Defaults to False. save_trajectories (bool): Save conversation trajectories to JSONL files. Defaults to False.
verbose (bool): Enable verbose logging for debugging. Defaults to False. verbose (bool): Enable verbose logging for debugging. Defaults to False.
log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses. Defaults to 20.
Toolset Examples: Toolset Examples:
- "research": Web search, extract, crawl + vision tools - "research": Web search, extract, crawl + vision tools
""" """
@ -725,7 +730,8 @@ def main(
enabled_toolsets=enabled_toolsets_list, enabled_toolsets=enabled_toolsets_list,
disabled_toolsets=disabled_toolsets_list, disabled_toolsets=disabled_toolsets_list,
save_trajectories=save_trajectories, save_trajectories=save_trajectories,
verbose_logging=verbose verbose_logging=verbose,
log_prefix_chars=log_prefix_chars
) )
except RuntimeError as e: except RuntimeError as e:
print(f"❌ Failed to initialize agent: {e}") print(f"❌ Failed to initialize agent: {e}")