File size: 2,766 Bytes
befa31b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Write Modelfile as UTF-8 without BOM (PS 5.1 Set-Content corrupts CJK).

Single source of truth for the ollama model definition.

ollama 0.40 has no `DESCRIPTION` Modelfile command (only: from, license,
template, system, adapter, draft, renderer, parser, parameter, message,
requires, capability). The manifest's only free-text slot is the
`ollama.image.license` layer, so --license-mode decides what goes in it:

  apache : license text only          -> Modelfile
  card   : full model card            -> Modelfile.card

Usage:
    py -3 scripts/write_modelfile.py [--license-mode apache|card]
"""
import argparse
import sys
from pathlib import Path

ROOT = Path(r"E:\AI edge\bio-enhance")
sys.path.insert(0, str(ROOT / "scripts"))
from prompt import SYSTEM_PROMPT  # noqa: E402

Q = '"' * 3

# Preferred GGUF (round3 / r3). It may be a symlink into a detached volume,
# so fall back to the byte-identical blob in the ollama blob store.
CANDIDATES = [
    ROOT / "output" / "bio-expert-q8-r3.gguf",
    Path(r"D:\ollama_data\blobs\sha256-569155cf3edc5c144201fb78352380dfa9a6b3904c4609bb48ade1a16c7920bd"),
]

APACHE = "Apache-2.0"


def pick_gguf():
    for c in CANDIDATES:
        try:
            if c.exists() and c.stat().st_size > 1_000_000_000:
                with open(c, "rb") as fh:
                    if fh.read(4) == b"GGUF":
                        return c
        except OSError:
            continue
    raise SystemExit("no usable GGUF found in: "
                     + ", ".join(str(c) for c in CANDIDATES))


def build(gguf, license_text, out_path):
    card = (ROOT / "model_card.md").read_text(encoding="utf-8").strip()
    mf = "\n".join([
        "FROM " + str(gguf),
        "",
        "SYSTEM " + Q + SYSTEM_PROMPT + Q,
        "",
        "LICENSE " + Q + license_text + Q,
        "",
        "PARAMETER temperature 0.7",
        "PARAMETER top_p 0.9",
        "PARAMETER num_ctx 32768",
        "PARAMETER repeat_penalty 1.1",
        "",
    ])
    out_path.write_text(mf, encoding="utf-8")   # no BOM
    raw = out_path.read_bytes()
    print(f"wrote {out_path}")
    print("  bytes:", len(raw), " BOM:", raw[:3] == b"\xef\xbb\xbf")
    print("  FROM:", gguf)
    print("  license chars:", len(license_text))
    print("  CJK ok:", "合曜生物" in raw.decode("utf-8"))
    return card


if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--license-mode", choices=["apache", "card"], default="apache")
    args = ap.parse_args()

    gguf = pick_gguf()
    card = (ROOT / "model_card.md").read_text(encoding="utf-8").strip()

    if args.license_mode == "card":
        build(gguf, card, ROOT / "Modelfile.card")
    else:
        build(gguf, APACHE, ROOT / "Modelfile")