Tuesday, 21 October 2025

#10 🤖Streamlit + LangChain — Build Agentic AI Dashboards

Combine the power of Streamlit (for RIA's [Rich Internet Application]  in pure Python) and LangChain (for agentic workflows, tools, memory, chains) to build sleek AI dashboards that think, act, and respond. Whether you’re creating a chat-agent, document assistant, or data-query bot — this guide gets you started fast. 🚀


1️⃣ Install the Libraries


!pip install streamlit langchain openai tiktoken

2️⃣ Build a Simple Agent Dashboard


import streamlit as st
from langchain.llms import OpenAI
from langchain import LLMChain
from langchain.prompts import PromptTemplate

st.set_page_config(page_title="Agentic AI Dashboard", layout="wide")
st.title("🤖 Ask Me Anything: AI Agent")

openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")

if openai_api_key:
    prompt = PromptTemplate(
        input_variables=["question"],
        template="You are a helpful assistant. Answer: {question}"
    )
    llm = OpenAI(openai_api_key=openai_api_key, temperature=0.5)
    chain = LLMChain(llm=llm, prompt=prompt)

    user_question = st.text_area("Enter your question:")
    if st.button("Ask"):
        response = chain.run(user_question)
        st.info(response)
else:
    st.warning("Please enter your OpenAI API Key in the sidebar.")

3️⃣ Add Tools and Agent with Thought Trace

Use LangChain Agent with tools and display agent thoughts/actions.


import streamlit as st
from langchain.agents import initialize_agent, Tool, AgentType
from langchain.llms import OpenAI
from langchain_community.callbacks.streamlit import StreamlitCallbackHandler

st.title("🛠️ Agent with Tools Demo")
openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")

if openai_api_key:
    llm = OpenAI(openai_api_key=openai_api_key, streaming=True)
    tools = [
        Tool(name="calculator", func=lambda x: eval(x), description="Evaluate math expressions")
    ]
    agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

    query = st.text_area("Ask the agent (it may use tools):")
    if st.button("Submit"):
        with st.container():
            callback = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
            result = agent.run(query, callbacks=[callback])
            st.success(result)
else:
    st.warning("Enter OpenAI API Key in sidebar.")

4️⃣ Document Upload + Q&A Dashboard


import streamlit as st
import pandas as pd
from langchain.chat_models import ChatOpenAI
from langchain.agents import create_pandas_dataframe_agent
from langchain.agents.agent_types import AgentType

st.title("📄 Ask Your Data Dashboard")
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")

if uploaded_file and openai_api_key:
    df = pd.read_csv(uploaded_file)
    st.dataframe(df.head())

    agent = create_pandas_dataframe_agent(
        llm=ChatOpenAI(openai_api_key=openai_api_key),
        df=df,
        agent_type=AgentType.OPENAI_FUNCTIONS,
        verbose=True
    )

    user_query = st.text_input("What do you want to ask about the data?")
    if st.button("Run Query"):
        response = agent.run(user_query)
        st.info(response)
else:
    st.warning("Please upload a CSV and enter OpenAI API Key.")

5️⃣ Deployment Tips

Once your dashboard works locally, deploy it via Streamlit Community Cloud — simply push your code + `requirements.txt` and click “Deploy”. Use `st.secrets.toml` to securely manage your OpenAI API key.


✅ What You Can Build Next

  • 🧠 Chatbots with memory, custom tools & chain logic
  • 📊 Data-agents that ingest documents, spreadsheets & databases
  • 🔁 Multi-step agent workflows (planning → executing → summarizing)

Bro, this opens up the door to full-fledged agentic AI dashboards — from assistants to analytics bots. Want me to prepare a **complete notebook + GitHub repo template** for this? Let’s go!

No comments:

Post a Comment

#21a Dunder Method - Samples

Python Dunder (Magic) Methods – Complete Guide with Demos Python Dunder (Magic) Methods – Complete Guide with Demos ...