|
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 os |
|
|
|
st.set_page_config(page_title="HUD Audit Guide", page_icon="π", layout="centered", initial_sidebar_state="auto", menu_items=None) |
|
|
|
test_key_print = os.environ['OPENAI_KEY'] |
|
|
|
openai.api_key = test_key_print |
|
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="π") |
|
|
|
if "messages" not in st.session_state.keys(): |
|
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 HUD Audit Guide and your job is to answer technical questions. Assume that all questions are related to the HUD Audit Guide and Ginnie Mae Issuers. 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) |
|
|
|
if prompt := st.chat_input("Your question"): |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.write(message["content"]) |
|
|
|
|
|
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) |