AutoGen Function Call 函数调用解析(二)
AutoGen Function Call 函数调用解析(二)
import os
from autogen import ConversableAgent, register_function, config_list_from_json# 配置LLM
config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST",
)player_white_config_list = config_list
player_black_config_list = config_listfrom typing import Listimport chess
import chess.svg
from IPython.display import display
from typing_extensions import Annotated# Initialize the board.
board = chess.Board()# Keep track of whether a move has been made.
made_move = False# 获取可以移动的位置
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:return "Possible moves are: " + ",".join([str(move) for move in board.legal_moves])# 移动
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:move = chess.Move.from_uci(move)board.push_uci(str(move))global made_movemade_move = True# Display the board.display(chess.svg.board(board, arrows=[(move.from_square, move.to_square)], fill={move.from_square: "gray"}, size=200))# Get the piece name.piece = board.piece_at(move.to_square)piece_symbol = piece.unicode_symbol()piece_name = (chess.piece_name(piece.piece_type).capitalize()if piece_symbol.isupper()else chess.piece_name(piece.piece_type))return f"Moved {piece_name} ({piece_symbol}) from {chess.SQUARE_NAMES[move.from_square]} to {chess.SQUARE_NAMES[move.to_square]}."# 白棋手
player_white = ConversableAgent(name="Player White",system_message="You are a chess player and you play as white. ""First call get_legal_moves() first, to get list of legal moves. ""Then call make_move(move) to make a move.",llm_config={"config_list": player_white_config_list, "cache_seed": None},
)# 黑棋手
player_black = ConversableAgent(name="Player Black",system_message="You are a chess player and you play as black. ""First call get_legal_moves() first, to get list of legal moves. ""Then call make_move(move) to make a move.",llm_config={"config_list": player_black_config_list, "cache_seed": None},
)# Check if the player has made a move, and reset the flag if move is made.
def check_made_move(msg):global made_moveif made_move:made_move = Falsereturn Trueelse:return False# 棋盘
board_proxy = ConversableAgent(name="Board Proxy",llm_config=False,# The board proxy will only terminate the conversation if the player has made a move.is_termination_msg=check_made_move,# The auto reply message is set to keep the player agent retrying until a move is made.default_auto_reply="Please make a move.",human_input_mode="NEVER",
)# 两个棋手分别注册 make_move 和 get_legal_moves 函数
register_function(make_move,caller=player_white,executor=board_proxy,name="make_move",description="Call this tool to make a move.",
)register_function(get_legal_moves,caller=player_white,executor=board_proxy,name="get_legal_moves",description="Get legal moves.",
)register_function(make_move,caller=player_black,executor=board_proxy,name="make_move",description="Call this tool to make a move.",
)register_function(get_legal_moves,caller=player_black,executor=board_proxy,name="get_legal_moves",description="Get legal moves.",
)# 嵌套
player_white.register_nested_chats(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",}],
)player_black.register_nested_chats(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",}],
)# Clear the board.
board = chess.Board()chat_result = player_black.initiate_chat(player_white,message="Let's play chess! Your move.",max_turns=4,
)