code: """
      This is the documentaion of the Llama2-7B-chat model from hugging face models
      This model has 7 billion parameters develped by Meta
      
      This is used for QnA purposes on local machine for testing...
      
      Model hardware config:
          - GPU: Nvidia RTX 40 Series (12GB) --> CUDA support 
          - RAM: 32GB
          - i7 processor 13th gen
  """
  import torch
  from transformers import BitsAndBytesConfig
  from langchain.embeddings.huggingface import HuggingFaceInstructEmbeddings
  
  from llama_index.llms import HuggingFaceLLM
  from llama_index import ServiceContext, SimpleDirectoryReader, \
      VectorStoreIndex, get_response_synthesizer, set_global_service_context
  from llama_index.retrievers import VectorIndexRetriever
  from llama_index.query_engine import RetrieverQueryEngine
  from llama_index.prompts import PromptTemplate
  from llama_index.storage.storage_context import StorageContext
  from llama_index.vector_stores import ChromaVectorStore
  from llama_index.postprocessor import SimilarityPostprocessor
  
  from chromadb import PersistentClient
  from chromadb.utils import embedding_functions
  
  from dotenv import load_dotenv
  from transformers import AutoTokenizer
  import os
  
  
  load_dotenv()
  HF_TOKEN = os.getenv("HF_TOKEN")
  
  LLM = "meta-llama/Llama-2-7b-chat-hf"
  EMBED_MODEL = "hkunlp/instructor-large"
  DEVICE_MAP = "auto"
  DEVICE = "cuda"
  
  
  class Llama2_7B_Chat:
      """Class for Llama-7B Chat model from HuggingFace"""
  
      def __init__(self) -> None:
          """Constrcutor of the class Llama2_7B_Chat"""
  
          print("==================== starting constructor... ======================")
  
          # Start chroma client
          self.__chroma_client = PersistentClient('./chroma_db')
  
          # for model bit quantization for more effiency in computation by the LLM
          self.__quantization_config = BitsAndBytesConfig(
              load_in_4bit=True,
              bnb_4bit_compute_dtype=torch.bfloat16,
              bnb_4bit_quant_type="nf4",
              bnb_4bit_use_double_quant=True,
              llm_int8_enable_fp32_cpu_offload=True
          )
          
          tokenizer = AutoTokenizer.from_pretrained(LLM)
  
          # HuggingFaceLLM object - uses pretrained models from HuggingFace (Llama2-7B-chat model)
          self.__llm = HuggingFaceLLM(
              model_name=LLM,
              tokenizer=tokenizer,
              is_chat_model=True,
              max_new_tokens=512,
              query_wrapper_prompt=PromptTemplate(
                  "<s> [INST] {query_str} [/INST]"),
              context_window=4000,
              model_kwargs={
                  "quantization_config": self.__quantization_config,
                  "token": HF_TOKEN
              },
              tokenizer_kwargs={
                  "token": HF_TOKEN
              },
              device_map=DEVICE_MAP
          )
  
          # embedding model - pretrained embedding model (it is wrapper around sentence_transformers)
          self.__embed_model = HuggingFaceInstructEmbeddings(
              model_name=EMBED_MODEL,
              model_kwargs={
                  "device": DEVICE
              }
          )
  
          self.__index = None
  
          # Service context
          self.__service_context = ServiceContext.from_defaults(
              llm=self.__llm, embed_model=self.__embed_model)
  
          set_global_service_context(self.__service_context)
  
      def create_index(self, data_dir: str) -> None:
          """Creates the Vector Index for querying with LLM"""
  
          print("============= creating index.... ================")
  
          # embedding function for chromadb
          embedding_func = embedding_functions.HuggingFaceEmbeddingFunction(
              api_key=HF_TOKEN,
              model_name=EMBED_MODEL
          )
  
          # Load the documents from data_dir
          docs = SimpleDirectoryReader(data_dir).load_data()
  
          # Creating collection in chroma database
          chroma_collection = self.__chroma_client.get_or_create_collection("data_embeddings",
                                                                            embedding_function=embedding_func)
  
          # Creating Chroma Vector Store
          vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
  
          # Create storage context using chroma vector store
          storage_context = StorageContext.from_defaults(
              vector_store=vector_store)
  
          self.__index = VectorStoreIndex.from_documents(docs, storage_context=storage_context)
  
      def start_query_engine(self):
          """Initialize the query engine"""
  
          print("=========== starting query engine... ===============")
  
          # configure retriever
          retriever = VectorIndexRetriever(
              index=self.__index,
              similarity_top_k=6
          )
  
          # configure node postproceesors
          s_processor = SimilarityPostprocessor(similarity_cutoff=0.65)
  
          # configure response synthesizer
          response_synthesizer = get_response_synthesizer()
  
          query_engine = RetrieverQueryEngine(
              retriever=retriever,
              node_postprocessors=[s_processor],
              response_synthesizer=response_synthesizer
          )
  
          return query_engine
  
      def ask_llm(self, user_query: str, query_engine):
          """
              Ask LLM for querying data based on context
  
              returns: (RESPONSE_TYPE, List[NodeWithScore])
          """
  
          # print("User asking -->", user_query)
  
          response = query_engine.query(user_query)
  
          return response, response.source_nodes
  
  
  def reset_model():
      """resets the model's knowledge base"""
  
      os.system("rm -rf Data_*")
      os.system("rm -rf vector_store_data/")
      os.system("rm -rf chroma_db/")
  
apis: llama_index.VectorStoreIndex.from_documents llama_index.get_response_synthesizer llama_index.postprocessor.SimilarityPostprocessor llama_index.query_engine.RetrieverQueryEngine llama_index.ServiceContext.from_defaults llama_index.vector_stores.ChromaVectorStore llama_index.prompts.PromptTemplate llama_index.storage.storage_context.StorageContext.from_defaults llama_index.SimpleDirectoryReader llama_index.retrievers.VectorIndexRetriever llama_index.set_global_service_context
extract_api: [((1191, 1204), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (1202, 1204), False, 'from dotenv import load_dotenv\n'), ((1216, 1237), 'os.getenv', 'os.getenv', (['"""HF_TOKEN"""'], {}), "('HF_TOKEN')\n", (1225, 1237), False, 'import os\n'), ((5540, 5566), 'os.system', 'os.system', (['"""rm -rf Data_*"""'], {}), "('rm -rf Data_*')\n", (5549, 5566), False, 'import os\n'), ((5571, 5609), 'os.system', 'os.system', (['"""rm -rf vector_store_data/"""'], {}), "('rm -rf vector_store_data/')\n", (5580, 5609), False, 'import os\n'), ((5614, 5644), 'os.system', 'os.system', (['"""rm -rf chroma_db/"""'], {}), "('rm -rf chroma_db/')\n", (5623, 5644), False, 'import os\n'), ((1669, 1700), 'chromadb.PersistentClient', 'PersistentClient', (['"""./chroma_db"""'], {}), "('./chroma_db')\n", (1685, 1700), False, 'from chromadb import PersistentClient\n'), ((1820, 2002), 'transformers.BitsAndBytesConfig', 'BitsAndBytesConfig', ([], {'load_in_4bit': '(True)', 'bnb_4bit_compute_dtype': 'torch.bfloat16', 'bnb_4bit_quant_type': '"""nf4"""', 'bnb_4bit_use_double_quant': '(True)', 'llm_int8_enable_fp32_cpu_offload': '(True)'}), "(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16,\n    bnb_4bit_quant_type='nf4', bnb_4bit_use_double_quant=True,\n    llm_int8_enable_fp32_cpu_offload=True)\n", (1838, 2002), False, 'from transformers import BitsAndBytesConfig\n'), ((2094, 2128), 'transformers.AutoTokenizer.from_pretrained', 'AutoTokenizer.from_pretrained', (['LLM'], {}), '(LLM)\n', (2123, 2128), False, 'from transformers import AutoTokenizer\n'), ((2919, 3010), 'langchain.embeddings.huggingface.HuggingFaceInstructEmbeddings', 'HuggingFaceInstructEmbeddings', ([], {'model_name': 'EMBED_MODEL', 'model_kwargs': "{'device': DEVICE}"}), "(model_name=EMBED_MODEL, model_kwargs={\n    'device': DEVICE})\n", (2948, 3010), False, 'from langchain.embeddings.huggingface import HuggingFaceInstructEmbeddings\n'), ((3159, 3235), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'self.__llm', 'embed_model': 'self.__embed_model'}), '(llm=self.__llm, embed_model=self.__embed_model)\n', (3187, 3235), False, 'from llama_index import ServiceContext, SimpleDirectoryReader, VectorStoreIndex, get_response_synthesizer, set_global_service_context\n'), ((3258, 3308), 'llama_index.set_global_service_context', 'set_global_service_context', (['self.__service_context'], {}), '(self.__service_context)\n', (3284, 3308), False, 'from llama_index import ServiceContext, SimpleDirectoryReader, VectorStoreIndex, get_response_synthesizer, set_global_service_context\n'), ((3558, 3652), 'chromadb.utils.embedding_functions.HuggingFaceEmbeddingFunction', 'embedding_functions.HuggingFaceEmbeddingFunction', ([], {'api_key': 'HF_TOKEN', 'model_name': 'EMBED_MODEL'}), '(api_key=HF_TOKEN,\n    model_name=EMBED_MODEL)\n', (3606, 3652), False, 'from chromadb.utils import embedding_functions\n'), ((4101, 4155), 'llama_index.vector_stores.ChromaVectorStore', 'ChromaVectorStore', ([], {'chroma_collection': 'chroma_collection'}), '(chroma_collection=chroma_collection)\n', (4118, 4155), False, 'from llama_index.vector_stores import ChromaVectorStore\n'), ((4242, 4297), 'llama_index.storage.storage_context.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'vector_store': 'vector_store'}), '(vector_store=vector_store)\n', (4270, 4297), False, 'from llama_index.storage.storage_context import StorageContext\n'), ((4335, 4405), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['docs'], {'storage_context': 'storage_context'}), '(docs, storage_context=storage_context)\n', (4366, 4405), False, 'from llama_index import ServiceContext, SimpleDirectoryReader, VectorStoreIndex, get_response_synthesizer, set_global_service_context\n'), ((4605, 4665), 'llama_index.retrievers.VectorIndexRetriever', 'VectorIndexRetriever', ([], {'index': 'self.__index', 'similarity_top_k': '(6)'}), '(index=self.__index, similarity_top_k=6)\n', (4625, 4665), False, 'from llama_index.retrievers import VectorIndexRetriever\n'), ((4763, 4810), 'llama_index.postprocessor.SimilarityPostprocessor', 'SimilarityPostprocessor', ([], {'similarity_cutoff': '(0.65)'}), '(similarity_cutoff=0.65)\n', (4786, 4810), False, 'from llama_index.postprocessor import SimilarityPostprocessor\n'), ((4884, 4910), 'llama_index.get_response_synthesizer', 'get_response_synthesizer', ([], {}), '()\n', (4908, 4910), False, 'from llama_index import ServiceContext, SimpleDirectoryReader, VectorStoreIndex, get_response_synthesizer, set_global_service_context\n'), ((4935, 5058), 'llama_index.query_engine.RetrieverQueryEngine', 'RetrieverQueryEngine', ([], {'retriever': 'retriever', 'node_postprocessors': '[s_processor]', 'response_synthesizer': 'response_synthesizer'}), '(retriever=retriever, node_postprocessors=[s_processor],\n    response_synthesizer=response_synthesizer)\n', (4955, 5058), False, 'from llama_index.query_engine import RetrieverQueryEngine\n'), ((2422, 2470), 'llama_index.prompts.PromptTemplate', 'PromptTemplate', (['"""<s> [INST] {query_str} [/INST]"""'], {}), "('<s> [INST] {query_str} [/INST]')\n", (2436, 2470), False, 'from llama_index.prompts import PromptTemplate\n'), ((3742, 3773), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', (['data_dir'], {}), '(data_dir)\n', (3763, 3773), False, 'from llama_index import ServiceContext, SimpleDirectoryReader, VectorStoreIndex, get_response_synthesizer, set_global_service_context\n')]