''' Wrapper to make the image processing functionality available as a structured tool for the agent. ''' from langchain_core.tools import StructuredTool from pydantic.v1 import BaseModel, Field # Assuming Pydantic v1 based on typical Langchain usage from typing import Optional, Type from tools.image_processor import process_image_and_answer class ImageAnalyzerInput(BaseModel): """Input schema for the Image Analyzer Tool.""" image_path: str = Field(description="The file path of the image to be analyzed.") question: str = Field(description="The question to answer about the image.") task_id: Optional[str] = Field(None, description="Optional task ID for tracking.") def run_image_analysis(image_path: str, question: str, task_id: Optional[str] = None) -> dict: """Runs the image processing and question answering logic.""" print(f"--- Calling Image Analyzer Tool --- (Task ID: {task_id})") print(f"Image path: {image_path}, Question: {question}") try: result = process_image_and_answer(image_path=image_path, question=question, task_id=task_id) print(f"Image analysis result: {result}") return result except Exception as e: print(f"Error in Image Analyzer Tool: {e}") return { "image_path": image_path, "question": question, "task_id": task_id, "answer": f"Error during image analysis: {str(e)}", "status": "Error in tool execution" } image_analyzer_tool = StructuredTool.from_function( func=run_image_analysis, name="ImageAnalyzerTool", description="Analyzes an image to answer a question about it. Provide the image file path and the specific question.", args_schema=ImageAnalyzerInput, # return_direct=False, # Set to True if the tool output should be returned directly to the user ) if __name__ == '__main__': # Example usage (for testing this module directly) # Ensure a dummy image exists for testing, or provide a valid path dummy_image_path_for_wrapper = "../dummy_image.png" # Relative to nodes directory # Create a dummy image in the root if it doesn't exist (from where run_local.py might be run) # This is a bit of a workaround for direct testing of the wrapper import os if not os.path.exists(dummy_image_path_for_wrapper): try: with open(dummy_image_path_for_wrapper, "w") as f: f.write("This is a dummy image file content for wrapper test.") print(f"Created dummy file for wrapper testing: {dummy_image_path_for_wrapper}") except IOError as e: print(f"Could not create dummy file for wrapper testing: {e}. Please create it manually or update path.") # dummy_image_path_for_wrapper = "path/to/your/actual/image.png" # Or provide a real image path if os.path.exists(dummy_image_path_for_wrapper): test_question = "What is the primary subject of the image?" test_task_id = "test-wrapper-001" print("\n--- Testing ImageAnalyzerTool directly ---") # Test by directly invoking the wrapped function (as the tool would) output = run_image_analysis( image_path=dummy_image_path_for_wrapper, question=test_question, task_id=test_task_id ) print("\n--- Tool Output ---") print(output) print("-------------------") print("\n--- Testing ImageAnalyzerTool via .invoke() (simulating LangChain agent call) ---") # Test by invoking the tool instance try: tool_input = { "image_path": dummy_image_path_for_wrapper, "question": test_question, "task_id": test_task_id } tool_output = image_analyzer_tool.invoke(input=tool_input) print("\n--- Tool .invoke() Output ---") print(tool_output) print("---------------------------") except Exception as e: print(f"Error invoking tool: {e}") else: print(f"Skipping wrapper example run as dummy image '{dummy_image_path_for_wrapper}' could not be prepared.")