File size: 1,538 Bytes
3bc9036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st

import sys
import os

# Add the directory containing this script to the Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

def chatbot_say(message):
    with st.chat_message("assistant", avatar="🔮"):
        st.markdown(message)

        # Add chatbot response to chat history
        st.session_state.chat_history.append(("assistant", message))

def run_chatbot():
    from perplexity_api import chat_completion

    # Initialise chat history in session state
    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = []

    # Display chat history
    for role, content in st.session_state.chat_history:
        with st.chat_message(role):
            st.markdown(content)

    # If user has entered a message, add it to chat history and get chatbot response
    if prompt := st.chat_input("Say something: "):
        # Display user message
        with st.chat_message("user"):
            st.markdown(prompt)
        
        # Add user message to chat history
        st.session_state.chat_history.append(("user", prompt))

        info = "none"
        if st.session_state.latest: 
            info = st.session_state.latest
            
        response = chat_completion(prompt, info, mode="normal")

        # Display chatbot response
        with st.chat_message("assistant", avatar="🔮"):
            st.markdown(response)

        # Add chatbot response to chat history
        st.session_state.chat_history.append(("assistant", response))