| | |
| | import numpy as np |
| | from sentence_transformers import SentenceTransformer |
| | import faiss |
| |
|
| | |
| | def load_index_and_passages(index_path="vectorstore/index.faiss", passage_path="vectorstore/passages.npy"): |
| | index = faiss.read_index(index_path) |
| | passages = np.load(passage_path, allow_pickle=True) |
| | return index, passages |
| |
|
| | |
| | def retrieve_answer(question, index, passages, top_k=1): |
| | model = SentenceTransformer('bert-base-multilingual-cased') |
| | question_emb = model.encode([question]) |
| | D, I = index.search(question_emb, top_k) |
| | return [passages[i] for i in I[0]] |
| |
|
| | |
| | def ask_qa(): |
| | index, passages = load_index_and_passages() |
| | while True: |
| | question = input("Сұрақ: ") |
| | if question.lower() in ["exit", "шығу"]: |
| | break |
| | answers = retrieve_answer(question, index, passages) |
| | print("Жауап:", answers[0]) |
| |
|
| | if __name__ == "__main__": |
| | ask_qa() |