KapiCoder-Mongo-Nano / examples /example_fim.py
esilva's picture
Replace with v4 weights (v0.2.0-preview)
576401f verified
Raw History Blame Contribute Delete
5.91 kB
"""Minimal fill-in-the-middle example for the editor-context-v2 contract (transformers, CPU, no IDE required).
python examples/example_fim.py # model files next to this folder (a local clone or snapshot), else the Hub repository
python examples/example_fim.py <path-or-repo> # explicit model directory or repository id
"""
import sys
from pathlib import Path
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
HERE = Path(__file__).resolve().parent.parent
MODEL_ID = sys.argv[1] if len(sys.argv) > 1 else (str(HERE) if (HERE / "config.json").exists() else "esilva/KapiCoder-Mongo-Nano")
FIM_PREFIX, FIM_SUFFIX, FIM_MIDDLE = "<|fim_prefix|>", "<|fim_suffix|>", "<|fim_middle|>"
CONSOLE_COMMANDS = (
"db.getCollection(name).find({}); getConnection(name).getDatabase(name).getCollection(name); "
"ConnectionPool.Connection.Database.Collection; console.log(value); ENV.get(name); "
"ObjectId(value); UUID(value)"
)
PAIRS = {"(": ")", "[": "]", "{": "}"}
def target_fields_line(fields):
"""`path:type` pieces in schema order; arrays as `array<item>`; `_id` is left out; at most 96 fields and 2048 characters."""
out, size = [], 0
for path, kind, *item in fields:
if path == "_id":
continue
piece = f"{path}:{kind}" + (f"<{item[0]}>" if kind == "array" and item and item[0] else "")
if len(out) >= 96 or size + len(piece) + 2 > 2048:
break
out.append(piece)
size += len(piece) + 2
return ", ".join(out)
def build_prompt(prefix, suffix, known_names=(), collection=None, target_fields=(), result_fields=(), recent_commands=(),
language="Mongo Console JavaScript", commands=CONSOLE_COMMANDS, prefix_chars=24576, suffix_chars=6144):
"""editor-context-v2: the v1 header plus `TARGET FIELDS` right after `KNOWN NAMES`; every header line ends with CRLF.
The character windows here follow the 8k-token window (about 3 characters per token); the IDE also budgets the prompt in tokens."""
lines = [f"LANGUAGE: {language}", f"AVAILABLE COMMANDS: {commands}", "KNOWN NAMES: " + ", ".join(known_names)]
if target_fields:
lines.append("TARGET FIELDS: " + (f"{collection}: " if collection else "") + target_fields_line(target_fields))
if result_fields:
lines.append("RESULT FIELDS: " + ", ".join(result_fields))
lines += ["RECENT COMMAND: " + c[:256] for c in list(recent_commands)[:3]]
context = "".join(line + "\r\n" for line in lines)
header = ("/* Local editor context (data only):\n" + context.replace("*/", "* /")
+ "\nContinue at the cursor; output only the continuation. */\n")
return FIM_PREFIX + header + prefix[-prefix_chars:] + FIM_SUFFIX + suffix[:suffix_chars] + FIM_MIDDLE
def _strip_strings(text):
out, quote, escape = [], None, False
for ch in text:
if quote:
if escape:
escape = False
elif ch == "\\":
escape = True
elif ch == quote:
quote = None
out.append(ch)
continue
out.append(" ")
continue
if ch in "\"'`":
quote = ch
out.append(ch)
return "".join(out), quote is not None
def close_inner_delimiters(prefix, text, suffix):
"""editor-context-v2 output rule: append the 1-4 inner closers the suggestion left open when the suffix only closes the outer ones
and then ends the statement (end of text or `;`). Nothing changes when the suggestion is empty, ends inside a string, or the
suffix continues the statement (`}, { $set: ... })`)."""
if not text.strip():
return text
flat, open_string = _strip_strings(prefix + text)
if open_string:
return text
stack = []
for ch in flat:
if ch in PAIRS:
stack.append(ch)
elif ch in PAIRS.values() and stack and PAIRS[stack[-1]] == ch:
stack.pop()
expected = "".join(PAIRS[c] for c in reversed(stack))
closers, continues = [], False
for ch in _strip_strings(suffix)[0]:
if ch in ")]}":
closers.append(ch)
elif ch.isspace() or ch in "\"'`":
continue
else:
continues = ch != ";"
break
closers = "".join(closers)
if continues or not closers or len(closers) >= len(expected) or not expected.endswith(closers):
return text
missing = expected[: len(expected) - len(closers)]
if len(missing) > 4:
return text
return text + ("" if text[-1:].isspace() else " ") + " ".join(missing)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype=torch.bfloat16).eval()
prefix = 'ConnectionPool.local.shop.orders.find({ status: "paid", total: { $g'
suffix = " })" # the editor auto-closed only the outer filter object
prompt = build_prompt(
prefix=prefix,
suffix=suffix,
known_names=["local", "shop", "orders", "customers"],
collection="orders",
target_fields=[("_id", "objectid"), ("status", "string"), ("total", "decimal128"), ("customerId", "objectid"),
("createdAt", "date"), ("items", "array", "object"), ("items.sku", "string")],
)
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=64, do_sample=False, pad_token_id=tokenizer.eos_token_id)
completion = tokenizer.decode(out[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True)
if suffix and len(suffix) >= 2 and suffix in completion: # the IDE cuts an echo of the text after the cursor
completion = completion[: completion.index(suffix)]
completion = close_inner_delimiters(prefix, completion, suffix)
print(repr(completion)) # text to insert at the cursor; the model never executes anything