Esb-Chatbot / app.py
GovindRaj's picture
Update app.py
de7681a
import streamlit as st
from llama_index import VectorStoreIndex, ServiceContext, Document
from llama_index.llms import OpenAI
import openai
from llama_index import SimpleDirectoryReader
# from PyPDF2 import PdfReader
st.set_page_config(page_title="ESB Chatbot", page_icon="πŸ¦™", layout="centered", initial_sidebar_state="auto", menu_items=None)
openai.api_key = "sk"
st.title("ESB Chatbot")
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 ESB Speakers!"}
]
@st.cache_resource(show_spinner=False)
def load_data():
with st.spinner(text="Loading and indexing the ESB docs – hang tight! This should take 1-2 minutes."):
reader = SimpleDirectoryReader(input_dir="MaleFemale.pdf", recursive=True)
docs = reader.load_data()
service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-4", temperature=0.8, system_prompt="AI Assistant, as a sales executive at Executive Speakers Bureau specializing in providing information about ESB services and speakers, ensure that only managed speakers from the dataset are included. Avoid duplicating speakers, and refrain from introducing speakers not part of the dataset. If no suitable speakers are available, kindly refrain from suggesting external speakers. Please furnish the top 10 managed speakers, accompanied by brief descriptions and profile links, considering speech topics from the specified data source."))
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 Bain Report and your job is to answer technical questions. Assume that all questions are related to the Bain Report. Keep your answers technical and based on facts – do not hallucinate features.")
if "chat_engine" not in st.session_state.keys(): # Initialize the chat engine
st.session_state.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 = 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