|
|
from smolagents import Tool |
|
|
import numpy as np |
|
|
import textwrap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FENTool(Tool): |
|
|
name = "_my_tool_fen" |
|
|
description = """ |
|
|
Convert a list of chess pieces into FEN notation |
|
|
To invoke the tool use code as below |
|
|
<code> |
|
|
loaded_fen = _my_tool_fen(chest_pieces="dummy") |
|
|
</code> |
|
|
""" |
|
|
|
|
|
inputs = { |
|
|
"chest_pieces": { |
|
|
"type": "string", |
|
|
"description": "string with chest pieces", |
|
|
} |
|
|
} |
|
|
|
|
|
output_type = "string" |
|
|
|
|
|
def __shortenFEN(self, fen): |
|
|
"""Reduce FEN to shortest form (ex. '111p11Q' becomes '3p2Q')""" |
|
|
return fen.replace('11111111','8').replace('1111111','7') \ |
|
|
.replace('111111','6').replace('11111','5') \ |
|
|
.replace('1111','4').replace('111','3').replace('11','2') |
|
|
|
|
|
def forward(self, chest_pieces: str) -> str: |
|
|
arr = textwrap.wrap(chest_pieces, 8) |
|
|
|
|
|
|
|
|
arr = arr[::-1] |
|
|
|
|
|
|
|
|
arr = [row[::-1] for row in arr] |
|
|
|
|
|
fen_long = "/".join(arr) |
|
|
|
|
|
|
|
|
fen_short = self.__shortenFEN(fen_long) |
|
|
|
|
|
|
|
|
fen_enhanced = f"{fen_short} b - - 0 1" |
|
|
print(f"FEN: {fen_enhanced}") |
|
|
|
|
|
_out = fen_enhanced |
|
|
return _out |
|
|
|