Surendradjh commited on
Commit
a438c33
·
verified ·
1 Parent(s): 2fc2b40

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +62 -0
  2. chat_history/sqlite.db +0 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_google_genai import ChatGoogleGenerativeAI
3
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
4
+ from langchain_core.output_parsers import StrOutputParser
5
+ from langchain_community.chat_message_histories import SQLChatMessageHistory
6
+ from langchain_core.runnables.history import RunnableWithMessageHistory
7
+ import os
8
+
9
+ API_KEY = os.getenv("api_key")
10
+
11
+ template = ChatPromptTemplate(
12
+ messages=[
13
+ ("system", "You're a helpful data science AI chatbot. Answer only questions related to data science and what he told you within a 300-word limit."),
14
+ MessagesPlaceholder(variable_name="chat_history"),
15
+ ("human", "{input}")
16
+ ]
17
+ )
18
+
19
+ model = ChatGoogleGenerativeAI(api_key=API_KEY, model="gemini-1.5-pro")
20
+ output = StrOutputParser()
21
+ chain = template | model | output
22
+
23
+ def messages_history(session_id):
24
+ return SQLChatMessageHistory(session_id=session_id, connection="sqlite:///Chat_history/sqlite.db")
25
+
26
+ conversation_chain = RunnableWithMessageHistory(
27
+ chain, messages_history, input_message_key="input", history_messages_key="chat_history"
28
+ )
29
+
30
+ with st.sidebar:
31
+ st.title("🤖 AI Data Science Chatbot")
32
+ st.header("User Login")
33
+ user_id = st.text_input("Enter your User ID:", key="user_id_input")
34
+
35
+ if not user_id:
36
+ st.warning("Please enter a User ID to start chatting.")
37
+ st.stop()
38
+
39
+ if "last_user_id" not in st.session_state or st.session_state.last_user_id != user_id:
40
+ st.session_state.chat_history = []
41
+ st.session_state.last_user_id = user_id
42
+
43
+ chat_history = messages_history(user_id).messages
44
+ st.session_state.chat_history = [(msg.type, msg.content) for msg in chat_history]
45
+
46
+ st.write("Welcome! Start chatting below:")
47
+
48
+ for role, message in st.session_state.chat_history:
49
+ st.chat_message(role).write(message)
50
+
51
+ user_input = st.chat_input("Type your message...")
52
+
53
+ if user_input:
54
+ st.session_state.chat_history.append(("user", user_input))
55
+ st.chat_message("user").write(user_input)
56
+
57
+ config = {"configurable": {"session_id": user_id}}
58
+ input_prompt = {"input": user_input}
59
+ response = conversation_chain.invoke(input_prompt, config=config)
60
+
61
+ st.session_state.chat_history.append(("assistant", response))
62
+ st.chat_message("assistant").write(response)
chat_history/sqlite.db ADDED
Binary file (16.4 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ langchain_core
3
+ langchain_community
4
+ langchain_google_genai