Math / agents /routing_agent.py
omkareswarhota1234's picture
Upload 71 files
dbbd3ec verified
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from agents.math_solver import try_math_solver
import os
import re
def route_query(query: str):
persist_dir = os.path.join("data", "embeddings")
# โœ… Load embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectordb = FAISS.load_local(persist_dir, embeddings, allow_dangerous_deserialization=True)
# โœ… Step 1: Search in FAISS
results = vectordb.similarity_search_with_score(query, k=1)
# โœ… If found in FAISS
if results and results[0][1] < 0.4:
print("๐Ÿ“˜ Answer is on data.txt")
return "KO"
else:
# โœ… Step 2: Try math solver
print("๐Ÿงฎ Not found in FAISS โ€” trying math solver...")
solver_result = try_math_solver(query)
print(f"๐Ÿงฉ Solver result: {solver_result}")
# โœ… Check if math solver succeeded
if solver_result and not any(bad in solver_result.lower() for bad in [
"sorry", "couldn't", "could not", "error", "invalid", "failed"
]):
print("โœ… Math solver succeeded!")
return solver_result
# โœ… If not solved by FAISS or math solver โ†’ LLM
print("๐ŸŒ Not found anywhere โ€” going for LLM")
return "LLM"