Spaces:
Configuration error
Configuration error
File size: 1,846 Bytes
4a14841 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | """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()
|