code: import streamlit as st
  import openai
  import os.path as op
  from llama_index import ServiceContext
  from llama_index import VectorStoreIndex,download_loader
  from pathlib import Path
  from llama_index.retrievers import VectorIndexRetriever
  from llama_index.llms import OpenAI
  
  
  # specify path to CSV file, OPENAI api_key, and model below
  FILE_PATH = "../data/siriraj_doctor_details.csv"
  assert op.exists(FILE_PATH), f"CSV file not found at {FILE_PATH}, please check the file path."
  openai.api_key = "sk-..."
  MODEL = "gpt-4"
  
  st.set_page_config(page_title="Chatbot for doctor appointment", page_icon="🦙", layout="centered", initial_sidebar_state="auto", menu_items=None)
  st.title("Chatbot for doctor appointment")
  st.info("แชทบอทช่วยตอบคำถามสำหรับการนัดหมายแพทย์ที่โรงพยาบาลศิริราช ปิยมหาราชการุณย์ ดูข้อมูลแพทย์เพิ่มเติมได้ที่ https://www.siphhospital.com/th/medical-services/find-doctor", icon="📃")
  
  system_prompt = """
  Given the following doctors' data in the CSV file and their embeddings, create a response in Thai to a patient asking about scheduling an appointment,\
  inquiring about the doctor's expertise, or seeking a recommendation for a doctor based on their needs. \
  Note that user may inquire in a more casual text and you need to understand infer what they need before response.\
  If user ask about doctor's data e.g. name, please provide information back in an easy to read format.\
  Use only the data provided. The response should be in Thai and do not hallucinate. \
  """
  
  llm = OpenAI(model=MODEL, system_prompt=system_prompt, temperature=0.3)
  service_context = ServiceContext.from_defaults(llm=llm)
  
  
  @st.cache_resource(show_spinner=False)
  def load_data(file_path: str):
      with st.spinner(text="Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes."):
          PandasCSVReader = download_loader("PandasCSVReader")
          loader = PandasCSVReader()
          docs = loader.load_data(file=Path(file_path))
          index = VectorStoreIndex.from_documents(docs, service_context=service_context)
      return index
  
  index = load_data(FILE_PATH)
  chat_engine = index.as_chat_engine(chat_mode="context")
  
  if "messages" not in st.session_state.keys(): # Initialize the chat messages history
      st.session_state.messages = [
          {"role": "assistant", "content": "สอบถามข้อมูลการนัดหมายแพทย์ได้ที่นี่ครับ"}
      ]
  
  if "chat_engine" not in st.session_state.keys(): # Initialize the chat engine
          st.session_state.chat_engine = chat_engine
  
  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})
  messages = getattr(st.session_state, 'messages', [])
  for message in 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 messages[-1]["role"] != "assistant":
      with st.chat_message("assistant"):
          with st.spinner("Thinking..."):
              response = st.session_state.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.ServiceContext.from_defaults llama_index.download_loader
extract_api: [((389, 409), 'os.path.exists', 'op.exists', (['FILE_PATH'], {}), '(FILE_PATH)\n', (398, 409), True, 'import os.path as op\n'), ((520, 669), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Chatbot for doctor appointment"""', 'page_icon': '"""🦙"""', 'layout': '"""centered"""', 'initial_sidebar_state': '"""auto"""', 'menu_items': 'None'}), "(page_title='Chatbot for doctor appointment', page_icon=\n    '🦙', layout='centered', initial_sidebar_state='auto', menu_items=None)\n", (538, 669), True, 'import streamlit as st\n'), ((665, 707), 'streamlit.title', 'st.title', (['"""Chatbot for doctor appointment"""'], {}), "('Chatbot for doctor appointment')\n", (673, 707), True, 'import streamlit as st\n'), ((708, 904), 'streamlit.info', 'st.info', (['"""แชทบอทช่วยตอบคำถามสำหรับการนัดหมายแพทย์ที่โรงพยาบาลศิริราช ปิยมหาราชการุณย์ ดูข้อมูลแพทย์เพิ่มเติมได้ที่ https://www.siphhospital.com/th/medical-services/find-doctor"""'], {'icon': '"""📃"""'}), "(\n    'แชทบอทช่วยตอบคำถามสำหรับการนัดหมายแพทย์ที่โรงพยาบาลศิริราช ปิยมหาราชการุณย์ ดูข้อมูลแพทย์เพิ่มเติมได้ที่ https://www.siphhospital.com/th/medical-services/find-doctor'\n    , icon='📃')\n", (715, 904), True, 'import streamlit as st\n'), ((1487, 1552), 'llama_index.llms.OpenAI', 'OpenAI', ([], {'model': 'MODEL', 'system_prompt': 'system_prompt', 'temperature': '(0.3)'}), '(model=MODEL, system_prompt=system_prompt, temperature=0.3)\n', (1493, 1552), False, 'from llama_index.llms import OpenAI\n'), ((1571, 1608), 'llama_index.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm'}), '(llm=llm)\n', (1599, 1608), False, 'from llama_index import ServiceContext\n'), ((1612, 1649), 'streamlit.cache_resource', 'st.cache_resource', ([], {'show_spinner': '(False)'}), '(show_spinner=False)\n', (1629, 1649), True, 'import streamlit as st\n'), ((2156, 2179), 'streamlit.session_state.keys', 'st.session_state.keys', ([], {}), '()\n', (2177, 2179), True, 'import streamlit as st\n'), ((2370, 2393), 'streamlit.session_state.keys', 'st.session_state.keys', ([], {}), '()\n', (2391, 2393), True, 'import streamlit as st\n'), ((2489, 2519), 'streamlit.chat_input', 'st.chat_input', (['"""Your question"""'], {}), "('Your question')\n", (2502, 2519), True, 'import streamlit as st\n'), ((2574, 2643), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'user', 'content': prompt}"], {}), "({'role': 'user', 'content': prompt})\n", (2606, 2643), True, 'import streamlit as st\n'), ((1690, 1802), 'streamlit.spinner', 'st.spinner', ([], {'text': '"""Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes."""'}), "(text=\n    'Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes.'\n    )\n", (1700, 1802), True, 'import streamlit as st\n'), ((1820, 1854), 'llama_index.download_loader', 'download_loader', (['"""PandasCSVReader"""'], {}), "('PandasCSVReader')\n", (1835, 1854), False, 'from llama_index import VectorStoreIndex, download_loader\n'), ((1960, 2030), 'llama_index.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['docs'], {'service_context': 'service_context'}), '(docs, service_context=service_context)\n', (1991, 2030), False, 'from llama_index import VectorStoreIndex, download_loader\n'), ((2765, 2797), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (2780, 2797), True, 'import streamlit as st\n'), ((2807, 2835), 'streamlit.write', 'st.write', (["message['content']"], {}), "(message['content'])\n", (2815, 2835), True, 'import streamlit as st\n'), ((2950, 2978), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (2965, 2978), True, 'import streamlit as st\n'), ((2993, 3018), 'streamlit.spinner', 'st.spinner', (['"""Thinking..."""'], {}), "('Thinking...')\n", (3003, 3018), True, 'import streamlit as st\n'), ((3043, 3084), 'streamlit.session_state.chat_engine.chat', 'st.session_state.chat_engine.chat', (['prompt'], {}), '(prompt)\n', (3076, 3084), True, 'import streamlit as st\n'), ((3097, 3124), 'streamlit.write', 'st.write', (['response.response'], {}), '(response.response)\n', (3105, 3124), True, 'import streamlit as st\n'), ((3211, 3252), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (['message'], {}), '(message)\n', (3243, 3252), True, 'import streamlit as st\n'), ((1927, 1942), 'pathlib.Path', 'Path', (['file_path'], {}), '(file_path)\n', (1931, 1942), False, 'from pathlib import Path\n')]