goketech commited on
Commit
1df09ca
·
verified ·
1 Parent(s): 5389d2a

Upload 2 files

Browse files
pages/1_💬_basic_chatbot.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import utils
2
+ import streamlit as st
3
+ from streaming import StreamHandler
4
+
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain.chains.conversation.base import ConversationChain
7
+
8
+ st.set_page_config(page_title="Chatbot", page_icon="💬")
9
+ st.header('Basic Chatbot')
10
+ st.write('Allows users to interact with a Language Model')
11
+
12
+ class BasicChatbot:
13
+
14
+ def __init__(self):
15
+ self.openai_model = utils.configure_openai()
16
+
17
+ def setup_chain(self):
18
+ llm = ChatOpenAI(model_name=self.openai_model, temperature=0, streaming=True)
19
+ chain = ConversationChain(llm=llm, verbose=True)
20
+ return chain
21
+
22
+ @utils.enable_chat_history
23
+ def main(self):
24
+ chain = self.setup_chain()
25
+ user_query = st.chat_input(placeholder="Ask me anything!")
26
+ if user_query:
27
+ utils.display_msg(user_query, 'user')
28
+ with st.chat_message("assistant"):
29
+ st_cb = StreamHandler(st.empty())
30
+ result = chain.invoke(
31
+ {"input":user_query},
32
+ {"callbacks": [st_cb]}
33
+ )
34
+ response = result["response"]
35
+ st.session_state.messages.append({"role": "assistant", "content": response})
36
+
37
+ if __name__ == "__main__":
38
+ obj = BasicChatbot()
39
+ obj.main()
pages/2_🧠_context_aware_chatbot.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import utils
2
+ import streamlit as st
3
+ from streaming import StreamHandler
4
+
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain.chains.conversation.base import ConversationChain
7
+ from langchain.memory.buffer import ConversationBufferMemory
8
+ from langchain.memory import ConversationSummaryMemory
9
+
10
+ st.set_page_config(page_title="Context aware chatbot", page_icon="🧠")
11
+ st.header('Context aware chatbot')
12
+ st.write('Enhancing Chatbot Interactions through Context Awareness')
13
+
14
+ class ContextChatbot:
15
+
16
+ def __init__(self):
17
+ self.openai_model = utils.configure_openai()
18
+
19
+ @st.cache_resource
20
+ def setup_chain(_self):
21
+ summarizer = ChatOpenAI(model_name= "gpt-4o", temperature=0, streaming=True)
22
+ memory = ConversationSummaryMemory(llm = summarizer)
23
+ llm = ChatOpenAI(model_name=_self.openai_model, temperature=0, streaming=True)
24
+ chain = ConversationChain(llm=llm, memory=memory, verbose=True)
25
+ return chain
26
+
27
+ @utils.enable_chat_history
28
+ def main(self):
29
+ chain = self.setup_chain()
30
+ user_query = st.chat_input(placeholder="Ask me anything!")
31
+ if user_query:
32
+ utils.display_msg(user_query, 'user')
33
+ with st.chat_message("assistant"):
34
+ st_cb = StreamHandler(st.empty())
35
+ result = chain.invoke(
36
+ {"input":user_query},
37
+ {"callbacks": [st_cb]}
38
+ )
39
+ response = result["response"]
40
+ st.session_state.messages.append({"role": "assistant", "content": response})
41
+
42
+ if __name__ == "__main__":
43
+ obj = ContextChatbot()
44
+ obj.main()
45
+