code: import streamlit as st
  import torch
  from glob import glob
  from pathlib import Path
  from llama_index.prompts.prompts import SimpleInputPrompt
  from llama_index import (
      set_global_service_context,
      ServiceContext,
      VectorStoreIndex,
      download_loader,
  )
  from langchain.embeddings import HuggingFaceEmbeddings
  from llama_index.embeddings import LangchainEmbedding
  from transformers import AutoTokenizer, AutoModelForCausalLM
  from llama_index.llms import HuggingFaceLLM
  
  PyMuPDFReader = download_loader("PyMuPDFReader")
  loader = PyMuPDFReader()
  model_name = "meta-llama/Llama-2-7b-chat-hf"
  auth_token = "*******************************"
  system_prompt = """<s>[INST] <<SYS>>
  You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.
  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. 
  Please ensure that your responses are socially unbiased and positive in nature.
  
  If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct.
  If you don't know the answer to a question, please don't share false information. <</SYS>>
  """  # Llama2's official system prompt
  
  
  @st.cache_resource
  def model_tokenizer_embedder(model_name, auth_token):
      tokenizer = AutoTokenizer.from_pretrained(
          model_name, cache_dir="./model/", use_auth_token=auth_token
      )
      model = AutoModelForCausalLM.from_pretrained(
          model_name,
          cache_dir="./model/",
          use_auth_token=auth_token,
          torch_dtype=torch.float16,
          load_in_8bit=True,
      )
  
      embedding_llm = LangchainEmbedding(
          HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
      )
  
      return tokenizer, model, embedding_llm
  
  
  def load_documents(directory):
      documents = []
      for item_path in glob(directory + "*.pdf"):
          # documents.extend(loader.load(file_path=Path(item_path), metadata=True))
          documents.extend(loader.load(file_path=item_path, metadata=True))
      return documents
  
  
  tokenizer, model, embedding_llm = model_tokenizer_embedder(model_name, auth_token)
  
  query_wrapper_prompt = SimpleInputPrompt("{query_str} [/INST]")
  
  llm = HuggingFaceLLM(
      context_window=4096,
      max_new_tokens=256,
      system_prompt=system_prompt,
      query_wrapper_prompt=query_wrapper_prompt,
      model=model,
      tokenizer=tokenizer,
  )
  
  service_context = ServiceContext.from_defaults(
      chunk_size=1024, llm=llm, embed_model=embedding_llm
  )
  set_global_service_context(service_context)
  
  documents = load_documents("./documents/")
  index = VectorStoreIndex.from_documents(documents)
  query_engine = index.as_query_engine()
  
  st.title("Llama2 RAG")
  
  prompt = st.text_input("Enter your prompt")
  if prompt:
      response = query_engine.query(prompt)
      st.write(response.response)
  
apis: llama_index.VectorStoreIndex.from_documents llama_index.ServiceContext.from_defaults llama_index.download_loader llama_index.llms.HuggingFaceLLM llama_index.prompts.prompts.SimpleInputPrompt llama_index.set_global_service_context
extract_api: [((495, 527), 'llama_index.download_loader', 'download_loader', (['"""PyMuPDFReader"""'], {}), "('PyMuPDFReader')\n", (510, 527), False, 'from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, download_loader\n'), ((2176, 2216), 'llama_index.prompts.prompts.SimpleInputPrompt', 'SimpleInputPrompt', (['"""{query_str} [/INST]"""'], {}), "('{query_str} [/INST]')\n", (2193, 2216), False, 'from llama_index.prompts.prompts import SimpleInputPrompt\n'), ((2224, 2394), 'llama_index.llms.HuggingFaceLLM', 'HuggingFaceLLM', ([], {'context_window': '(4096)', 'max_new_tokens': '(256)', 'system_prompt': 'system_prompt', 'query_wrapper_prompt': 'query_wrapper_prompt', 'model': 'model', 'tokenizer': 'tokenizer'}), '(context_window=4096, max_new_tokens=256, system_prompt=\n    system_prompt, query_wrapper_prompt=query_wrapper_prompt, model=model,\n    tokenizer=tokenizer)\n', (2238, 2394), False, 'from llama_index.llms import HuggingFaceLLM\n'), ((2432, 2518), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'chunk_size': '(1024)', 'llm': 'llm', 'embed_model': 'embedding_llm'}), '(chunk_size=1024, llm=llm, embed_model=\n    embedding_llm)\n', (2460, 2518), False, 'from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, download_loader\n'), ((2520, 2563), 'llama_index.set_global_service_context', 'set_global_service_context', (['service_context'], {}), '(service_context)\n', (2546, 2563), False, 'from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, download_loader\n'), ((2616, 2658), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {}), '(documents)\n', (2647, 2658), False, 'from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, download_loader\n'), ((2699, 2721), 'streamlit.title', 'st.title', (['"""Llama2 RAG"""'], {}), "('Llama2 RAG')\n", (2707, 2721), True, 'import streamlit as st\n'), ((2732, 2766), 'streamlit.text_input', 'st.text_input', (['"""Enter your prompt"""'], {}), "('Enter your prompt')\n", (2745, 2766), True, 'import streamlit as st\n'), ((1330, 1424), 'transformers.AutoTokenizer.from_pretrained', 'AutoTokenizer.from_pretrained', (['model_name'], {'cache_dir': '"""./model/"""', 'use_auth_token': 'auth_token'}), "(model_name, cache_dir='./model/',\n    use_auth_token=auth_token)\n", (1359, 1424), False, 'from transformers import AutoTokenizer, AutoModelForCausalLM\n'), ((1447, 1594), 'transformers.AutoModelForCausalLM.from_pretrained', 'AutoModelForCausalLM.from_pretrained', (['model_name'], {'cache_dir': '"""./model/"""', 'use_auth_token': 'auth_token', 'torch_dtype': 'torch.float16', 'load_in_8bit': '(True)'}), "(model_name, cache_dir='./model/',\n    use_auth_token=auth_token, torch_dtype=torch.float16, load_in_8bit=True)\n", (1483, 1594), False, 'from transformers import AutoTokenizer, AutoModelForCausalLM\n'), ((1863, 1888), 'glob.glob', 'glob', (["(directory + '*.pdf')"], {}), "(directory + '*.pdf')\n", (1867, 1888), False, 'from glob import glob\n'), ((2824, 2851), 'streamlit.write', 'st.write', (['response.response'], {}), '(response.response)\n', (2832, 2851), True, 'import streamlit as st\n'), ((1687, 1739), 'langchain.embeddings.HuggingFaceEmbeddings', 'HuggingFaceEmbeddings', ([], {'model_name': '"""all-MiniLM-L6-v2"""'}), "(model_name='all-MiniLM-L6-v2')\n", (1708, 1739), False, 'from langchain.embeddings import HuggingFaceEmbeddings\n')]