Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +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:///
|
| 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)
|
|
|
|
| 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:///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)
|