Views
No views yet
all-MiniLM-L6-v2-code-search-512 is a lightweight, high-accuracy sentence-transformers fine-tuned specifically for semantic code search and code embeddings.90.3% Accuracy@1, significantly outperforming the base all-MiniLM-L6-v2 model while keeping the same 33M parameter footprint.1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer('isuruwijesiri/all-MiniLM-L6-v2-code-search-512')
4
5# Encode code
6code_embedding = model.encode("def hello_world():\n print('Hello, World!')")
7
8# Encode natural language description
9query_embedding = model.encode("function that prints hello world")
10
11# Calculate similarity
12from sklearn.metrics.pairwise import cosine_similarity
13similarity = cosine_similarity([query_embedding], [code_embedding])[0][0]
14print(f"Similarity: {similarity:.4f}")npm install @xenova/transformers1import {{ pipeline }} from '@xenova/transformers';
2
3// Load the model (auto-downloads and caches)
4const extractor = await pipeline('feature-extraction', 'isuruwijesiri/all-MiniLM-L6-v2-code-search-512', {
5 quantized: false // false = model.onnx (86MB), true = model_quantized.onnx (22MB)
6});
7
8// Generate embeddings
9const output = await extractor('def add(a, b): return a + b', {{
10 pooling: 'mean',
11 normalize: true
12}});
13
14// Use the embedding
15const embedding = Array.from(output.data);
16console.log(embedding); // [384] dimensional vector1from sentence_transformers import SentenceTransformer, util
2
3# Load model
4model = SentenceTransformer('isuruwijesiri/all-MiniLM-L6-v2-code-search-512')
5
6# Your code snippets
7code_snippets = [
8 "def calculate_sum(a, b):\n return a + b",
9 "def find_max(numbers):\n return max(numbers)",
10 "class User:\n def __init__(self, name):\n self.name = name"
11]
12
13# Natural language query
14query = "function to add two numbers"
15
16# Encode
17query_emb = model.encode(query, convert_to_tensor=True)
18code_embs = model.encode(code_snippets, convert_to_tensor=True)
19
20# Find most similar
21similarities = util.cos_sim(query_emb, code_embs)[0]
22best_match_idx = similarities.argmax().item()
23
24print(f"Best match: {code_snippets[best_match_idx]}")
25print(f"Similarity: {similarities[best_match_idx]:.4f}")1import {{ pipeline }} from '@xenova/transformers';
2
3const extractor = await pipeline('feature-extraction', 'isuruwijesiri/all-MiniLM-L6-v2-code-search-512', {
4 quantized: false // false = model.onnx (86MB), true = model_quantized.onnx (22MB)
5});
6
7// Your code snippets
8const codeSnippets = [
9 "def add(a, b): return a + b",
10 "def multiply(x, y): return x * y",
11 "class User: pass"
12];
13
14// Search query
15const query = "function to add two numbers";
16
17// Get embeddings
18const queryEmb = await extractor(query, {{ pooling: 'mean', normalize: true }});
19const codeEmbs = await Promise.all(
20 codeSnippets.map(code => extractor(code, {{ pooling: 'mean', normalize: true }}))
21);
22
23// Calculate similarities (dot product of normalized vectors = cosine similarity)
24const similarities = codeEmbs.map(codeEmb =>
25 Array.from(queryEmb.data).reduce((sum, val, i) =>
26 sum + val * codeEmb.data[i], 0
27 )
28);
29
30console.log(similarities); // [0.87, 0.23, 0.15] - first one is most similar!| Metric | Score |
|---|---|
| MRR@10 | 0.9259 |
| Accuracy@1 | 0.9030 |
| Accuracy@3 | 0.9440 |
| Accuracy@5 | 0.9540 |
| Accuracy@10 | 0.9705 |
| Recall@1 | 0.9030 |
| Recall@5 | 0.9540 |
| Recall@10 | 0.9705 |
| NDCG@10 | 0.9367 |
| MAP@100 | 0.9269 |
all-MiniLM-L6-v2 on code search tasks:| Metric | Base Model | Fine-tuned | Improvement |
|---|---|---|---|
| MRR@10 | 0.7759 | 0.9259 | +0.1500 (+19.3%) |
| Accuracy@1 | 0.7175 | 0.9030 | +0.1855 (+25.9%) |
| Accuracy@5 | 0.8555 | 0.9540 | +0.0985 (+11.5%) |
| Recall@10 | 0.8840 | 0.9705 | +0.0865 (+9.8%) |
| NDCG@10 | 0.8022 | 0.9367 | +0.1344 (+16.8%) |
1model.max_seq_length = 256 # Faster inference for short code
2model.max_seq_length = 512 # Default, best accuracy (recommended)1@misc{all_MiniLM_L6_v2_code_search_512,
2 author = {isuruwijesiri},
3 title = {all-MiniLM-L6-v2-code-search-512},
4 year = {2026},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/isuruwijesiri/all-MiniLM-L6-v2-code-search-512}
7}