Spaces:
Sleeping
Sleeping
File size: 24,472 Bytes
d12a6df |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
import json
import os
import re
from typing import Any, Dict, List, Tuple
from PIL import Image
from agentflow.engine.factory import create_llm_engine
from agentflow.models.formatters import MemoryVerification, NextStep, QueryAnalysis
from agentflow.models.memory import Memory
class Planner:
def __init__(self, llm_engine_name: str, toolbox_metadata: dict = None, available_tools: List = None,
verbose: bool = False, base_url: str = None, is_multimodal: bool = False, check_model: bool = True, temperature : float = .0):
self.llm_engine_name = llm_engine_name
self.is_multimodal = is_multimodal
# self.llm_engine_mm = create_llm_engine(model_string=llm_engine_name, is_multimodal=False, base_url=base_url, temperature = temperature)
self.llm_engine_fixed = create_llm_engine(model_string="dashscope", is_multimodal=False, temperature = temperature)
self.llm_engine = create_llm_engine(model_string=llm_engine_name, is_multimodal=False, base_url=base_url, temperature = temperature)
self.toolbox_metadata = toolbox_metadata if toolbox_metadata is not None else {}
self.available_tools = available_tools if available_tools is not None else []
self.verbose = verbose
def get_image_info(self, image_path: str) -> Dict[str, Any]:
image_info = {}
if image_path and os.path.isfile(image_path):
image_info["image_path"] = image_path
try:
with Image.open(image_path) as img:
width, height = img.size
image_info.update({
"width": width,
"height": height
})
except Exception as e:
print(f"Error processing image file: {str(e)}")
return image_info
def generate_base_response(self, question: str, image: str, max_tokens: int = 2048) -> str:
image_info = self.get_image_info(image)
input_data = [question]
if image_info and "image_path" in image_info:
try:
with open(image_info["image_path"], 'rb') as file:
image_bytes = file.read()
input_data.append(image_bytes)
except Exception as e:
print(f"Error reading image file: {str(e)}")
print("Input data of `generate_base_response()`: ", input_data)
self.base_response = self.llm_engine(input_data, max_tokens=max_tokens)
# self.base_response = self.llm_engine_fixed(input_data, max_tokens=max_tokens)
return self.base_response
def analyze_query(self, question: str, image: str) -> str:
image_info = self.get_image_info(image)
if self.is_multimodal:
query_prompt = f"""
Task: Analyze the given query with accompanying inputs and determine the skills and tools needed to address it effectively.
Available tools: {self.available_tools}
Metadata for the tools: {self.toolbox_metadata}
Image: {image_info}
Query: {question}
Instructions:
1. Carefully read and understand the query and any accompanying inputs.
2. Identify the main objectives or tasks within the query.
3. List the specific skills that would be necessary to address the query comprehensively.
4. Examine the available tools in the toolbox and determine which ones might relevant and useful for addressing the query. Make sure to consider the user metadata for each tool, including limitations and potential applications (if available).
5. Provide a brief explanation for each skill and tool you've identified, describing how it would contribute to answering the query.
Your response should include:
1. A concise summary of the query's main points and objectives, as well as content in any accompanying inputs.
2. A list of required skills, with a brief explanation for each.
3. A list of relevant tools from the toolbox, with a brief explanation of how each tool would be utilized and its potential limitations.
4. Any additional considerations that might be important for addressing the query effectively.
Please present your analysis in a clear, structured format.
"""
else:
query_prompt = f"""
Task: Analyze the given query to determine necessary skills and tools.
Inputs:
- Query: {question}
- Available tools: {self.available_tools}
- Metadata for tools: {self.toolbox_metadata}
Instructions:
1. Identify the main objectives in the query.
2. List the necessary skills and tools.
3. For each skill and tool, explain how it helps address the query.
4. Note any additional considerations.
Format your response with a summary of the query, lists of skills and tools with explanations, and a section for additional considerations.
Be biref and precise with insight.
"""
input_data = [query_prompt]
if image_info:
try:
with open(image_info["image_path"], 'rb') as file:
image_bytes = file.read()
input_data.append(image_bytes)
except Exception as e:
print(f"Error reading image file: {str(e)}")
print("Input data of `analyze_query()`: ", input_data)
# self.query_analysis = self.llm_engine_mm(input_data, response_format=QueryAnalysis)
# self.query_analysis = self.llm_engine(input_data, response_format=QueryAnalysis)
self.query_analysis = self.llm_engine_fixed(input_data, response_format=QueryAnalysis)
return str(self.query_analysis).strip()
def extract_context_subgoal_and_tool(self, response: Any) -> Tuple[str, str, str]:
def normalize_tool_name(tool_name: str) -> str:
"""
Normalizes a tool name robustly using regular expressions.
It handles any combination of spaces and underscores as separators.
"""
def to_canonical(name: str) -> str:
# Split the name by any sequence of one or more spaces or underscores
parts = re.split('[ _]+', name)
# Join the parts with a single underscore and convert to lowercase
return "_".join(part.lower() for part in parts)
normalized_input = to_canonical(tool_name)
for tool in self.available_tools:
if to_canonical(tool) == normalized_input:
return tool
return f"No matched tool given: {tool_name}"
try:
if isinstance(response, str):
# Attempt to parse the response as JSON
try:
response_dict = json.loads(response)
response = NextStep(**response_dict)
except Exception as e:
print(f"Failed to parse response as JSON: {str(e)}")
if isinstance(response, NextStep):
print("arielg 1")
context = response.context.strip()
sub_goal = response.sub_goal.strip()
tool_name = response.tool_name.strip()
else:
print("arielg 2")
text = response.replace("**", "")
# Pattern to match the exact format
pattern = r"Context:\s*(.*?)Sub-Goal:\s*(.*?)Tool Name:\s*(.*?)\s*(?:```)?\s*(?=\n\n|\Z)"
# Find all matches
matches = re.findall(pattern, text, re.DOTALL)
# Return the last match (most recent/relevant)
context, sub_goal, tool_name = matches[-1]
context = context.strip()
sub_goal = sub_goal.strip()
tool_name = normalize_tool_name(tool_name)
except Exception as e:
print(f"Error extracting context, sub-goal, and tool name: {str(e)}")
return None, None, None
return context, sub_goal, tool_name
def generate_next_step(self, question: str, image: str, query_analysis: str, memory: Memory, step_count: int, max_step_count: int, json_data: Any = None) -> Any:
if self.is_multimodal:
prompt_generate_next_step = f"""
Task: Determine the optimal next step to address the given query based on the provided analysis, available tools, and previous steps taken.
Context:
Query: {question}
Image: {image}
Query Analysis: {query_analysis}
Available Tools:
{self.available_tools}
Tool Metadata:
{self.toolbox_metadata}
Previous Steps and Their Results:
{memory.get_actions()}
Current Step: {step_count} in {max_step_count} steps
Remaining Steps: {max_step_count - step_count}
Instructions:
1. Analyze the context thoroughly, including the query, its analysis, any image, available tools and their metadata, and previous steps taken.
2. Determine the most appropriate next step by considering:
- Key objectives from the query analysis
- Capabilities of available tools
- Logical progression of problem-solving
- Outcomes from previous steps
- Current step count and remaining steps
3. Select ONE tool best suited for the next step, keeping in mind the limited number of remaining steps.
4. Formulate a specific, achievable sub-goal for the selected tool that maximizes progress towards answering the query.
Response Format:
Your response MUST follow this structure:
1. Justification: Explain your choice in detail.
2. Context, Sub-Goal, and Tool: Present the context, sub-goal, and the selected tool ONCE with the following format:
Context: <context>
Sub-Goal: <sub_goal>
Tool Name: <tool_name>
Where:
- <context> MUST include ALL necessary information for the tool to function, structured as follows:
* Relevant data from previous steps
* File names or paths created or used in previous steps (list EACH ONE individually)
* Variable names and their values from previous steps' results
* Any other context-specific information required by the tool
- <sub_goal> is a specific, achievable objective for the tool, based on its metadata and previous outcomes.
It MUST contain any involved data, file names, and variables from Previous Steps and Their Results that the tool can act upon.
- <tool_name> MUST be the exact name of a tool from the available tools list.
Rules:
- Select only ONE tool for this step.
- The sub-goal MUST directly address the query and be achievable by the selected tool.
- The Context section MUST include ALL necessary information for the tool to function, including ALL relevant file paths, data, and variables from previous steps.
- The tool name MUST exactly match one from the available tools list: {self.available_tools}.
- Avoid redundancy by considering previous steps and building on prior results.
- Your response MUST conclude with the Context, Sub-Goal, and Tool Name sections IN THIS ORDER, presented ONLY ONCE.
- Include NO content after these three sections.
Example (do not copy, use only as reference):
Justification: [Your detailed explanation here]
Context: Image path: "example/image.jpg", Previous detection results: [list of objects]
Sub-Goal: Detect and count the number of specific objects in the image "example/image.jpg"
Tool Name: Object_Detector_Tool
Remember: Your response MUST end with the Context, Sub-Goal, and Tool Name sections, with NO additional content afterwards.
"""
else:
prompt_generate_next_step = f"""
Task: Determine the optimal next step to address the query using available tools and previous steps.
Context:
- **Query:** {question}
- **Query Analysis:** {query_analysis}
- **Available Tools:** {self.available_tools}
- **Toolbox Metadata:** {self.toolbox_metadata}
- **Previous Steps:** {memory.get_actions()}
Instructions:
1. Analyze the query, previous steps, and available tools.
2. Select the **single best tool** for the next step.
3. Formulate a specific, achievable **sub-goal** for that tool.
4. Provide all necessary **context** (data, file names, variables) for the tool to function.
Response Format:
1. **Justification:** Explain your choice of tool and sub-goal.
2. **Context:** Provide all necessary information for the tool.
3. **Sub-Goal:** State the specific objective for the tool.
4. **Tool Name:** State the exact name of the selected tool.
Rules:
- Select only ONE tool.
- The sub-goal must be directly achievable by the selected tool.
- The Context section must contain all information the tool needs to function.
- The response must end with the Context, Sub-Goal, and Tool Name sections in that order, with no extra content.
"""
next_step = self.llm_engine(prompt_generate_next_step, response_format=NextStep)
if json_data is not None:
json_data[f"action_predictor_{step_count}_prompt"] = prompt_generate_next_step
json_data[f"action_predictor_{step_count}_response"] = str(next_step)
return next_step
def verificate_context(self, question: str, image: str, query_analysis: str, memory: Memory, step_count: int = 0, json_data: Any = None) -> Any:
image_info = self.get_image_info(image)
if self.is_multimodal:
prompt_memory_verification = f"""
Task: Thoroughly evaluate the completeness and accuracy of the memory for fulfilling the given query, considering the potential need for additional tool usage.
Context:
Query: {question}
Image: {image_info}
Available Tools: {self.available_tools}
Toolbox Metadata: {self.toolbox_metadata}
Initial Analysis: {query_analysis}
Memory (tools used and results): {memory.get_actions()}
Detailed Instructions:
1. Carefully analyze the query, initial analysis, and image (if provided):
- Identify the main objectives of the query.
- Note any specific requirements or constraints mentioned.
- If an image is provided, consider its relevance and what information it contributes.
2. Review the available tools and their metadata:
- Understand the capabilities and limitations and best practices of each tool.
- Consider how each tool might be applicable to the query.
3. Examine the memory content in detail:
- Review each tool used and its execution results.
- Assess how well each tool's output contributes to answering the query.
4. Critical Evaluation (address each point explicitly):
a) Completeness: Does the memory fully address all aspects of the query?
- Identify any parts of the query that remain unanswered.
- Consider if all relevant information has been extracted from the image (if applicable).
b) Unused Tools: Are there any unused tools that could provide additional relevant information?
- Specify which unused tools might be helpful and why.
c) Inconsistencies: Are there any contradictions or conflicts in the information provided?
- If yes, explain the inconsistencies and suggest how they might be resolved.
d) Verification Needs: Is there any information that requires further verification due to tool limitations?
- Identify specific pieces of information that need verification and explain why.
e) Ambiguities: Are there any unclear or ambiguous results that could be clarified by using another tool?
- Point out specific ambiguities and suggest which tools could help clarify them.
5. Final Determination:
Based on your thorough analysis, decide if the memory is complete and accurate enough to generate the final output, or if additional tool usage is necessary.
Response Format:
If the memory is complete, accurate, AND verified:
Explanation:
<Provide a detailed explanation of why the memory is sufficient. Reference specific information from the memory and explain its relevance to each aspect of the task. Address how each main point of the query has been satisfied.>
Conclusion: STOP
If the memory is incomplete, insufficient, or requires further verification:
Explanation:
<Explain in detail why the memory is incomplete. Identify specific information gaps or unaddressed aspects of the query. Suggest which additional tools could be used, how they might contribute, and why their input is necessary for a comprehensive response.>
Conclusion: CONTINUE
IMPORTANT: Your response MUST end with either 'Conclusion: STOP' or 'Conclusion: CONTINUE' and nothing else. Ensure your explanation thoroughly justifies this conclusion.
"""
else:
prompt_memory_verification = f"""
Task: Evaluate if the current memory is complete and accurate enough to answer the query, or if more tools are needed.
Context:
- **Query:** {question}
- **Available Tools:** {self.available_tools}
- **Toolbox Metadata:** {self.toolbox_metadata}
- **Initial Analysis:** {query_analysis}
- **Memory (Tools Used & Results):** {memory.get_actions()}
Instructions:
1. Review the query, initial analysis, and memory.
2. Assess the completeness of the memory: Does it fully address all parts of the query?
3. Check for potential issues:
- Are there any inconsistencies or contradictions?
- Is any information ambiguous or in need of verification?
4. Determine if any unused tools could provide missing information.
Final Determination:
- If the memory is sufficient, explain why and conclude with "STOP".
- If more information is needed, explain what's missing, which tools could help, and conclude with "CONTINUE".
IMPORTANT: The response must end with either "Conclusion: STOP" or "Conclusion: CONTINUE".
"""
input_data = [prompt_memory_verification]
if image_info:
try:
with open(image_info["image_path"], 'rb') as file:
image_bytes = file.read()
input_data.append(image_bytes)
except Exception as e:
print(f"Error reading image file: {str(e)}")
# stop_verification = self.llm_engine_mm(input_data, response_format=MemoryVerification)
stop_verification = self.llm_engine_fixed(input_data, response_format=MemoryVerification)
# stop_verification = self.llm_engine(input_data, response_format=MemoryVerification)
if json_data is not None:
json_data[f"verifier_{step_count}_prompt"] = input_data
json_data[f"verifier_{step_count}_response"] = str(stop_verification)
return stop_verification
def extract_conclusion(self, response: Any) -> tuple:
if isinstance(response, str):
# Attempt to parse the response as JSON
try:
response_dict = json.loads(response)
response = MemoryVerification(**response_dict)
except Exception as e:
print(f"Failed to parse response as JSON: {str(e)}")
if isinstance(response, MemoryVerification):
analysis = response.analysis
stop_signal = response.stop_signal
if stop_signal:
return analysis, 'STOP'
else:
return analysis, 'CONTINUE'
else:
analysis = response
pattern = r'conclusion\**:?\s*\**\s*(\w+)'
matches = list(re.finditer(pattern, response, re.IGNORECASE | re.DOTALL))
# if match:
# conclusion = match.group(1).upper()
# if conclusion in ['STOP', 'CONTINUE']:
# return conclusion
if matches:
conclusion = matches[-1].group(1).upper()
if conclusion in ['STOP', 'CONTINUE']:
return analysis, conclusion
# If no valid conclusion found, search for STOP or CONTINUE anywhere in the text
if 'stop' in response.lower():
return analysis, 'STOP'
elif 'continue' in response.lower():
return analysis, 'CONTINUE'
else:
print("No valid conclusion (STOP or CONTINUE) found in the response. Continuing...")
return analysis, 'CONTINUE'
def generate_final_output(self, question: str, image: str, memory: Memory) -> str:
image_info = self.get_image_info(image)
if self.is_multimodal:
prompt_generate_final_output = f"""
Task: Generate the final output based on the query, image, and tools used in the process.
Context:
Query: {question}
Image: {image_info}
Actions Taken:
{memory.get_actions()}
Instructions:
1. Review the query, image, and all actions taken during the process.
2. Consider the results obtained from each tool execution.
3. Incorporate the relevant information from the memory to generate the step-by-step final output.
4. The final output should be consistent and coherent using the results from the tools.
Output Structure:
Your response should be well-organized and include the following sections:
1. Summary:
- Provide a brief overview of the query and the main findings.
2. Detailed Analysis:
- Break down the process of answering the query step-by-step.
- For each step, mention the tool used, its purpose, and the key results obtained.
- Explain how each step contributed to addressing the query.
3. Key Findings:
- List the most important discoveries or insights gained from the analysis.
- Highlight any unexpected or particularly interesting results.
4. Answer to the Query:
- Directly address the original question with a clear and concise answer.
- If the query has multiple parts, ensure each part is answered separately.
5. Additional Insights (if applicable):
- Provide any relevant information or insights that go beyond the direct answer to the query.
- Discuss any limitations or areas of uncertainty in the analysis.
6. Conclusion:
- Summarize the main points and reinforce the answer to the query.
- If appropriate, suggest potential next steps or areas for further investigation.
"""
else:
prompt_generate_final_output = f"""
Task: Generate the final output based on the query and the results from all tools used.
Context:
- **Query:** {question}
- **Actions Taken:** {memory.get_actions()}
Instructions:
1. Review the query and the results from all tool executions.
2. Incorporate the relevant information to create a coherent, step-by-step final output.
"""
input_data = [prompt_generate_final_output]
if image_info:
try:
with open(image_info["image_path"], 'rb') as file:
image_bytes = file.read()
input_data.append(image_bytes)
except Exception as e:
print(f"Error reading image file: {str(e)}")
# final_output = self.llm_engine_mm(input_data)
# final_output = self.llm_engine(input_data)
final_output = self.llm_engine_fixed(input_data)
return final_output
def generate_direct_output(self, question: str, image: str, memory: Memory) -> str:
image_info = self.get_image_info(image)
if self.is_multimodal:
prompt_generate_final_output = f"""
Context:
Query: {question}
Image: {image_info}
Initial Analysis:
{self.query_analysis}
Actions Taken:
{memory.get_actions()}
Please generate the concise output based on the query, image information, initial analysis, and actions taken. Break down the process into clear, logical, and conherent steps. Conclude with a precise and direct answer to the query.
Answer:
"""
else:
prompt_generate_final_output = f"""
Task: Generate a concise final answer to the query based on all provided context.
Context:
- **Query:** {question}
- **Initial Analysis:** {self.query_analysis}
- **Actions Taken:** {memory.get_actions()}
Instructions:
1. Review the query and the results from all actions.
2. Synthesize the key findings into a clear, step-by-step summary of the process.
3. Provide a direct, precise answer to the original query.
Output Structure:
1. **Process Summary:** A clear, step-by-step breakdown of how the query was addressed, including the purpose and key results of each action.
2. **Answer:** A direct and concise final answer to the query.
"""
input_data = [prompt_generate_final_output]
if image_info:
try:
with open(image_info["image_path"], 'rb') as file:
image_bytes = file.read()
input_data.append(image_bytes)
except Exception as e:
print(f"Error reading image file: {str(e)}")
# final_output = self.llm_engine(input_data)
final_output = self.llm_engine_fixed(input_data)
# final_output = self.llm_engine_mm(input_data)
return final_output
|