Package: chatchess
ChatChessA simple python package to play chess with ChatGPTInstallationpip install chatchessUsageImport the package:fromChatChessimportChatChessFirst aGameobject needs to be decalerd as follows:bot=ChatChess.Game("OPENAI_API_KEY")Additional parametersself.model = "gpt-3.5-turbo": ChatGPT model to be used. 3.5-turbo recommended, see models and pricing here:https://openai.com/pricingbot.maxTokens = 10: Set max_tokens passed to ChatGPT on each movebot.maxFails = 5: Amount of times to retry sending prompt to ChatGPT when invalid move is returnedbot.maxTime = 5: Maximum amount of seconds to wait for ChatGPT answer before timing outbot.prompt = {"normal" : "", "failed" : "", "start" : ""}: The prompts to send to ChatGPT at each game statebot.board = chess.Board(): Chess board objectbot.printDebug = False: Print debug info - occaisonaly usefulOutputmove = 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 moveFunctionsMain functionsmove = bot.play("e4"): Plays the player's move, then ChatGPT's response - returns ChatGPT's movemove = getGPTMove(): Plays ChatGPT's move in the current position - returns ChatGPT's moveOther functionsbot.pushPlayerMove("e4"): Push a move without ChatGPT respondingprompt = bot.createPrompt(): Creates prompt to send to ChatGPT based on current position and previous fails - returns promptresponse = bot.askGPT(prompt): Queries ChatGPT prompt based on set parameters, times out afterbot.maxTimesecondsmove = bot.handleResponse(response, player): Searches for chess move in string - adds it to self.move as playerErrorsMoveLimitError: Move fail limit reachedBadInputMoveError: Move inputted can't be playedExamplesSimple player vs ChatGPT gamefromChatChessimportChatChessbot=ChatChess.Game("OPENAI_API_KEY")# Set API keywhileTrue:print(bot.board)# Print the boardbot.play(input("Make a move: "))# Ask player to make a move, then ChatGPT respondsifbot.board.is_game_over():# Break if game overbreakSimple ChatGPT vs ChatGPT gamefromChatChessimportChatChessimportchess.pgnfromdatetimeimportdatebot=ChatChess.Game("OPENAI_API_KEY")# Set API keywhileTrue:bot.getGPTMove()# Ask ChatGPT to make a moveprint(bot.message)# Print move and infoifbot.board.is_game_over():# Break if game overgame=chess.pgn.Game.from_board(bot.board)# Create PGN from gamegame.headers["Event"]="ChatChess test"game.headers["Date"]=date.today().strftime("%d.%m.%Y")game.headers["White"]="ChatGPT"game.headers["Black"]="ChatGPT"print(game)breakFunction for returning ChatGPT moves as FEN from a set position (eg. for a Lichess bot)fromChatChessimportChatChessimportchessbot=ChatChess.Game("OPENAI_API_KEY")# Set API keydefgetGPTMove():bot.board=chess.Board()# Pass board to ChatChessbot.getGPTMove()# Ask ChatGPT to make a movereturnbot.move["ChatGPT"]["FEN"].fen()# Return FEN moveWhen setting the board forbot.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 rateInfoUsespython-chess -https://github.com/niklasf/python-chessopenai-python -https://github.com/openai/openai-python