"""Convert text to embedding vectors.""" from functools import cache from sentence_transformers import SentenceTransformer from rag_chatbot.shared.settings import settings from .config import EMBEDDING_MODEL_NAME # Long inputs are cut at this length to bound memory use. Data Card chunks are # far shorter; only unusually long descriptions reach it. MAX_SEQUENCE_LENGTH = settings.embedding.max_sequence_length # Instruction-tuned embedding models (such as Qwen3-Embedding) encode queries # with a task instruction and documents without one. Models without a "query" # prompt encode both the same way. QUERY_INSTRUCTION = ( "Instruct: Given a question about AI models, retrieve Data Card sections " "of models that answer it\nQuery:" ) @cache def _get_model() -> SentenceTransformer: print(f"Loading embedding model: {EMBEDDING_MODEL_NAME}") model = SentenceTransformer(EMBEDDING_MODEL_NAME, device=settings.embedding.device or None) if model.device.type == "cpu": # The weights load as bfloat16, which is very slow on CPUs without bf16 hardware. model.float() if model.max_seq_length is None or model.max_seq_length > MAX_SEQUENCE_LENGTH: model.max_seq_length = MAX_SEQUENCE_LENGTH return model def encode_documents(texts: list[str]) -> list[list[float]]: embeddings = _get_model().encode( texts, batch_size=8, convert_to_tensor=False, normalize_embeddings=True, ) return [embedding.tolist() for embedding in embeddings] def encode_query(text: str) -> list[float]: model = _get_model() prompt = QUERY_INSTRUCTION if "query" in (model.prompts or {}) else None embedding = model.encode( text, prompt=prompt, convert_to_tensor=False, normalize_embeddings=True, ) return embedding.tolist()