code: from typing import Dict, Any
  import asyncio
  
  # Create a new event loop
  loop = asyncio.new_event_loop()
  
  # Set the event loop as the current event loop
  asyncio.set_event_loop(loop)
  
  from llama_index import (
      VectorStoreIndex,
      ServiceContext,
      download_loader,
  )
  from llama_index.llama_pack.base import BaseLlamaPack
  from llama_index.llms import OpenAI
  import streamlit as st
  from streamlit_pills import pills
  
  st.set_page_config(
      page_title=f"Chat with Snowflake's Wikipedia page, powered by LlamaIndex",
      page_icon="🦙",
      layout="centered",
      initial_sidebar_state="auto",
      menu_items=None,
  )
  
  if "messages" not in st.session_state:  # Initialize the chat messages history
      st.session_state["messages"] = [
          {"role": "assistant", "content": "Ask me a question about Snowflake!"}
      ]
  
  st.title(
      f"Chat with Snowflake's Wikipedia page, powered by LlamaIndex 💬🦙"
  )
  st.info(
      "This example is powered by the **[Llama Hub Wikipedia Loader](https://llamahub.ai/l/wikipedia)**. Use any of [Llama Hub's many loaders](https://llamahub.ai/) to retrieve and chat with your data via a Streamlit app.",
      icon="ℹ️",
  )
  
  def add_to_message_history(role, content):
      message = {"role": role, "content": str(content)}
      st.session_state["messages"].append(
          message
      )  # Add response to message history
  
  @st.cache_resource
  def load_index_data():
      WikipediaReader = download_loader(
          "WikipediaReader", custom_path="local_dir"
      )
      loader = WikipediaReader()
      docs = loader.load_data(pages=["Snowflake Inc."])
      service_context = ServiceContext.from_defaults(
          llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5)
      )
      index = VectorStoreIndex.from_documents(
          docs, service_context=service_context
      )
      return index
  
  index = load_index_data()
  
  selected = pills(
      "Choose a question to get started or write your own below.",
      [
          "What is Snowflake?",
          "What company did Snowflake announce they would acquire in October 2023?",
          "What company did Snowflake acquire in March 2022?",
          "When did Snowflake IPO?",
      ],
      clearable=True,
      index=None,
  )
  
  if "chat_engine" not in st.session_state:  # Initialize the query engine
      st.session_state["chat_engine"] = index.as_chat_engine(
          chat_mode="context", verbose=True
      )
  
  for message in st.session_state["messages"]:  # Display the prior chat messages
      with st.chat_message(message["role"]):
          st.write(message["content"])
  
  # To avoid duplicated display of answered pill questions each rerun
  if selected and selected not in st.session_state.get(
      "displayed_pill_questions", set()
  ):
      st.session_state.setdefault("displayed_pill_questions", set()).add(selected)
      with st.chat_message("user"):
          st.write(selected)
      with st.chat_message("assistant"):
          response = st.session_state["chat_engine"].stream_chat(selected)
          response_str = ""
          response_container = st.empty()
          for token in response.response_gen:
              response_str += token
              response_container.write(response_str)
          add_to_message_history("user", selected)
          add_to_message_history("assistant", response)
  
  if prompt := st.chat_input(
      "Your question"
  ):  # Prompt for user input and save to chat history
      add_to_message_history("user", prompt)
  
      # Display the new question immediately after it is entered
      with st.chat_message("user"):
          st.write(prompt)
  
      # If last message is not from assistant, generate a new response
      # if st.session_state["messages"][-1]["role"] != "assistant":
      with st.chat_message("assistant"):
          response = st.session_state["chat_engine"].stream_chat(prompt)
          response_str = ""
          response_container = st.empty()
          for token in response.response_gen:
              response_str += token
              response_container.write(response_str)
          # st.write(response.response)
          add_to_message_history("assistant", response.response)
  
      # Save the state of the generator
      st.session_state["response_gen"] = response.response_gen
  
apis: llama_index.VectorStoreIndex.from_documents llama_index.llms.OpenAI llama_index.download_loader
extract_api: [((78, 102), 'asyncio.new_event_loop', 'asyncio.new_event_loop', ([], {}), '()\n', (100, 102), False, 'import asyncio\n'), ((151, 179), 'asyncio.set_event_loop', 'asyncio.set_event_loop', (['loop'], {}), '(loop)\n', (173, 179), False, 'import asyncio\n'), ((420, 607), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': 'f"""Chat with Snowflake\'s Wikipedia page, powered by LlamaIndex"""', 'page_icon': '"""🦙"""', 'layout': '"""centered"""', 'initial_sidebar_state': '"""auto"""', 'menu_items': 'None'}), '(page_title=\n    f"Chat with Snowflake\'s Wikipedia page, powered by LlamaIndex",\n    page_icon=\'🦙\', layout=\'centered\', initial_sidebar_state=\'auto\',\n    menu_items=None)\n', (438, 607), True, 'import streamlit as st\n'), ((821, 896), 'streamlit.title', 'st.title', (['f"""Chat with Snowflake\'s Wikipedia page, powered by LlamaIndex 💬🦙"""'], {}), '(f"Chat with Snowflake\'s Wikipedia page, powered by LlamaIndex 💬🦙")\n', (829, 896), True, 'import streamlit as st\n'), ((903, 1149), 'streamlit.info', 'st.info', (['"""This example is powered by the **[Llama Hub Wikipedia Loader](https://llamahub.ai/l/wikipedia)**. Use any of [Llama Hub\'s many loaders](https://llamahub.ai/) to retrieve and chat with your data via a Streamlit app."""'], {'icon': '"""ℹ️"""'}), '(\n    "This example is powered by the **[Llama Hub Wikipedia Loader](https://llamahub.ai/l/wikipedia)**. Use any of [Llama Hub\'s many loaders](https://llamahub.ai/) to retrieve and chat with your data via a Streamlit app."\n    , icon=\'ℹ️\')\n', (910, 1149), True, 'import streamlit as st\n'), ((1841, 2131), 'streamlit_pills.pills', 'pills', (['"""Choose a question to get started or write your own below."""', "['What is Snowflake?',\n    'What company did Snowflake announce they would acquire in October 2023?',\n    'What company did Snowflake acquire in March 2022?',\n    'When did Snowflake IPO?']"], {'clearable': '(True)', 'index': 'None'}), "('Choose a question to get started or write your own below.', [\n    'What is Snowflake?',\n    'What company did Snowflake announce they would acquire in October 2023?',\n    'What company did Snowflake acquire in March 2022?',\n    'When did Snowflake IPO?'], clearable=True, index=None)\n", (1846, 2131), False, 'from streamlit_pills import pills\n'), ((1412, 1471), 'llama_index.download_loader', 'download_loader', (['"""WikipediaReader"""'], {'custom_path': '"""local_dir"""'}), "('WikipediaReader', custom_path='local_dir')\n", (1427, 1471), False, 'from llama_index import VectorStoreIndex, ServiceContext, download_loader\n'), ((1700, 1770), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['docs'], {'service_context': 'service_context'}), '(docs, service_context=service_context)\n', (1731, 1770), False, 'from llama_index import VectorStoreIndex, ServiceContext, download_loader\n'), ((3246, 3276), 'streamlit.chat_input', 'st.chat_input', (['"""Your question"""'], {}), "('Your question')\n", (3259, 3276), True, 'import streamlit as st\n'), ((2445, 2477), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (2460, 2477), True, 'import streamlit as st\n'), ((2487, 2515), 'streamlit.write', 'st.write', (["message['content']"], {}), "(message['content'])\n", (2495, 2515), True, 'import streamlit as st\n'), ((2770, 2793), 'streamlit.chat_message', 'st.chat_message', (['"""user"""'], {}), "('user')\n", (2785, 2793), True, 'import streamlit as st\n'), ((2803, 2821), 'streamlit.write', 'st.write', (['selected'], {}), '(selected)\n', (2811, 2821), True, 'import streamlit as st\n'), ((2831, 2859), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (2846, 2859), True, 'import streamlit as st\n'), ((2989, 2999), 'streamlit.empty', 'st.empty', ([], {}), '()\n', (2997, 2999), True, 'import streamlit as st\n'), ((3450, 3473), 'streamlit.chat_message', 'st.chat_message', (['"""user"""'], {}), "('user')\n", (3465, 3473), True, 'import streamlit as st\n'), ((3483, 3499), 'streamlit.write', 'st.write', (['prompt'], {}), '(prompt)\n', (3491, 3499), True, 'import streamlit as st\n'), ((3645, 3673), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (3660, 3673), True, 'import streamlit as st\n'), ((3801, 3811), 'streamlit.empty', 'st.empty', ([], {}), '()\n', (3809, 3811), True, 'import streamlit as st\n'), ((1635, 1681), 'llama_index.llms.OpenAI', 'OpenAI', ([], {'model': '"""gpt-3.5-turbo"""', 'temperature': '(0.5)'}), "(model='gpt-3.5-turbo', temperature=0.5)\n", (1641, 1681), False, 'from llama_index.llms import OpenAI\n')]