reddmann007 commited on
Commit
5d7d804
·
verified ·
1 Parent(s): 46cf1cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -26,25 +26,22 @@ llm = ChatGroq(
26
  # ... (Keep your imports and LLM initialization the same)
27
 
28
  # 4. Load Math Tools
29
- tools = load_tools(["llm-math"], llm=llm)
 
30
 
31
- # --- ADD THE NEW BLOCK HERE ---
32
- system_message = (
33
- "You are a strict Mathematics Assistant. "
34
- "Your ONLY task is to solve mathematical problems using your tools. "
35
- "If a user asks about anything other than math (such as history, general knowledge, "
36
- "coding, or personal opinions), you must politely refuse and explain that you "
37
- "only provide assistance with mathematics."
 
38
  )
39
 
40
- agent_executor = create_react_agent(
41
- llm,
42
- tools,
43
- state_modifier=system_message
44
- )
45
- # --- END OF NEW BLOCK ---
46
-
47
- # ... (Keep the Chat Interface logic below this)
48
 
49
  # 5. Chat Interface Setup
50
  if "messages" not in st.session_state:
@@ -66,9 +63,14 @@ if prompt := st.chat_input("Ask me a complex math question!"):
66
  with st.chat_message("assistant"):
67
  with st.spinner("Thinking..."):
68
  try:
69
- # Invoke the agent
70
- response = agent_executor.invoke({"messages": [("user", prompt)]})
71
-
 
 
 
 
 
72
  # Get the final response from the message list
73
  final_answer = response["messages"][-1].content
74
 
 
26
  # ... (Keep your imports and LLM initialization the same)
27
 
28
  # 4. Load Math Tools
29
+ # Define the strict instructions as a SystemMessage
30
+ from langchain_core.messages import SystemMessage
31
 
32
+ system_message = SystemMessage(
33
+ content=(
34
+ "You are a strict Mathematics Assistant. "
35
+ "Your ONLY task is to solve mathematical problems using your tools. "
36
+ "If a user asks about anything other than math (such as history, general knowledge, "
37
+ "coding, or personal opinions), you must politely refuse and explain that you "
38
+ "only provide assistance with mathematics."
39
+ )
40
  )
41
 
42
+ # Re-create the agent WITHOUT the state_modifier keyword
43
+ # We will pass the system message in the actual chat logic instead
44
+ agent_executor = create_react_agent(llm, tools)
 
 
 
 
 
45
 
46
  # 5. Chat Interface Setup
47
  if "messages" not in st.session_state:
 
63
  with st.chat_message("assistant"):
64
  with st.spinner("Thinking..."):
65
  try:
66
+ # Change this line:
67
+ # response = agent_executor.invoke({"messages": [("user", prompt)]})
68
+
69
+ # To this:
70
+ response = agent_executor.invoke({
71
+ "messages": [system_message, ("user", prompt)]
72
+ })
73
+
74
  # Get the final response from the message list
75
  final_answer = response["messages"][-1].content
76