|
|
from sentence_transformers import SentenceTransformer |
|
|
import faiss |
|
|
import numpy as np |
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Loading embedding model...") |
|
|
embedder = SentenceTransformer("all-MiniLM-L6-v2") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Loading vector database...") |
|
|
index = faiss.read_index("vector.index") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open("documents.txt", "r", encoding="utf-8") as f: |
|
|
documents = [line.strip() for line in f.readlines()] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Loading language model...") |
|
|
qa_model = pipeline( |
|
|
"text2text-generation", |
|
|
model="google/flan-t5-base", |
|
|
max_new_tokens=120 |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ask_bot(question, top_k=3, similarity_threshold=1.0): |
|
|
|
|
|
q_embedding = embedder.encode([question]) |
|
|
|
|
|
|
|
|
distances, indices = index.search(np.array(q_embedding), top_k) |
|
|
|
|
|
|
|
|
if distances[0][0] > similarity_threshold: |
|
|
return "I don't know based on the given data." |
|
|
|
|
|
|
|
|
context = "\n".join([documents[i] for i in indices[0]]) |
|
|
|
|
|
|
|
|
prompt = f""" |
|
|
You are a domain question answering assistant. |
|
|
Use ONLY the context below. |
|
|
If the answer is not clearly present in the context, reply exactly: |
|
|
I don't know based on the given data. |
|
|
|
|
|
Context: |
|
|
{context} |
|
|
|
|
|
Question: |
|
|
{question} |
|
|
|
|
|
Answer: |
|
|
""" |
|
|
|
|
|
|
|
|
output = qa_model(prompt)[0]["generated_text"].strip() |
|
|
|
|
|
|
|
|
if output.lower().startswith("flask") or output.lower().startswith("react") or output.lower().startswith("seo"): |
|
|
|
|
|
if question.lower() not in context.lower(): |
|
|
return "I don't know based on the given data." |
|
|
|
|
|
return output |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("\nAI Agent ready. Type 'exit' to quit.\n") |
|
|
|
|
|
while True: |
|
|
question = input("Ask: ") |
|
|
|
|
|
if question.lower() in ["exit", "quit"]: |
|
|
print("Goodbye!") |
|
|
break |
|
|
|
|
|
answer = ask_bot(question) |
|
|
print("\nBot:", answer, "\n") |
|
|
|