IPL_Embedchain / app.py
vraman54's picture
Update app.py
eaba68a verified
raw
history blame
No virus
2.48 kB
import os
from embedchain import App
import streamlit as st
config = {
'llm': {
'provider': 'huggingface',
'config': {
'model': 'meta-llama/Meta-Llama-3-8B-Instruct',
'max_tokens': 200,
'top_p': 0.5
}
},
'embedder': {
'provider': 'huggingface',
'config': {
'model': 'sentence-transformers/all-mpnet-base-v2'
}
}
}
st.title("πŸ’¬ Chatbot")
st.caption("πŸš€ An Embedchain app powered by Llama3!")
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "assistant",
"content": """
Hi! I'm a chatbot. I can answer your questions based on IPL Wiki Page""",
}
]
os.environ["HUGGINGFACE_ACCESS_TOKEN"] = st.secrets["HF_TOKEN"]
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Ask me anything!"):
app = App.from_config(config = config)
app.add("https://en.wikipedia.org/wiki/Indian_Premier_League")
if prompt.startswith("/add"):
with st.chat_message("user"):
st.markdown(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
prompt = prompt.replace("/add", "").strip()
with st.chat_message("assistant"):
message_placeholder = st.empty()
message_placeholder.markdown("Adding to knowledge base...")
app.add(prompt)
message_placeholder.markdown(f"Added {prompt} to knowledge base!")
st.session_state.messages.append({"role": "assistant", "content": f"Added {prompt} to knowledge base!"})
st.stop()
with st.chat_message("user"):
st.markdown(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("assistant"):
msg_placeholder = st.empty()
msg_placeholder.markdown("Thinking...")
full_response = ""
full_response_string = []
for response in app.chat("Remove the context information and send the response for the prompt-" + prompt):
msg_placeholder.empty()
full_response += response
full_response_string.clear()
full_response_string = full_response.rsplit('Answer:', 1)
msg_placeholder.markdown(full_response_string[1])
st.session_state.messages.append({"role": "assistant", "content": full_response_string[1]})