Spaces:
Sleeping
Sleeping
File size: 3,323 Bytes
710f415 |
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 77 78 79 80 81 82 83 84 85 86 |
import streamlit as st
from langchain_groq import ChatGroq
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
from langchain.agents import initialize_agent, AgentType
from langchain.callbacks import StreamlitCallbackHandler
import os
from dotenv import load_dotenv
# Load API keys from environment variables
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Initialize Search Tools
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
search = DuckDuckGoSearchRun(name="Search")
# --------------------------- Streamlit UI Setup ---------------------------
# Page Configuration
st.set_page_config(page_title="π LangChain Search Assistant", page_icon="π", layout="wide")
# Custom Styling
st.markdown(
"""
<style>
body { background-color: #f0f2f6; }
.stChatMessage { border-radius: 12px; padding: 10px; margin-bottom: 10px; }
.stChatMessage-user { background-color: #4a90e2; color: white; }
.stChatMessage-assistant { background-color: #f8f9fa; color: black; }
.stSpinner { font-size: 18px; font-weight: bold; }
</style>
""",
unsafe_allow_html=True
)
# Title & Description
st.title("π LangChain - Chat with Search")
st.markdown(
"This chatbot can **search the web, retrieve articles from Arxiv, Wikipedia**, and more.\n\n"
"π‘ Try **asking about recent discoveries, technical concepts, or general knowledge!**"
)
# --------------------------- Chat Memory ---------------------------
# Initialize session state
if "messages" not in st.session_state:
st.session_state["messages"] = [
{"role": "assistant", "content": "Hi! I'm a chatbot that can search the web. How can I help you?"}
]
# Display previous messages
for msg in st.session_state.messages:
role = "π§βπ» User" if msg["role"] == "user" else "π€ Assistant"
st.chat_message(msg["role"]).markdown(f"**{role}**: {msg['content']}")
# --------------------------- Chat Input & Processing ---------------------------
# User Input
if prompt := st.chat_input("Ask me anything..."):
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").markdown(f"**π§βπ» User**: {prompt}")
# Initialize LLM
llm = ChatGroq(groq_api_key=api_key, model_name="llama-3.3-70b-versatile", streaming=True)
tools = [search, arxiv, wiki]
search_agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors=True
)
with st.chat_message("assistant"):
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
with st.spinner("π Searching..."):
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
st.session_state.messages.append({"role": "assistant", "content": response})
st.markdown(f"**π€ Assistant**: {response}") |