Datasets:
Upload rlm_ask.py with huggingface_hub
Browse files- rlm_ask.py +88 -0
rlm_ask.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
rlm-ask: answer a question about a file too large for a model's context window,
|
| 4 |
+
using a small local model recursively.
|
| 5 |
+
|
| 6 |
+
Designed to be called by Claude Code (or any agent) as a plain shell tool, rather
|
| 7 |
+
than to sit behind Claude Code's model endpoint. The agent keeps its own loop and
|
| 8 |
+
tool calling; this is a subroutine it hands bulk reading to.
|
| 9 |
+
|
| 10 |
+
python rlm_ask.py --file huge.log --query "which error appears most often?"
|
| 11 |
+
cat huge.log | python rlm_ask.py --query "..." --model gemma4:e4b
|
| 12 |
+
|
| 13 |
+
When to use it, from measurements in this repo:
|
| 14 |
+
- Context EXCEEDS the model's window -> use this. A direct call silently
|
| 15 |
+
truncates and answers from the fragment it saw.
|
| 16 |
+
- Context FITS the window -> do NOT use this. Recursion adds error; a plain
|
| 17 |
+
call scored better (OOLONG-131k: 0.428 direct vs 0.269 recursive).
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
import argparse
|
| 23 |
+
import sys
|
| 24 |
+
import time
|
| 25 |
+
from pathlib import Path
|
| 26 |
+
|
| 27 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 28 |
+
|
| 29 |
+
from rlm_haiku.rlm_repl import RLM_REPL # noqa: E402
|
| 30 |
+
from rlm_haiku.utils.llm import Usage # noqa: E402
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def main() -> int:
|
| 34 |
+
p = argparse.ArgumentParser(description="Ask a question about an oversized file.")
|
| 35 |
+
p.add_argument("--query", "-q", required=True)
|
| 36 |
+
p.add_argument("--file", "-f", help="input file; omit to read stdin")
|
| 37 |
+
p.add_argument("--model", "-m", default="gemma4:e4b", help="ollama model tag")
|
| 38 |
+
p.add_argument("--sub-model", default=None, help="model for recursive calls")
|
| 39 |
+
p.add_argument("--backend", default="ollama", choices=("ollama", "claude"))
|
| 40 |
+
p.add_argument("--max-iters", type=int, default=10)
|
| 41 |
+
p.add_argument("--workers", type=int, default=4)
|
| 42 |
+
p.add_argument("--base-url", default="http://127.0.0.1:11434")
|
| 43 |
+
p.add_argument("--quiet", action="store_true", help="print only the answer")
|
| 44 |
+
a = p.parse_args()
|
| 45 |
+
|
| 46 |
+
if a.file:
|
| 47 |
+
context = Path(a.file).read_text(errors="replace")
|
| 48 |
+
elif not sys.stdin.isatty():
|
| 49 |
+
context = sys.stdin.read()
|
| 50 |
+
else:
|
| 51 |
+
p.error("provide --file or pipe input on stdin")
|
| 52 |
+
|
| 53 |
+
if not context.strip():
|
| 54 |
+
print("error: empty context", file=sys.stderr)
|
| 55 |
+
return 2
|
| 56 |
+
|
| 57 |
+
started = time.time()
|
| 58 |
+
usage = Usage()
|
| 59 |
+
rlm = RLM_REPL(
|
| 60 |
+
model=a.model,
|
| 61 |
+
recursive_model=a.sub_model or a.model,
|
| 62 |
+
backend=a.backend,
|
| 63 |
+
max_iterations=a.max_iters,
|
| 64 |
+
max_workers=a.workers,
|
| 65 |
+
usage=usage,
|
| 66 |
+
# A small root model will otherwise answer from an empty REPL on some
|
| 67 |
+
# inputs -- one call, never having looked at the context.
|
| 68 |
+
require_repl=True,
|
| 69 |
+
)
|
| 70 |
+
try:
|
| 71 |
+
answer = rlm.completion(context=context, query=a.query)
|
| 72 |
+
except Exception as e: # noqa: BLE001
|
| 73 |
+
print(f"error: {type(e).__name__}: {e}", file=sys.stderr)
|
| 74 |
+
return 1
|
| 75 |
+
|
| 76 |
+
print(answer.strip())
|
| 77 |
+
if not a.quiet:
|
| 78 |
+
u = usage.as_dict()
|
| 79 |
+
print(
|
| 80 |
+
f"\n[rlm-ask] {len(context):,} chars | {u['calls']} calls | "
|
| 81 |
+
f"{u['errors']} errors | {time.time() - started:.0f}s | model={a.model}",
|
| 82 |
+
file=sys.stderr,
|
| 83 |
+
)
|
| 84 |
+
return 0
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
raise SystemExit(main())
|