Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,24 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
st.
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
st.
|
| 19 |
-
st.stop()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
tools = load_tools(["llm-math"], llm=llm)
|
| 26 |
-
# TODO: Create the agent using the same function we used in your terminal script
|
| 27 |
-
return create_react_agent(llm, tools)
|
| 28 |
-
|
| 29 |
-
agent_executor = get_agent()
|
| 30 |
-
|
| 31 |
-
# 3. Chat History Setup
|
| 32 |
-
if "messages" not in st.session_state:
|
| 33 |
-
st.session_state.messages = []
|
| 34 |
-
|
| 35 |
-
for message in st.session_state.messages:
|
| 36 |
-
with st.chat_message(message["role"]):
|
| 37 |
-
st.markdown(message["content"])
|
| 38 |
-
|
| 39 |
-
# 4. The Interaction Loop
|
| 40 |
-
if prompt := st.chat_input("What math problem should I solve?"):
|
| 41 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 42 |
-
with st.chat_message("user"):
|
| 43 |
-
st.markdown(prompt)
|
| 44 |
-
|
| 45 |
-
with st.chat_message("assistant"):
|
| 46 |
-
with st.spinner("Thinking..."):
|
| 47 |
-
# TODO: Call the agent_executor with the correct input format
|
| 48 |
-
# Hint: We used .invoke({"messages": [("human", prompt)]}) before!
|
| 49 |
-
result = agent_executor.invoke({"messages": [("human", prompt)]})
|
| 50 |
-
|
| 51 |
-
# Extract the text from the last message in the response
|
| 52 |
-
final_answer = result["messages"][-1].content
|
| 53 |
-
st.markdown(final_answer)
|
| 54 |
-
|
| 55 |
-
st.session_state.messages.append({"role": "assistant", "content": final_answer})
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
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 Config (Best practice to put this at the very top)
|
| 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("⚠️ Waiting for GROQ_API_KEY... Please add it to your Space Secrets in Settings.")
|
| 18 |
+
st.info("After adding the secret, you may need to 'Restart' the Space.")
|
| 19 |
+
st.stop()
|
| 20 |
+
else:
|
| 21 |
+
st.success("🚀 API Key connected! The agent is ready.")
|
| 22 |
+
|
| 23 |
+
# Simple placeholder for your agent logic
|
| 24 |
+
st.write("Next step: Initialize your ChatGroq model here.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|