Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,59 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
from
|
| 5 |
-
from langchain_community.tools.tavily.
|
| 6 |
-
|
| 7 |
-
# from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper
|
| 8 |
from langchain_groq import ChatGroq
|
| 9 |
from langgraph.graph import StateGraph, START, END
|
| 10 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 11 |
from langgraph.checkpoint.memory import MemorySaver
|
|
|
|
| 12 |
from langgraph.graph.message import add_messages
|
| 13 |
from typing import Annotated
|
| 14 |
from typing_extensions import TypedDict
|
| 15 |
-
from langchain_tavily.tool import TavilySearchResults # ✅ Corrected import
|
| 16 |
from tools import get_all_tools
|
| 17 |
|
| 18 |
-
#
|
| 19 |
load_dotenv()
|
| 20 |
os.environ["TAVILY_API_KEY"] = os.getenv("TAVILY_API_KEY")
|
| 21 |
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
tool_map = {
|
| 25 |
-
"Execute Code": "execute_code",
|
| 26 |
-
"Explain Code": "explain_code",
|
| 27 |
-
"Web Search": "web_search",
|
| 28 |
-
"Deep Think": "deep_think",
|
| 29 |
-
"Analyze Code": "analyze_code",
|
| 30 |
-
"Code Generator": "code_generator"
|
| 31 |
-
}
|
| 32 |
|
| 33 |
-
#
|
| 34 |
class State(TypedDict):
|
| 35 |
messages: Annotated[list, add_messages]
|
| 36 |
name: str
|
| 37 |
birthday: str
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
st.set_page_config(page_title="LangGraph Chatbot", layout="wide")
|
| 41 |
|
|
|
|
| 42 |
st.markdown("""
|
| 43 |
<style>
|
| 44 |
.stChatMessage {
|
|
@@ -47,24 +62,42 @@ st.markdown("""
|
|
| 47 |
border-radius: 12px;
|
| 48 |
max-width: 90%;
|
| 49 |
}
|
| 50 |
-
.user {
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
#floating-container {
|
| 54 |
-
display: flex;
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
.floating-popup {
|
| 59 |
-
margin-top: 0.5rem;
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
</style>
|
| 63 |
""", unsafe_allow_html=True)
|
| 64 |
|
| 65 |
st.title("🧠 LangGraph Chatbot (Groq + Tools)")
|
| 66 |
|
| 67 |
-
#
|
| 68 |
if "thread_id" not in st.session_state:
|
| 69 |
st.session_state.thread_id = "1"
|
| 70 |
if "chat_history" not in st.session_state:
|
|
@@ -72,13 +105,14 @@ if "chat_history" not in st.session_state:
|
|
| 72 |
if "show_upload" not in st.session_state:
|
| 73 |
st.session_state.show_upload = False
|
| 74 |
if "selected_tools" not in st.session_state:
|
| 75 |
-
st.session_state.selected_tools = ["
|
| 76 |
|
| 77 |
-
#
|
| 78 |
for msg in st.session_state.chat_history:
|
| 79 |
role = "user" if isinstance(msg, HumanMessage) else "bot"
|
| 80 |
st.markdown(f"<div class='stChatMessage {role}'>{msg.content}</div>", unsafe_allow_html=True)
|
| 81 |
|
|
|
|
| 82 |
with st.container():
|
| 83 |
with st.form("chat_form", clear_on_submit=True):
|
| 84 |
st.markdown('<div id="floating-container">', unsafe_allow_html=True)
|
|
@@ -91,6 +125,7 @@ with st.container():
|
|
| 91 |
|
| 92 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 93 |
|
|
|
|
| 94 |
if toggle:
|
| 95 |
st.session_state.show_upload = not st.session_state.show_upload
|
| 96 |
|
|
@@ -101,41 +136,15 @@ with st.container():
|
|
| 101 |
|
| 102 |
st.session_state.selected_tools = st.multiselect(
|
| 103 |
"🛠 Select Tools:",
|
| 104 |
-
|
| 105 |
default=st.session_state.selected_tools
|
| 106 |
)
|
| 107 |
|
|
|
|
| 108 |
submitted = st.form_submit_button(label="Send")
|
| 109 |
-
|
| 110 |
if submitted and user_input:
|
| 111 |
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
| 112 |
-
|
| 113 |
-
# ---------------------- Get Selected Tools ----------------------
|
| 114 |
-
all_tools = get_all_tools()
|
| 115 |
-
selected_tool_names = [tool_map[name] for name in st.session_state.selected_tools]
|
| 116 |
-
tools = [tool for tool in all_tools if tool.name in selected_tool_names]
|
| 117 |
-
|
| 118 |
-
# ---------------------- LLM & Graph ----------------------
|
| 119 |
-
llm = ChatGroq(model="deepseek-r1-distill-llama-70b")
|
| 120 |
-
llm_with_tools = llm.bind_tools(tools=tools)
|
| 121 |
-
|
| 122 |
-
def ai_assistance(state: State):
|
| 123 |
-
result = llm_with_tools.invoke(state["messages"])
|
| 124 |
-
return {"messages": [result]}
|
| 125 |
-
|
| 126 |
-
builder = StateGraph(State)
|
| 127 |
-
builder.add_node("AI_Assistance", ai_assistance)
|
| 128 |
-
builder.add_node("tools", ToolNode(tools))
|
| 129 |
-
|
| 130 |
-
builder.add_edge(START, "AI_Assistance")
|
| 131 |
-
builder.add_conditional_edges("AI_Assistance", tools_condition)
|
| 132 |
-
builder.add_edge("tools", "AI_Assistance")
|
| 133 |
-
|
| 134 |
-
memory = MemorySaver()
|
| 135 |
-
graph = builder.compile(checkpointer=memory)
|
| 136 |
-
|
| 137 |
config = {"configurable": {"thread_id": st.session_state.thread_id}}
|
| 138 |
result = graph.invoke({"messages": st.session_state.chat_history}, config=config)
|
| 139 |
-
|
| 140 |
st.session_state.chat_history.append(result["messages"][-1])
|
| 141 |
st.rerun()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper
|
| 7 |
+
from langchain_community.tools.tavily.search import TavilySearchResults # ✅ Corrected import
|
|
|
|
|
|
|
| 8 |
from langchain_groq import ChatGroq
|
| 9 |
from langgraph.graph import StateGraph, START, END
|
| 10 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 11 |
from langgraph.checkpoint.memory import MemorySaver
|
| 12 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
| 13 |
from langgraph.graph.message import add_messages
|
| 14 |
from typing import Annotated
|
| 15 |
from typing_extensions import TypedDict
|
|
|
|
| 16 |
from tools import get_all_tools
|
| 17 |
|
| 18 |
+
# Load environment
|
| 19 |
load_dotenv()
|
| 20 |
os.environ["TAVILY_API_KEY"] = os.getenv("TAVILY_API_KEY")
|
| 21 |
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
| 22 |
|
| 23 |
+
tools = get_all_tools()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# LangGraph State
|
| 26 |
class State(TypedDict):
|
| 27 |
messages: Annotated[list, add_messages]
|
| 28 |
name: str
|
| 29 |
birthday: str
|
| 30 |
|
| 31 |
+
# LLM
|
| 32 |
+
llm = ChatGroq(model="deepseek-r1-distill-llama-70b")
|
| 33 |
+
llm_with_tools = llm.bind_tools(tools=tools)
|
| 34 |
+
|
| 35 |
+
# Memory
|
| 36 |
+
memory = MemorySaver()
|
| 37 |
+
|
| 38 |
+
# Define node
|
| 39 |
+
def ai_assistance(state: State):
|
| 40 |
+
result = llm_with_tools.invoke(state["messages"])
|
| 41 |
+
return {"messages": [result]}
|
| 42 |
+
|
| 43 |
+
# Build LangGraph
|
| 44 |
+
builder = StateGraph(State)
|
| 45 |
+
builder.add_node("AI_Assistance", ai_assistance)
|
| 46 |
+
builder.add_node("tools", ToolNode(tools))
|
| 47 |
+
|
| 48 |
+
builder.add_edge(START, "AI_Assistance")
|
| 49 |
+
builder.add_conditional_edges("AI_Assistance", tools_condition)
|
| 50 |
+
builder.add_edge("tools", "AI_Assistance")
|
| 51 |
+
graph = builder.compile(checkpointer=memory)
|
| 52 |
+
|
| 53 |
+
# Streamlit UI setup
|
| 54 |
st.set_page_config(page_title="LangGraph Chatbot", layout="wide")
|
| 55 |
|
| 56 |
+
# --- Custom Chat UI ---
|
| 57 |
st.markdown("""
|
| 58 |
<style>
|
| 59 |
.stChatMessage {
|
|
|
|
| 62 |
border-radius: 12px;
|
| 63 |
max-width: 90%;
|
| 64 |
}
|
| 65 |
+
.user {
|
| 66 |
+
background-color: #dcf8c6;
|
| 67 |
+
align-self: flex-end;
|
| 68 |
+
}
|
| 69 |
+
.bot {
|
| 70 |
+
background-color: #f1f0f0;
|
| 71 |
+
align-self: flex-start;
|
| 72 |
+
}
|
| 73 |
+
.input-box {
|
| 74 |
+
display: flex;
|
| 75 |
+
align-items: center;
|
| 76 |
+
gap: 0.5rem;
|
| 77 |
+
}
|
| 78 |
#floating-container {
|
| 79 |
+
display: flex;
|
| 80 |
+
align-items: center;
|
| 81 |
+
justify-content: space-between;
|
| 82 |
+
padding: 0.25rem 0.75rem;
|
| 83 |
+
background-color: #f9f9f9;
|
| 84 |
+
border-radius: 0.75rem;
|
| 85 |
+
margin-top: 1rem;
|
| 86 |
+
border: 1px solid #ccc;
|
| 87 |
}
|
| 88 |
.floating-popup {
|
| 89 |
+
margin-top: 0.5rem;
|
| 90 |
+
padding: 0.5rem;
|
| 91 |
+
border-radius: 0.5rem;
|
| 92 |
+
border: 1px solid #ccc;
|
| 93 |
+
background-color: white;
|
| 94 |
}
|
| 95 |
</style>
|
| 96 |
""", unsafe_allow_html=True)
|
| 97 |
|
| 98 |
st.title("🧠 LangGraph Chatbot (Groq + Tools)")
|
| 99 |
|
| 100 |
+
# Session State Init
|
| 101 |
if "thread_id" not in st.session_state:
|
| 102 |
st.session_state.thread_id = "1"
|
| 103 |
if "chat_history" not in st.session_state:
|
|
|
|
| 105 |
if "show_upload" not in st.session_state:
|
| 106 |
st.session_state.show_upload = False
|
| 107 |
if "selected_tools" not in st.session_state:
|
| 108 |
+
st.session_state.selected_tools = ["Arxiv", "Wikipedia", "Tavily"]
|
| 109 |
|
| 110 |
+
# Chat History
|
| 111 |
for msg in st.session_state.chat_history:
|
| 112 |
role = "user" if isinstance(msg, HumanMessage) else "bot"
|
| 113 |
st.markdown(f"<div class='stChatMessage {role}'>{msg.content}</div>", unsafe_allow_html=True)
|
| 114 |
|
| 115 |
+
# Chat Input Box
|
| 116 |
with st.container():
|
| 117 |
with st.form("chat_form", clear_on_submit=True):
|
| 118 |
st.markdown('<div id="floating-container">', unsafe_allow_html=True)
|
|
|
|
| 125 |
|
| 126 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 127 |
|
| 128 |
+
# Tool Selector & Upload Panel
|
| 129 |
if toggle:
|
| 130 |
st.session_state.show_upload = not st.session_state.show_upload
|
| 131 |
|
|
|
|
| 136 |
|
| 137 |
st.session_state.selected_tools = st.multiselect(
|
| 138 |
"🛠 Select Tools:",
|
| 139 |
+
["Arxiv", "Wikipedia", "Tavily"],
|
| 140 |
default=st.session_state.selected_tools
|
| 141 |
)
|
| 142 |
|
| 143 |
+
# Submit Action
|
| 144 |
submitted = st.form_submit_button(label="Send")
|
|
|
|
| 145 |
if submitted and user_input:
|
| 146 |
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
config = {"configurable": {"thread_id": st.session_state.thread_id}}
|
| 148 |
result = graph.invoke({"messages": st.session_state.chat_history}, config=config)
|
|
|
|
| 149 |
st.session_state.chat_history.append(result["messages"][-1])
|
| 150 |
st.rerun()
|