many improvements

This commit is contained in:
Luxferre
2026-08-15 08:27:24 +03:00
parent 398754dabb
commit e6acb066ca
9 changed files with 283 additions and 22 deletions
+36 -2
View File
@@ -35,6 +35,21 @@ def get_cfg(path="model.cfg"):
if not d.get("api_key") and "OPENAI_API_KEY" in os.environ: d["api_key"] = os.environ["OPENAI_API_KEY"]
return d
def set_cfg(path, k, v):
lines, found = [], False
if os.path.exists(path):
for ln in open(path, encoding="utf-8"):
s = ln.strip()
if not s.startswith("#") and "=" in s and s.split("=", 1)[0].strip() == k:
lines.append(f"{k}={v}\n")
found = True
else:
lines.append(ln)
if not found:
if lines and not lines[-1].endswith("\n"): lines[-1] += "\n"
lines.append(f"{k}={v}\n")
with open(path, "w", encoding="utf-8") as f: f.writelines(lines)
def num(cfg, k, d):
try: return type(d)(cfg.get(k, d))
except (TypeError, ValueError): return d
@@ -99,6 +114,11 @@ def llm(cfg, msgs, tools):
if k and k != "-": h["Authorization"] = "Bearer " + k
st = cfg.get("stream", "true").lower() in ("true", "1", "yes")
p = {"model": cfg["model"], "temperature": float(cfg.get("temperature", 0.7)), "messages": msgs, "stream": st}
for k, v in cfg.items():
if k in ("endpoint", "api_key", "timeout", "shell_timeout", "max_al_iterations", "color"): continue
try: p[k] = json.loads(v)
except Exception: p[k] = v
p["messages"], p["stream"] = msgs, st
if tools: p["tools"] = tools
pend = c("...requesting...", 1, 2)
for i, dly in enumerate(fib + [0]):
@@ -331,10 +351,24 @@ def main():
msgs = nm; autosave(msgs)
print(c(f"[compacted to {len(msgs)} messages]", 32)); print(c("--- summary ---", 33) + "\n" + c(sm, 2))
continue
elif u.startswith("/cfg"):
parts = u.split(None, 2)
if len(parts) == 2:
k = parts[1]
if k in cfg: print(c(f"{k}={cfg[k]}", 32))
else: print(c(f"{k} not set", 31))
elif len(parts) >= 3:
k, v = parts[1], parts[2]
set_cfg("model.cfg", k, v)
cfg = get_cfg("model.cfg")
_COL = col(cfg)
print(c(f"[config updated: {k}={v}]", 32))
else: print(c("Usage: /cfg <param> [val]", 31))
continue
elif u == "/help":
print(c("Bantam commands:", 1, 36))
for k, v in [("/quit", "exit"), ("/clear", "reset to system prompt"), ("/save", "save session"), ("/list", "list sessions"), ("/load <id>", "load session"), ("/compact", "compact context"), ("/help", "show help")]:
print(c(f" {k:<12}", 1, 32) + v)
for k, v in [("/quit", "exit"), ("/clear", "reset to system prompt"), ("/save", "save session"), ("/list", "list sessions"), ("/load <id>", "load session"), ("/compact", "compact context"), ("/cfg <k> [v]", "get/set config"), ("/help", "show help")]:
print(c(f" {k:<15}", 1, 32) + v)
continue
msgs.append({"role": "user", "content": u})
AL(cfg, msgs, sp); autosave(msgs)