File size: 2,039 Bytes
adc37f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
import warnings

import streamlit as st
from dotenv import load_dotenv

from src.chat_rag_agent import ChatRagAgent
from src.utils import render_chat_history, get_render_assistant_message

warnings.filterwarnings("ignore")

load_dotenv()

st.set_page_config(
    page_icon="images/logo2.png",
    initial_sidebar_state="collapsed"

)


@st.cache_resource(show_spinner=False)
def get_chat_rag_agent():
    return ChatRagAgent()


def calc_progress_perc():
    return min(round(st.session_state["ctx_len"] / chat_rag_agent.n_ctx * 100), 100)


def pbar_callback():
    pbar.progress(calc_progress_perc(), "Chat history limit")


with st.spinner("Engine loading"):
    chat_rag_agent = get_chat_rag_agent()

if "messages" not in st.session_state or st.sidebar.button("Clear chat history"):
    st.session_state["input_blocked"] = False
    st.session_state["messages"] = []
    st.session_state["ctx_len"] = 0
    st.title("Christian compass")
    st.markdown("What theological questions you have?")

pbar = st.sidebar.progress(calc_progress_perc(), "Chat history limit")

user_message = st.chat_input(disabled=st.session_state["input_blocked"])
if user_message:
    if not st.session_state["input_blocked"]:
        (message_generator, n_tokens), sources = chat_rag_agent.chat(
            st.session_state["messages"],
            user_message
        )
        st.session_state["ctx_len"] = n_tokens
        st.session_state["messages"].append(
            {
                "role": "user",
                "content": user_message
            }
        )
    render_chat_history()
    if not st.session_state["input_blocked"]:
        pbar_callback()
        message = get_render_assistant_message(message_generator, sources, pbar_callback)
        st.session_state["messages"].append(
            {
                "role": "assistant",
                "content": message
            }
        )
if st.session_state["ctx_len"] >= chat_rag_agent.n_ctx:
    st.session_state["input_blocked"] = True
    st.info("Chat history limit reached")