Spaces:
Sleeping
Sleeping
| 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" |