Your question is Cosine Similarity Search Function. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
RBC NOMI may compare an embedding for a new customer interaction with stored embeddings to retrieve the most relevant matches. Implement an exact similarity search using cosine similarity.
Given a query vector, a list of candidate vectors, and an integer k, return the indices of the k candidates with the highest cosine similarity to the query. Return indices in descending similarity order. If two candidates have equal similarity, place the smaller index first.
For vectors a and b, cosine similarity is:
(a · b) / (||a|| × ||b||)
If either vector has zero magnitude, define its similarity as 0. All vectors have the same positive dimension, and 1 <= k <= number of candidates.
query, a list of real numbers; vectors, a list of equal-length real-number lists; k, a positive integer.k integer indices identifying the most similar candidate vectors.def cosine_search(query, vectors, k):