Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 

The domain of artificial intelligence considers chess play as a conventional challenge. Therefore, I’ve decided to use chess as a first try of AutoGen with ChatGPT and together with a Stockfish chess engine API to play a chess match.

ChatGPT, developed by OpenAI, is an AI model that, while not specifically designed for chess, can propose moves based on opening books and past games. However, it lacks the ability to discern whether a move is valid or advantageous. AutoGen complements this by using ChatGPT to generate and execute code.

The Match

The match is the game Poole versus HAL 9000 from the 2001: A Space Odyssey movie. Before the game is over HAL 9000 explains Poole that he has no chance and Poole accepts his loss. I decided to let the game continue until checkmate was achieved.

Stockfish is a powerful open-source chess engine and Jacute Technologies provides a REST API to a Stockfish instance. The API call is made as a GET request, with the Forsyth–Edwards Notation (FEN) string, depth, and mode as parameters. The depth is limited to 13, which is acceptable as a more accurate result is not necessary.

The Prompt

AutoGen is a framework that works with multi-agent systems and ChatGPT. A prompt is used to define the tasks of an agent.

For the chess agent prompt, I used only obvious data and defined the prompt with the following conditions:

  • The REST GET url from the page was known.
  • The depth had to be less than 14.
  • The FEN definition was taken from the Poole versus HAL 9000 game.
  • No information about the response was provided.
  • I let AutoGen to decide how to create a chess board image and a gif file.

With these assumptions, I 'engineered' this prompt:

you are a chess agent
continue the game "5rk1/2p1bppp/Q7/1p2n3/5n2/2P2q2/PP1P1PbP/RNBBR1K1 w - - 2 16" by calling the API
play with depth 13 until the game is over
this is the api call as GET: w - - 5 11&depth=5&mode=bestmove
draw after each move the chess board and generate at the end a gif

The Challenge

ChatGPT comprehends the API call and the significance of the parameters, but it's unfamiliar with the structure of the response. The initial expectation was that "bestmove" is a property of the result:

best_move = response.json()['bestmove']

This led to an error which the generated Python program displayed. ChatGPT then revised the program based on this output which then simply returned the result string:

{'success': True, 'data': 'bestmove h2h3 ponder f6h5'}

Based on this string, ChatGPT interpreted that the "data" property contains the string "bestmove" and the second string has the required value. It has changed the code accordingly:

# Extract the 'bestmove' from the 'data' string
data = response.json()['data']
best_move = data.split(' ')[1]

With this adjustment, the game was played with a depth of 13 until white was check mate. AutoGen ended with this information:

Great! The script has executed successfully. It should have generated a GIF file named "chess_game.gif" in the same directory.
This GIF file shows the progression of the chess game. If the 'data' key was not found in the response data,
the script would have printed out the response data and stopped the game. 

This is the generated gif file:

Poole vs HAL 9000Poole vs HAL 9000


This example showcases the adaptability and flexibility of ChatGPT together with AutoGen, and how it can be used with other tools like the Stockfish chess engine API to play a game of chess. It's fascinating how AI can interact with different systems and adapt to unforeseen situations.