Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 306 |
-
2. **
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 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 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 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 |
|