Rickesh commited on
Commit
dcb62fb
·
verified ·
1 Parent(s): e0a924b

Upload folder using huggingface_hub

Browse files
tbench/__init__.py ADDED
File without changes
tbench/claude-code-setup.sh.j2 ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ apt-get update
4
+ apt-get install -y curl
5
+
6
+ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
7
+
8
+ source "$HOME/.nvm/nvm.sh"
9
+
10
+ nvm install 22
11
+ npm -v
12
+
13
+ npm install -g @anthropic-ai/claude-code@{{ version }}
tbench/local_claude_agent.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Terminal-Bench agent: Claude Code driven by a local Ollama model.
3
+
4
+ Terminal-Bench's stock ClaudeCodeAgent forwards only ANTHROPIC_API_KEY and
5
+ ANTHROPIC_MODEL, so it always talks to api.anthropic.com. Ollama >=0.32 serves an
6
+ Anthropic-compatible /v1/messages, so pointing ANTHROPIC_BASE_URL at it is enough
7
+ to run the whole benchmark against local GGUF weights.
8
+
9
+ Usage:
10
+ tb run --agent-import-path tbench.local_claude_agent:LocalClaudeCodeAgent \
11
+ --model ornith15-9b-claude-coder --dataset terminal-bench-core
12
+
13
+ Containers cannot reach a host-bound 127.0.0.1, so OLLAMA_ANTHROPIC_BASE_URL must
14
+ name an address routable from inside Docker (the bridge gateway, not localhost).
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import os
20
+
21
+ from pathlib import Path
22
+
23
+ from terminal_bench.agents.installed_agents.claude_code import (
24
+ claude_code_agent as _upstream_mod,
25
+ )
26
+ from terminal_bench.agents.installed_agents.claude_code.claude_code_agent import (
27
+ ClaudeCodeAgent,
28
+ )
29
+
30
+ # Terminal-Bench resolves the setup template via inspect.getfile(self.__class__),
31
+ # i.e. next to the *subclass*, not the class that owns the template. Keep a synced
32
+ # copy beside this module so subclassing does not break the install step, and
33
+ # re-sync on every import so a tb upgrade cannot leave us on a stale template.
34
+ _TEMPLATE = "claude-code-setup.sh.j2"
35
+ _upstream = Path(_upstream_mod.__file__).parent / _TEMPLATE
36
+ _local = Path(__file__).parent / _TEMPLATE
37
+ if _upstream.exists() and (
38
+ not _local.exists() or _local.read_bytes() != _upstream.read_bytes()
39
+ ):
40
+ _local.write_bytes(_upstream.read_bytes())
41
+
42
+ # Docker bridge gateway. Overridable for host-gateway or a remote Ollama.
43
+ DEFAULT_BASE_URL = "http://172.17.0.1:11435"
44
+
45
+
46
+ class LocalClaudeCodeAgent(ClaudeCodeAgent):
47
+ """Claude Code, but every request goes to a local Ollama model."""
48
+
49
+ @staticmethod
50
+ def name() -> str:
51
+ return "local-claude-code"
52
+
53
+ @property
54
+ def _env(self) -> dict[str, str]:
55
+ model = (self._model_name or os.environ.get("ANTHROPIC_MODEL", ""))
56
+ for prefix in ("anthropic/", "ollama/", "openai/"):
57
+ model = model.removeprefix(prefix)
58
+ if not model:
59
+ raise ValueError("pass --model <ollama model tag>")
60
+
61
+ base_url = os.environ.get("OLLAMA_ANTHROPIC_BASE_URL", DEFAULT_BASE_URL)
62
+ return {
63
+ "ANTHROPIC_BASE_URL": base_url,
64
+ # Ollama ignores the value but Claude Code refuses to start without one.
65
+ "ANTHROPIC_AUTH_TOKEN": os.environ.get("ANTHROPIC_AUTH_TOKEN", "ollama"),
66
+ "ANTHROPIC_API_KEY": os.environ.get("ANTHROPIC_API_KEY", "ollama"),
67
+ "ANTHROPIC_MODEL": model,
68
+ # Claude Code silently routes cheap side-tasks (titles, file triage) to a
69
+ # small model. Unset, those hit Anthropic-named models Ollama has never
70
+ # heard of and the run dies mid-task with confusing 404s.
71
+ "ANTHROPIC_SMALL_FAST_MODEL": model,
72
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": model,
73
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": model,
74
+ "ANTHROPIC_DEFAULT_OPUS_MODEL": model,
75
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
76
+ "DISABLE_TELEMETRY": "1",
77
+ "DISABLE_ERROR_REPORTING": "1",
78
+ "FORCE_AUTO_BACKGROUND_TASKS": "1",
79
+ "ENABLE_BACKGROUND_TASKS": "1",
80
+ }
tbench/ollama_bridge.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Expose the host's Ollama to Docker containers.
4
+
5
+ Ollama binds 127.0.0.1:11434, which is unreachable from a container's network
6
+ namespace. Terminal-Bench runs every task in a container, so Claude Code inside
7
+ it cannot see the model without a forwarder.
8
+
9
+ Binds the Docker bridge address ONLY (default 172.17.0.1), so this does not
10
+ expose Ollama on the LAN, and dies with the process. stdlib only -- no socat.
11
+
12
+ python3 tbench/ollama_bridge.py &
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import argparse
18
+ import socket
19
+ import socketserver
20
+ import threading
21
+
22
+
23
+ class Handler(socketserver.BaseRequestHandler):
24
+ upstream: tuple[str, int] = ("127.0.0.1", 11434)
25
+
26
+ def handle(self) -> None:
27
+ try:
28
+ up = socket.create_connection(self.upstream, timeout=30)
29
+ except OSError:
30
+ return
31
+ # Long-poll generations can idle for minutes; a read timeout here would
32
+ # sever a working request mid-stream.
33
+ up.settimeout(None)
34
+ self.request.settimeout(None)
35
+ with up:
36
+ a = threading.Thread(target=self._pipe, args=(self.request, up), daemon=True)
37
+ a.start()
38
+ self._pipe(up, self.request)
39
+ a.join()
40
+
41
+ @staticmethod
42
+ def _pipe(src: socket.socket, dst: socket.socket) -> None:
43
+ try:
44
+ while chunk := src.recv(65536):
45
+ dst.sendall(chunk)
46
+ except OSError:
47
+ pass
48
+ finally:
49
+ try:
50
+ dst.shutdown(socket.SHUT_WR)
51
+ except OSError:
52
+ pass
53
+
54
+
55
+ class Server(socketserver.ThreadingTCPServer):
56
+ allow_reuse_address = True
57
+ daemon_threads = True
58
+
59
+
60
+ def main() -> None:
61
+ p = argparse.ArgumentParser()
62
+ p.add_argument("--bind", default="172.17.0.1", help="docker bridge address")
63
+ p.add_argument("--port", type=int, default=11435)
64
+ p.add_argument("--to-host", default="127.0.0.1")
65
+ p.add_argument("--to-port", type=int, default=11434)
66
+ a = p.parse_args()
67
+
68
+ Handler.upstream = (a.to_host, a.to_port)
69
+ with Server((a.bind, a.port), Handler) as s:
70
+ print(f"bridge {a.bind}:{a.port} -> {a.to_host}:{a.to_port}", flush=True)
71
+ s.serve_forever()
72
+
73
+
74
+ if __name__ == "__main__":
75
+ main()
tbench/run_bench.sh ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Terminal-Bench baselines for local GGUF models driving Claude Code via Ollama.
3
+ #
4
+ # Prereq (needs your approval, run once per boot):
5
+ # socat TCP-LISTEN:11435,bind=172.17.0.1,fork,reuseaddr TCP:127.0.0.1:11434 &
6
+ # Docker containers cannot reach Ollama's 127.0.0.1 bind; this exposes it on the
7
+ # docker bridge only, not the LAN.
8
+ #
9
+ # Usage: ./tbench/run_bench.sh <ollama-model> [n_tasks] [n_concurrent]
10
+ set -euo pipefail
11
+
12
+ MODEL="${1:?usage: run_bench.sh <ollama-model> [n_tasks] [n_concurrent]}"
13
+ N_TASKS="${2:-10}"
14
+ N_CONC="${3:-2}"
15
+ # Pin the dataset: "head" resolved to an archive with no tasks/ dir, and an
16
+ # unpinned version is not comparable to the published leaderboard.
17
+ DATASET="${DATASET:-terminal-bench-core==0.1.1}"
18
+
19
+ REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
20
+ TB="${TB_BIN:-$REPO/.tb/bin/tb}"
21
+ BRIDGE="${OLLAMA_ANTHROPIC_BASE_URL:-http://172.17.0.1:11435}"
22
+
23
+ # Fail loudly here rather than let every task die inside its container with an
24
+ # opaque connection error 20 minutes in.
25
+ if ! curl -s --max-time 5 "${BRIDGE}/api/version" >/dev/null; then
26
+ echo "ERROR: Ollama not reachable at ${BRIDGE} (needed from inside Docker)." >&2
27
+ echo "Start the bridge: socat TCP-LISTEN:11435,bind=172.17.0.1,fork,reuseaddr TCP:127.0.0.1:11434 &" >&2
28
+ exit 1
29
+ fi
30
+ # Exact tag match. A prefix match would accept gemma4:31b when asked for
31
+ # gemma4:e4b and silently benchmark the wrong model. Capture first: `grep -q`
32
+ # exits early, SIGPIPEs ollama, and pipefail would report a false negative.
33
+ MODELS="$(ollama list | awk 'NR>1 {print $1}')"
34
+ if ! grep -qxF -- "$MODEL" <<<"$MODELS" && ! grep -qxF -- "${MODEL}:latest" <<<"$MODELS"; then
35
+ echo "ERROR: ollama has no model '${MODEL}'. Available:" >&2
36
+ echo "$MODELS" | sed 's/^/ /' >&2
37
+ exit 1
38
+ fi
39
+
40
+ export OLLAMA_ANTHROPIC_BASE_URL="$BRIDGE"
41
+ export ANTHROPIC_API_KEY=ollama
42
+ export ANTHROPIC_AUTH_TOKEN=ollama
43
+ export PYTHONPATH="$REPO:${PYTHONPATH:-}"
44
+
45
+ RUN_ID="tb_$(echo "$MODEL" | tr '/:' '__')_${N_TASKS}"
46
+ echo "[tbench] dataset=$DATASET"
47
+ echo "[tbench] model=$MODEL tasks=$N_TASKS concurrent=$N_CONC bridge=$BRIDGE"
48
+
49
+ "$TB" run \
50
+ --agent-import-path tbench.local_claude_agent:LocalClaudeCodeAgent \
51
+ --model "$MODEL" \
52
+ --dataset "$DATASET" \
53
+ --n-tasks "$N_TASKS" \
54
+ --n-concurrent "$N_CONC" \
55
+ --run-id "$RUN_ID" \
56
+ --no-livestream