Spaces:
Runtime error
Runtime error
File size: 2,673 Bytes
7cf86d4 06f2510 7cf86d4 3dde7e5 7cf86d4 06f2510 7cf86d4 06f2510 7cf86d4 06f2510 7cf86d4 06f2510 7cf86d4 06f2510 7cf86d4 32456d5 4c77664 e6eb5f8 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import streamlit as st
from chatbot import RAGChatbot
import os
from dotenv import load_dotenv
import warnings
warnings.filterwarnings("ignore")
# Load environment variables from .env file
load_dotenv()
# Initialize chatbot and load session state for messages
@st.cache_resource
def initialize_chatbot():
chatbot = RAGChatbot(
pinecone_api_key=os.getenv('PINECONE_API_KEY'),
index_name='test',
)
# chatbot.ingest_data('./Data', empty=False)
return chatbot
chatbot = initialize_chatbot()
st.title("RAG Chatbot")
# Initialize session state for messages if it doesn't exist
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "Hi! I am Wagner, a highly intelligent and friendly AI assistant. I am developed to provide answers related to Daniel and Daniel's work"}
]
# Display chat history with icons for user and bot
def display_chat_messages():
for message in st.session_state.messages:
if message["role"] == "user":
with st.chat_message(message["role"], avatar="π€"): # User icon
st.markdown(message["content"])
elif message["role"] == "assistant":
with st.chat_message(message["role"], avatar="π€"): # Bot icon
st.markdown(message["content"])
# Call the function to display past messages
display_chat_messages()
# Input prompt from the user, placed below the past messages
prompt = st.chat_input("Ask me anything!")
# If there's a prompt, send it to the chatbot and get the response
if prompt:
# Add user input to the message history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display the user message immediately in the chat
with st.chat_message("user", avatar="π€"):
st.markdown(prompt)
# Get chatbot response
response, sources = chatbot.query_chatbot(prompt, k=15, rerank=True, past_messages=st.session_state.messages)
# Add chatbot response to the message history
st.session_state.messages.append({"role": "assistant", "content": response})
# Display the bot's response immediately after user input
with st.chat_message("assistant", avatar="π€"):
st.markdown(response)
# Optionally display relevant documents with metadata
if prompt and sources:
st.subheader("Relevant Documents")
if type(sources) != str:
docs = sources
for i, doc in enumerate(docs):
st.write(f"**Document {i+1}:**")
st.json({"source": doc})
elif type(sources) == str and sources != 'None':
st.write(f"**Document {1}:**")
st.json({"source": sources}) |