Spaces:
Configuration error
Configuration error
Sync from GitHub via hub-sync
Browse filesFiles changed (5)
- DockerfileDeleted-20
- README.md-20
- app.pyAdded+238
- requirements.txt+15-3
- src/streamlit_app.pyDeleted-40
1234567891011121314151617181920FROM python:3.13.5-slim
WORKDIR /app
RUN apt-get update && apt-get install -y \ build-essential \ curl \ git \ && rm -rf /var/lib/apt/lists/*
COPY requirements.txt ./COPY src/ ./src/
RUN pip3 install -r requirements.txt
EXPOSE 8501
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]1234567891011121314151617181920---title: Search Engine LLMemoji: 🚀colorFrom: redcolorTo: redsdk: dockerapp_port: 8501tags:- streamlitpinned: falseshort_description: Streamlit template spacelicense: apache-2.0---
# Welcome to Streamlit!
Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [communityforums](https://discuss.streamlit.io).123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238import hashlibimport os
import streamlit as stfrom dotenv import load_dotenvfrom langchain.agents import create_agentfrom langchain_community.callbacks.streamlit import StreamlitCallbackHandlerfrom langchain_community.tools import ArxivQueryRun, DuckDuckGoSearchRun, WikipediaQueryRunfrom langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapperfrom langchain_chroma import Chromafrom langchain_core.documents import Documentfrom langchain_core.tools import toolfrom langchain_groq import ChatGroqfrom langchain_huggingface import HuggingFaceEmbeddingsfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom pypdf import PdfReader
load_dotenv()
if os.getenv("HF_TOKEN"): os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
@st.cache_resourcedef get_embeddings(): return HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
def build_retriever(uploaded_files): documents = []
for uploaded_file in uploaded_files: file_bytes = uploaded_file.getvalue()
if uploaded_file.type == "application/pdf" or uploaded_file.name.lower().endswith(".pdf"): reader = PdfReader(uploaded_file) for page_number, page in enumerate(reader.pages, start=1): page_text = page.extract_text() or "" if page_text.strip(): documents.append( Document( page_content=page_text, metadata={"source": uploaded_file.name, "page": page_number}, ) ) else: text = file_bytes.decode("utf-8", errors="ignore") if text.strip(): documents.append( Document( page_content=text, metadata={"source": uploaded_file.name}, ) )
if not documents: return None
text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500) splits = text_splitter.split_documents(documents) vectorstore = Chroma.from_documents(documents=splits, embedding=get_embeddings()) return vectorstore.as_retriever(search_kwargs={"k": 4})
def uploaded_files_signature(uploaded_files): digest = hashlib.sha256() for uploaded_file in uploaded_files: digest.update(uploaded_file.name.encode("utf-8")) digest.update(uploaded_file.getvalue()) return digest.hexdigest()
def create_documents_tool(retriever): @tool("uploaded_documents") def uploaded_documents(query: str) -> str: """Search the uploaded documents for information relevant to the user's question.""" docs = retriever.invoke(query) if not docs: return "No relevant uploaded document content was found."
chunks = [] for doc in docs: source = doc.metadata.get("source", "uploaded document") page = doc.metadata.get("page") label = f"{source}, page {page}" if page else source chunks.append(f"Source: {label}\n{doc.page_content}")
return "\n\n".join(chunks)
return uploaded_documents
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")
def default_messages(): return [ { "role": "assistant", "content": "Hi, choose tools from the sidebar and ask me anything.", } ]
st.set_page_config(page_title="LangChain Enhanced Tools Chat", page_icon="🔎")st.title("🔎 LangChain Chat with Selectable Tools")st.write( "Choose the tools you want to enable, then ask questions in the chat. " "When document chat is enabled, upload files in the sidebar first.")
with st.sidebar: st.header("Settings") api_key = os.getenv("GROQ_API_KEY") if api_key: st.success("Groq API key loaded from .env.") else: st.warning("GROQ_API_KEY is missing from .env or the environment.")
st.header("Tools") use_search = st.checkbox("Search", value=True) use_wiki = st.checkbox("Wikipedia", value=True) use_arxiv = st.checkbox("Arxiv", value=True) use_documents = st.checkbox("Uploaded documents", value=False)
uploaded_files = [] if use_documents: uploaded_files = st.file_uploader( "Add document/s", type=["pdf", "txt", "md"], accept_multiple_files=True, help="Upload PDFs or text files to chat against them.", )
current_tool_selection = { "search": use_search, "wiki": use_wiki, "arxiv": use_arxiv, "documents": use_documents,}
if "tool_selection" not in st.session_state: st.session_state["tool_selection"] = current_tool_selectionelif st.session_state["tool_selection"] != current_tool_selection: st.session_state["tool_selection"] = current_tool_selection st.session_state["messages"] = default_messages() st.session_state["chat_memory"] = [] st.toast("Tool selection changed. Chat was reinitialized.")
if "messages" not in st.session_state: st.session_state["messages"] = default_messages()
if "chat_memory" not in st.session_state: st.session_state["chat_memory"] = []
if "document_retriever_signature" not in st.session_state: st.session_state["document_retriever_signature"] = None
if "document_retriever" not in st.session_state: st.session_state["document_retriever"] = None
enabled_tools = []if use_search: enabled_tools.append(search)if use_wiki: enabled_tools.append(wiki)if use_arxiv: enabled_tools.append(arxiv)
if use_documents: if uploaded_files: signature = uploaded_files_signature(uploaded_files) if signature != st.session_state["document_retriever_signature"]: with st.sidebar.spinner("Indexing uploaded documents..."): st.session_state["document_retriever"] = build_retriever(uploaded_files) st.session_state["document_retriever_signature"] = signature
if st.session_state["document_retriever"]: enabled_tools.append(create_documents_tool(st.session_state["document_retriever"])) st.sidebar.success("Documents are ready for chat.") else: st.sidebar.warning("No readable text was found in the uploaded documents.") else: st.sidebar.info("Upload document/s to enable the document chat tool.")else: st.session_state["document_retriever"] = None st.session_state["document_retriever_signature"] = None
for msg in st.session_state.messages: st.chat_message(msg["role"]).write(msg["content"])
if prompt := st.chat_input(placeholder="What is machine learning?"): if not api_key: st.error("GROQ_API_KEY is missing. Add it to your environment or .env file.") st.stop()
if not enabled_tools: st.error("Select at least one tool from the sidebar before chatting.") st.stop()
st.session_state.messages.append({"role": "user", "content": prompt}) st.session_state["chat_memory"].append({"role": "user", "content": prompt}) st.chat_message("user").write(prompt)
llm = ChatGroq(groq_api_key=api_key, model_name="llama-3.1-8b-instant", streaming=True) search_agent = create_agent( model=llm, tools=enabled_tools, system_prompt=( "You are a helpful assistant. Use only the enabled tools when they are useful. " "If uploaded documents are enabled and the user asks about their files, use the " "uploaded_documents tool before answering. Provide concise answers and mention " "document sources when using uploaded document content." ), )
with st.chat_message("assistant"): st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False) result = search_agent.invoke( {"messages": st.session_state["chat_memory"]}, config={"callbacks": [st_cb]}, ) response = result["messages"][-1].content st.session_state.messages.append({"role": "assistant", "content": response}) st.session_state["chat_memory"].append({"role": "assistant", "content": response}) st.write(response)123123456789101112131415altairpandasstreamlitarxiv==3.0.0chromadb==1.5.7ddgs==9.13.1langchain==1.2.15langchain-chroma==1.1.0langchain-community==0.4.1langchain-core==1.2.30langchain-groq==1.1.2langchain-huggingface==1.2.1langchain-text-splitters==1.1.1pypdf==6.10.2python-dotenv==1.2.2sentence-transformers==5.4.1streamlit==1.56.0wikipedia==1.4.012345678910111213141516171819202122232425262728293031323334353637383940import altair as altimport numpy as npimport pandas as pdimport streamlit as st
"""# Welcome to Streamlit!
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [communityforums](https://discuss.streamlit.io).
In the meantime, below is an example of what you can do with just a few lines of code:"""
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
indices = np.linspace(0, 1, num_points)theta = 2 * np.pi * num_turns * indicesradius = indices
x = radius * np.cos(theta)y = radius * np.sin(theta)
df = pd.DataFrame({ "x": x, "y": y, "idx": indices, "rand": np.random.randn(num_points),})
st.altair_chart(alt.Chart(df, height=700, width=700) .mark_point(filled=True) .encode( x=alt.X("x", axis=None), y=alt.Y("y", axis=None), color=alt.Color("idx", legend=None, scale=alt.Scale()), size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])), ))DockerfileDeleted-20
README.md-20
app.pyAdded+238
requirements.txt+15-3
src/streamlit_app.pyDeleted-40