Spaces:
Sleeping
Sleeping
import logging | |
import os | |
import requests | |
from langchain_community.vectorstores import FAISS | |
from langchain_community.embeddings import HuggingFaceEmbeddings | |
class VectorStore: | |
vectorstore = "index-BAAI_bge-m3-1500-200-recursive_splitter-CA_ES_UE" | |
def __init__(self, embeddings_model): | |
# load vectore store | |
embeddings = HuggingFaceEmbeddings(model_name=embeddings_model, model_kwargs={'device': 'cpu'}) | |
self.vectore_store = FAISS.load_local(self.vectorstore, embeddings, allow_dangerous_deserialization=True) | |
logging.info("RAG loaded!") | |
def get_context(self, instruction, number_of_contexts=2): | |
documentos = self.vectore_store.similarity_search_with_score(instruction, k=number_of_contexts) | |
return self.beautiful_context(documentos) | |
def beautiful_context(self, docs): | |
text_context = "" | |
full_context = "" | |
source_context = [] | |
for doc in docs: | |
text_context += doc[0].page_content | |
full_context += doc[0].metadata["Títol de la norma"] + "\n\n" | |
full_context += doc[0].metadata["url"] + "\n\n" | |
full_context += doc[0].page_content + "\n" | |
source_context.append(doc[0].metadata["url"]) | |
return full_context | |