import os import openai from smolagents import Tool openai.api_key = os.getenv("OPENAI_API_KEY") class ImageAnalyzer(Tool): name = "image_analyzer" description = "Analyze the given image and describe or reason about its contents." inputs = { "image_path": { "type": "string", "description": "Path to the image file (e.g., a chessboard image)." }, "question": { "type": "string", "description": "The question to answer about the image (e.g., best chess move)." } } output_type = "string" def forward(self, image_path: str, question: str) -> str: with open(image_path, "rb") as image_file: response = openai.chat.completions.create( model="gpt-4-vision-preview", messages=[ {"role": "user", "content": [ {"type": "text", "text": question}, {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + image_file.read().encode("base64").decode()}} ]} ], max_tokens=500 ) return response.choices[0].message.content.strip()