Alimubariz124 commited on
Commit
cf4f8aa
·
verified ·
1 Parent(s): 61e33cd

Create qa_engine.py

Browse files
Files changed (1) hide show
  1. qa_engine.py +16 -0
qa_engine.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ def query_faiss(query, index, embedder, chunks, top_k=3):
4
+ query_vector = embedder.encode([query])
5
+ D, I = index.search(np.array(query_vector), top_k)
6
+ retrieved_chunks = [chunks[i] for i in I[0]]
7
+ return "\n\n".join(retrieved_chunks)
8
+
9
+ def build_prompt(context, question):
10
+ return f"""You are an AI assistant. Use the following context to answer the question.
11
+
12
+ Context:
13
+ {context}
14
+
15
+ Question: {question}
16
+ Answer:"""