-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimproved_simple_chatbot.py
63 lines (49 loc) · 2.02 KB
/
improved_simple_chatbot.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
54
55
56
57
58
59
60
61
62
63
############################
# Improved Simple Chat Bot #
############################
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
import streamlit as st
import os
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.filterwarnings(action="ignore", category=DeprecationWarning)
load_dotenv()
os.environ["GOOGLE_API_KEY"] = os.environ.get("GENAI_API_KEY")
# Function to load the Google AI model and get responses
class GetGoogleAI():
def __init__(self):
self.llm = ChatGoogleGenerativeAI(
model="gemini-1.5-pro",
temperature=0,
max_retries=2,
)
def get_response(self, question):
return self.llm.predict(question)
# Main function, only run when file is directly called
def main():
# Initialing Google AI
chatAI = GetGoogleAI()
st.set_page_config(page_title="Simply Q&A ChatBot")
st.header("LangChain ChatBot")
# Creating a session state to store messages so they persist during run time
if "messages" not in st.session_state:
st.session_state.messages = []
# If there are messages, display them with appropirate roles
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat box for message input from the user
if input := st.chat_input("Ask Question"):
st.session_state.messages.append({"role": "user", "content": input})
# Show user's input
with st.chat_message("user"):
st.markdown(input)
# Get response from AI
ai_response = chatAI.get_response(input)
st.session_state.messages.append({"role": "assistant", "content": ai_response})
# Show assistant's input
with st.chat_message("assistant"):
st.markdown(ai_response)
if __name__ == "__main__":
main()