Commit ·
bd83de1
1
Parent(s): 80eaceb
Add colorful, detailed logging for better visibility
Browse files- Added ANSI color codes for different message types in terminal output
- Color-coded tool calls and their results for better distinction
- Added detailed logging for LLM inputs and outputs
- Highlighted final minimal answers in bold green
- Increased snippet size for better context in logs
- Improved visual separation between different parts of execution
- direct_agent_fixed.py +71 -26
- direct_integration.py +44 -4
direct_agent_fixed.py
CHANGED
|
@@ -484,19 +484,32 @@ THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank o
|
|
| 484 |
def _handle_tool_call(self, tool_calls):
|
| 485 |
results = []
|
| 486 |
|
| 487 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
for tool_call in tool_calls:
|
| 489 |
function_name = tool_call.function.name
|
| 490 |
tool_call_id = tool_call.id
|
| 491 |
|
| 492 |
-
print(f"EXECUTING TOOL: {function_name} [id: {tool_call_id}]")
|
| 493 |
|
| 494 |
try:
|
| 495 |
function_args = json.loads(tool_call.function.arguments)
|
| 496 |
-
print(f"ARGUMENTS: {json.dumps(function_args, indent=2)}")
|
| 497 |
except Exception as e:
|
| 498 |
error_msg = f"Error parsing arguments: {str(e)}"
|
| 499 |
-
print(f"ERROR: {error_msg}")
|
| 500 |
results.append({
|
| 501 |
"tool_call_id": tool_call_id,
|
| 502 |
"function_name": function_name,
|
|
@@ -510,20 +523,20 @@ THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank o
|
|
| 510 |
if function_name == "analyze_excel":
|
| 511 |
file_path = function_args.get("file_path", "")
|
| 512 |
query = function_args.get("query", "")
|
| 513 |
-
print(f"EXCEL ANALYSIS: file={file_path}, query={query}")
|
| 514 |
result = self.tools[function_name].run(file_path, query)
|
| 515 |
else:
|
| 516 |
# For all other tools, just pass the first argument
|
| 517 |
first_arg = list(function_args.values())[0] if function_args else ""
|
| 518 |
-
print(f"TOOL INPUT: {first_arg}")
|
| 519 |
result = self.tools[function_name].run(first_arg)
|
| 520 |
|
| 521 |
# Truncate result for logging if it's too long
|
| 522 |
result_preview = result
|
| 523 |
-
if len(result_preview) >
|
| 524 |
-
result_preview = result_preview[:
|
| 525 |
|
| 526 |
-
print(f"TOOL RESULT: {result_preview}")
|
| 527 |
|
| 528 |
results.append({
|
| 529 |
"tool_call_id": tool_call_id,
|
|
@@ -532,7 +545,7 @@ THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank o
|
|
| 532 |
})
|
| 533 |
except Exception as e:
|
| 534 |
error_msg = f"Error executing tool {function_name}: {str(e)}"
|
| 535 |
-
print(f"ERROR: {error_msg}")
|
| 536 |
results.append({
|
| 537 |
"tool_call_id": tool_call_id,
|
| 538 |
"function_name": function_name,
|
|
@@ -540,14 +553,14 @@ THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank o
|
|
| 540 |
})
|
| 541 |
else:
|
| 542 |
error_msg = f"Tool {function_name} not found"
|
| 543 |
-
print(f"ERROR: {error_msg}")
|
| 544 |
results.append({
|
| 545 |
"tool_call_id": tool_call_id,
|
| 546 |
"function_name": function_name,
|
| 547 |
"result": error_msg
|
| 548 |
})
|
| 549 |
|
| 550 |
-
print("======================\n")
|
| 551 |
return results
|
| 552 |
|
| 553 |
def _call_openrouter_with_retry(self, messages, max_retries=5):
|
|
@@ -555,23 +568,42 @@ THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank o
|
|
| 555 |
retry_count = 0
|
| 556 |
base_delay = 2 # Start with a 2-second delay
|
| 557 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
# Log the messages being sent to the LLM
|
| 559 |
-
print("\n=== SENDING TO LLM ===")
|
| 560 |
for msg in messages:
|
| 561 |
role = msg.get("role", "unknown")
|
| 562 |
content = msg.get("content", "")
|
| 563 |
|
| 564 |
# Truncate content for readability if it's too long
|
| 565 |
-
if content and len(content) >
|
| 566 |
-
content = content[:
|
| 567 |
|
| 568 |
# Handle tool messages specially
|
| 569 |
if role == "tool":
|
| 570 |
tool_call_id = msg.get("tool_call_id", "unknown")
|
| 571 |
-
print(f"TOOL RESPONSE [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
else:
|
| 573 |
print(f"{role.upper()}: {content}")
|
| 574 |
-
print("=======================\n")
|
| 575 |
|
| 576 |
while retry_count < max_retries:
|
| 577 |
try:
|
|
@@ -600,17 +632,17 @@ THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank o
|
|
| 600 |
if hasattr(response, 'choices') and response.choices and hasattr(response.choices[0], 'message'):
|
| 601 |
message = response.choices[0].message
|
| 602 |
|
| 603 |
-
print("\n=== LLM RESPONSE ===")
|
| 604 |
if hasattr(message, 'content') and message.content:
|
| 605 |
-
print(f"CONTENT: {message.content}")
|
| 606 |
|
| 607 |
if hasattr(message, 'tool_calls') and message.tool_calls:
|
| 608 |
-
print(f"TOOL CALLS [{len(message.tool_calls)}]:")
|
| 609 |
for tool_call in message.tool_calls:
|
| 610 |
function_name = tool_call.function.name
|
| 611 |
function_args = tool_call.function.arguments
|
| 612 |
-
print(f"
|
| 613 |
-
print("====================\n")
|
| 614 |
|
| 615 |
# Return the response
|
| 616 |
return response
|
|
@@ -798,10 +830,23 @@ FINAL REMINDER: Your answer must be MINIMAL. Return ONLY the specific informatio
|
|
| 798 |
original_answer = message.content
|
| 799 |
minimal_answer = self._enforce_minimal_answer(original_answer, question)
|
| 800 |
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
|
| 806 |
return minimal_answer
|
| 807 |
|
|
|
|
| 484 |
def _handle_tool_call(self, tool_calls):
|
| 485 |
results = []
|
| 486 |
|
| 487 |
+
# ANSI color codes for terminal output
|
| 488 |
+
COLORS = {
|
| 489 |
+
"RED": "\033[91m",
|
| 490 |
+
"GREEN": "\033[92m",
|
| 491 |
+
"YELLOW": "\033[93m",
|
| 492 |
+
"BLUE": "\033[94m",
|
| 493 |
+
"MAGENTA": "\033[95m",
|
| 494 |
+
"CYAN": "\033[96m",
|
| 495 |
+
"WHITE": "\033[97m",
|
| 496 |
+
"BOLD": "\033[1m",
|
| 497 |
+
"RESET": "\033[0m"
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
print(f"\n{COLORS['BOLD']}{COLORS['YELLOW']}=== TOOL EXECUTION ==={COLORS['RESET']}")
|
| 501 |
for tool_call in tool_calls:
|
| 502 |
function_name = tool_call.function.name
|
| 503 |
tool_call_id = tool_call.id
|
| 504 |
|
| 505 |
+
print(f"{COLORS['BOLD']}EXECUTING TOOL: {COLORS['YELLOW']}{function_name}{COLORS['RESET']} [id: {tool_call_id}]")
|
| 506 |
|
| 507 |
try:
|
| 508 |
function_args = json.loads(tool_call.function.arguments)
|
| 509 |
+
print(f"{COLORS['YELLOW']}ARGUMENTS:{COLORS['RESET']} {json.dumps(function_args, indent=2)}")
|
| 510 |
except Exception as e:
|
| 511 |
error_msg = f"Error parsing arguments: {str(e)}"
|
| 512 |
+
print(f"{COLORS['RED']}ERROR:{COLORS['RESET']} {error_msg}")
|
| 513 |
results.append({
|
| 514 |
"tool_call_id": tool_call_id,
|
| 515 |
"function_name": function_name,
|
|
|
|
| 523 |
if function_name == "analyze_excel":
|
| 524 |
file_path = function_args.get("file_path", "")
|
| 525 |
query = function_args.get("query", "")
|
| 526 |
+
print(f"{COLORS['YELLOW']}EXCEL ANALYSIS:{COLORS['RESET']} file={file_path}, query={query}")
|
| 527 |
result = self.tools[function_name].run(file_path, query)
|
| 528 |
else:
|
| 529 |
# For all other tools, just pass the first argument
|
| 530 |
first_arg = list(function_args.values())[0] if function_args else ""
|
| 531 |
+
print(f"{COLORS['YELLOW']}TOOL INPUT [{function_name}]:{COLORS['RESET']} {first_arg}")
|
| 532 |
result = self.tools[function_name].run(first_arg)
|
| 533 |
|
| 534 |
# Truncate result for logging if it's too long
|
| 535 |
result_preview = result
|
| 536 |
+
if len(result_preview) > 1000:
|
| 537 |
+
result_preview = result_preview[:997] + "..."
|
| 538 |
|
| 539 |
+
print(f"{COLORS['GREEN']}TOOL RESULT [{function_name}]:{COLORS['RESET']} {result_preview}")
|
| 540 |
|
| 541 |
results.append({
|
| 542 |
"tool_call_id": tool_call_id,
|
|
|
|
| 545 |
})
|
| 546 |
except Exception as e:
|
| 547 |
error_msg = f"Error executing tool {function_name}: {str(e)}"
|
| 548 |
+
print(f"{COLORS['RED']}ERROR:{COLORS['RESET']} {error_msg}")
|
| 549 |
results.append({
|
| 550 |
"tool_call_id": tool_call_id,
|
| 551 |
"function_name": function_name,
|
|
|
|
| 553 |
})
|
| 554 |
else:
|
| 555 |
error_msg = f"Tool {function_name} not found"
|
| 556 |
+
print(f"{COLORS['RED']}ERROR:{COLORS['RESET']} {error_msg}")
|
| 557 |
results.append({
|
| 558 |
"tool_call_id": tool_call_id,
|
| 559 |
"function_name": function_name,
|
| 560 |
"result": error_msg
|
| 561 |
})
|
| 562 |
|
| 563 |
+
print(f"{COLORS['YELLOW']}======================={COLORS['RESET']}\n")
|
| 564 |
return results
|
| 565 |
|
| 566 |
def _call_openrouter_with_retry(self, messages, max_retries=5):
|
|
|
|
| 568 |
retry_count = 0
|
| 569 |
base_delay = 2 # Start with a 2-second delay
|
| 570 |
|
| 571 |
+
# ANSI color codes for terminal output
|
| 572 |
+
COLORS = {
|
| 573 |
+
"RED": "\033[91m",
|
| 574 |
+
"GREEN": "\033[92m",
|
| 575 |
+
"YELLOW": "\033[93m",
|
| 576 |
+
"BLUE": "\033[94m",
|
| 577 |
+
"MAGENTA": "\033[95m",
|
| 578 |
+
"CYAN": "\033[96m",
|
| 579 |
+
"WHITE": "\033[97m",
|
| 580 |
+
"BOLD": "\033[1m",
|
| 581 |
+
"RESET": "\033[0m"
|
| 582 |
+
}
|
| 583 |
+
|
| 584 |
# Log the messages being sent to the LLM
|
| 585 |
+
print(f"\n{COLORS['BOLD']}{COLORS['MAGENTA']}=== SENDING TO LLM ==={COLORS['RESET']}")
|
| 586 |
for msg in messages:
|
| 587 |
role = msg.get("role", "unknown")
|
| 588 |
content = msg.get("content", "")
|
| 589 |
|
| 590 |
# Truncate content for readability if it's too long
|
| 591 |
+
if content and len(content) > 1000:
|
| 592 |
+
content = content[:997] + "..."
|
| 593 |
|
| 594 |
# Handle tool messages specially
|
| 595 |
if role == "tool":
|
| 596 |
tool_call_id = msg.get("tool_call_id", "unknown")
|
| 597 |
+
print(f"{COLORS['YELLOW']}TOOL RESPONSE [{tool_call_id}]:{COLORS['RESET']} {content}")
|
| 598 |
+
elif role == "system":
|
| 599 |
+
print(f"{COLORS['BLUE']}SYSTEM:{COLORS['RESET']} {content}")
|
| 600 |
+
elif role == "user":
|
| 601 |
+
print(f"{COLORS['GREEN']}USER:{COLORS['RESET']} {content}")
|
| 602 |
+
elif role == "assistant":
|
| 603 |
+
print(f"{COLORS['CYAN']}ASSISTANT:{COLORS['RESET']} {content}")
|
| 604 |
else:
|
| 605 |
print(f"{role.upper()}: {content}")
|
| 606 |
+
print(f"{COLORS['MAGENTA']}======================={COLORS['RESET']}\n")
|
| 607 |
|
| 608 |
while retry_count < max_retries:
|
| 609 |
try:
|
|
|
|
| 632 |
if hasattr(response, 'choices') and response.choices and hasattr(response.choices[0], 'message'):
|
| 633 |
message = response.choices[0].message
|
| 634 |
|
| 635 |
+
print(f"\n{COLORS['BOLD']}{COLORS['CYAN']}=== LLM RESPONSE ==={COLORS['RESET']}")
|
| 636 |
if hasattr(message, 'content') and message.content:
|
| 637 |
+
print(f"{COLORS['CYAN']}CONTENT:{COLORS['RESET']} {message.content}")
|
| 638 |
|
| 639 |
if hasattr(message, 'tool_calls') and message.tool_calls:
|
| 640 |
+
print(f"{COLORS['BOLD']}{COLORS['YELLOW']}TOOL CALLS [{len(message.tool_calls)}]:{COLORS['RESET']}")
|
| 641 |
for tool_call in message.tool_calls:
|
| 642 |
function_name = tool_call.function.name
|
| 643 |
function_args = tool_call.function.arguments
|
| 644 |
+
print(f"{COLORS['YELLOW']} → {function_name}({function_args}){COLORS['RESET']}")
|
| 645 |
+
print(f"{COLORS['CYAN']}===================={COLORS['RESET']}\n")
|
| 646 |
|
| 647 |
# Return the response
|
| 648 |
return response
|
|
|
|
| 830 |
original_answer = message.content
|
| 831 |
minimal_answer = self._enforce_minimal_answer(original_answer, question)
|
| 832 |
|
| 833 |
+
# ANSI color codes for terminal output
|
| 834 |
+
COLORS = {
|
| 835 |
+
"RED": "\033[91m",
|
| 836 |
+
"GREEN": "\033[92m",
|
| 837 |
+
"YELLOW": "\033[93m",
|
| 838 |
+
"BLUE": "\033[94m",
|
| 839 |
+
"MAGENTA": "\033[95m",
|
| 840 |
+
"CYAN": "\033[96m",
|
| 841 |
+
"WHITE": "\033[97m",
|
| 842 |
+
"BOLD": "\033[1m",
|
| 843 |
+
"RESET": "\033[0m"
|
| 844 |
+
}
|
| 845 |
+
|
| 846 |
+
print(f"\n{COLORS['BOLD']}{COLORS['WHITE']}=== ANSWER PROCESSING ==={COLORS['RESET']}")
|
| 847 |
+
print(f"{COLORS['CYAN']}ORIGINAL ANSWER:{COLORS['RESET']}\n{original_answer}")
|
| 848 |
+
print(f"\n{COLORS['BOLD']}{COLORS['GREEN']}MINIMAL ANSWER:{COLORS['RESET']} {minimal_answer}")
|
| 849 |
+
print(f"{COLORS['WHITE']}========================={COLORS['RESET']}\n")
|
| 850 |
|
| 851 |
return minimal_answer
|
| 852 |
|
direct_integration.py
CHANGED
|
@@ -91,13 +91,33 @@ def run_agent_on_questions(questions):
|
|
| 91 |
task_id = question["task_id"]
|
| 92 |
question_text = question["question"]
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# Create a separator line for better logging visibility
|
| 95 |
separator = "=" * 80
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
logger.info(f"\n{separator}")
|
| 97 |
logger.info(f"QUESTION {i+1}/{len(questions)} (Task ID: {task_id})")
|
| 98 |
logger.info(separator)
|
| 99 |
-
|
| 100 |
-
# Log the full question text
|
| 101 |
logger.info(f"QUESTION TEXT:\n{question_text}")
|
| 102 |
logger.info(separator)
|
| 103 |
|
|
@@ -156,11 +176,31 @@ def run_agent_on_questions(questions):
|
|
| 156 |
# Call the actual agent
|
| 157 |
result = self.agent(question, context)
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
# Create an all-caps delimiter for the final answer
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
logger.info("FINAL MINIMAL ANSWER:")
|
| 162 |
logger.info(result)
|
| 163 |
-
logger.info(
|
| 164 |
|
| 165 |
return result
|
| 166 |
|
|
|
|
| 91 |
task_id = question["task_id"]
|
| 92 |
question_text = question["question"]
|
| 93 |
|
| 94 |
+
# ANSI color codes for terminal output
|
| 95 |
+
COLORS = {
|
| 96 |
+
"RED": "\033[91m",
|
| 97 |
+
"GREEN": "\033[92m",
|
| 98 |
+
"YELLOW": "\033[93m",
|
| 99 |
+
"BLUE": "\033[94m",
|
| 100 |
+
"MAGENTA": "\033[95m",
|
| 101 |
+
"CYAN": "\033[96m",
|
| 102 |
+
"WHITE": "\033[97m",
|
| 103 |
+
"BOLD": "\033[1m",
|
| 104 |
+
"RESET": "\033[0m"
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
# Create a separator line for better logging visibility
|
| 108 |
separator = "=" * 80
|
| 109 |
+
|
| 110 |
+
# Colorful question display in console
|
| 111 |
+
print(f"\n{COLORS['BOLD']}{COLORS['MAGENTA']}{separator}{COLORS['RESET']}")
|
| 112 |
+
print(f"{COLORS['BOLD']}{COLORS['MAGENTA']}QUESTION {i+1}/{len(questions)} (Task ID: {task_id}){COLORS['RESET']}")
|
| 113 |
+
print(f"{COLORS['BOLD']}{COLORS['MAGENTA']}{separator}{COLORS['RESET']}")
|
| 114 |
+
print(f"{COLORS['BOLD']}{COLORS['GREEN']}QUESTION TEXT:{COLORS['RESET']}\n{question_text}")
|
| 115 |
+
print(f"{COLORS['BOLD']}{COLORS['MAGENTA']}{separator}{COLORS['RESET']}")
|
| 116 |
+
|
| 117 |
+
# Also log to file
|
| 118 |
logger.info(f"\n{separator}")
|
| 119 |
logger.info(f"QUESTION {i+1}/{len(questions)} (Task ID: {task_id})")
|
| 120 |
logger.info(separator)
|
|
|
|
|
|
|
| 121 |
logger.info(f"QUESTION TEXT:\n{question_text}")
|
| 122 |
logger.info(separator)
|
| 123 |
|
|
|
|
| 176 |
# Call the actual agent
|
| 177 |
result = self.agent(question, context)
|
| 178 |
|
| 179 |
+
# ANSI color codes for terminal output
|
| 180 |
+
COLORS = {
|
| 181 |
+
"RED": "\033[91m",
|
| 182 |
+
"GREEN": "\033[92m",
|
| 183 |
+
"YELLOW": "\033[93m",
|
| 184 |
+
"BLUE": "\033[94m",
|
| 185 |
+
"MAGENTA": "\033[95m",
|
| 186 |
+
"CYAN": "\033[96m",
|
| 187 |
+
"WHITE": "\033[97m",
|
| 188 |
+
"BOLD": "\033[1m",
|
| 189 |
+
"RESET": "\033[0m"
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
# Create an all-caps delimiter for the final answer
|
| 193 |
+
delimiter = "=" * 80
|
| 194 |
+
print(f"\n{COLORS['BOLD']}{COLORS['GREEN']}{delimiter}")
|
| 195 |
+
print(f"{COLORS['BOLD']}{COLORS['GREEN']}FINAL MINIMAL ANSWER:{COLORS['RESET']}")
|
| 196 |
+
print(f"{COLORS['BOLD']}{result}{COLORS['RESET']}")
|
| 197 |
+
print(f"{COLORS['BOLD']}{COLORS['GREEN']}{delimiter}{COLORS['RESET']}\n")
|
| 198 |
+
|
| 199 |
+
# Also log to file
|
| 200 |
+
logger.info(delimiter)
|
| 201 |
logger.info("FINAL MINIMAL ANSWER:")
|
| 202 |
logger.info(result)
|
| 203 |
+
logger.info(delimiter)
|
| 204 |
|
| 205 |
return result
|
| 206 |
|