code: import pathlib
  import tempfile
  from io import BytesIO
  
  import openai
  import streamlit as st
  from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
  from llama_index.core.chat_engine import ContextChatEngine
  from llama_index.llms.openai import OpenAI
  
  from sidebar import sidebar_params
  
  st.set_page_config(page_title="Chat with Documents", layout="wide", page_icon="🔥")
  st.title("Chat with Documents")
  
  
  @st.cache_resource(show_spinner=False)
  def build_chat_engine(file: BytesIO, temperature: float) -> ContextChatEngine:
      with st.spinner("Loading and indexing the document..."):
          with tempfile.TemporaryDirectory() as temp_dir:
              temp_file_path = pathlib.Path(temp_dir) / file.name
              with open(temp_file_path, "wb") as f:
                  f.write(file.getbuffer())
              reader = SimpleDirectoryReader(input_files=[temp_file_path])
              documents = reader.load_data()
  
          llm = OpenAI(model="gpt-3.5-turbo", temperature=temperature)
          index = VectorStoreIndex.from_documents(documents)
          return index.as_chat_engine(chat_mode="context", llm=llm, verbose=True)
  
  
  def add_message(role: str, content: str):
      st.session_state.messages.append({"role": role, "content": content})
  
  
  openai_api_key, temperature = sidebar_params()
  
  uploaded_file = st.file_uploader(
      "Upload a pdf, docx, or txt file",
      type=["pdf", "docx", "txt"],
  )
  
  if not openai_api_key or not uploaded_file:
      st.stop()
  
  openai.api_key = openai_api_key
  chat_engine = build_chat_engine(uploaded_file, temperature)
  
  if "messages" not in st.session_state or st.sidebar.button("Clear message history"):
      st.session_state.messages = [
          {
              "role": "assistant",
              "content": "Ask me questions about the uploaded document!",
          },
      ]
  
  for message in st.session_state.messages:
      with st.chat_message(message["role"]):
          st.write(message["content"])
  
  if user_query := st.chat_input("Ask questions about the document..."):
      with st.chat_message("user"):
          st.write(user_query)
      add_message("user", user_query)
  
      with st.chat_message("assistant"), st.spinner("Generating response..."):
          response = chat_engine.chat(user_query).response
          st.write(response)
      add_message("assistant", response)
  
apis: llama_index.core.VectorStoreIndex.from_documents llama_index.core.SimpleDirectoryReader llama_index.llms.openai.OpenAI
extract_api: [((300, 386), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Chat with Documents"""', 'layout': '"""wide"""', 'page_icon': '"""🔥"""'}), "(page_title='Chat with Documents', layout='wide',\n    page_icon='🔥')\n", (318, 386), True, 'import streamlit as st\n'), ((383, 414), 'streamlit.title', 'st.title', (['"""Chat with Documents"""'], {}), "('Chat with Documents')\n", (391, 414), True, 'import streamlit as st\n'), ((418, 455), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (435, 455), True, 'import streamlit as st\n'), ((1282, 1298), 'sidebar.sidebar_params', 'sidebar_params', ([], {}), '()\n', (1296, 1298), False, 'from sidebar import sidebar_params\n'), ((1316, 1401), 'streamlit.file_uploader', 'st.file_uploader', (['"""Upload a pdf, docx, or txt file"""'], {'type': "['pdf', 'docx', 'txt']"}), "('Upload a pdf, docx, or txt file', type=['pdf', 'docx', 'txt']\n    )\n", (1332, 1401), True, 'import streamlit as st\n'), ((1181, 1249), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': role, 'content': content}"], {}), "({'role': role, 'content': content})\n", (1213, 1249), True, 'import streamlit as st\n'), ((1457, 1466), 'streamlit.stop', 'st.stop', ([], {}), '()\n', (1464, 1466), True, 'import streamlit as st\n'), ((1602, 1644), 'streamlit.sidebar.button', 'st.sidebar.button', (['"""Clear message history"""'], {}), "('Clear message history')\n", (1619, 1644), True, 'import streamlit as st\n'), ((1953, 2005), 'streamlit.chat_input', 'st.chat_input', (['"""Ask questions about the document..."""'], {}), "('Ask questions about the document...')\n", (1966, 2005), True, 'import streamlit as st\n'), ((544, 594), 'streamlit.spinner', 'st.spinner', (['"""Loading and indexing the document..."""'], {}), "('Loading and indexing the document...')\n", (554, 594), True, 'import streamlit as st\n'), ((939, 993), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'model': '"""gpt-3.5-turbo"""', 'temperature': 'temperature'}), "(model='gpt-3.5-turbo', temperature=temperature)\n", (945, 993), False, 'from llama_index.llms.openai import OpenAI\n'), ((1010, 1052), 'llama_index.core.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {}), '(documents)\n', (1041, 1052), False, 'from llama_index.core import SimpleDirectoryReader, VectorStoreIndex\n'), ((1864, 1896), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (1879, 1896), True, 'import streamlit as st\n'), ((1906, 1934), 'streamlit.write', 'st.write', (["message['content']"], {}), "(message['content'])\n", (1914, 1934), True, 'import streamlit as st\n'), ((2016, 2039), 'streamlit.chat_message', 'st.chat_message', (['"""user"""'], {}), "('user')\n", (2031, 2039), True, 'import streamlit as st\n'), ((2049, 2069), 'streamlit.write', 'st.write', (['user_query'], {}), '(user_query)\n', (2057, 2069), True, 'import streamlit as st\n'), ((2116, 2144), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (2131, 2144), True, 'import streamlit as st\n'), ((2146, 2182), 'streamlit.spinner', 'st.spinner', (['"""Generating response..."""'], {}), "('Generating response...')\n", (2156, 2182), True, 'import streamlit as st\n'), ((2249, 2267), 'streamlit.write', 'st.write', (['response'], {}), '(response)\n', (2257, 2267), True, 'import streamlit as st\n'), ((609, 638), 'tempfile.TemporaryDirectory', 'tempfile.TemporaryDirectory', ([], {}), '()\n', (636, 638), False, 'import tempfile\n'), ((829, 880), 'llama_index.core.SimpleDirectoryReader', 'SimpleDirectoryReader', ([], {'input_files': '[temp_file_path]'}), '(input_files=[temp_file_path])\n', (850, 880), False, 'from llama_index.core import SimpleDirectoryReader, VectorStoreIndex\n'), ((681, 703), 'pathlib.Path', 'pathlib.Path', (['temp_dir'], {}), '(temp_dir)\n', (693, 703), False, 'import pathlib\n')]