File size: 1,013 Bytes
1a1545d 1ab2af1 1a1545d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from sentence_transformers import SentenceTransformer,util
import pickle
import pandas as pd
embedder = SentenceTransformer('msmarco-MiniLM-L-6-v3')
questions = pd.read_csv('questions.csv')
# Generating embeddings using msmarco minilm model
corpus_embeddings = embedder.encode(questions["question_text"], show_progress_bar=True)
# Writing the embeddings output to a pickle file that will be loaded by inference module.
with open('questions-embeddings.pkl', "wb") as fOut:
pickle.dump(corpus_embeddings, fOut)
# Validation
prompt="How can medicare help me?"
print(prompt)
prompt_embedding = embedder.encode(prompt, convert_to_tensor=True)
hits = util.semantic_search(prompt_embedding, corpus_embeddings, top_k=10)
hits = pd.DataFrame(hits[0], columns=['corpus_id', 'score'])
# Filter out all hits with score less than 0.5
hits = hits[(hits[['score']]>0.5).all(axis=1)]
questions_match = list(questions.iloc[hits['corpus_id']]['question_text'].values)
print(type(questions_match))
print(questions_match)
|