【編者按】如何訓(xùn)練 ChatGPT 實(shí)現(xiàn)自己想要的代碼與功能,秘訣就是編寫更具交互性和前瞻性的設(shè)計(jì)提示,本文作者分享了如何利用 ChatGPT 設(shè)計(jì)出可控制的代碼步驟。
原文鏈接:https://www.friendlyskies.NET/maybe/a-quick-way-to-get-more-creative-coding-control-with-chatgpt
作者 | Marc Carson
譯者 | 彎月
出品 | CSDN(ID:CSDNnews)
你想通過(guò) ChatGPT 更好地控制代碼的實(shí)現(xiàn)嗎?你希望 ChatGPT 的使用更加有趣,或者進(jìn)一步激發(fā)靈感嗎?
如果你的答案是肯定,請(qǐng)嘗試使用更具交互性、前瞻性的設(shè)計(jì)提示,它可以提供各種想法,比如下面這個(gè)例子(粗體是我想強(qiáng)調(diào)的部分):
Write a bash with three functions: Allow a user to select how many dice to roll, roll the dice, and announce the results graphically. For each function, allow me to pick from three different ways of creating the function.
編寫一個(gè) bash 腳本,其中包含三個(gè)功能:允許用戶選擇擲多少個(gè)骰子、擲骰子,并以圖形方式宣布結(jié)果。 對(duì)于每個(gè)函數(shù),請(qǐng)?zhí)峁┤N不同的函數(shù)創(chuàng)建方式,讓我從中進(jìn)行選擇。
得到的代碼如下:
#!/bin/bash # Function 1: Allowing the user to select how many dice to roll # Option 1: Read input from user select_dice_count_1{ read-p "Enter the number of dice to roll: "dice_count echo$dice_count } # Option 2: Using command-line arguments select_dice_count_2{ dice_count= $1 echo$dice_count } # Option 3: Using a default value select_dice_count_3{ dice_count=1 echo$dice_count } # Function 2: Rolling the dice # Option 1: Using the $RANDOM variable roll_dice_1{ echo$((RANDOM % 6 + 1)) } # Option 2: Using the shuf command roll_dice_2{ shuf -i 1-6 -n 1 } # Option 3: Using an array to represent the dice faces roll_dice_3{ dice_faces=( "1""2""3""4""5""6") index=$((RANDOM % 6)) echo" ${dice_faces[$index]}" } # Function 3: Announcing the results graphically # Option 1: Using a for loop and printf announce_results_1{ for((i = 1; i <= $1; i++)); do printf"[Dice $i] - $2n" done } # Option 2: Using echo and a separator line announce_results_2{ for((i = 1; i <= $1; i++)); do echo"Dice $i: $2" done echo"-----------------" } # Option 3: Using ASCII art for graphical representation announce_results_3{ for((i = 1; i <= $1; i++)); do echo"╔═══╗" echo"║ $2║" echo"╚═══╝" done } # MAIn # Selecting dice count dice_count=$(select_dice_count_1) # Change the function name to select_dice_count_2 or select_dice_count_3 for different options # Rolling the dice results= "" for((i = 1; i <= dice_count; i++)); do result=$(roll_dice_1) # Change the function name to roll_dice_2 or roll_dice_3 for different options results+= " $result" done # Announcing the results announce_results_1 $dice_count" $results" # Change the function name to announce_results_2 or announce_results_3 for different options接下來(lái),你只需要修改函數(shù)名稱(ChatGPT 在注釋中指出了這些位置)。
你是否注意到,現(xiàn)在實(shí)際上你面臨著軟件設(shè)計(jì)問(wèn)題,而不僅僅是“如何編寫代碼”的問(wèn)題?
這表明,使用 ChatGPT 編寫代碼的時(shí)候,你還可以進(jìn)行一些設(shè)計(jì),而不僅僅是簡(jiǎn)單地獲取代碼。
不要止步于此
隨著創(chuàng)意源泉的涌現(xiàn),你可以而且應(yīng)該繼續(xù)尋求更多有創(chuàng)意或有趣的想法。例如,后續(xù)的提示可以這么寫:
Give 3 more creative options for the display of the result.
提供三個(gè)有創(chuàng)意的顯示結(jié)果的方式。
ChatGPT 生成的代碼提供了如下三種方式:
1)用點(diǎn)表示的 ASCII 骰子;
2)ANSI 彩色骰子圖形;
3)表情符骰子。
你還可以進(jìn)一步,比如我要求 ChatGPT 給出三個(gè)更有創(chuàng)意的選項(xiàng),然后它提供了一種播放擲骰子音效的方法,還有兩種制作擲骰子動(dòng)畫的方法。
看到音效選項(xiàng),我非常驚訝,我不禁在想這是一個(gè)無(wú)聊的腳本,還是一個(gè)更具創(chuàng)意的有趣腳本?
為設(shè)計(jì)增加一些靈活性
這個(gè)簡(jiǎn)單的技巧可以給你提供更多方向性,以作者的身份控制腳本或程序的初始風(fēng)格、感覺(jué)和功能。
你可以快速審查各種不同的設(shè)計(jì)選擇,更快地開發(fā)出更好的結(jié)果。
此外,開發(fā)完成后,將來(lái)再修改會(huì)變得更加困難,主要受限于個(gè)人的記憶力和耐心,因此這種更具交互性的設(shè)計(jì)會(huì)更有優(yōu)勢(shì)。
請(qǐng)注意,你需要根據(jù)腳本或編程的風(fēng)格來(lái)修改代碼。請(qǐng)務(wù)必利用 ChatGPT 可以使用的額外提示來(lái)指定你希望查看的代碼類型的詳細(xì)信息。
ChatGPT 也可用于編程之外的工作
這個(gè)技巧還可用于向 ChatGPT 詢問(wèn)與代碼無(wú)關(guān)的技巧和其他查詢。
例如,在詢問(wèn)有關(guān)指定主題的技巧時(shí),你可以添加如下內(nèi)容:
Present the tips in three different styles, only presenting the first tip of each style at first. Allow me to choose which style I like best before continuing with the rest of the tips.
以三種不同的方式呈現(xiàn)技巧,首先呈現(xiàn)每種風(fēng)格的第一個(gè)技巧。在顯示后續(xù)技巧前,請(qǐng)?jiān)试S我選擇我最喜歡的風(fēng)格。
ChatGPT 的輸出可以給你更多啟發(fā),讓你更清楚地思考使用這些信息的方式。這意味著,在與 ChatGPT 合作設(shè)計(jì)的時(shí)候,你可以運(yùn)用概念化技能,以獲得更好的結(jié)果。
你會(huì)發(fā)現(xiàn),這樣得到的結(jié)果更有創(chuàng)意。至少,不會(huì)感覺(jué)自己只是在做復(fù)制粘貼。
總結(jié)和一些最終想法
這種編寫提示的小技巧不僅可以增加工作的樂(lè)趣,而且在我看來(lái),還可以讓你更好地了解 ChatGPT 的眾多功能。
大多數(shù)人在尋求編程方面的幫助時(shí),只注重一個(gè)單一的“好”結(jié)果。但其實(shí),在與 ChatGPT 交互時(shí),注重拓寬思路,有利于獲得更好的結(jié)果。






