Spaces:
Sleeping
Sleeping
Download qa_engine.py from Alimubariz124/chat-with-data: direct link, hf CLI and curl.
- Browser
- Download file 445 Bytes
-
https://huggingface.co/spaces/Alimubariz124/chat-with-data/resolve/main/qa_engine.py
- Command line
-
hf download hf://spaces/Alimubariz124/chat-with-data/qa_engine.py
-
curl -L -o qa_engine.py https://huggingface.co/spaces/Alimubariz124/chat-with-data/resolve/main/qa_engine.py
445 Bytes
| import numpy as np | |
| def query_faiss(query, index, embedder, chunks, top_k=3): | |
| query_vector = embedder.encode([query]) | |
| D, I = index.search(np.array(query_vector), top_k) | |
| retrieved_chunks = [chunks[i] for i in I[0]] | |
| return "\n\n".join(retrieved_chunks) | |
| def build_prompt(context, question): | |
| return f"""You are an AI assistant. Use the following context to answer the question. | |
| Context: | |
| {context} | |
| Question: {question} | |
| Answer:""" |