defllm_make_move(model: Llama, prompt: str) -> str:
""" Call a model with a prompt """
res = model(prompt, stream=False, max_tokens=1024, temperature=0.8)
return res["choices"][0]["text"]
board = [["E","E","E"],["E","E","E"],["E","E","E"]]
defboard_to_string(board_data: List) -> str:
""" Convert board to the string representation """return"\n".join([" ".join(x) for x in board_data])
输出看起来像这样:
E E E
E E E
E E E
现在,创建模型提示:
sys_prompt1 = """You play a tic-tac-toe game. You make a move by placing X, your opponent plays by placing O. Empty cells are marked with E. You can place X only to the empty cell."""
sys_prompt2 = """You play a tic-tac-toe game. You make a move by placing O, your opponent plays by placing X. Empty cells are marked with E. You can place O only to the empty cell."""
game_prompt = """What is your next move? Think in steps. Each row and column should be in range 1..3. Write the answer in JSON as {"ROW": ROW, "COLUMN": COLUMN}."""
Llama-2 和 Llama-3 的提示格式不同:
template_llama2 = f"""<s>[INST]<<SYS>>{sys_prompt1}<</SYS>> Here is the board image: __BOARD__\n {game_prompt} [/INST]"""
template_llama3 = f"""<|begin_of_text|> <|start_header_id|>system<|end_header_id|>{sys_prompt2}<|eot_id|> <|start_header_id|>user<|end_header_id|> Here is the board image: __BOARD__\n {game_prompt} <|eot_id|> <|start_header_id|>assistant<|end_header_id|>"""
defmake_move(board_data: List, move: Optional[dict], symb: str):
""" Update board with a new symbol """
row, col = int(move["ROW"]), int(move["COLUMN"])
if1 <= row <= 3and1 <= col <= 3:
if board_data[row - 1][col - 1] == "E":
board_data[row - 1][col - 1] = symb
else:
print(f"Wrong move: cell {row}:{col} is not empty")
else:
print("Wrong move: incorrect index")
检查游戏是否结束:
defcheck_for_end_game(board_data: List) -> bool:
""" Check if there are no empty cells available """return board_to_string(board_data).find("E") == -1defcheck_for_win(board_data: List) -> bool:
""" Check if the game is over """# Check Horizontal and Vertical linesfor ind inrange(3):
if board_data[ind][0] == board_data[ind][1] == board_data[ind][2] and board_data[ind][0] != "E":
print(f"{board_data[ind][0]} win!")
returnTrueif board_data[0][ind] == board_data[1][ind] == board_data[2][ind] and board_data[0][ind] != "E":
print(f"{board_data[0][ind]} win!")
returnTrue# Check Diagonalsif board_data[0][0] == board_data[1][1] == board_data[2][2] and board_data[1][1] != "E"or board_data[2][0] == board_data[1][1] == board_data[0][2] and board_data[1][1] != "E":
print(f"{board_data[1][1]} win!")
returnTruereturnFalse
>>> Prompt:<s>[INST]<<SYS>>You play a tic-tac-toe game. You make a move by placing X, your opponent plays by placing O. Empty cells are marked with E. You can place X onlyto the empty cell.<</SYS>> Here is the board image: X X O X O E E E E What is your next move? Think in steps. Eachrowandcolumn should be inrange1..3. Write the answer in JSON as {"ROW": ROW,"COLUMN": COLUMN}.[/INST]
>>> Response: Since there arenoempty cells on the board, I will place my X in the center cell: {"ROW":2,"COLUMN":1}.
>>>Prompt:<s>[INST]<<SYS>>You play a tic-tac-toe game. You make a move by placing X, your opponent plays by placing O. Empty cells are marked with E. You can place X only to the empty cell.<</SYS>> Here is the board image: O E E E X E E E E ...>>>Response: My next move would be to place my X in the center cell, which is empty. Here is the updated board: O E E E X X E E E JSON representation of my move: {"ROW":2,"COLUMN":2}