embedding-strategies – skill Khác cho agent AI
Hướng dẫn chọn và tối ưu mô hình embedding cho ứng dụng tìm kiếm vector.
Cài skill embedding-strategies bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/embedding-strategies-5632e.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /embedding-strategies – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/embedding-strategies-5632e.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $embedding-strategies – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/embedding-strategies-5632e.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /embedding-strategies – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/embedding-strategies-5632e.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /embedding-strategies – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Embedding Strategies
Guide to selecting and optimizing embedding models for vector search applications.
Do not use this skill when
- The task is unrelated to embedding strategies
- You need a different domain or tool outside this scope
Instructions
- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
- If detailed examples are required, open
resources/implementation-playbook.md.
Use this skill when
- Choosing embedding models for RAG
- Optimizing chunking strategies
- Fine-tuning embeddings for domains
- Comparing embedding model performance
- Reducing embedding dimensions
- Handling multilingual content
Core Concepts
Model table is a historical candidate list, not a current leaderboard. Verify exact model-card token limits, dimensions, license, revision and query/document prefixes before use; count tokens with that model’s tokenizer. Approve private-document transfer/provider cost. Preserve original text and source offsets; stripping symbols can destroy code, monetary signs, equations and identifiers.
1. Embedding Model Comparison
| Model | Dimensions | Max Tokens | Best For |
|-------|------------|------------|----------|
| text-embedding-3-large | 3072 | 8191 | High accuracy |
| text-embedding-3-small | 1536 | 8191 | Cost-effective |
| voyage-2 | 1024 | 4000 | Code, legal |
| bge-large-en-v1.5 | 1024 | 512 | Open source |
| all-MiniLM-L6-v2 | 384 | 256 | Fast, lightweight |
| multilingual-e5-large | 1024 | 512 | Multi-language |
2. Embedding Pipeline
Document → Chunking → Preprocessing → Embedding Model → Vector
↓
[Overlap, Size] [Clean, Normalize] [API/Local]Templates
Template 1: OpenAI Embeddings
from openai import OpenAI
from typing import List
import numpy as np
client = OpenAI()
def get_embeddings(
texts: List[str],
model: str = "text-embedding-3-small",
dimensions: int = None
) -> List[List[float]]:
"""Get embeddings from OpenAI."""
# Handle batching for large lists
batch_size = 100
all_embeddings = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
kwargs = {"input": batch, "model": model}
if dimensions:
kwargs["dimensions"] = dimensions
response = client.embeddings.create(**kwargs)
embeddings = [item.embedding for item in response.data]
all_embeddings.extend(embeddings)
return all_embeddings
def get_embedding(text: str, **kwargs) -> List[float]:
"""Get single embedding."""
return get_embeddings([text], **kwargs)[0]
# Dimension reduction with OpenAI
def get_reduced_embedding(text: str, dimensions: int = 512) -> List[float]:
"""Get embedding with reduced dimensions (Matryoshka)."""
return get_embedding(
text,
model="text-embedding-3-small",
dimensions=dimensions
)Template 2: Local Embeddings with Sentence Transformers
from sentence_transformers import SentenceTransformer
from typing import List, Optional
import numpy as np
class LocalEmbedder:
"""Local embedding with sentence-transformers."""
def __init__(
self,
model_name: str = "BAAI/bge-large-en-v1.5",
device: str = "cpu",
query_prefix: str = ""
):
self.model = SentenceTransformer(model_name, device=device)
self.query_prefix = query_prefix # Choose from the selected model card.
def embed(
self,
texts: List[str],
normalize: bool = True,
show_progress: bool = False
) -> np.ndarray:
"""Embed texts with optional normalization."""
embeddings = self.model.encode(
texts,
normalize_embeddings=normalize,
show_progress_bar=show_progress,
convert_to_numpy=True
)
return embeddings
def embed_query(self, query: str) -> np.ndarray:
"""Embed a query with the explicitly configured model prefix."""
return self.embed([self.query_prefix + query])[0]
def embed_documents(self, documents: List[str]) -> np.ndarray:
"""Embed documents for indexing."""
return self.embed(documents)
# E5 model with instructions
class E5Embedder:
def __init__(self, model_name: str = "intfloat/multilingual-e5-large"):
self.model = SentenceTransformer(model_name)
def embed_query(self, query: str) -> np.ndarray:
return self.model.encode(f"query: {query}")
def embed_document(self, document: str) -> np.ndarray:
return self.model.encode(f"passage: {document}")Template 3: Chunking Strategies
from typing import List, Tuple
import re
def chunk_by_tokens(
text: str,
chunk_size: int = 512,
chunk_overlap: int = 50,
tokenizer=None
) -> List[str]:
"""Chunk text by token count."""
if not isinstance(chunk_size, int) or not isinstance(chunk_overlap, int):
raise ValueError("chunk size and overlap must be integers")
if chunk_size <= 0 or not 0 <= chunk_overlap < chunk_size:
raise ValueError("require chunk_size > 0 and 0 <= overlap < chunk_size")
if tokenizer is None:
import tiktoken
tokenizer = tiktoken.get_encoding("cl100k_base")
tokens = tokenizer.encode(text)
chunks = []
start = 0
while start < len(tokens):
end = start + chunk_size
chunk_tokens = tokens[start:end]
chunk_text = tokenizer.decode(chunk_tokens)
chunks.append(chunk_text)
if end >= len(tokens):
break
start = end - chunk_overlap
return chunks
def chunk_by_sentences(
text: str,
max_chunk_size: int = 1000,
min_chunk_size: int = 100
) -> List[str]:
"""Group sentences by approximate character length, not tokenizer length.
min_chunk_size is not enforced; oversized individual sentences require a
second token-bounded splitting pass before embedding. NLTK resources must
already be installed; no implicit network download is supplied here.
"""
import nltk
sentences = nltk.sent_tokenize(text)
chunks = []
current_chunk = []
current_size = 0
for sentence in sentences:
sentence_size = len(sentence)
if current_size + sentence_size > max_chunk_size and current_chunk:
chunks.append(" ".join(current_chunk))
current_chunk = []
current_size = 0
current_chunk.append(sentence)
current_size += sentence_size
if current_chunk:
chunks.append(" ".join(current_chunk))
return chunks
def chunk_by_semantic_sections(
text: str,
headers_pattern: str = r'^#{1,3}\s+.+$'
) -> List[Tuple[str, str]]:
"""Chunk markdown by headers, preserving hierarchy."""
lines = text.split('\n')
chunks = []
current_header = ""
current_content = []
for line in lines:
if re.match(headers_pattern, line, re.MULTILINE):
if current_content:
chunks.append((current_header, '\n'.join(current_content)))
current_header = line
current_content = []
else:
current_content.append(line)
if current_content:
chunks.append((current_header, '\n'.join(current_content)))
return chunks
def recursive_character_splitter(
text: str,
chunk_size: int = 1000,
chunk_overlap: int = 200,
separators: List[str] = None
) -> List[str]:
"""Bounded character chunks, preferring a separator inside each window."""
if chunk_size <= 0 or not 0 <= chunk_overlap < chunk_size:
raise ValueError("require chunk_size > 0 and 0 <= overlap < chunk_size")
separators = separators or ["\n\n", "\n", ". ", " "]
chunks = []
start = 0
while start < len(text):
end = min(start + chunk_size, len(text))
if end < len(text):
for separator in separators:
if not separator:
continue
boundary = text.rfind(separator, start + chunk_overlap + 1, end)
if boundary >= 0:
end = boundary + len(separator)
break
chunks.append(text[start:end])
if end == len(text):
break
start = end - chunk_overlap
return chunksTemplate 4: Domain-Specific Embedding Pipeline
class DomainEmbeddingPipeline:
"""Pipeline for domain-specific embeddings."""
def __init__(
self,
embedding_model: str = "text-embedding-3-small",
chunk_size: int = 512,
chunk_overlap: int = 50,
preprocessing_fn=None
):
self.embedding_model = embedding_model
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
self.preprocess = preprocessing_fn or self._default_preprocess
def _default_preprocess(self… (nội dung đầy đủ có trong gói tải về)
Skill liên quan
- umap-learn – Áp dụng UMAP-learn cho giảm chiều phi tuyến, nhúng 2D/3D, tiền xử lý phân cụm, UMAP có…
- emergency-card – Tạo thẻ tóm tắt thông tin y tế để truy cập nhanh trong trường hợp khẩn cấp. Dùng khi…
- employee-suggestion-hub – Sổ ghi góp ý: người gửi hoặc cờ ẩn danh, danh mục, số phiếu bình chọn, người review…
- energy-procurement – Kiến thức chuyên môn về mua sắm điện và khí đốt, tối ưu biểu giá, quản lý phí nhu cầu…
- enterprise-vpn-attack – Ma trận tấn công thiết bị truy cập từ xa / SSL VPN bên ngoài
- entropy-box – Entropy Box, bộ biên dịch tri thức cho embodied-AI: biến yêu cầu có giới hạn thành quy…
- environment-setup-guide – Hướng dẫn lập trình viên thiết lập môi trường phát triển với công cụ, dependencies và…
- eol-resistor-calculator – Tính toán và kiểm tra mạch điện trở cuối đường (EOL, SEOL, DEOL, TEOL) cho bảng báo động…