code
The similarity_detector function in Python calculates semantic similarity between a RAG context and an LLM answer using BedrockEmbeddings and cosine similarity, returning a float score representing the difference between the two embeddings. def similarity_detector( context: str, answer: str, llm: BedrockEmbeddings, ) -> float: if len(context) == 0 or len(answer) == 0: return 0.0 # calculate embeddings context_emb = llm.embed_query(context) answer_emb = llm.embed_query(answer) context_emb = np.array(context_emb).reshape(1, -1) answer_emb = np.array(answer_emb).reshape(1, -1) sim_score = cosine_similarity(context_emb, answer_emb) return 1 - sim_score[0][0]
Authors
Sources
- Detect hallucinations for RAG-based systems - AWS aws.amazon.com via serper
Referenced by nodes (2)
- Python concept
- cosine similarity concept