File size: 1,560 Bytes
08606a2
9040eb5
14bade9
077b0b8
 
 
9040eb5
08606a2
 
9040eb5
14bade9
 
9040eb5
75bfa09
14bade9
75bfa09
 
9040eb5
 
a22f65f
60a59c6
a22f65f
9040eb5
 
14bade9
9040eb5
 
14bade9
9040eb5
 
 
 
 
 
 
 
 
 
14bade9
9040eb5
 
 
 
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
import json

import streamlit as st

from chatbot.utils import download_test_data
from chatbot.utils import load_data

# Initialize message history
st.header("Chat with André's research 💬 📚")

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 André's research!"}]

# Load config values
with open(r"config.json") as config_file:
    config_details = json.load(config_file)


def main():
    # setup dataset
    download_test_data()
    index = load_data(config_details)
    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


if __name__ == "__main__":
    main()