Text Generation
Transformers
Safetensors
English
Portuguese
qwen2
mongodb
code
fim
autocomplete
synthetic-data
conversational
Eval Results (legacy)
text-generation-inference
Instructions to use esilva/KapiCoder-Mongo-Nano with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use esilva/KapiCoder-Mongo-Nano with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="esilva/KapiCoder-Mongo-Nano") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("esilva/KapiCoder-Mongo-Nano") model = AutoModelForCausalLM.from_pretrained("esilva/KapiCoder-Mongo-Nano", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use esilva/KapiCoder-Mongo-Nano with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "esilva/KapiCoder-Mongo-Nano" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "esilva/KapiCoder-Mongo-Nano", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/esilva/KapiCoder-Mongo-Nano
- SGLang
How to use esilva/KapiCoder-Mongo-Nano with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "esilva/KapiCoder-Mongo-Nano" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "esilva/KapiCoder-Mongo-Nano", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "esilva/KapiCoder-Mongo-Nano" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "esilva/KapiCoder-Mongo-Nano", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use esilva/KapiCoder-Mongo-Nano with Docker Model Runner:
docker model run hf.co/esilva/KapiCoder-Mongo-Nano
Download examples/example_fim.py from esilva/KapiCoder-Mongo-Nano: direct link, hf CLI and curl.
- Browser
- Download file 5.91 kB
-
https://huggingface.co/esilva/KapiCoder-Mongo-Nano/resolve/main/examples/example_fim.py
- Command line
-
hf download hf://esilva/KapiCoder-Mongo-Nano/examples/example_fim.py
-
curl -L -o example_fim.py https://huggingface.co/esilva/KapiCoder-Mongo-Nano/resolve/main/examples/example_fim.py
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 | |