Last active
February 27, 2024 19:56
-
-
Save fsndzomga/9f42e50be432122b06ee7cc64ec11665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chess | |
import chess.engine | |
from langchain.agents import load_tools | |
from langchain.agents import initialize_agent | |
from langchain.agents import AgentType | |
from langchain.llms import OpenAI | |
from config import OPENAI_API_KEY, SERPAPI_API_KEY | |
import os | |
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY | |
os.environ['SERPAPI_API_KEY'] = SERPAPI_API_KEY | |
llm = OpenAI(temperature=0) | |
tools = load_tools(["serpapi"], llm=llm) | |
agent_executor = initialize_agent(tools, | |
llm, | |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
verbose=True) | |
# Initialize a chess board and the chess engine | |
engine = chess.engine.SimpleEngine.popen_uci("/opt/homebrew/Cellar/stockfish/16/bin/stockfish") | |
def play_game(): | |
moves = [] # Variable to store the list of moves | |
board = chess.Board() | |
def get_agent_move(board): | |
feedback = "" | |
while True: | |
prompt = f"Current board: {board}\nMove history: {moves}\nChoose the next move for black in UCI format. The available legal moves are {list(board.legal_moves)}. The move you select should be among the legal moves in UCI format like this:'Move.from_uci('g7g5')' {feedback}" | |
response = agent_executor.invoke({"input": prompt}) | |
next_move = response["output"][-6:-2] | |
try: | |
move = chess.Move.from_uci(next_move) | |
if move in board.legal_moves: | |
return move | |
else: | |
feedback = f"Agent's generated move {move} is not valid currently." | |
except: | |
feedback = "Failed to parse the Agent's generated move. Retrying..." | |
while not board.is_game_over(): | |
if board.turn: # True for white's turn, False for black's turn | |
result = engine.play(board, chess.engine.Limit(time=2.0)) | |
board.push(result.move) | |
moves.append(result.move.uci()) # Store UCI move in the list | |
else: | |
move = get_agent_move(board) | |
board.push(move) | |
moves.append(move.uci()) # Store UCI move in the list | |
print(board) | |
print("\n\n") | |
# Check the result of the game | |
winner = "" | |
if board.is_checkmate(): | |
if board.turn: | |
winner = "Black" | |
else: | |
winner = "White" | |
elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition() or board.is_variant_draw(): | |
winner = "Draw" | |
# Return the result | |
if winner == "Black": | |
return "Agent wins by checkmate." | |
elif winner == "White": | |
return "Stockfish wins by checkmate." | |
else: | |
return "The game is a draw." | |
# Number of games to play | |
n_games = 10 | |
# Initialize a dictionary to store the results | |
results = {"GPT-4 wins": 0, "Stockfish wins": 0, "Draw": 0} | |
# Run the game n times | |
for i in range(n_games): | |
print(f"Starting game {i+1}...") | |
result = play_game() | |
print(result) | |
# Update the results dictionary based on the outcome of the game | |
if "Agent wins" in result: | |
results["GPT-4 wins"] += 1 | |
elif "Stockfish wins" in result: | |
results["Stockfish wins"] += 1 | |
else: | |
results["Draw"] += 1 | |
print(f"Game {i+1} finished.\n\n") | |
# Print the final results | |
print("Final results after playing", n_games, "games:") | |
print("Agent won:", results["Agent wins"], "games") | |
print("Stockfish won:", results["Stockfish wins"], "games") | |
print("Draw:", results["Draw"], "games") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment