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时,请确保板不仅来自设定的位置