Mistral FIM integration
This commit is contained in:
@@ -59,3 +59,11 @@ lsp-enable
|
|||||||
|
|
||||||
lsp-inlay-diagnostics-enable global
|
lsp-inlay-diagnostics-enable global
|
||||||
|
|
||||||
|
|
||||||
|
declare-option str fim_tmp "/tmp/kak-fim.buf"
|
||||||
|
|
||||||
|
define-command fim-complete -docstring "FIM-Completion am Cursor (Mistral)" %{
|
||||||
|
write -force %opt{fim_tmp}
|
||||||
|
execute-keys "!python3 %val{config}/mistral-fim.py %opt{fim_tmp} %val{cursor_line} %val{cursor_column}<ret>"
|
||||||
|
}
|
||||||
|
map global user f ': fim-complete<ret>' -docstring 'FIM complete'
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#!/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
|
||||||
Reference in New Issue
Block a user