-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (45 loc) · 1.53 KB
/
main.py
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
import streamlit as st
from streamlit_chat import message
from dotenv import load_dotenv
import os
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
SystemMessage,
HumanMessage,
AIMessage
)
def init():
load_dotenv()
# Load the OPENAI API key from the environment variable
if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "":
print("OPENAI_API_KEY is not set")
exit(1)
else:
print("OPENAI_API_KEY is set")
st.set_page_config(
page_title="My Personal Chatbot",
page_icon="🤖",
)
def main():
init()
st.header("My Personal Chatbot 🤖")
chat = ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo")
if "messages" not in st.session_state:
st.session_state.messages = [
SystemMessage(content="You are a helpful assistant.")
]
with st.sidebar:
user_input = st.text_input("Your message: ", key="user_input")
if user_input:
st.session_state.messages.append(HumanMessage(content=user_input))
with st.spinner("Thinking..."):
response = chat(st.session_state.messages)
st.session_state.messages.append(AIMessage(content=response.content))
messages = st.session_state.get('messages', [])
for i, msg in enumerate(messages[1:]):
if i % 2 == 0:
message(msg.content, is_user=True, key=str(i) + "_user")
else:
message(msg.content, is_user=False, key=str(i) + "_ai")
if __name__ == '__main__':
main()