import os
import logging
import pickle
import ssl

import dill
import langchain
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI, GooglePalm
from langchain.chains import LLMChain, RetrievalQAWithSourcesChain, AnalyzeDocumentChain
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
from langchain.document_loaders import TextLoader, UnstructuredURLLoader
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import StrOutputParser
from dotenv import load_dotenv

class Vectorizer():
    llm = OpenAI(temperature=0.7, max_tokens=1024)
    embeddings = OpenAIEmbeddings()
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
    logging.getLogger("httpx").setLevel(logging.WARNING)
    logger = logging.getLogger(__name__)
    
    def __init__(self, file_path: str):
        self.file_path = os.path.join(os.getcwd(), 'vectors', f'{file_path[:-4]}.pkl')
        
    def vector(self, split_docs: list, ) -> bool:
        self.logger.info('docs: %s', len(split_docs))
        # Using OpenAIEmbeddings models to provide further correlational data for our resulting vector for better semantic relationship identification
        vector_index = FAISS.from_documents(split_docs, self.embeddings)
        self.logger.info('Vector embedding created')
        
        # Exclude SSLContext from pickling
        dill._dill._reverse_typemap[type(ssl.create_default_context())] = None
        
        with open(self.file_path, 'wb') as f:
            dill.dump(vector_index, f)
            self.logger.info('Vector index saved')
        
        return True
    
    def load_index(self):
        if os.path.exists(self.file_path):
            with open(self.file_path, 'rb') as f:
                vector_index = dill.load(f)
                self.logger.info('Vector index loaded')
            
            return vector_index
        else:
            self.logger.info('Vector index not found at the provided file path')
            return False

langchain.vectorstores.FAISS.from_documents langchain.embeddings.OpenAIEmbeddings langchain.llms.OpenAI
[((670, 710), 'langchain.llms.OpenAI', 'OpenAI', ([], {'temperature': '(0.7)', 'max_tokens': '(1024)'}), '(temperature=0.7, max_tokens=1024)\n', (676, 710), False, 'from langchain.llms import OpenAI, GooglePalm\n'), ((728, 746), 'langchain.embeddings.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {}), '()\n', (744, 746), False, 'from langchain.embeddings import OpenAIEmbeddings\n'), ((751, 858), 'logging.basicConfig', 'logging.basicConfig', ([], {'format': '"""%(asctime)s - %(name)s - %(levelname)s - %(message)s"""', 'level': 'logging.INFO'}), "(format=\n    '%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)\n", (770, 858), False, 'import logging\n'), ((924, 951), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (941, 951), False, 'import logging\n'), ((1371, 1420), 'langchain.vectorstores.FAISS.from_documents', 'FAISS.from_documents', (['split_docs', 'self.embeddings'], {}), '(split_docs, self.embeddings)\n', (1391, 1420), False, 'from langchain.vectorstores import FAISS\n'), ((1821, 1851), 'os.path.exists', 'os.path.exists', (['self.file_path'], {}), '(self.file_path)\n', (1835, 1851), False, 'import os\n'), ((858, 884), 'logging.getLogger', 'logging.getLogger', (['"""httpx"""'], {}), "('httpx')\n", (875, 884), False, 'import logging\n'), ((1035, 1046), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (1044, 1046), False, 'import os\n'), ((1672, 1698), 'dill.dump', 'dill.dump', (['vector_index', 'f'], {}), '(vector_index, f)\n', (1681, 1698), False, 'import dill\n'), ((1567, 1595), 'ssl.create_default_context', 'ssl.create_default_context', ([], {}), '()\n', (1593, 1595), False, 'import ssl\n'), ((1934, 1946), 'dill.load', 'dill.load', (['f'], {}), '(f)\n', (1943, 1946), False, 'import dill\n')]