code: """
  Chat with transcripts stored by Llamaindex in Weaviate Cloud
  """
  import openai
  import streamlit as st
  import weaviate
  from llama_index import ServiceContext
  from llama_index import VectorStoreIndex
  from llama_index.llms import OpenAI
  from llama_index.vector_stores import WeaviateVectorStore
  
  st.set_page_config(
      page_title="Chat with my Youtube Channel",
      page_icon="🦙",
      layout="centered",
      initial_sidebar_state="auto",
      menu_items=None,
  )
  
  openai.api_key = st.secrets["OPENAI_API_KEY"]
  
  weaviate_index = "LlamaIndex"
  
  
  def init_session_state():
      if "messages" not in st.session_state.keys():
          st.session_state["messages"] = [
              {"role": "assistant", "content": "Ask me a question"}
          ]
  
  
  @st.cache_resource(show_spinner=False)
  def load_weaviate_client():
      client = weaviate.Client(
          url=st.secrets["WEAVIATE_URL"],
          auth_client_secret=weaviate.AuthApiKey(api_key=st.secrets["WEAVIATE_API_KEY"]),
          additional_headers={"X-OpenAI-Api-Key": st.secrets["OPENAI_API_KEY"]},
      )
      return client
  
  
  @st.cache_resource(show_spinner=False)
  def load_weaviate_llamaindex(index_name: str):
      weaviate_client = load_weaviate_client()
  
      if not weaviate_client.schema.exists(index_name):
          st.error(
              f"Index / Weaviate class {index_name} does not exist. Please build it beforehand"
          )
          st.stop()
  
      vector_store = WeaviateVectorStore(
          weaviate_client=weaviate_client, index_name=index_name
      )
      service_context = ServiceContext.from_defaults(
          llm=OpenAI(
              model="gpt-3.5-turbo",
              temperature=0.2,
              system_prompt="You are an expert on the Streamlit Python library. 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.",
          )
      )
      loaded_index = VectorStoreIndex.from_vector_store(
          vector_store, service_context=service_context
      )
      return loaded_index
  
  
  def display_chat_history():
      for message in st.session_state["messages"]:
          with st.chat_message(message["role"]):
              st.write(message["content"])
  
  
  st.header("Chat with my Youtube Channel")
  
  init_session_state()
  
  index = load_weaviate_llamaindex(weaviate_index)
  chat_engine = index.as_chat_engine(
      chat_mode="condense_question", verbose=True
  )
  
  # Prompt for user input and save to chat history
  if prompt := st.chat_input("Your question"):
      st.session_state.messages.append(
          {"role": "user", "content": prompt}
  )
  
  # Display the prior chat messages
  display_chat_history()
  
  # 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_vector_store llama_index.llms.OpenAI llama_index.vector_stores.WeaviateVectorStore
extract_api: [((297, 443), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Chat with my Youtube Channel"""', 'page_icon': '"""🦙"""', 'layout': '"""centered"""', 'initial_sidebar_state': '"""auto"""', 'menu_items': 'None'}), "(page_title='Chat with my Youtube Channel', page_icon='🦙',\n    layout='centered', initial_sidebar_state='auto', menu_items=None)\n", (315, 443), True, 'import streamlit as st\n'), ((739, 776), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (756, 776), True, 'import streamlit as st\n'), ((1069, 1106), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (1086, 1106), True, 'import streamlit as st\n'), ((2235, 2276), 'streamlit.header', 'st.header', (['"""Chat with my Youtube Channel"""'], {}), "('Chat with my Youtube Channel')\n", (2244, 2276), True, 'import streamlit as st\n'), ((1414, 1489), 'llama_index.vector_stores.WeaviateVectorStore', 'WeaviateVectorStore', ([], {'weaviate_client': 'weaviate_client', 'index_name': 'index_name'}), '(weaviate_client=weaviate_client, index_name=index_name)\n', (1433, 1489), False, 'from llama_index.vector_stores import WeaviateVectorStore\n'), ((1946, 2032), 'llama_index.VectorStoreIndex.from_vector_store', 'VectorStoreIndex.from_vector_store', (['vector_store'], {'service_context': 'service_context'}), '(vector_store, service_context=\n    service_context)\n', (1980, 2032), False, 'from llama_index import VectorStoreIndex\n'), ((2498, 2528), 'streamlit.chat_input', 'st.chat_input', (['"""Your question"""'], {}), "('Your question')\n", (2511, 2528), True, 'import streamlit as st\n'), ((2534, 2603), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'user', 'content': prompt}"], {}), "({'role': 'user', 'content': prompt})\n", (2566, 2603), True, 'import streamlit as st\n'), ((594, 617), 'streamlit.session_state.keys', 'st.session_state.keys', ([], {}), '()\n', (615, 617), True, 'import streamlit as st\n'), ((1262, 1363), 'streamlit.error', 'st.error', (['f"""Index / Weaviate class {index_name} does not exist. Please build it beforehand"""'], {}), "(\n    f'Index / Weaviate class {index_name} does not exist. Please build it beforehand'\n    )\n", (1270, 1363), True, 'import streamlit as st\n'), ((1384, 1393), 'streamlit.stop', 'st.stop', ([], {}), '()\n', (1391, 1393), True, 'import streamlit as st\n'), ((2804, 2832), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (2819, 2832), True, 'import streamlit as st\n'), ((902, 961), 'weaviate.AuthApiKey', 'weaviate.AuthApiKey', ([], {'api_key': "st.secrets['WEAVIATE_API_KEY']"}), "(api_key=st.secrets['WEAVIATE_API_KEY'])\n", (921, 961), False, 'import weaviate\n'), ((1568, 1883), 'llama_index.llms.OpenAI', 'OpenAI', ([], {'model': '"""gpt-3.5-turbo"""', 'temperature': '(0.2)', 'system_prompt': '"""You are an expert on the Streamlit Python library. 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.2, system_prompt=\n    'You are an expert on the Streamlit Python library. 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", (1574, 1883), False, 'from llama_index.llms import OpenAI\n'), ((2158, 2190), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (2173, 2190), True, 'import streamlit as st\n'), ((2204, 2232), 'streamlit.write', 'st.write', (["message['content']"], {}), "(message['content'])\n", (2212, 2232), True, 'import streamlit as st\n'), ((2847, 2872), 'streamlit.spinner', 'st.spinner', (['"""Thinking..."""'], {}), "('Thinking...')\n", (2857, 2872), True, 'import streamlit as st\n'), ((2934, 2961), 'streamlit.write', 'st.write', (['response.response'], {}), '(response.response)\n', (2942, 2961), True, 'import streamlit as st\n'), ((3095, 3136), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (['message'], {}), '(message)\n', (3127, 3136), True, 'import streamlit as st\n')]