Improved token counting for OpenAI endpoints (llama.cpp token count -> cached prompt_tokens from last output -> naive string estimate). Logger refactoring with JSON format debugging to log file. Various bugfixes. Removed unused templates.py
This commit is contained in:
@@ -4,7 +4,6 @@ import time
|
||||
import logging
|
||||
import json
|
||||
import ast
|
||||
import contextlib
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
|
||||
@@ -12,27 +11,28 @@ logger = logging.getLogger(__name__)
|
||||
console = Console()
|
||||
|
||||
|
||||
def init_trace_file(debug, log_dir="logs"):
|
||||
def init_trace_file(log_dir="logs"):
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
||||
filename = os.path.join(log_dir, f"trace_{timestamp}.json")
|
||||
if debug: logger.debug(f"Trace logging initialized: {filename}")
|
||||
logger.debug("Trace logging initialized: %s", filename)
|
||||
return filename
|
||||
|
||||
def save_agent_trace(filepath, messages, full_history=None):
|
||||
def save_agent_trace(filepath, messages, step=0):
|
||||
try:
|
||||
data_to_save = {
|
||||
"timestamp": time.time(),
|
||||
"step": step,
|
||||
"context_window": messages
|
||||
}
|
||||
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
json.dump(data_to_save, f, indent=2, ensure_ascii=False)
|
||||
with open(filepath, 'a', encoding='utf-8') as f:
|
||||
f.write(json.dumps(data_to_save, ensure_ascii=False) + "\n")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save trace file: {e}")
|
||||
logger.error("Failed to save trace file: %s", e)
|
||||
|
||||
|
||||
def _analyze_code_safety(code_str):
|
||||
@@ -101,10 +101,9 @@ def safeguard_and_repair(debug, client, messages, schema, original_code):
|
||||
if is_safe:
|
||||
return original_code
|
||||
|
||||
logger.warning("Safeguard triggered: %s (Line: %s)", error_msg, line_no)
|
||||
if debug:
|
||||
logger.warning(f"Safeguard triggered: {error_msg} (Line: {line_no})")
|
||||
console.print(Panel(f"{error_msg}", title="Safeguard Interrupt", style="bold red"))
|
||||
|
||||
console.print(Panel(
|
||||
f"[italic]{error_msg}[/italic]",
|
||||
title="Unsafe Code",
|
||||
@@ -146,8 +145,7 @@ def safeguard_and_repair(debug, client, messages, schema, original_code):
|
||||
return full_fixed_code
|
||||
|
||||
except json.JSONDecodeError:
|
||||
if debug: logger.error("Snippet repair failed to parse. Falling back to full repair.")
|
||||
pass
|
||||
logger.error("Snippet repair failed to parse. Falling back to full repair.")
|
||||
|
||||
repair_messages = messages + [
|
||||
{"role": "assistant", "content": json.dumps({
|
||||
@@ -171,11 +169,11 @@ def safeguard_and_repair(debug, client, messages, schema, original_code):
|
||||
except json.JSONDecodeError:
|
||||
return ""
|
||||
|
||||
def compress_history(debug, client, messages, keep_last_pairs=2):
|
||||
def compress_history(client, messages, keep_last_pairs=2):
|
||||
keep_count = keep_last_pairs * 2
|
||||
|
||||
if len(messages) < (2 + 2 + keep_count):
|
||||
if debug: logger.warning("History too short to compress, but context is full. Crashing safely.")
|
||||
logger.warning("History too short to compress, but context is full. Crashing safely.")
|
||||
return messages
|
||||
|
||||
to_compress = messages[2:-keep_count]
|
||||
@@ -194,7 +192,7 @@ def compress_history(debug, client, messages, keep_last_pairs=2):
|
||||
f"--- HISTORY START ---\n{history_text}\n--- HISTORY END ---"
|
||||
)
|
||||
|
||||
if debug: logger.debug(f"Compressing {len(to_compress)} messages...")
|
||||
logger.debug("Compressing %d messages...", len(to_compress))
|
||||
|
||||
summary_text = client.completion([{"role": "user", "content": summary_prompt}])
|
||||
|
||||
@@ -205,10 +203,10 @@ def compress_history(debug, client, messages, keep_last_pairs=2):
|
||||
|
||||
new_messages = [messages[0], messages[1]] + [summary_message] + messages[-keep_count:]
|
||||
|
||||
if debug: logger.info(f"Compression complete. Reduced {len(messages)} msgs to {len(new_messages)}.")
|
||||
logger.info("Compression complete. Reduced %d msgs to %d.", len(messages), len(new_messages))
|
||||
return new_messages
|
||||
|
||||
def generate_final_report(debug, client, task_text, raw_answer):
|
||||
def generate_final_report(client, task_text, raw_answer):
|
||||
system_prompt = (
|
||||
"You are a professional report writer. "
|
||||
"Your goal is to convert the provided Raw Data into a clear, concise, "
|
||||
@@ -226,7 +224,7 @@ def generate_final_report(debug, client, task_text, raw_answer):
|
||||
Write the final response in natural language (Markdown).
|
||||
"""
|
||||
|
||||
if debug: logger.debug("Generating natural language report...")
|
||||
logger.debug("Generating natural language report...")
|
||||
return client.completion([
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt}
|
||||
@@ -237,5 +235,5 @@ def load_file(filepath):
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
return f.read()
|
||||
except FileNotFoundError:
|
||||
logger.error(f"File not found: {filepath}")
|
||||
logger.error("File not found: %s", filepath)
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user