Skip to content

Instantly share code, notes, and snippets.

@zseta
Created November 5, 2025 18:54
Show Gist options
  • Select an option

  • Save zseta/f775e689c2349d8c471d4c04474cc4dc to your computer and use it in GitHub Desktop.

Select an option

Save zseta/f775e689c2349d8c471d4c04474cc4dc to your computer and use it in GitHub Desktop.
def calc_cosine_similarity(self, vec1, vec2):
"""Calculate cosine similarity between two vectors."""
v1, v2 = np.array(vec1), np.array(vec2)
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
def search_cache(self, embedding, threshold=0.80):
"""
Returns the most similar response if it is above the threshold. Otherwise returns `None`.
"""
k = 1
cql = "SELECT * FROM prompts ORDER BY prompt_embedding ANN OF %s LIMIT %s;"
results = scylla_client.query_data(cql, [embedding, k])
if len(results) > 0:
cached_response = results[0]
similarity = self.calc_cosine_similarity(embedding, cached_response['prompt_embedding'])
print("Similarity score:", similarity)
if similarity >= threshold:
return cached_response['llm_response']
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment