Test-LLM / pages /1_πŸ’¬_basic_chatbot.py
PraiseCode's picture
Create pages/1_πŸ’¬_basic_chatbot.py
4a82f95 verified
raw
history blame contribute delete
No virus
1.29 kB
import utils
import streamlit as st
from streaming import StreamHandler
from langchain_openai import ChatOpenAI
from langchain.chains.conversation.base import ConversationChain
st.set_page_config(page_title="Chatbot", page_icon="πŸ’¬")
st.header('Basic Chatbot')
st.write('Allows users to interact with a Language Model')
class BasicChatbot:
def __init__(self):
self.openai_model = utils.configure_openai()
def setup_chain(self):
llm = ChatOpenAI(model_name=self.openai_model, temperature=0, streaming=True)
chain = ConversationChain(llm=llm, verbose=True)
return chain
@utils.enable_chat_history
def main(self):
chain = self.setup_chain()
user_query = st.chat_input(placeholder="Ask me anything!")
if user_query:
utils.display_msg(user_query, 'user')
with st.chat_message("assistant"):
st_cb = StreamHandler(st.empty())
result = chain.invoke(
{"input":user_query},
{"callbacks": [st_cb]}
)
response = result["response"]
st.session_state.messages.append({"role": "assistant", "content": response})
if __name__ == "__main__":
obj = BasicChatbot()
obj.main()