code: # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
  
  import os
  import requests
  import chainlit as cl
  from dotenv import load_dotenv
  
  import llama_index
  from llama_index.core import set_global_handler
  
  from llama_index.llms.openai import OpenAI
  from llama_index.embeddings.openai import OpenAIEmbedding
  from llama_index.core import Settings
  from llama_index.core import (
      SimpleDirectoryReader,
      load_index_from_storage,
      VectorStoreIndex,
      StorageContext,
  )
  
  from llama_index.vector_stores.faiss import FaissVectorStore
  import faiss
  
  # ChatOpenAI Templates
  system_template = """You are a helpful assistant who always speaks in a pleasant tone!
  """
  
  user_template = """{input}
  Think through your response step by step.
  """
  # query_engine = index.as_query_engine()
  # response = query_engine.query("Who is the E-VP, Operations - and how old are they?")
  # print(response.response)
  #
  # response = query_engine.query("What is the gross carrying amount of Total Amortizable Intangible Assets for Jan 29, 2023?")
  # print(response.response)
  # if storage folder exists and is not empty, load the index from it else from documents
  
  
  @cl.on_chat_start
  async def start_chat():
      load_dotenv()
  
      set_global_handler("wandb", run_args={"project": "aie1-llama-index-middleterm"})
      wandb_callback = llama_index.core.global_handler
  
      Settings.llm = OpenAI(temperature=0.1, model="gpt-3.5-turbo")
      Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
  
      index = None
      if os.path.exists("./storage") and os.listdir("./storage"):
          vector_store = FaissVectorStore.from_persist_dir("./storage")
          storage_context = StorageContext.from_defaults(
              vector_store=vector_store, persist_dir="./storage"
          )
          index = load_index_from_storage(storage_context=storage_context)
      else:
          with requests.get('https://d18rn0p25nwr6d.cloudfront.net/CIK-0001045810/1cbe8fe7-e08a-46e3-8dcc-b429fc06c1a4.pdf',
                            stream=True) as r:
              r.raise_for_status()  # Raises a HTTPError if the response status code is 4XX/5XX
              os.makedirs(os.path.dirname('nvidia_data/paper.pdf'), exist_ok=True)
              with open('nvidia_data/paper.pdf', 'wb') as f:
                  for chunk in r.iter_content(chunk_size=8192):
                      f.write(chunk)
          documents = SimpleDirectoryReader('nvidia_data/').load_data()
          faiss_index = faiss.IndexFlatL2(1536)
          storage_context = StorageContext.from_defaults(vector_store=FaissVectorStore(faiss_index=faiss_index))
          index = VectorStoreIndex.from_documents(
              documents,
              storage_context=storage_context,
              persist_dir="./storage"
          )
  
      cl.user_session.set("wandb_callback", wandb_callback)
      cl.user_session.set("query_engine", index.as_query_engine())
  
  
  @cl.on_message
  async def main(message: cl.Message):
      Settings.callback_manager = cl.user_session.get("wandb_callback")
      query_engine = cl.user_session.get("query_engine")
      template = (f"You are a helpful assistant who always speaks in a pleasant tone! responds to user input with a step by step guide using this context: {message.content} input: {input}")
      response = query_engine.query(template)
  
      response_message = cl.Message(content="")
      for token in response.response:
          await response_message.stream_token(token=token)
  
      await response_message.send()
  
  
  @cl.on_stop
  def on_stop():
      print("The user wants to stop the task!")
      cl.user_session.get("wandb_callback").finish()
  
  
  @cl.on_chat_end
  def on_chat_end():
      print("The user disconnected!")
      cl.user_session.get("wandb_callback").finish()
apis: llama_index.core.StorageContext.from_defaults llama_index.core.set_global_handler llama_index.vector_stores.faiss.FaissVectorStore.from_persist_dir llama_index.core.VectorStoreIndex.from_documents llama_index.core.SimpleDirectoryReader llama_index.llms.openai.OpenAI llama_index.vector_stores.faiss.FaissVectorStore llama_index.core.load_index_from_storage llama_index.embeddings.openai.OpenAIEmbedding
extract_api: [((1242, 1255), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (1253, 1255), False, 'from dotenv import load_dotenv\n'), ((1261, 1346), 'llama_index.core.set_global_handler', 'set_global_handler', (['"""wandb"""'], {'run_args': "{'project': 'aie1-llama-index-middleterm'}"}), "('wandb', run_args={'project': 'aie1-llama-index-middleterm'}\n    )\n", (1279, 1346), False, 'from llama_index.core import set_global_handler\n'), ((1415, 1461), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'temperature': '(0.1)', 'model': '"""gpt-3.5-turbo"""'}), "(temperature=0.1, model='gpt-3.5-turbo')\n", (1421, 1461), False, 'from llama_index.llms.openai import OpenAI\n'), ((1489, 1536), 'llama_index.embeddings.openai.OpenAIEmbedding', 'OpenAIEmbedding', ([], {'model': '"""text-embedding-3-small"""'}), "(model='text-embedding-3-small')\n", (1504, 1536), False, 'from llama_index.embeddings.openai import OpenAIEmbedding\n'), ((2795, 2848), 'chainlit.user_session.set', 'cl.user_session.set', (['"""wandb_callback"""', 'wandb_callback'], {}), "('wandb_callback', wandb_callback)\n", (2814, 2848), True, 'import chainlit as cl\n'), ((3000, 3037), 'chainlit.user_session.get', 'cl.user_session.get', (['"""wandb_callback"""'], {}), "('wandb_callback')\n", (3019, 3037), True, 'import chainlit as cl\n'), ((3057, 3092), 'chainlit.user_session.get', 'cl.user_session.get', (['"""query_engine"""'], {}), "('query_engine')\n", (3076, 3092), True, 'import chainlit as cl\n'), ((3349, 3371), 'chainlit.Message', 'cl.Message', ([], {'content': '""""""'}), "(content='')\n", (3359, 3371), True, 'import chainlit as cl\n'), ((1562, 1589), 'os.path.exists', 'os.path.exists', (['"""./storage"""'], {}), "('./storage')\n", (1576, 1589), False, 'import os\n'), ((1594, 1617), 'os.listdir', 'os.listdir', (['"""./storage"""'], {}), "('./storage')\n", (1604, 1617), False, 'import os\n'), ((1642, 1688), 'llama_index.vector_stores.faiss.FaissVectorStore.from_persist_dir', 'FaissVectorStore.from_persist_dir', (['"""./storage"""'], {}), "('./storage')\n", (1675, 1688), False, 'from llama_index.vector_stores.faiss import FaissVectorStore\n'), ((1715, 1800), 'llama_index.core.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'vector_store': 'vector_store', 'persist_dir': '"""./storage"""'}), "(vector_store=vector_store, persist_dir='./storage'\n    )\n", (1743, 1800), False, 'from llama_index.core import SimpleDirectoryReader, load_index_from_storage, VectorStoreIndex, StorageContext\n'), ((1834, 1890), 'llama_index.core.load_index_from_storage', 'load_index_from_storage', ([], {'storage_context': 'storage_context'}), '(storage_context=storage_context)\n', (1857, 1890), False, 'from llama_index.core import SimpleDirectoryReader, load_index_from_storage, VectorStoreIndex, StorageContext\n'), ((2492, 2515), 'faiss.IndexFlatL2', 'faiss.IndexFlatL2', (['(1536)'], {}), '(1536)\n', (2509, 2515), False, 'import faiss\n'), ((2643, 2747), 'llama_index.core.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {'storage_context': 'storage_context', 'persist_dir': '"""./storage"""'}), "(documents, storage_context=storage_context,\n    persist_dir='./storage')\n", (2674, 2747), False, 'from llama_index.core import SimpleDirectoryReader, load_index_from_storage, VectorStoreIndex, StorageContext\n'), ((1914, 2046), 'requests.get', 'requests.get', (['"""https://d18rn0p25nwr6d.cloudfront.net/CIK-0001045810/1cbe8fe7-e08a-46e3-8dcc-b429fc06c1a4.pdf"""'], {'stream': '(True)'}), "(\n    'https://d18rn0p25nwr6d.cloudfront.net/CIK-0001045810/1cbe8fe7-e08a-46e3-8dcc-b429fc06c1a4.pdf'\n    , stream=True)\n", (1926, 2046), False, 'import requests\n'), ((3579, 3616), 'chainlit.user_session.get', 'cl.user_session.get', (['"""wandb_callback"""'], {}), "('wandb_callback')\n", (3598, 3616), True, 'import chainlit as cl\n'), ((3703, 3740), 'chainlit.user_session.get', 'cl.user_session.get', (['"""wandb_callback"""'], {}), "('wandb_callback')\n", (3722, 3740), True, 'import chainlit as cl\n'), ((2187, 2227), 'os.path.dirname', 'os.path.dirname', (['"""nvidia_data/paper.pdf"""'], {}), "('nvidia_data/paper.pdf')\n", (2202, 2227), False, 'import os\n'), ((2420, 2457), 'llama_index.core.SimpleDirectoryReader', 'SimpleDirectoryReader', (['"""nvidia_data/"""'], {}), "('nvidia_data/')\n", (2441, 2457), False, 'from llama_index.core import SimpleDirectoryReader, load_index_from_storage, VectorStoreIndex, StorageContext\n'), ((2584, 2625), 'llama_index.vector_stores.faiss.FaissVectorStore', 'FaissVectorStore', ([], {'faiss_index': 'faiss_index'}), '(faiss_index=faiss_index)\n', (2600, 2625), False, 'from llama_index.vector_stores.faiss import FaissVectorStore\n')]