Skip to main content

ChatChess

A simple python package to play chess with ChatGPT

Installation

pip install chatchess

Usage

Import the package:

from ChatChess import ChatChess

First a Game object needs to be decalerd as follows:

bot = ChatChess.Game("OPENAI_API_KEY")

Additional parameters

  • self.model = "gpt-3.5-turbo": ChatGPT model to be used. 3.5-turbo recommended, see models and pricing here: https://openai.com/pricing
  • bot.maxTokens = 10: Set max_tokens passed to ChatGPT on each move
  • bot.maxFails = 5: Amount of times to retry sending prompt to ChatGPT when invalid move is returned
  • bot.maxTime = 5: Maximum amount of seconds to wait for ChatGPT answer before timing out
  • bot.prompt = {"normal" : "", "failed" : "", "start" : ""}: The prompts to send to ChatGPT at each game state
  • bot.board = chess.Board(): Chess board object
  • bot.printDebug = False: Print debug info - occaisonaly useful

Output

  • move = bot.move["ChatGPT"]["uci"]: Returns the last move of given player (ChatGPT / input) in the given format (uci / san)
  • message = bot.message: Returns the move into after each GPT move

Functions

Main functions

  • move = bot.play("e4"): Plays the player's move, then ChatGPT's response - returns ChatGPT's move
  • move = getGPTMove(): Plays ChatGPT's move in the current position - returns ChatGPT's move

Other functions

  • bot.pushPlayerMove("e4"): Push a move without ChatGPT responding
  • prompt = bot.createPrompt(): Creates prompt to send to ChatGPT based on current position and previous fails - returns prompt
  • response = bot.askGPT(prompt): Queries ChatGPT prompt based on set parameters, times out after bot.maxTime seconds
  • move = bot.handleResponse(response, player): Searches for chess move in string - adds it to self.move as player

Errors

  • MoveLimitError: Move fail limit reached
  • BadInputMoveError: Move inputted can't be played

Examples

Simple player vs ChatGPT game

from ChatChess import ChatChess

bot = ChatChess.Game("OPENAI_API_KEY")  # Set API key

while True:
    print(bot.board)  # Print the board
    bot.play(input("Make a move: "))  # Ask player to make a move, then ChatGPT responds
    if bot.board.is_game_over():  # Break if game over
        break

Simple ChatGPT vs ChatGPT game

from ChatChess import ChatChess
import chess.pgn
from datetime import date

bot = ChatChess.Game("OPENAI_API_KEY")  # Set API key

while True:
    bot.getGPTMove()  # Ask ChatGPT to make a move
    print(bot.message)  # Print move and info
    if bot.board.is_game_over():  # Break if game over
        game = chess.pgn.Game.from_board(bot.board)  # Create PGN from game
        game.headers["Event"] = "ChatChess test"
        game.headers["Date"] = date.today().strftime("%d.%m.%Y")
        game.headers["White"] = "ChatGPT"
        game.headers["Black"] = "ChatGPT"

        print(game)
        break

Function for returning ChatGPT moves as FEN from a set position (eg. for a Lichess bot)

from ChatChess import ChatChess
import chess

bot = ChatChess.Game("OPENAI_API_KEY")  # Set API key

def getGPTMove():
    bot.board = chess.Board()  # Pass board to ChatChess
    bot.getGPTMove()  # Ask ChatGPT to make a move
    return bot.move["ChatGPT"]["FEN"].fen()  # Return FEN move

When setting the board for bot.board, make sure that the board is not simply from a set position, but has the moves in pgn format to increase chatgpt move success rate

Info

Uses

Metadata

Release files for chatchess 1.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chatchess 1.1.9
File Size Uploaded
chatchess-1.1.9.tar.gz 17.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chatchess 1.1.9
File Interpreter ABI Platform
chatchess-1.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 34.0 kB

Release files / chatchess-1.1.9.tar.gz

Download URL chatchess-1.1.9.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0ea327d95b69db559694cfde6d840ab805d61b57375887fbd23079d5caf0679b
BLAKE2b-256 checksum
How to use checksums
a250d4de92296030fa3c77879e30dba8a27b32f50ce5e3c57909919848b73423
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.6

Release files / chatchess-1.1.9-py3-none-any.whl

Download URL chatchess-1.1.9-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
885e2c53d9262f1399fb35621feff62154eddb61dab8edcf918b60dd41ed5b56
BLAKE2b-256 checksum
How to use checksums
fdacf773f544939532ecfaabc8b01e4c5c16b9da3f6835a39b3d4e4ae8cb8c00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.1.9 This release

2 release files

1.1.8

1 release file

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.13

1 release file

1.0.12

1 release file

1.0.11

1 release file

1.0.10

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

2 release files

1.0.1

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page