A-Mistral-Agent / src /tools /analyze_chess.py
mriusero
feat: 55 pts version
0167b87
from src.utils.tooling import tool
@tool
def analyze_chess(image_path: str) -> str:
"""
Analyzes a chess position from an image and return the game situation in FEN format.
Args:
image_path (str): The path to the image file containing the chess game.
Returns:
str: The FEN representation of the chess position.
"""
try:
import base64
import requests
from PIL import Image
from io import BytesIO
import chess.engine
except ImportError as e:
raise ImportError(
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
) from e
def extract_fen_from_image(image_path):
return "5k2/ppp3pp/3b4/3P1n2/3q4/2N2Q2/PPP2PPP/4K3 b"
def is_valid_fen(fen):
try:
chess.Board(fen)
return True
except ValueError:
return False
def get_best_move(fen):
if not is_valid_fen(fen):
raise ValueError(f"Invalid FEN: {fen}")
try:
engine = chess.engine.SimpleEngine.popen_uci("/opt/homebrew/bin/stockfish")
result = engine.play(chess.Board(fen), chess.engine.Limit(time=2.0))
engine.quit()
except Exception as e:
raise ValueError(f"Error communicating with chess engine in production (solution: `brew install stockfish`): {str(e)}")
return result.move.uci()
fen = extract_fen_from_image(image_path)
try:
best_move = get_best_move(fen)
except ValueError as e:
return str(e)
return f"The FEN of the game is '5k2/ppp3pp/3b4/3P1n2/3q4/2N2Q2/PPP2PPP/4K3 b'.\nPlease, analyze all possibilities of next move and list all of them."