A package to play chess with ChatGPT
Project description
# ChatChess
A simple python package to play chess with ChatGPT
Installation
pip install -i https://test.pypi.org/simple/ 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:
bot.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.prompt = {"normal" : "", "failed" : "", "start" : ""}
: The prompts to send to ChatGPt at each game statebot.board = chess.Board()
: Chess board objectbot.message = ""
: Set to move into after each GPT movebot.printDebug = False
: Print debug info - occaisonaly useful
Functions
Main functions
move = 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 move
Other functions
bot.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 parametersmove = bot.handleResponse(response)
: Gets move from reponse - returns move
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 a set position
import chess
import chess.fen
from ChatChess import ChatChess
bot = ChatChess.Game("OPENAI_API_KEY") # Set API key
bot.board = chess.Board("rnbq1bnr/ppppkppp/8/4p3/4P3/8/PPPPKPPP/RNBQ1BNR w - - 2 3") # Set position
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
print(str(chess.pgn.Game.from_board(bot.board)))
break
Info
Uses
- python-chess - https://github.com/niklasf/python-chess
- openai-python - https://github.com/openai/openai-python
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
chatchess-0.0.2.tar.gz
(11.8 kB
view hashes)
Built Distribution
chatchess-0.0.2-py3-none-any.whl
(11.9 kB
view hashes)
Close
Hashes for chatchess-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2139e8a4ac1046cc5df4a4e9fbbada06c42afe66b4976089219b6d91d7d17fd |
|
MD5 | 2c5f0530c9788f67e63c55b6ab127be0 |
|
BLAKE2b-256 | ee9e24f8aec5148826ef6c4b8555ffe25e0d26b0edf084a6e5a76dbb18291e3a |