File size: 1,708 Bytes
fd5f784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from llm import build_rag_chain
from setup import setup

st.set_page_config(layout="wide")

st.title("LinuxGPT")

l_col, r_col = st.columns((3, 1))

if "trigger" not in st.session_state:
    st.session_state["trigger"] = False

def on_enter():
    st.session_state["trigger"] = True

with r_col:
    submit_button, openai_models = setup()

# chat input goes here:
with l_col:
    user_question = st.text_area(
        "Ask about Linux fundamentals",
        on_change=on_enter,
    )
    # BELOW IS TEMPORARY, JUST FOR DEMOS!
    api_key = st.secrets["api_key"]
    if (submit_button or st.session_state["trigger"]) and api_key and user_question:
        rag_chain = build_rag_chain(api_key=api_key)

        formatted_history = [
            {"role": "user", "content": item["question"]} if idx % 2 == 0
            else {"role": "assistant", "content": item["answer"]}
            for idx, item in enumerate(st.session_state.get("history", []))
        ]
        with st.spinner("Thinking..."):
            # invoke answer
            result = rag_chain.invoke({"input": user_question, "chat_history": formatted_history})
            answer = result['answer']

            st.header("LinuxGPT says:")
            st.write(answer)

            # add to chat history automatically
            if 'history' not in st.session_state:
                st.session_state['history'] = []
            st.session_state['history'].append({"question": user_question, "answer": answer})

    else:
        st.write("Please provide an input. No API key is needed for demoing")

# clear history
if st.button("Clear History"):
    st.session_state['history'] = []
    st.write("Chat history cleared")