File size: 3,940 Bytes
337edc7
 
 
dbc25e1
337edc7
 
 
faec4fe
 
 
dbc25e1
b8fb1a3
 
 
dbc25e1
 
 
 
 
337edc7
 
1b557f0
337edc7
 
 
 
1b557f0
337edc7
 
 
 
 
 
 
ceb7a0d
337edc7
 
 
b8fb1a3
 
 
 
 
337edc7
 
 
 
 
 
 
 
 
 
 
 
16b7301
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import streamlit as st
from llama_index import VectorStoreIndex, ServiceContext, Document
from llama_index.llms import OpenAI
from langchain.llms import OpenAI
import openai
from llama_index import SimpleDirectoryReader

st.set_page_config(page_title="HUD Audit Guide", page_icon="πŸ‚", layout="centered", initial_sidebar_state="auto", menu_items=None)
st.title("Ask the HUD Audit Guide πŸ’¬πŸ€–")
st.info("Check out more info on the complete HUD Audit Guide at the official [website](https://www.hudoig.gov/library/single-audit-guidance/hud-consolidated-audit-guide)", icon="πŸ“ƒ")


#openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')
openai_api_key = "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"

def generate_response(input_text):
    llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
    st.info(llm(input_text))

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 the HUD Audit Guide - Chapter 6 - Ginnie Mae Issuers of Mortgage-Backed Securities Audit Guidance!"}
    ]

@st.cache_resource(show_spinner=False)
def load_data():
    with st.spinner(text="Loading and indexing the HUD Audit Guide – 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.5, system_prompt="You are an expert on the Streamlit Python library and 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."))
        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 Streamlit Python library and 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.")
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
    #if not openai_api_key.startswith('sk-'):
    #    st.warning('Please enter your OpenAI API key!', icon='⚠')
    #else: # Only allow the user to proceed if it appears they've entered a valid OpenAI API key
    #    st.session_state.messages.append({"role": "user", "content": prompt})
    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

try:
    if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
        if not openai_api_key.startswith('sk-'):
            st.warning('Please enter your OpenAI API key!', icon='⚠')
        else: # Only allow the user to proceed if it appears they've entered a valid OpenAI API key
            st.session_state.messages.append({"role": "user", "content": prompt})
            # ... (rest of the code)
except Exception as e:
    st.error(f"An error occurred: {e}")