Spaces:
Sleeping
Sleeping
File size: 1,190 Bytes
25859e7 30003b4 25859e7 30003b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from langchain_core.tools import tool
import requests
@tool
def get_best_move(fen: str) -> str:
"""
Given the description of a chessboard using FEN notation, returns the next best move.
Args:
fen (str): The description of the chessboard position in FEN notation.
Returns:
str: The description of the next best move.
"""
url = "https://lichess.org/api/cloud-eval"
params = {
"fen": fen,
"multiPv": 1, # only top move
"syzygy": 5 # enable tablebase for 5-piece positions
}
response = requests.get(url, params=params)
if response.status_code != 200:
return f"API error: {response.status_code} - {response.text}"
data = response.json()
if not data.get("pvs"):
return "No best move found."
best_line = data["pvs"][0]
score = best_line["cp"] if "cp" in best_line else f"Mate in {best_line.get('mate')}"
move = best_line["moves"].split()[0]
# Optional: threshold to define a "guaranteed win"
if "mate" in best_line or best_line.get("cp", 0) > 500:
return move
return f"No clearly winning move. Best suggestion: {move} (score: {score})"
|