Ajustes de max tokens
This commit is contained in:
@@ -150,9 +150,23 @@ class BaseAgent:
|
|||||||
final_args = tool["arguments"] or chunk.tool_arguments or ""
|
final_args = tool["arguments"] or chunk.tool_arguments or ""
|
||||||
try:
|
try:
|
||||||
args = json.loads(final_args) if final_args else {}
|
args = json.loads(final_args) if final_args else {}
|
||||||
except json.JSONDecodeError:
|
tool["parse_error"] = None
|
||||||
logger.warning("Failed to parse tool args: %s", final_args[:200])
|
except json.JSONDecodeError as e:
|
||||||
|
# Args truncados o malformados — causa tipica: el modelo
|
||||||
|
# excedio max_tokens a mitad de la serializacion JSON
|
||||||
|
# del tool_use (ej. escribiendo un fichero grande).
|
||||||
|
logger.warning(
|
||||||
|
"Failed to parse tool args for %s (%d chars): %s... | err: %s",
|
||||||
|
tool.get("name", "?"), len(final_args), final_args[:200], str(e)[:100],
|
||||||
|
)
|
||||||
args = {}
|
args = {}
|
||||||
|
# Guardamos el raw para poder generar un fingerprint distinto
|
||||||
|
# al de otros fallos y un mensaje util para el modelo.
|
||||||
|
tool["parse_error"] = {
|
||||||
|
"raw": final_args,
|
||||||
|
"raw_hash": hashlib.md5(final_args.encode()).hexdigest()[:8],
|
||||||
|
"message": str(e)[:200],
|
||||||
|
}
|
||||||
tool["parsed_arguments"] = args
|
tool["parsed_arguments"] = args
|
||||||
tool_calls.append(tool)
|
tool_calls.append(tool)
|
||||||
|
|
||||||
@@ -194,6 +208,27 @@ class BaseAgent:
|
|||||||
# Execute tool calls and add COMPLETE results to conversation
|
# Execute tool calls and add COMPLETE results to conversation
|
||||||
duplicates_this_step = 0
|
duplicates_this_step = 0
|
||||||
for tc in tool_calls:
|
for tc in tool_calls:
|
||||||
|
# Si los args no se pudieron parsear (p.ej. truncados por max_tokens),
|
||||||
|
# NO ejecutamos la tool. En su lugar devolvemos un mensaje al modelo
|
||||||
|
# explicando el problema para que pueda ajustar el siguiente intento
|
||||||
|
# (dividir el contenido, acortar, etc.). Fingerprint incluye el hash
|
||||||
|
# del raw para distinguir fallos distintos.
|
||||||
|
if tc.get("parse_error"):
|
||||||
|
pe = tc["parse_error"]
|
||||||
|
conversation.append({
|
||||||
|
"role": "tool",
|
||||||
|
"tool_call_id": tc["id"],
|
||||||
|
"content": (
|
||||||
|
f"[ERROR] No se pudieron parsear los argumentos del tool "
|
||||||
|
f"'{tc['name']}'. Los argumentos llegaron truncados o mal "
|
||||||
|
f"formados (probablemente excediste el limite de max_tokens "
|
||||||
|
f"al serializar el tool_use). Recibido {len(pe['raw'])} chars. "
|
||||||
|
f"Error: {pe['message']}. "
|
||||||
|
f"Reintenta dividiendo el contenido en varios tool calls mas "
|
||||||
|
f"pequenos o reduciendo el tamano del argumento 'content'."
|
||||||
|
),
|
||||||
|
})
|
||||||
|
continue
|
||||||
fp_raw = f"{tc['name']}:{json.dumps(tc.get('parsed_arguments', {}), sort_keys=True)}"
|
fp_raw = f"{tc['name']}:{json.dumps(tc.get('parsed_arguments', {}), sort_keys=True)}"
|
||||||
fp = hashlib.md5(fp_raw.encode()).hexdigest()
|
fp = hashlib.md5(fp_raw.encode()).hexdigest()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user