File size: 2,797 Bytes
94b7b89
6a70465
 
b67d34a
 
94b7b89
6a70465
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c03f2c4
 
94b7b89
 
 
 
 
c03f2c4
6a70465
94b7b89
 
6a70465
c03f2c4
94b7b89
 
6a70465
c2d5266
682a315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94b7b89
c2d5266
94b7b89
c2d5266
 
 
 
 
 
 
 
 
6a70465
 
c2d5266
6a70465
 
c2d5266
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
78
79
80
81
82
83
84
85
86
87
88
import streamlit as st
import time
from langchain.schema import HumanMessage, SystemMessage, AIMessage
from langchain.chat_models import ChatOpenAI


def get_chatmodel_response(question):
    # Retry logic
    max_retries = 3
    retries = 0

    while retries < max_retries:
        try:
            st.session_state['flowmessages'].append(HumanMessage(content=question))
            answer = chat(st.session_state['flowmessages'])
            st.session_state['flowmessages'].append(AIMessage(content=answer.content))
            return answer.content
        except Exception as e:
            print(f"Error: {e}")
            if "Rate limit" in str(e):
                print(f"Rate limit exceeded. Waiting and retrying...")
                time.sleep(5)  # Adjust the waiting time as needed
                retries += 1
            else:
                print("Unhandled exception. Please try again later.")
                break

    print("Exceeded the maximum number of retries. Please try again later.")
    return None

# Streamlit app setup
st.set_page_config(page_title="Sisi Chatbot")
st.header("Hey, I'm Sisi!")

from dotenv import load_dotenv
load_dotenv()
import os

# ChatOpenAI class
chat = ChatOpenAI(temperature=0.5)

if 'flowmessages' not in st.session_state:
    st.session_state['flowmessages'] = [
        SystemMessage(content="You are an AI Friend, your name is Sisi and you was developed by Sailesh on December 5 2023. You have to be a nice friend and a AI assistant to the users and help them with what information they need. It should be short and sharp")
    ]

# Streamlit UI
with st.form(key='my_form',clear_on_submit=True):
    st.markdown(
        """
        <style>
            .stTextInput {
                border-radius: 15px;
                padding: 12px;
                margin-top: 10px;
                margin-bottom: 10px;
                box-shadow: 2px 2px 5px #888888;
                border: 1px solid #dddddd;
                font-size: 16px;
                width: 100%;  /* Make the input box full width */
                height: 100px;  /* Set the height of the input box */
            }
        </style>
        """,
        unsafe_allow_html=True
    )

    input_question = st.text_input("Type here.", key="input")

    submit = st.form_submit_button("Submit")

# If the "Submit" button is clicked
if submit:

    
    # Display loading message while processing
    with st.spinner("Analyzing..."):
        st.header(":blue[You]", divider=True)
        st.caption(input_question)
        
        st.header("Sisi", divider=True)
        response = get_chatmodel_response(input_question)

    if response is not None:
       
        st.write(response)
    else:
        st.subheader("Error: Unable to get response. Please try again later.")