Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,21 +4,57 @@ from langchain_groq import ChatGroq
|
|
| 4 |
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 5 |
from langgraph.prebuilt import create_react_agent
|
| 6 |
|
| 7 |
-
# 1. Page
|
| 8 |
st.set_page_config(page_title="AI Math Agent", page_icon="🧮")
|
| 9 |
-
|
| 10 |
st.title("My First AI Math Agent 🧮")
|
| 11 |
|
| 12 |
# 2. Secret Retrieval
|
| 13 |
api_key = os.getenv("GROQ_API_KEY")
|
| 14 |
|
| 15 |
-
# 3. Validation Logic
|
| 16 |
if not api_key:
|
| 17 |
-
st.warning("⚠️
|
| 18 |
-
st.info("After adding the secret, you may need to 'Restart' the Space.")
|
| 19 |
st.stop()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 5 |
from langgraph.prebuilt import create_react_agent
|
| 6 |
|
| 7 |
+
# 1. Page Configuration
|
| 8 |
st.set_page_config(page_title="AI Math Agent", page_icon="🧮")
|
|
|
|
| 9 |
st.title("My First AI Math Agent 🧮")
|
| 10 |
|
| 11 |
# 2. Secret Retrieval
|
| 12 |
api_key = os.getenv("GROQ_API_KEY")
|
| 13 |
|
|
|
|
| 14 |
if not api_key:
|
| 15 |
+
st.warning("⚠️ Please add your GROQ_API_KEY to the Space Secrets in Settings.")
|
|
|
|
| 16 |
st.stop()
|
| 17 |
+
|
| 18 |
+
# 3. Initialize the Brain (LLM)
|
| 19 |
+
# We use temperature=0 for mathematical precision
|
| 20 |
+
llm = ChatGroq(
|
| 21 |
+
model_name="llama-3.3-70b-versatile",
|
| 22 |
+
temperature=0,
|
| 23 |
+
groq_api_key=api_key
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# 4. Load Math Tools & Create Agent
|
| 27 |
+
# 'llm-math' uses numexpr for advanced reasoning
|
| 28 |
+
tools = load_tools(["llm-math"], llm=llm)
|
| 29 |
+
agent_executor = create_react_agent(llm, tools)
|
| 30 |
+
|
| 31 |
+
# 5. Chat Interface Setup
|
| 32 |
+
if "messages" not in st.session_state:
|
| 33 |
+
st.session_state.messages = []
|
| 34 |
+
|
| 35 |
+
# Display conversation history
|
| 36 |
+
for message in st.session_state.messages:
|
| 37 |
+
with st.chat_message(message["role"]):
|
| 38 |
+
st.markdown(message["content"])
|
| 39 |
+
|
| 40 |
+
# 6. User Input Logic
|
| 41 |
+
if prompt := st.chat_input("Ask me a complex math question!"):
|
| 42 |
+
# Add user message to history
|
| 43 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 44 |
+
with st.chat_message("user"):
|
| 45 |
+
st.markdown(prompt)
|
| 46 |
+
|
| 47 |
+
# Generate Agent Response
|
| 48 |
+
with st.chat_message("assistant"):
|
| 49 |
+
with st.spinner("Thinking..."):
|
| 50 |
+
try:
|
| 51 |
+
# Invoke the agent
|
| 52 |
+
response = agent_executor.invoke({"messages": [("user", prompt)]})
|
| 53 |
+
|
| 54 |
+
# Get the final response from the message list
|
| 55 |
+
final_answer = response["messages"][-1].content
|
| 56 |
+
|
| 57 |
+
st.markdown(final_answer)
|
| 58 |
+
st.session_state.messages.append({"role": "assistant", "content": final_answer})
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"An error occurred: {e}")
|