displaying ui
This commit is contained in:
parent
56081c673f
commit
1df6d6d62f
1 changed files with 149 additions and 117 deletions
|
|
@ -1,9 +1,6 @@
|
|||
import os
|
||||
import chess
|
||||
import chess.svg
|
||||
import streamlit as st
|
||||
from typing import List, Annotated
|
||||
from IPython.display import display, SVG
|
||||
from autogen import ConversableAgent, register_function
|
||||
|
||||
# Initialize session state for the OpenAI API key and game state
|
||||
|
|
@ -15,6 +12,8 @@ if "made_move" not in st.session_state:
|
|||
st.session_state.made_move = False
|
||||
if "board_svg" not in st.session_state:
|
||||
st.session_state.board_svg = None
|
||||
if "move_history" not in st.session_state:
|
||||
st.session_state.move_history = []
|
||||
|
||||
# Streamlit sidebar for OpenAI API key input
|
||||
st.sidebar.title("Chess Agent Configuration")
|
||||
|
|
@ -24,12 +23,12 @@ if openai_api_key:
|
|||
st.sidebar.success("API key saved!")
|
||||
|
||||
# Function to get legal moves
|
||||
def available_moves() -> Annotated[str, "A list of available legal moves in the UCI format"]:
|
||||
def available_moves() -> str:
|
||||
available_moves = [str(move) for move in st.session_state.board.legal_moves]
|
||||
return "Available moves are: " + ",".join(available_moves)
|
||||
|
||||
# Function to make a move
|
||||
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
||||
def make_move(move: str) -> str:
|
||||
try:
|
||||
chess_move = chess.Move.from_uci(move)
|
||||
if chess_move not in st.session_state.board.legal_moves:
|
||||
|
|
@ -46,6 +45,7 @@ def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "
|
|||
size=400
|
||||
)
|
||||
st.session_state.board_svg = board_svg # Save SVG to session state
|
||||
st.session_state.move_history.append(board_svg) # Save move history
|
||||
|
||||
# Get moved piece details
|
||||
moved_piece = st.session_state.board.piece_at(chess_move.to_square)
|
||||
|
|
@ -81,6 +81,7 @@ def check_made_move(msg):
|
|||
|
||||
# Initialize players and proxy agent if API key is provided
|
||||
if st.session_state.openai_api_key:
|
||||
try:
|
||||
player_white_config_list = [
|
||||
{
|
||||
"model": "gpt-4o-mini",
|
||||
|
|
@ -158,11 +159,8 @@ if st.session_state.openai_api_key:
|
|||
trigger=player_black,
|
||||
chat_queue=[
|
||||
{
|
||||
# The initial message is the one received by the player agent from
|
||||
# the other player agent.
|
||||
"sender": board_proxy,
|
||||
"recipient": player_white,
|
||||
# The final message is sent to the player agent.
|
||||
"summary_method": "last_msg",
|
||||
}
|
||||
],
|
||||
|
|
@ -172,22 +170,39 @@ if st.session_state.openai_api_key:
|
|||
trigger=player_white,
|
||||
chat_queue=[
|
||||
{
|
||||
# The initial message is the one received by the player agent from
|
||||
# the other player agent.
|
||||
"sender": board_proxy,
|
||||
"recipient": player_black,
|
||||
# The final message is sent to the player agent.
|
||||
"summary_method": "last_msg",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
# Streamlit UI for playing the game
|
||||
st.title("Chess Agent Game")
|
||||
st.title("Chess with AG2 Agents")
|
||||
st.info("""
|
||||
This chess game is played between two AutoGen AI agents:
|
||||
- **Player White**: A GPT-4o-mini powered chess player controlling white pieces
|
||||
- **Player Black**: A GPT-4o-mini powered chess player controlling black pieces
|
||||
|
||||
The game is managed by a **Board Proxy Agent** that:
|
||||
- Validates all moves
|
||||
- Updates the chess board
|
||||
- Manages turn-taking between players
|
||||
- Provides legal move information
|
||||
""")
|
||||
|
||||
# Display the initial board state before the game starts
|
||||
initial_board_svg = chess.svg.board(st.session_state.board, size=300)
|
||||
st.subheader("Initial Board")
|
||||
st.image(initial_board_svg)
|
||||
|
||||
if st.button("Start Game"):
|
||||
st.session_state.board.reset()
|
||||
st.session_state.made_move = False
|
||||
st.session_state.board_svg = None
|
||||
st.session_state.move_history = [] # Reset move history
|
||||
st.session_state.board_svg = chess.svg.board(st.session_state.board, size=300)
|
||||
st.info("The AI agents will now play against each other. Each agent will analyze the board, "
|
||||
"request legal moves, and make strategic decisions.")
|
||||
st.write("Game started! White's turn.")
|
||||
|
||||
# Initiate the chat between Player_White and Board_Proxy
|
||||
|
|
@ -195,9 +210,26 @@ if st.session_state.openai_api_key:
|
|||
recipient=player_white,
|
||||
message="Let's play chess! You go first, its your move.",
|
||||
max_turns=5,
|
||||
summary_method="reflection_with_llm" # Set a high enough number to allow the game to complete
|
||||
summary_method="reflection_with_llm"
|
||||
)
|
||||
st.markdown(chat_result.summary)
|
||||
|
||||
# Display the move history (boards for each move)
|
||||
st.subheader("Move History")
|
||||
for i, move_svg in enumerate(st.session_state.move_history):
|
||||
st.write(f"Move {i + 1}")
|
||||
st.image(move_svg)
|
||||
|
||||
# Reset Game button
|
||||
if st.button("Reset Game"):
|
||||
st.session_state.board.reset()
|
||||
st.session_state.made_move = False
|
||||
st.session_state.move_history = []
|
||||
st.session_state.board_svg = None
|
||||
st.write("Game reset! Click 'Start Game' to begin a new game.")
|
||||
|
||||
except Exception as e:
|
||||
st.error(f"An error occurred: {e}. Please check your API key and try again.")
|
||||
|
||||
else:
|
||||
st.warning("Please enter your OpenAI API key in the sidebar to start the game.")
|
||||
Loading…
Reference in a new issue