Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +86 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_groq import ChatGroq
|
| 3 |
+
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 4 |
+
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
| 5 |
+
from langchain.agents import initialize_agent, AgentType
|
| 6 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Load API keys from environment variables
|
| 11 |
+
load_dotenv()
|
| 12 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 13 |
+
|
| 14 |
+
# Initialize Search Tools
|
| 15 |
+
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 16 |
+
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 17 |
+
|
| 18 |
+
wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 19 |
+
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
|
| 20 |
+
|
| 21 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 22 |
+
|
| 23 |
+
# --------------------------- Streamlit UI Setup ---------------------------
|
| 24 |
+
|
| 25 |
+
# Page Configuration
|
| 26 |
+
st.set_page_config(page_title="π LangChain Search Assistant", page_icon="π", layout="wide")
|
| 27 |
+
|
| 28 |
+
# Custom Styling
|
| 29 |
+
st.markdown(
|
| 30 |
+
"""
|
| 31 |
+
<style>
|
| 32 |
+
body { background-color: #f0f2f6; }
|
| 33 |
+
.stChatMessage { border-radius: 12px; padding: 10px; margin-bottom: 10px; }
|
| 34 |
+
.stChatMessage-user { background-color: #4a90e2; color: white; }
|
| 35 |
+
.stChatMessage-assistant { background-color: #f8f9fa; color: black; }
|
| 36 |
+
.stSpinner { font-size: 18px; font-weight: bold; }
|
| 37 |
+
</style>
|
| 38 |
+
""",
|
| 39 |
+
unsafe_allow_html=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Title & Description
|
| 43 |
+
st.title("π LangChain - Chat with Search")
|
| 44 |
+
st.markdown(
|
| 45 |
+
"This chatbot can **search the web, retrieve articles from Arxiv, Wikipedia**, and more.\n\n"
|
| 46 |
+
"π‘ Try **asking about recent discoveries, technical concepts, or general knowledge!**"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# --------------------------- Chat Memory ---------------------------
|
| 50 |
+
|
| 51 |
+
# Initialize session state
|
| 52 |
+
if "messages" not in st.session_state:
|
| 53 |
+
st.session_state["messages"] = [
|
| 54 |
+
{"role": "assistant", "content": "Hi! I'm a chatbot that can search the web. How can I help you?"}
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
# Display previous messages
|
| 58 |
+
for msg in st.session_state.messages:
|
| 59 |
+
role = "π§βπ» User" if msg["role"] == "user" else "π€ Assistant"
|
| 60 |
+
st.chat_message(msg["role"]).markdown(f"**{role}**: {msg['content']}")
|
| 61 |
+
|
| 62 |
+
# --------------------------- Chat Input & Processing ---------------------------
|
| 63 |
+
|
| 64 |
+
# User Input
|
| 65 |
+
if prompt := st.chat_input("Ask me anything..."):
|
| 66 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 67 |
+
st.chat_message("user").markdown(f"**π§βπ» User**: {prompt}")
|
| 68 |
+
|
| 69 |
+
# Initialize LLM
|
| 70 |
+
llm = ChatGroq(groq_api_key=api_key, model_name="llama-3.3-70b-versatile", streaming=True)
|
| 71 |
+
tools = [search, arxiv, wiki]
|
| 72 |
+
|
| 73 |
+
search_agent = initialize_agent(
|
| 74 |
+
tools=tools,
|
| 75 |
+
llm=llm,
|
| 76 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 77 |
+
handle_parsing_errors=True
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with st.chat_message("assistant"):
|
| 81 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 82 |
+
with st.spinner("π Searching..."):
|
| 83 |
+
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 84 |
+
|
| 85 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 86 |
+
st.markdown(f"**π€ Assistant**: {response}")
|
requirements.txt
ADDED
|
Binary file (522 Bytes). View file
|
|
|