reddmann007 commited on
Commit
a10b07c
·
verified ·
1 Parent(s): 1077aeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -10
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 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.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}")