AlbertoFor's picture
Edit tools
c657a71
raw
history blame
1.4 kB
from langchain_core.tools.base import BaseTool
from chessimg2pos import predict_fen
from stockfish import Stockfish
import chess
class ChessTool(BaseTool):
name : str = "chess_tool"
description : str = "Given the path of an image, this tool returns the best next move that can be done on the chessboard. You must give ONLY the PATH of the image here! Pass in input b or w as color_turn based on whose turn is it. Use w if unspecified."
def _run(self, img_path: str, color_turn: str) -> str:
# Get the FEN string
#fen = predict_fen("./downloaded_files/image.png")
if color_turn == "b":
ranks = fen.split('/')
rotated_matrix = []
for old_row in reversed(ranks):
rotated_matrix.append(list(reversed(old_row)))
final_fen = "/".join(["".join(row) for row in rotated_matrix])
for length in reversed(range(2, 9)):
final_fen = final_fen.replace(length * "1", str(length))
else:
final_fen = fen
fen = f"{final_fen} {color_turn} - - 0 1"
fen = f"3r2k1/pp3pp1/4b2p/7Q/3n4/PqBBR2P/5PP1/6K1 {color_turn} - - 0 1"
stockfish = Stockfish(path="C:/Users/FORMAGGA/Documents/personal/stockfish-windows-x86-64-avx2/stockfish/stockfish-windows-x86-64-avx2.exe")
stockfish.set_fen_position(fen)
return stockfish.get_best_move()