Spaces:
Sleeping
Sleeping
Create qa_engine.py
Browse files- 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:"""
|