GPT-4, Chess Parter

I’ve had more than a few discussions with GPT, discussing topics ranging from technical to philosophical to the everyday. Along the way, I got to thinking about machine learning models—like AlphaGo—that not only mastered specific games to become world champions but also evolved into entities like AlphaZero that could generalize across different games with impressive skill.

Can a language model like GPT-4, famous for slinging words, actually throw down in the gaming ring? How versatile can this digital brain get?

So I asked it.

convo
convo

And it responded with confidence. It even has the capability to play a game in ASCII right in the chat!

With this, I figured it had datasets of chess games in its training data. The engineers must have gathered thousands of game summaries, likely move histories turned into string parsable format, and thrown them into the mix. I figure openings, finishing techniques, and special moves like castling aren’t difficult to add in as well.

Given that it can play a live game in a chat, I extrapolated that it could be used as an opponent in a chess engine.

User vs. GPT-4

I searched online and found a chess library in Python, python-chess. I setup a regular game, and had the turns alternate between the user and an API call to GPT-4. Every time it is GPT-4’s turn, the board and past moves are fed into the prompt for context. There are also instructions to specifically reply in Standard Algebraic Notation with nothing else, so the move can be parsed.

Originally, the ASCII board looked visually stretched in the terminal, so I changed the styling and spacing to make it square again.

Then I added another API call so it makes a funny comment every move.

example user vs. gpt-4
example user vs. gpt-4

GPT-4 vs. GPT-4

If it can play a user, it can play itself, right?

I made a copy of the script and changed the game loop to call the GPT-4 move function for every move, instead of alternating with the user.

Here’s GPT-4 playing, and dissing, itself:

example gpt-4 vs. gpt-4
example gpt-4 vs. gpt-4

Not the most friendly to your API usage rate and monthly bill, but definitely entertaining.

Would love to see a statistical analysis study done on the gameplay, and also what ELO ranking it plays at.

Wanna Play?

If you want to try it out yourself, check out the Github Repo here.

Installation

Before running the program, ensure you have Python installed on your system. Then, install the required Python packages using pip:

pip install python-chess openai python-dotenv

You'll also need to obtain an API key from OpenAI and set it in an .env file in the same directory as the script:

OPENAI_API_KEY='your_api_key_here'

Usage

To play a game against GPT-4, run the gptchess.py script:

python gptchess.py

To watch GPT-4 play against itself, run the gptchess-inception.py script:

python gptchess-inception.py

Note: Keep an eye on your OpenAI API usage, since each move and comment incurs costs. The API call for the comments can be deleted easily to save cost in the game loop function.

Subscribe to jeffy yu
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.