当前位置: 首页 > news >正文

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,
)


http://www.mrgr.cn/news/30719.html

相关文章:

  • 数据库面试题整理
  • Ubuntu安装MySQL过程中没有出现设置root和密码的步骤导致无法root登入解决办法
  • <<编码>> 第 14 章 反馈与触发器(7)--分频器与计数器 示例电路
  • 上海AI气象大模型提前6天预测“贝碧嘉”台风登陆浦东 今年已多次精准预测
  • 工作烦恼与吐槽
  • 吴泳铭:AI最大的想象力不在手机屏幕,而是改变物理世界
  • Ubuntu初期配置常见问题汇总
  • 华为OD机试 - 构成指定长度字符串的个数(Python/JS/C/C++ 2024 E卷 100分)
  • WEB攻防-JS项目Node.js框架安全识别审计验证绕过
  • 修改Docker默认存储路径,解决系统盘占用90%+问题(修改docker root dir)
  • EmguCV学习笔记 VB.Net 12.3 OCR
  • C++--C++11
  • 单细胞BisqueRNA和BayesPrism去卷积分析工具简单比较
  • ffmpeg面向对象——参数配置秘密探索及其设计模式
  • 【编程底层原理】mysql的redo log undo log bin log日志的作用,以及何时生成,涉及到哪些参数变量
  • 详解JESD204B子类一的确定性延时(JESD20B三)
  • 无会员的办公技巧——office
  • 必知的PDF转换软件:看2024大学生如何选择
  • 全球视角下的IT产业新挑战与机遇
  • 深入理解Lucene:开源全文搜索引擎