Spaces:
Sleeping
Sleeping
File size: 1,193 Bytes
5518038 7b240d9 5518038 7b240d9 5518038 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import json
from langchain.vectorstores import FAISS
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
# Load the FAISS vector store from the directory 'faiss_index'
db = FAISS.load_local(
"faiss_index",
HuggingFaceEmbeddings(model_name='sentence-transformers/paraphrase-MiniLM-L6-v2'),
allow_dangerous_deserialization=True
)
#Load attrributes along with their supported operators
attribute_data = json.load(open("attribute_data.json"))
# Connect query to FAISS index using a retriever
retriever = db.as_retriever(
search_type="similarity",
search_kwargs={"k": 5}
)
# Modify fetch function to include operators
def fetch(query):
res = retriever.get_relevant_documents(query)
docs = []
for i in res:
attribute_line = i.page_content.split(",")[0] # "attribute: X"
attribute = attribute_line.split(": ")[1] # Extract "X"
# Find the matching operators from attribute_data
operators = next((item["operators"] for item in attribute_data if item["attribute"] == attribute), [])
operators_list = f"operators: {operators}"
docs.append(f"{i.page_content}, {operators_list}")
return docs |