Spaces:
Running
Running
File size: 13,800 Bytes
a622ede cdf6171 a622ede 8dd9efe a622ede cdf6171 8dd9efe cdf6171 8dd9efe cdf6171 8dd9efe cdf6171 a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe 06ea0aa 3d5cebe 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe cdf6171 a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede 8dd9efe a622ede | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | from __future__ import annotations
import argparse
import asyncio
import inspect
import json
import os
import sys
import time
from typing import Any, Callable
from .constants import (
DEFAULT_MAX_CALLS,
DEFAULT_MONTY_MAX_ALLOCATIONS,
DEFAULT_MONTY_MAX_MEMORY,
DEFAULT_MONTY_MAX_RECURSION_DEPTH,
DEFAULT_TIMEOUT_SEC,
INTERNAL_STRICT_MODE,
MAX_CALLS_LIMIT,
)
from .runtime_context import build_runtime_helper_environment
from .validation import (
_coerce_jsonish_python_literals,
_compact_result_metadata,
_summarize_limit_hit,
_truncate_result_payload,
_validate_generated_code,
_wrap_raw_result,
)
class MontyExecutionError(RuntimeError):
def __init__(self, message: str, api_calls: int, trace: list[dict[str, Any]]):
super().__init__(message)
self.api_calls = api_calls
self.trace = trace
def _query_debug_enabled() -> bool:
value = os.environ.get("MONTY_DEBUG_QUERY", "")
return value.strip().lower() in {"1", "true", "yes", "on"}
def _execution_debug_enabled() -> bool:
value = os.environ.get("MONTY_DEBUG_EXECUTION", "")
if value.strip().lower() in {"1", "true", "yes", "on"}:
return True
return _query_debug_enabled()
def _debug_log(*parts: Any) -> None:
if not _execution_debug_enabled():
return
print("[monty-debug]", *parts, file=sys.stderr)
sys.stderr.flush()
def _log_generated_query(
*, query: str | None, code: str, max_calls: int | None, timeout_sec: int | None
) -> None:
if not _query_debug_enabled():
return
if query:
print("[monty-debug] query:", file=sys.stderr)
print(query, file=sys.stderr)
print("[monty-debug] max_calls:", max_calls, file=sys.stderr)
print("[monty-debug] timeout_sec:", timeout_sec, file=sys.stderr)
print("[monty-debug] code:", file=sys.stderr)
print(code, file=sys.stderr)
sys.stderr.flush()
def _introspect_helper_signatures() -> dict[str, set[str]]:
env = build_runtime_helper_environment(
max_calls=DEFAULT_MAX_CALLS,
strict_mode=INTERNAL_STRICT_MODE,
timeout_sec=DEFAULT_TIMEOUT_SEC,
)
signatures = {
name: {
parameter.name for parameter in inspect.signature(fn).parameters.values()
}
for name, fn in env.helper_functions.items()
}
return signatures
async def _run_with_monty(
*,
code: str,
query: str | None,
max_calls: int,
strict_mode: bool,
timeout_sec: int,
) -> dict[str, Any]:
_debug_log(
"run_monty:start",
f"max_calls={max_calls}",
f"timeout_sec={timeout_sec}",
f"strict_mode={strict_mode}",
)
try:
import pydantic_monty
except Exception as e:
raise RuntimeError(
"pydantic_monty is not installed. Install with `uv pip install pydantic-monty`."
) from e
env = build_runtime_helper_environment(
max_calls=max_calls,
strict_mode=strict_mode,
timeout_sec=timeout_sec,
)
m = pydantic_monty.Monty(
code,
inputs=["query", "max_calls"],
script_name="monty_agent.py",
type_check=False,
)
def _collecting_wrapper(
helper_name: str, fn: Callable[..., Any]
) -> Callable[..., Any]:
async def wrapped(*args: Any, **kwargs: Any) -> Any:
started = time.perf_counter()
_debug_log(
"helper:start",
helper_name,
f"args={len(args)}",
f"kwargs={sorted(kwargs)}",
)
result = await fn(*args, **kwargs)
summary = _summarize_limit_hit(helper_name, result)
if summary is not None and len(env.limit_summaries) < 20:
env.limit_summaries.append(summary)
elapsed_ms = round((time.perf_counter() - started) * 1000, 2)
_debug_log(
"helper:end",
helper_name,
f"elapsed_ms={elapsed_ms}",
f"api_calls={env.call_count['n']}",
)
return result
return wrapped
limits: pydantic_monty.ResourceLimits = {
"max_duration_secs": float(timeout_sec),
"max_memory": DEFAULT_MONTY_MAX_MEMORY,
"max_allocations": DEFAULT_MONTY_MAX_ALLOCATIONS,
"max_recursion_depth": DEFAULT_MONTY_MAX_RECURSION_DEPTH,
}
try:
_debug_log("run_monty:invoke")
result = await pydantic_monty.run_monty_async(
m,
inputs={"query": query or "", "max_calls": max_calls},
external_functions={
name: _collecting_wrapper(name, fn)
for name, fn in env.helper_functions.items()
},
limits=limits,
)
_debug_log("run_monty:return", f"api_calls={env.call_count['n']}")
except Exception as e:
_debug_log("run_monty:error", type(e).__name__, str(e))
raise MontyExecutionError(str(e), env.call_count["n"], env.trace) from e
if env.call_count["n"] == 0:
if env.internal_helper_used["used"]:
return {
"output": _truncate_result_payload(result),
"api_calls": env.call_count["n"],
"trace": env.trace,
"limit_summaries": env.limit_summaries,
}
if isinstance(result, dict) and result.get("ok") is True:
meta = result.get("meta") if isinstance(result.get("meta"), dict) else {}
source = meta.get("source")
if isinstance(source, str) and source.startswith("internal://"):
return {
"output": _truncate_result_payload(result),
"api_calls": env.call_count["n"],
"trace": env.trace,
"limit_summaries": env.limit_summaries,
}
latest_helper_error = env.latest_helper_error_box.get("value")
if latest_helper_error is not None:
return {
"output": _truncate_result_payload(latest_helper_error),
"api_calls": env.call_count["n"],
"trace": env.trace,
"limit_summaries": env.limit_summaries,
}
if (
isinstance(result, dict)
and result.get("ok") is False
and isinstance(result.get("error"), str)
):
return {
"output": _truncate_result_payload(result),
"api_calls": env.call_count["n"],
"trace": env.trace,
"limit_summaries": env.limit_summaries,
}
raise MontyExecutionError(
"Code completed without calling any external API function",
env.call_count["n"],
env.trace,
)
if not any(step.get("ok") is True for step in env.trace):
if (
isinstance(result, dict)
and result.get("ok") is False
and isinstance(result.get("error"), str)
):
return {
"output": _truncate_result_payload(result),
"api_calls": env.call_count["n"],
"trace": env.trace,
"limit_summaries": env.limit_summaries,
}
raise MontyExecutionError(
"Code completed without a successful API call; refusing non-live fallback result",
env.call_count["n"],
env.trace,
)
return {
"output": _truncate_result_payload(result),
"api_calls": env.call_count["n"],
"trace": env.trace,
"limit_summaries": env.limit_summaries,
}
def _prepare_query_inputs(
*,
query: str | None,
code: str,
max_calls: int | None,
timeout_sec: int | None,
) -> tuple[str, str, int, int]:
if not code or not code.strip():
raise ValueError("code is required")
normalized_query = str(query or "").strip()
resolved_max_calls = DEFAULT_MAX_CALLS if max_calls is None else max_calls
resolved_timeout_sec = DEFAULT_TIMEOUT_SEC if timeout_sec is None else timeout_sec
normalized_max_calls = max(1, min(int(resolved_max_calls), MAX_CALLS_LIMIT))
normalized_timeout_sec = int(resolved_timeout_sec)
normalized_code = _coerce_jsonish_python_literals(code.strip())
_validate_generated_code(normalized_code)
return normalized_query, normalized_code, normalized_max_calls, normalized_timeout_sec
async def _execute_query(
*,
query: str | None,
code: str,
max_calls: int | None,
timeout_sec: int | None,
) -> dict[str, Any]:
_debug_log("execute_query:start")
prepared_query, prepared_code, prepared_max_calls, prepared_timeout = (
_prepare_query_inputs(
query=query,
code=code,
max_calls=max_calls,
timeout_sec=timeout_sec,
)
)
_debug_log(
"execute_query:prepared",
f"query_len={len(prepared_query)}",
f"code_len={len(prepared_code)}",
f"max_calls={prepared_max_calls}",
f"timeout_sec={prepared_timeout}",
)
_log_generated_query(
query=prepared_query,
code=prepared_code,
max_calls=prepared_max_calls,
timeout_sec=prepared_timeout,
)
return await _run_with_monty(
code=prepared_code,
query=prepared_query,
max_calls=prepared_max_calls,
strict_mode=INTERNAL_STRICT_MODE,
timeout_sec=prepared_timeout,
)
async def hf_hub_query(
code: str,
query: str | None = None,
max_calls: int | None = DEFAULT_MAX_CALLS,
timeout_sec: int | None = DEFAULT_TIMEOUT_SEC,
) -> dict[str, Any]:
"""Use natural-language queries to explore the Hugging Face Hub.
Best for read-only Hub discovery, lookup, ranking, and relationship questions
across users, organizations, repositories, activity, followers, likes,
discussions, and collections.
"""
try:
run = await _execute_query(
query=query,
code=code,
max_calls=max_calls,
timeout_sec=timeout_sec,
)
return {
"ok": True,
"data": _compact_result_metadata(run["output"]),
"error": None,
"api_calls": run["api_calls"],
}
except MontyExecutionError as e:
return {
"ok": False,
"data": None,
"error": str(e),
"api_calls": e.api_calls,
}
except Exception as e:
return {
"ok": False,
"data": None,
"error": str(e),
"api_calls": 0,
}
async def hf_hub_query_raw(
code: str,
query: str | None = None,
max_calls: int | None = DEFAULT_MAX_CALLS,
timeout_sec: int | None = DEFAULT_TIMEOUT_SEC,
) -> Any:
"""Use natural-language queries to explore the Hugging Face Hub in raw mode.
Best for read-only Hub discovery, lookup, ranking, and relationship
questions when the caller wants a runtime-owned raw envelope:
``result`` contains the generated script's final `result` value and ``meta`` contains
execution details such as timing, call counts, and limit summaries.
"""
started = time.perf_counter()
try:
run = await _execute_query(
query=query,
code=code,
max_calls=max_calls,
timeout_sec=timeout_sec,
)
elapsed_ms = int((time.perf_counter() - started) * 1000)
return _wrap_raw_result(
_compact_result_metadata(run["output"]),
ok=True,
api_calls=run["api_calls"],
elapsed_ms=elapsed_ms,
limit_summaries=run.get("limit_summaries"),
)
except MontyExecutionError as e:
elapsed_ms = int((time.perf_counter() - started) * 1000)
return _wrap_raw_result(
None,
ok=False,
api_calls=e.api_calls,
elapsed_ms=elapsed_ms,
error=str(e),
)
except Exception as e:
elapsed_ms = int((time.perf_counter() - started) * 1000)
return _wrap_raw_result(
None,
ok=False,
api_calls=0,
elapsed_ms=elapsed_ms,
error=str(e),
)
def _arg_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(description="Monty-backed API chaining tool (v3)")
p.add_argument("--query", default=None, help="Optional natural language query/context")
p.add_argument("--code", default=None, help="Inline Monty code to execute")
p.add_argument(
"--code-file", default=None, help="Path to .py file with Monty code to execute"
)
p.add_argument(
"--max-calls",
type=int,
default=DEFAULT_MAX_CALLS,
help="Max external API/helper calls",
)
p.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT_SEC)
return p
def main() -> int:
args = _arg_parser().parse_args()
code = args.code
if args.code_file:
with open(args.code_file, "r", encoding="utf-8") as f:
code = f.read()
if not code:
print(
json.dumps(
{"ok": False, "error": "Either --code or --code-file is required"},
ensure_ascii=False,
)
)
return 1
try:
out = asyncio.run(
hf_hub_query(
code=code,
query=args.query,
max_calls=args.max_calls,
timeout_sec=args.timeout,
)
)
print(json.dumps(out, ensure_ascii=False))
return 0 if out.get("ok") else 1
except Exception as e:
print(json.dumps({"ok": False, "error": str(e)}, ensure_ascii=False))
return 1
|