ChatChess
v1.1.9
一個簡單的python軟件包,可以與chatgpt下棋
pip install chatchess
導入包:
from ChatChess import ChatChess首先, Game對象需要如下刪節:
bot = ChatChess . Game ( "OPENAI_API_KEY" )self.model = "gpt-3.5-turbo" :要使用的chatgpt模型。建議使用3.5-Turbo,請參閱此處的模型和價格:https://openai.com/pricingbot.maxTokens = 10 :設置max_tokens在每一步中傳遞給chatgptbot.maxFails = 5 :返回無效移動時重試提示的次數bot.maxTime = 5 :最多等待chatgpt答案的秒數bot.prompt = {"normal" : "", "failed" : "", "start" : ""} :在每個遊戲狀態下發送到chatgpt的提示bot.board = chess.Board() :國際象棋闆對象bot.printDebug = False :打印調試信息 - occaisonaly有用move = bot.move["ChatGPT"]["uci"] :以給定格式(uci / san)返回給定播放器(chatgpt / input)的最後一步message = bot.message :每次GPT移動後返回移動主要功能
move = bot.play("e4") :扮演玩家的舉動,然後播放chatgpt的回應 - 返回chatgpt的移動move = getGPTMove() :播放chatgpt的動作在當前位置 - 返回chatgpt的動作其他功能
bot.pushPlayerMove("e4") :在沒有chatgpt響應的情況下推動移動prompt = bot.createPrompt() :創建提示以根據當前位置和以前的失敗發送到chatgpt-返回提示response = bot.askGPT(prompt) :根據設定參數查詢bot.maxTime提示move = bot.handleResponse(response, player) :搜索國際象棋在字符串中移動 - 將其添加到self.move作為播放器MoveLimitError :達到移動失敗極限BadInputMoveError :無法播放輸入的移動 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 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 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在設置bot.board時,請確保板不僅來自設定的位置