reddmann007 commited on
Commit
9ae6322
·
verified ·
1 Parent(s): c941151

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from langchain_groq import ChatGroq
5
+ from langchain_community.agent_toolkits.load_tools import load_tools
6
+ from langgraph.prebuilt import create_react_agent
7
+ import langchainhub as hub
8
+
9
+ # 1. Setup Page & Secrets
10
+ st.set_page_config(page_title="My First AI Math Agent", layout="centered")
11
+ st.title("My First AI Math Agent 🧮")
12
+
13
+ # Look for API Key in environment (Local .env or HF Secrets)
14
+ load_dotenv()
15
+ api_key = os.getenv("GROQ_API_KEY")
16
+
17
+ if not api_key:
18
+ st.error("Missing GROQ_API_KEY! Please check your secrets.")
19
+ st.stop()
20
+
21
+ # 2. Initialize the Agent (Cached so it doesn't reload every click)
22
+ @st.cache_resource
23
+ def get_agent():
24
+ llm = ChatGroq(model_name="llama-3.3-70b-versatile", temperature=0)
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})