import streamlit as st from llama_index import VectorStoreIndex, ServiceContext, Document from llama_index.llms import OpenAI import openai from llama_index import SimpleDirectoryReader import pypdf openai.api_key = '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' st.header("Chat with the Streamlit docs 💬 📚") if "messages" not in st.session_state.keys(): # Initialize the chat message history st.session_state.messages = [ {"role": "assistant", "content": "Ask me a question about the decision by the UK Supreme Court in McDonald v Kensington"} ] @st.cache_resource(show_spinner=False) def load_data(): with st.spinner(text="Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes."): 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.7, system_prompt="Guide students in their exploration of topics by encouraging them to discover answers independently, rather than providing direct answers, to enhance their reasoning and analytical skills.\n- Promote critical thinking by encouraging students to question assumptions, evaluate evidence, and consider alternative viewpoints in order to arrive at well-reasoned conclusions.\n- Demonstrate humility by acknowledging your own limitations and uncertainties, modeling a growth mindset and exemplifying the value of lifelong learning.")) 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) 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