Tata Consultancy Services (North America) needs a basic semantic retrieval component that ranks document embeddings against a query embedding. Implement vector search from scratch using cosine similarity, without machine learning or numerical libraries.
Given documents, a list of equal-length numeric vectors, a numeric query vector, and k, return the indices of the k documents with the highest cosine similarity to the query.
documents, a list of lists of numbers; query, a list of numbers with the same dimension; and integer k.k integer indices, sorted by descending cosine similarity. If scores tie, return the smaller index first.dot(a, b) / (||a|| * ||b||). If either vector has zero magnitude, define its similarity as 0.0.def vector_search(documents, query, k):