# GPT-4, Chess Parter

By [jeffy yu](https://paragraph.com/@lljeffy) · 2023-12-04

---

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](https://storage.googleapis.com/papyrus_images/2dd4c0978623cc564973ee8514b68c71980f7ea494259c8d97b71d0a3911fe36.png)

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](https://storage.googleapis.com/papyrus_images/625280690b45842dc45bf0e387c4f23a22340b5b120be49163d5958f4228f216.gif)

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](https://storage.googleapis.com/papyrus_images/534b8c4a7072ea420bb138092e7f6102ade2cae054cc6f82d1934361b67dd315.gif)

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](https://github.com/yu-jeffy/gpt-chess).

### 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._

---

*Originally published on [jeffy yu](https://paragraph.com/@lljeffy/gpt-4-chess-parter)*
