from sentence_transformers import SentenceTransformer, util model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") documents = [ {"id": "doc-001", "text": "Set up billing alerts for monthly cloud spending."}, {"id": "doc-002", "text": "Reset expired password links from the account security page."}, {"id": "doc-003", "text": "Rotate SSH keys for production deployment hosts."}, {"id": "doc-004", "text": "Renew TLS certificates before the web server reload."}, {"id": "doc-005", "text": "Export customer invoices from the finance dashboard."}, ] query = "password reset link expired" corpus = [item["text"] for item in documents] corpus_embeddings = model.encode_document( corpus, convert_to_tensor=True, normalize_embeddings=True, show_progress_bar=False, ) query_embedding = model.encode_query( query, convert_to_tensor=True, normalize_embeddings=True, show_progress_bar=False, ) hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=3)[0] print(f"Corpus embeddings: {tuple(corpus_embeddings.shape)}") print(f"Query: {query}") for rank, hit in enumerate(hits, start=1): item = documents[hit["corpus_id"]] print(f"{rank}. {item['id']} score={hit['score']:.4f} text={item['text']}") top_item = documents[hits[0]["corpus_id"]] if top_item["id"] == "doc-002": print("Semantic search check: pass")