| 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') |
|
|
| |
| corpus_embeddings = embedder.encode(questions["question_text"], show_progress_bar=True) |
|
|
| |
| with open('questions-embeddings.pkl', "wb") as fOut: |
| pickle.dump(corpus_embeddings, fOut) |
|
|
| |
| 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']) |
|
|
| |
| 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) |
|
|