wu981526092 commited on
Commit
f29226f
·
1 Parent(s): a469640
agentgraph/reconstruction/prompt_reconstructor.py CHANGED
@@ -264,33 +264,70 @@ class PromptReconstructor:
264
  # Place system prompt first
265
  complete_prompt = system_role
266
 
267
- # Format tool definitions with better structure to match example
 
268
  if tool_definitions:
269
  for tool_def in tool_definitions:
270
- # Extract tool name, args, and description to reformat
271
  tool_name = ""
272
  tool_args = "{}"
273
  tool_desc = ""
274
 
275
- if "Tool Name:" in tool_def:
276
- name_start = tool_def.find("Tool Name:") + len("Tool Name:")
277
- name_end = tool_def.find("\n", name_start) if "\n" in tool_def[name_start:] else len(tool_def)
278
- tool_name = tool_def[name_start:name_end].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
- if "Tool Arguments:" in tool_def:
281
- args_start = tool_def.find("Tool Arguments:") + len("Tool Arguments:")
282
- args_end = tool_def.find("\n", args_start) if "\n" in tool_def[args_start:] else len(tool_def)
283
- tool_args = tool_def[args_start:args_end].strip()
284
 
285
- if "Tool Description:" in tool_def:
286
- desc_start = tool_def.find("Tool Description:") + len("Tool Description:")
287
- desc_end = tool_def.find("\n", desc_start) if "\n" in tool_def[desc_start:] else len(tool_def)
288
- tool_desc = tool_def[desc_start:desc_end].strip()
289
-
290
- # Format the tool entry more closely to the example format
291
  complete_prompt += f"Tool Name: {tool_name}\n"
292
  complete_prompt += f"Tool Arguments: {tool_args}\n"
293
  complete_prompt += f"Tool Description: {tool_desc}\n\n"
 
 
 
294
 
295
  # Enhanced response format with realistic agent reasoning patterns
296
  complete_prompt += "RESPONSE FORMAT - Follow this structure for each step:\n\n"
@@ -299,7 +336,7 @@ class PromptReconstructor:
299
  complete_prompt += "Reasoning: [Analyze the current situation and explain your thinking process]\n"
300
  complete_prompt += "Task Analysis: [Break down what needs to be done and identify requirements]\n"
301
  complete_prompt += "Tool Selection: [Choose appropriate tool and justify why]\n"
302
- complete_prompt += "Action: [Tool name from: " + ", ".join([t.split("Tool Name:")[1].strip().split("\n")[0] for t in tool_definitions if "Tool Name:" in t]) + "]\n"
303
  complete_prompt += "Action Input: [JSON object with parameters, using \" for keys and values]\n"
304
  complete_prompt += "Observation: [Result of the action]\n"
305
  complete_prompt += "```\n\n"
 
264
  # Place system prompt first
265
  complete_prompt = system_role
266
 
267
+ # Enhanced tool definitions processing with fallback handling
268
+ tool_names_for_action = []
269
  if tool_definitions:
270
  for tool_def in tool_definitions:
271
+ # Extract tool name, args, and description with improved parsing
272
  tool_name = ""
273
  tool_args = "{}"
274
  tool_desc = ""
275
 
276
+ # Check if tool definition follows expected format
277
+ if "Tool Name:" in tool_def and "Tool Arguments:" in tool_def and "Tool Description:" in tool_def:
278
+ # Parse structured format
279
+ if "Tool Name:" in tool_def:
280
+ name_start = tool_def.find("Tool Name:") + len("Tool Name:")
281
+ name_end = tool_def.find("\n", name_start) if "\n" in tool_def[name_start:] else len(tool_def)
282
+ tool_name = tool_def[name_start:name_end].strip()
283
+
284
+ if "Tool Arguments:" in tool_def:
285
+ args_start = tool_def.find("Tool Arguments:") + len("Tool Arguments:")
286
+ args_end = tool_def.find("\n", args_start) if "\n" in tool_def[args_start:] else len(tool_def)
287
+ tool_args = tool_def[args_start:args_end].strip()
288
+
289
+ if "Tool Description:" in tool_def:
290
+ desc_start = tool_def.find("Tool Description:") + len("Tool Description:")
291
+ desc_end = len(tool_def) # Take rest of the text for description
292
+ tool_desc = tool_def[desc_start:desc_end].strip()
293
+ else:
294
+ # Fallback: Extract from tool entity if structured format not available
295
+ # Find the tool entity that matches this definition
296
+ for entity_id, entity in self.entities.items():
297
+ if entity["type"] == "Tool" and entity.get("raw_prompt") == tool_def:
298
+ tool_name = entity["name"]
299
+ # Generate reasonable arguments based on tool purpose
300
+ tool_desc = tool_def
301
+ # Create basic arguments structure
302
+ if "search" in tool_name.lower():
303
+ tool_args = '{"query": "search_term", "max_results": 10}'
304
+ elif "load" in tool_name.lower() or "read" in tool_name.lower():
305
+ tool_args = '{"file_path": "path/to/file", "format": "auto"}'
306
+ elif "calculat" in tool_name.lower() or "analyz" in tool_name.lower():
307
+ tool_args = '{"data": "input_data", "method": "default"}'
308
+ elif "validat" in tool_name.lower() or "check" in tool_name.lower():
309
+ tool_args = '{"data": "target_data", "criteria": "standard"}'
310
+ else:
311
+ tool_args = '{"input": "data"}'
312
+ break
313
+
314
+ # If still no tool name found, use a generic name
315
+ if not tool_name:
316
+ tool_name = "UnknownTool"
317
+ tool_desc = tool_def if tool_def else "Tool description not available"
318
+ tool_args = '{"input": "data"}'
319
 
320
+ # Add to tool names list for action format
321
+ if tool_name and tool_name != "UnknownTool":
322
+ tool_names_for_action.append(tool_name)
 
323
 
324
+ # Format the tool entry
 
 
 
 
 
325
  complete_prompt += f"Tool Name: {tool_name}\n"
326
  complete_prompt += f"Tool Arguments: {tool_args}\n"
327
  complete_prompt += f"Tool Description: {tool_desc}\n\n"
328
+ else:
329
+ # No tools available - provide clear message
330
+ complete_prompt += "No tools are currently available for this task.\n\n"
331
 
332
  # Enhanced response format with realistic agent reasoning patterns
333
  complete_prompt += "RESPONSE FORMAT - Follow this structure for each step:\n\n"
 
336
  complete_prompt += "Reasoning: [Analyze the current situation and explain your thinking process]\n"
337
  complete_prompt += "Task Analysis: [Break down what needs to be done and identify requirements]\n"
338
  complete_prompt += "Tool Selection: [Choose appropriate tool and justify why]\n"
339
+ complete_prompt += "Action: [Tool name from: " + ", ".join(tool_names_for_action) + "]\n"
340
  complete_prompt += "Action Input: [JSON object with parameters, using \" for keys and values]\n"
341
  complete_prompt += "Observation: [Result of the action]\n"
342
  complete_prompt += "```\n\n"