code: import streamlit as st
  from llama_index import VectorStoreIndex, ServiceContext, Document
  from llama_index.llms import OpenAI
  import openai
  from llama_index import SimpleDirectoryReader
  
  st.set_page_config(page_title="Chat with the docs, powered by LlamaIndex")
  openai.api_key = st.secrets.openai_key
  st.title("Chat with the custom docs, using  LlamaIndex")
           
  if "messages" not in st.session_state.keys(): # Initialize the chat messages history
      st.session_state.messages = [
          {"role": "assistant", "content": "Ask me a question about Streamlit's open-source Python library!"}
      ]
  
  @st.cache_resource(show_spinner=False)
  def load_data():
      with st.spinner(text="Loading and indexing the Streamlit docs"):
          reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
          docs = reader.load_data()
          service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features."))
          index = VectorStoreIndex.from_documents(docs, service_context=service_context)
          return index
  
  index = load_data()
  # chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
  chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
  
  if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
      st.session_state.messages.append({"role": "user", "content": prompt})
  
  for message in st.session_state.messages: # Display the prior chat messages
      with st.chat_message(message["role"]):
          st.write(message["content"])
  
  # If last message is not from assistant, generate a new response
  if st.session_state.messages[-1]["role"] != "assistant":
      with st.chat_message("assistant"):
          with st.spinner("Thinking..."):
              response = chat_engine.chat(prompt)
              st.write(response.response)
              message = {"role": "assistant", "content": response.response}
              st.session_state.messages.append(message) # Add response to message history
apis: llama_index.VectorStoreIndex.from_documents llama_index.llms.OpenAI llama_index.SimpleDirectoryReader
extract_api: [((193, 267), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Chat with the docs, powered by LlamaIndex"""'}), "(page_title='Chat with the docs, powered by LlamaIndex')\n", (211, 267), True, 'import streamlit as st\n'), ((309, 365), 'streamlit.title', 'st.title', (['"""Chat with the custom docs, using  LlamaIndex"""'], {}), "('Chat with the custom docs, using  LlamaIndex')\n", (317, 365), True, 'import streamlit as st\n'), ((618, 655), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (635, 655), True, 'import streamlit as st\n'), ((399, 422), 'streamlit.session_state.keys', 'st.session_state.keys', ([], {}), '()\n', (420, 422), True, 'import streamlit as st\n'), ((1802, 1832), 'streamlit.chat_input', 'st.chat_input', (['"""Your question"""'], {}), "('Your question')\n", (1815, 1832), True, 'import streamlit as st\n'), ((1888, 1957), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'user', 'content': prompt}"], {}), "({'role': 'user', 'content': prompt})\n", (1920, 1957), True, 'import streamlit as st\n'), ((684, 742), 'streamlit.spinner', 'st.spinner', ([], {'text': '"""Loading and indexing the Streamlit docs"""'}), "(text='Loading and indexing the Streamlit docs')\n", (694, 742), True, 'import streamlit as st\n'), ((762, 819), 'llama_index.SimpleDirectoryReader', 'SimpleDirectoryReader', ([], {'input_dir': '"""./data"""', 'recursive': '(True)'}), "(input_dir='./data', recursive=True)\n", (783, 819), False, 'from llama_index import SimpleDirectoryReader\n'), ((1243, 1313), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['docs'], {'service_context': 'service_context'}), '(docs, service_context=service_context)\n', (1274, 1313), False, 'from llama_index import VectorStoreIndex, ServiceContext, Document\n'), ((2047, 2079), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (2062, 2079), True, 'import streamlit as st\n'), ((2090, 2118), 'streamlit.write', 'st.write', (["message['content']"], {}), "(message['content'])\n", (2098, 2118), True, 'import streamlit as st\n'), ((2255, 2283), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (2270, 2283), True, 'import streamlit as st\n'), ((2299, 2324), 'streamlit.spinner', 'st.spinner', (['"""Thinking..."""'], {}), "('Thinking...')\n", (2309, 2324), True, 'import streamlit as st\n'), ((2388, 2415), 'streamlit.write', 'st.write', (['response.response'], {}), '(response.response)\n', (2396, 2415), True, 'import streamlit as st\n'), ((2504, 2545), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (['message'], {}), '(message)\n', (2536, 2545), True, 'import streamlit as st\n'), ((915, 1234), 'llama_index.llms.OpenAI', 'OpenAI', ([], {'model': '"""gpt-3.5-turbo"""', 'temperature': '(0.5)', 'system_prompt': '"""You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features."""'}), "(model='gpt-3.5-turbo', temperature=0.5, system_prompt=\n    'You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.'\n    )\n", (921, 1234), False, 'from llama_index.llms import OpenAI\n')]