30 lines
1.1 KiB
Python
Executable File
30 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import json, os, sys, urllib.request
|
|
|
|
buf, line, col = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
|
|
CTX_BEFORE, CTX_AFTER = 120, 60 # Kontext-Budget in Zeilen
|
|
|
|
lines = open(buf, encoding="utf-8").read().split("\n")
|
|
before, cur, after = lines[:line-1], (lines[line-1] if line-1 < len(lines) else ""), lines[line:]
|
|
|
|
prefix = "\n".join(before + [cur[:col-1]])
|
|
suffix = "\n".join([cur[col-1:]] + after)
|
|
prefix = "\n".join(prefix.split("\n")[-CTX_BEFORE:]) # nach hinten kappen
|
|
suffix = "\n".join(suffix.split("\n")[:CTX_AFTER])
|
|
|
|
body = json.dumps({
|
|
"model": "codestral-latest",
|
|
"prompt": prefix, "suffix": suffix,
|
|
"max_tokens": 96, "temperature": 0.2,
|
|
}).encode()
|
|
|
|
req = urllib.request.Request(
|
|
"https://api.mistral.ai/v1/fim/completions", data=body,
|
|
headers={"Authorization": "Bearer " + os.environ["MISTRAL_API_KEY"],
|
|
"Content-Type": "application/json"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=20) as r:
|
|
sys.stdout.write(json.load(r)["choices"][0]["message"]["content"])
|
|
except Exception:
|
|
pass # fail silent → leeres Insert
|