File size: 6,506 Bytes
5ed8c75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run a small deterministic BlitzKode GGUF evaluation.



This is intentionally lightweight: it verifies practical coding behavior on a

small, repeatable prompt set and writes machine-readable results. It is not a

replacement for a benchmark such as HumanEval, MBPP, or SWE-bench.

"""

from __future__ import annotations

import argparse
import json
import os
import re
import time
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from typing import Any, cast

import llama_cpp

REPO_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_MODEL_PATH = REPO_ROOT / "blitzkode.gguf"
DEFAULT_OUTPUT_PATH = REPO_ROOT / "docs" / "evaluation_results.json"
STOP_TOKENS = ["<|im_end|>", "<|im_start|>user"]
SYSTEM_PROMPT = (
    "<|im_start|>system\n"
    "You are BlitzKode, an AI coding assistant created by Sajad. "
    "Write clean, efficient, and practical code. If you do not know something, say so."
    "<|im_end|>"
)


@dataclass(frozen=True)
class EvalCase:
    name: str
    prompt: str
    checks: list[Callable[[str], bool]]
    max_tokens: int = 180


def contains_all(*needles: str) -> Callable[[str], bool]:
    def _check(text: str) -> bool:
        lowered = text.lower()
        return all(needle.lower() in lowered for needle in needles)

    return _check


def regex(pattern: str) -> Callable[[str], bool]:
    compiled = re.compile(pattern, re.IGNORECASE | re.DOTALL)

    def _check(text: str) -> bool:
        return compiled.search(text) is not None

    return _check


def build_prompt(user_prompt: str) -> str:
    return "\n".join(
        [
            SYSTEM_PROMPT,
            f"<|im_start|>user\n{user_prompt}<|im_end|>",
            "<|im_start|>assistant\n",
        ]
    )


def eval_cases() -> list[EvalCase]:
    return [
        EvalCase(
            name="python_factorial",
            prompt="Write a Python function named factorial that handles 0, positive integers, and rejects negative input.",
            checks=[contains_all("def factorial", "return"), regex(r"raise\s+ValueError|if\s+n\s*<\s*0")],
        ),
        EvalCase(
            name="binary_search",
            prompt="Implement iterative binary search in Python. Return the index or -1.",
            checks=[contains_all("def binary_search", "mid", "-1"), regex(r"while\s+\w+\s*<=\s*\w+"), regex(r"//\s*2")],
        ),
        EvalCase(
            name="sql_top_users",
            prompt="Write SQL to return the top 5 users by order count from users and orders tables.",
            checks=[contains_all("select", "join", "group by", "order by"), regex(r"limit\s+5|top\s+5")],
        ),
        EvalCase(
            name="unknown_api_uncertainty",
            prompt="What is the exact signature of imaginary_blitz_api()? If you are not sure, say you do not know.",
            checks=[regex(r"do not know|don't know|not sure|not have enough|cannot verify")],
            max_tokens=96,
        ),
    ]


def load_model(model_path: Path, n_ctx: int, n_threads: int, n_batch: int, n_gpu_layers: int) -> llama_cpp.Llama:
    return llama_cpp.Llama(
        model_path=str(model_path),
        n_ctx=n_ctx,
        n_threads=n_threads,
        n_batch=n_batch,
        n_gpu_layers=n_gpu_layers,
        use_mmap=True,
        use_mlock=False,
        verbose=False,
        seed=42,
    )


def run_case(llm: llama_cpp.Llama, case: EvalCase) -> dict[str, Any]:
    started = time.perf_counter()
    raw = cast(
        dict[str, Any],
        llm(
            build_prompt(case.prompt),
            max_tokens=case.max_tokens,
            temperature=0.0,
            top_p=0.95,
            top_k=20,
            repeat_penalty=1.05,
            stop=STOP_TOKENS,
        ),
    )
    elapsed = time.perf_counter() - started
    text = str(raw["choices"][0]["text"]).strip()
    check_results = [check(text) for check in case.checks]
    return {
        "name": case.name,
        "passed": all(check_results),
        "checks_passed": sum(check_results),
        "checks_total": len(check_results),
        "latency_seconds": round(elapsed, 3),
        "prompt": case.prompt,
        "response": text,
    }


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--model", type=Path, default=Path(os.getenv("BLITZKODE_MODEL_PATH", DEFAULT_MODEL_PATH)))
    parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT_PATH)
    parser.add_argument("--ctx", type=int, default=int(os.getenv("BLITZKODE_N_CTX", "2048")))
    parser.add_argument("--threads", type=int, default=int(os.getenv("BLITZKODE_THREADS", str(max(1, min(8, os.cpu_count() or 1))))))
    parser.add_argument("--batch", type=int, default=int(os.getenv("BLITZKODE_BATCH", "256")))
    parser.add_argument("--gpu-layers", type=int, default=int(os.getenv("BLITZKODE_GPU_LAYERS", "0")))
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    model_path = args.model.resolve()
    if not model_path.exists():
        raise SystemExit(f"Model file not found: {model_path}")

    started = time.perf_counter()
    llm = load_model(model_path, args.ctx, args.threads, args.batch, args.gpu_layers)
    load_seconds = time.perf_counter() - started

    cases = eval_cases()
    results = [run_case(llm, case) for case in cases]
    passed = sum(1 for result in results if result["passed"])
    total = len(results)
    total_latency = sum(float(result["latency_seconds"]) for result in results)

    payload = {
        "model_path": str(model_path),
        "load_seconds": round(load_seconds, 3),
        "settings": {
            "ctx": args.ctx,
            "threads": args.threads,
            "batch": args.batch,
            "gpu_layers": args.gpu_layers,
        },
        "summary": {
            "passed": passed,
            "total": total,
            "pass_rate": round(passed / total, 3),
            "total_generation_seconds": round(total_latency, 3),
        },
        "results": results,
    }

    args.output.parent.mkdir(parents=True, exist_ok=True)
    args.output.write_text(json.dumps(payload, indent=2), encoding="utf-8")

    print(json.dumps(payload["summary"], indent=2))
    print(f"Wrote {args.output}")


if __name__ == "__main__":
    main()