File size: 3,194 Bytes
5f76022
 
 
 
 
 
 
 
 
 
 
d39ba62
5f76022
 
5af5437
cc5db75
5f76022
 
 
 
cc5db75
 
 
cc59879
 
 
 
 
cff4658
ea87593
2be6f2e
cff4658
cc5db75
d39ba62
cc5db75
 
 
 
cff4658
 
cc5db75
 
 
873fd0b
 
25634f6
873fd0b
25634f6
5f76022
 
 
b88af3f
5f76022
 
 
 
 
b88af3f
5f76022
 
 
 
 
b88af3f
 
 
8a716b8
5f76022
 
b88af3f
5f76022
 
 
8552e3a
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
73
74
75
import streamlit as st
import openai
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Set the OpenAI API key from the environment variables
openai.api_key = os.getenv("API_KEY")

# Streamlit app:
def main():
    st.title("Tech Support Chatbot")
    st.text(" ")
    st.text("This is a demo of a customised tech support chatbot for ACME INC.")
    
    # Initialize or continue the session's conversation history
    if 'history' not in st.session_state:
        st.session_state.history = []
    if 'input_text' not in st.session_state:
        st.session_state.input_text = ""

    #Credit
    st.write(" ")
    st.write("Visit us [here](https://ai-solutions.ai) for more AI Solutions.")
    st.write(" ")

    # User input with a unique key that gets reset
    user_input = st.text_input("Tell us what IT issues you are experiencing:", key="input_text", on_change=handle_input)

    # Display the conversation history
    display_conversation()

def handle_input():
    user_input = st.session_state.input_text
    if user_input:
        response = get_message(user_input)
        st.session_state.history.append(f"User: {user_input}")
        st.session_state.history.append(f"IT Support: {response}")
        st.session_state.input_text = ""  # Clear the input after submission

def display_conversation():
    for i, message in enumerate(st.session_state.history[-10:]):  # Display last 10 messages
        if i % 2 == 0:
            st.markdown(f"<div style='background-color:#f5f5f5;padding:10px;border-radius:10px;'>{message}</div>&nbsp;", unsafe_allow_html=True)
        else:
            st.markdown(f"<div style='background-color:#e1f5fe;padding:10px;border-radius:10px;'>{message}</div>&nbsp;", unsafe_allow_html=True)

def get_message(user_input):
    context = """
    You are an IT Support chatbot specialized in tech support for our company ACME INC. Please do not ask for screenshots or images.
    You should be proficient in diagnosing and troubleshooting software issues, hardware issues, and issues with peripheral devices.
    If you think that a reboot/restart is needed, please explain clearly how this can help fix the user issue in detail.
    You should also be capable of addressing network connectivity issues, providing guidance on security and privacy features, 
    helping with data management and navigating through system settings for application permissions and software updates.
    Additionally, you have knowledge about commonly used productivity applications and tools that may help users in their daily tasks.
    Please keep the conversation focused solely around IT Support.
    """
    
    conversation_history = st.session_state.history
    conversation_history_with_context = [context] + conversation_history

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "system", "content": context}] + [{"role": "user", "content": msg} for msg in conversation_history] + [{"role": "user", "content": user_input}],
        max_tokens=450,
    )

    return response['choices'][0]['message']['content'].strip()

if __name__ == "__main__":
    main()