gabejavitt commited on
Commit
99b68de
·
verified ·
1 Parent(s): b975bf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -240,22 +240,34 @@ defined_tools = [
240
  class AgentState(TypedDict):
241
  messages: Annotated[list[AnyMessage], add_messages]
242
 
243
- # --- Custom Conditional Edge ---
244
  def should_continue(state: AgentState):
245
- """Custom logic to decide whether to continue or end."""
 
 
 
246
  last_message = state['messages'][-1]
 
247
  if isinstance(last_message, AIMessage):
248
  if last_message.tool_calls:
 
249
  if last_message.tool_calls[0].get("name") == "final_answer_tool":
250
  print("--- Condition: Saw final_answer_tool, ending graph. ---")
251
  return END
252
  else:
 
253
  print("--- Condition: Saw other tools, calling tools node. ---")
254
  return "tools"
255
- # This path should ideally not be taken if the prompt is followed
256
- print("--- Condition: No tool call detected, ending graph (forcing agent to use final_answer_tool). ---")
257
- return END
 
 
 
 
 
 
258
 
 
259
  # --- Basic Agent Definition ---
260
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
261
  class BasicAgent:
@@ -286,6 +298,8 @@ Your primary goal is to provide **only the concise, factual, and direct answer**
286
  * **THE ONLY WAY** to provide a final answer is by calling the `final_answer_tool`.
287
  * **DO NOT** include conversational filler (e.g., "The answer is...").
288
  * **DO NOT** explain your reasoning unless it's inside a `code_interpreter` comment.
 
 
289
 
290
  You have access to the following tools:
291
  {tool_descriptions}
@@ -302,30 +316,24 @@ You have access to the following tools:
302
  * Do not add any text before or after the JSON block.
303
 
304
  **REASONING PROCESS & STOPPING CONDITION:**
305
- 1. **Analyze:** Read the user's question carefully. Identify the goal.
306
- 2. **Plan:** Break down the problem into a sequence of logical steps.
307
- * **For logic puzzles (like riddles):** You *must* use the `code_interpreter`. Your *first* step is to write out your step-by-step logical reasoning as Python comments *before* writing any solving code.
308
- 3. **Execute:** Choose the *one* best tool for the *next* step in your plan.
309
- 4. **Call Tool:** Call the chosen tool using the exact JSON format. Do not add any other text.
310
- 5. **Analyze Output:** Receive the tool's output (either a success message or an error).
311
- 6. **DECISION POINT:**
312
- a. **Answer Found?** If the tool output directly contains the final answer (or makes it trivial to calculate), your *very next* action MUST be to call `final_answer_tool` with that answer.
313
- b. **Error Occurred?** If the tool returned an error message:
314
- * **STOP.** Do not try the *exact same tool call* again.
315
- * **Rethink:** Analyze the error message. What went wrong?
316
- * **Correct:** Try a *different* tool, *different* arguments, or a *different approach* entirely.
317
- * If you are completely stuck after an error, call `final_answer_tool` with a concise error message (e.g., "Error: Could not find user").
318
- c. **More Steps Needed?** If the answer is not yet found and there was no error, go back to step 2 (Plan) to determine the next logical step.
319
  7. **FINAL OUTPUT:** The graph will stop *only* when you call `final_answer_tool`. Do not provide the answer in any other way."""
320
  # =============================================================
321
 
322
  print("Initializing Groq LLM Endpoint...")
323
  try:
324
- chat_llm = ChatGroq(
325
- temperature=0.01,
326
- groq_api_key=GROQ_API_KEY,
327
- model_name="qwen/qwen3-32b" # <-- Your change is here
328
- )
329
  print("✅ Groq LLM Endpoint initialized with llama-3.1-8b-instant.")
330
  except Exception as e: print(f"Error initializing Groq: {e}"); raise
331
 
 
240
  class AgentState(TypedDict):
241
  messages: Annotated[list[AnyMessage], add_messages]
242
 
 
243
  def should_continue(state: AgentState):
244
+ """
245
+ Custom logic to decide whether to continue or end.
246
+ Now allows for a "reasoning loop".
247
+ """
248
  last_message = state['messages'][-1]
249
+
250
  if isinstance(last_message, AIMessage):
251
  if last_message.tool_calls:
252
+ # Check for the final answer tool
253
  if last_message.tool_calls[0].get("name") == "final_answer_tool":
254
  print("--- Condition: Saw final_answer_tool, ending graph. ---")
255
  return END
256
  else:
257
+ # Any other tool call goes to the tools node
258
  print("--- Condition: Saw other tools, calling tools node. ---")
259
  return "tools"
260
+
261
+ # --- THIS IS THE NEW LOGIC ---
262
+ # If the last message is from the AI and has NO tool calls (i.e., it's a "thought"),
263
+ # loop back to the agent node to let it "think" again.
264
+ print("--- Condition: No tool call detected. Looping back to agent. ---")
265
+ return "agent"
266
+
267
+ # The old "END" path is removed. The only way to END
268
+ # is to explicitly call final_answer_tool.
269
 
270
+
271
  # --- Basic Agent Definition ---
272
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
273
  class BasicAgent:
 
298
  * **THE ONLY WAY** to provide a final answer is by calling the `final_answer_tool`.
299
  * **DO NOT** include conversational filler (e.g., "The answer is...").
300
  * **DO NOT** explain your reasoning unless it's inside a `code_interpreter` comment.
301
+ * **DO NOT** mix plain text and tool-call JSON in the same response.
302
+ * **DO NOT** use XML formats like `<function=...>` or `<code_interpreter>`. **THIS WILL FAIL.**
303
 
304
  You have access to the following tools:
305
  {tool_descriptions}
 
316
  * Do not add any text before or after the JSON block.
317
 
318
  **REASONING PROCESS & STOPPING CONDITION:**
319
+ 1. **Analyze:** Read the question. Break it down into logical steps.
320
+ 2. **DECIDE:** Do you have enough information to call a tool, or do you need to write down your plan first?
321
+ 3. **ACT (Two Options):**
322
+ a. **Write Plan (Chain of Thought):** If you are not ready to call a tool, or if the problem is a complex logic puzzle, respond with your step-by-step reasoning plan as **plain text**. This allows you to "think" and add your plan to memory before your next step.
323
+ b. **Call Tool:** If you are ready, call **one** tool using the JSON format.
324
+ 4. **Analyze Output:** After a tool is called, you will receive its output.
325
+ 5. **GOTO 1:** Repeat the process. Analyze the new information and decide your next step (think, or call another tool).
326
+ 6. **STOPPING:** The *only* way to provide the final answer is by calling `final_answer_tool`.
 
 
 
 
 
 
327
  7. **FINAL OUTPUT:** The graph will stop *only* when you call `final_answer_tool`. Do not provide the answer in any other way."""
328
  # =============================================================
329
 
330
  print("Initializing Groq LLM Endpoint...")
331
  try:
332
+ chat_llm = ChatGroq(
333
+ temperature=0.01,
334
+ groq_api_key=GROQ_API_KEY,
335
+ model_name="qwen/qwen3-32b" # <-- Your change is here
336
+ )
337
  print("✅ Groq LLM Endpoint initialized with llama-3.1-8b-instant.")
338
  except Exception as e: print(f"Error initializing Groq: {e}"); raise
339