Created
October 27, 2023 21:31
-
-
Save arafattehsin/64abaf16c534ee246d28f24a4dd5a679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get embeddings of an riddle / landmark answer | |
public async Task<IList<ReadOnlyMemory<float>>> GetEmbeddings(string answer) | |
{ | |
return await textEmbeddingGeneration.GenerateEmbeddingsAsync(new List<string> { answer }); | |
} | |
// Calculate Cosine Similarity between two answers | |
public async Task<double> CalculateSimilarity(IList<ReadOnlyMemory<float>> origEmbedding, string guessedAnswer) | |
{ | |
try | |
{ | |
var guessEmbedding = await GetEmbeddings(guessedAnswer); | |
var similarity = origEmbedding.First().Span.CosineSimilarity(guessEmbedding.First().Span); | |
return Math.Round(similarity, 2) * 100; | |
} | |
catch(Exception ex) | |
{ | |
throw new Exception(ex.Message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment