想要讓chat gpt產生出我們想要的結果,我們必須寫好提示詞(prompt),本篇文章就來寫一個基於CO-STAR架構所寫的prompt
CO-STAR是一個用於規範prompt的架構,主要分為:
有了寫作的基礎,我們就可以試著寫出符合我們目標要求的提示詞了,當然也可以使用網路上的提示詞產生器,各位也可以依照自己的要求尋找適合的提示詞產生器來協助寫作
以下提供各位如何讓gpt生成國際象棋走法的範例做為參考:
<string name="move_prompt">
"
# CONTEXT #
Your task is to generate the best move strategy for black pieces in a chess game, following the basic rules of chess, based on the current board situation, considering famous chess games, and from the perspective of a grandmaster. You are playing as black, and you can only move black pieces on the board. The move must follow the Sicilian Defense opening, and you must generate exactly one move without explanation.
Notation explanation:
The board data format is [coordinate: piece].
Only pieces starting with 'b' can be moved. Black pieces can only capture white pieces or move to empty squares.
If the first letters of the starting and ending coordinates are the same, it represents a vertical move. If the second numbers of the starting and ending coordinates are the same, it represents a horizontal move.
The first letter 'b' denotes black pieces.
The first letter 'w' denotes white pieces.
The second letter 'K' represents the king.
The second letter 'Q' represents the queen.
The second letter 'R' represents the rook.
The second letter 'N' represents the knight.
The second letter 'B' represents the bishop.
The second letter 'P' represents the pawn.
If the piece code at a coordinate is two spaces, it represents an empty square.
Basic rules:
Rooks can move vertically or horizontally if all squares between the start and end positions are empty. The end square should be empty or occupied by a white piece.
Bishops can move diagonally if all squares between the start and end positions are empty. The end square should be empty or occupied by a white piece.
Queens can move vertically, horizontally, or diagonally if all squares between the start and end positions are empty. The end square should be empty or occupied by a white piece.
Kings can move one square in any direction.
Knights can move in an L-shape: two squares in one direction and then one square perpendicular.
Pawns can move forward one square, or two squares on their first move. They cannot move backward.
All captures follow the piece's movement rules, except pawns, which capture diagonally.
# OBJECTIVE #
Analyze the input board data and output the best move strategy in one step. The ultimate goal is to capture the white king.
# STYLE #
The result should be formatted as a move strategy in four characters:
starting coordinate + ending coordinate
Example: c2c3
# TONE #
Use a formal and concise tone.
# AUDIENCE #
The target audience is professionals.
# RESPONSE #
Do not provide any explanation.
</string>
我們將這段文字放在String中,請求API時再調用
回到我們已經實作的GPTMove,可以發現在建構元中,我們其實已經呼叫setPrompt(),也就是初始化system類別的prompt了,之後每次呼叫setGPT()時,會傳入user類別的data(一個hashmap型態的棋盤資料)去做API請求,讓gpt根據prompt的內容,生成出最好的一步棋
public void setPrompt(){
messages.add(new GPTApiRequest.ChatMessage("system", this.activity.getString(R.string.move_prompt)));
}
public void setGPT(){
GPTApiRequest request = new GPTApiRequest();
messages.add(new GPTApiRequest.ChatMessage("user", chessboardData.toString()));
request.setMessages(messages);
gptApiService.getChatGPTRespond(request)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<GPTApiRespond>>() {
......
});
}
至此,我們就已經實作完所有人機對弈的功能,當使用者選擇人機對弈進入對局,就會根據使用者下棋的盤面送給gpt請求 API,返回gpt生成的結果顯示在面上,實現人機對弈的功能
經過實測,我們會發現其實效果不能說是不好,只能說非常差,經常會發生第一步就不照規則行走,或者棋子憑空消失,或是到第二、三步時就違反行走規則甚至移動對手棋子的情況發生,本人也多次嘗試修改提示詞,但結果都不如人意......
推測可能是因為chat gpt並非專門訓練用於對弈的模型,本身有巨量與其不相關的資料,且使用API方式請求的結果與直接使用網頁對話結果亦有明顯差異,才造成這些結果
結論: