Spaces:
Running
Running
import streamlit as st | |
from streamlit_chat import message | |
from dotenv import load_dotenv | |
from langchain.chat_models import ChatOpenAI | |
from langchain.schema import SystemMessage, HumanMessage,AIMessage | |
################################################################################## | |
##variable d'environement | |
load_dotenv() | |
################################################################################## | |
##streamlit page | |
def start(): | |
st.set_page_config(page_title="heeloo") | |
st.header('chatbot') | |
st.divider() | |
start() | |
#message('how are you') | |
#message('how are you', is_user=True) | |
################################################################################### | |
def main(): | |
chat = ChatOpenAI(temperature='0.5') | |
if "messages" not in st.session_state: | |
st.session_state.messages=[ | |
SystemMessage(content="You are an assistant") | |
] | |
#create input | |
input_user= st.text_input("Ask Question ", placeholder ="Ask Question") | |
#create button | |
button=st.button('Ask' ,use_container_width=True) | |
if button: | |
#add message to message history | |
st.session_state.messages.append(HumanMessage(content=input_user)) | |
#send message history to chat | |
resposne =chat(st.session_state.messages) | |
#display response | |
message(resposne, is_user=False) | |
#add response to message hisotry | |
st.session_state.messages.append(AIMessage(content=resposne.content)) | |
#get list of messages | |
messages = st.session_state.get('messages',[]) | |
#:1 start with index 1 | |
for i, msg in enumerate(messages): | |
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') | |
main() | |