def getmostsimilarquestion(userText, history): print(userText) Compute embedding for the…
- User
def get_most_similar_question(userText, history): print(userText)
code# Compute embedding for the user input text using the initial retrieval model inp_emb = retrieval_model.encode([userText]) # Calculate similarity scores between user input text and data embeddings corr = np.inner(inp_emb, data_emb).flatten() # Get indices of the top 5 most similar texts top_5_idx = np.argsort(corr)[-5:] print('idx',top_5_idx) top_5_values = corr[top_5_idx] print('values',top_5_values) # Retrieve the texts/questions corresponding to these indices top_5_texts = new_df.iloc[top_5_idx, 1].values # Column 1 contains the texts/questions print(top_5_texts)# Re-encode these top 5 texts using the new retrieval model
data_emb_1 = new_retrieval_model(top_5_texts)
# Calculate similarity with the input text using the new model's embeddings
inp_emb_1 = new_retrieval_model([userText])
corr_1 = np.inner(inp_emb_1, data_emb_1).flatten()
print('corr_1',corr_1)
# Identify the highest similarity score and its index
top_1_idx = np.argmax(corr_1)
print('top_1_idx',top_1_idx)
top_1_idx = top_1_idx+1
print('top_1_idx_11',top_1_idx)
top_1_value = corr_1[top_1_idx]
print('top_1_value',top_1_value)
top_1_texts = top_5_texts[top_1_idx+1]
print('top_1_texts',top_1_texts)
codeprint('Matching score:', top_5_values[0])print('Matching score second time:', top_1_value)
code - Assistant