File size: 2,994 Bytes
5f9eb33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
import streamlit as st
import openai
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Configure OpenAI API
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# System prompt template
SYSTEM_PROMPT = """You are an AI assistant for the University of Engineering and Technology (UET). 

Your role is to provide accurate and helpful information about {university}.

You should:

1. Provide information about academic programs, admission requirements, and campus facilities

2. Answer questions about university policies, procedures, and important dates

3. Guide students about registration, courses, and academic matters

4. Share information about research opportunities and faculty

5. Direct users to official university resources when needed



Always maintain a professional and helpful tone. If you're unsure about any information, 

acknowledge the limitation and suggest contacting the university directly."""

def get_chatbot_response(messages):
    """Get response from OpenAI API"""
    try:
        response = client.chat.completions.create(
            model="gpt-3.5-turbo", 
            messages=messages,
            temperature=0.7,
            max_tokens=500
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

def main():
    st.title("UET Chatbot")
    st.write("Welcome to the UET Chatbot! Ask me anything about UET Peshawar or UET Mardan.")

    # Dropdown to select university
    university = st.selectbox("Select University", ["UET Peshawar", "UET Mardan"])

    # Initialize chat history
    if "messages" not in st.session_state:
        st.session_state.messages = [{"role": "system", "content": SYSTEM_PROMPT.format(university=university)}]

    # Update system prompt if university changes
    if st.session_state.messages[0]["content"] != SYSTEM_PROMPT.format(university=university):
        st.session_state.messages = [{"role": "system", "content": SYSTEM_PROMPT.format(university=university)}]

    # Display chat history
    for message in st.session_state.messages:
        if message["role"] != "system":
            with st.chat_message(message["role"]):
                st.write(message["content"])

    # Chat input
    if prompt := st.chat_input("What would you like to know?"):
        # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": prompt})
        
        # Display user message
        with st.chat_message("user"):
            st.write(prompt)

        # Get chatbot response
        with st.chat_message("assistant"):
            response = get_chatbot_response(st.session_state.messages)
            st.write(response)
            
        # Add assistant response to chat history
        st.session_state.messages.append({"role": "assistant", "content": response})

if __name__ == "__main__":
    main()