Programming by Prompt (การเขียนโปรแกรมด้วย Prompt)
นี่เป็นเทคโนโลยีที่ใหม่มาก และเรายังจับทางไม่ได้ดีนักว่าจะใช้มันยังไง มันชวนให้นึกถึงตอนที่เราเริ่มบุกเบิก Java และเว็บครั้งแรกๆ ยังมีใครจำ applet ได้ไหมครับ ต้องใช้เวลาหลายปีกว่าเราจะคิดหลักการพื้นฐานในการใช้ Java/C# ได้ดี และต้องใช้เวลาอีกหลายปีกว่าจะขัดเกลาหลักการเหล่านั้นให้ลงตัว
เรื่องเดียวกันนี้เกิดขึ้นกับ Ruby และ Python ตอนแรกเราก็สะเปะสะปะเดินผิดเดินถูก แล้วค่อยๆ หาทางที่ดีที่สุด จนเจอ และเชื่อผมเถอะครับ ตอนที่ FORTRAN เพิ่งออกมาใหม่ๆ ก็มีทั้งความกลัว ความทุกข์ใจ ความสงสัย และการเดิน ผิดเดินถูกเต็มไปหมด
เรื่องเดียวกันนี้ก็จะเกิดขึ้นกับ AI เช่นกัน ณ ตอนนี้เรายังไม่รู้เลยว่าต้องทำอย่างไร เราเพิ่งจะเริ่มสะเปะสะปะ เดินผิดเดินถูกในการเขียนโปรแกรมด้วย prompt เท่านั้น เรายังไม่รู้เลยว่าจะทำมันให้ดีได้อย่างไร
ยกตัวอย่างเช่น ลองดู prompt ต่อไปนี้ที่ผมป้อนให้ Grok 2 เมื่อไม่กี่วันก่อน:
2 . Grok 3 beta (March 19, 2025).
write a clojure program that finds all sentences
in a given file that contain a given substring.
Choose one of those sentences at random and find
the next word in the substring, or a . if no word follows.
Add the found word to the substring and remove the
first if the substring is longer than three words.
Repeat the entire process until the substring
ends in a . or has exceeded 50 words.
Accumulate all the words that were found and
return them appended to the initial substring.
นี่คือโค้ดที่ Grok สร้างขึ้นมา มันทำงานได้—ในระดับหนึ่ง
(ns markov
(:require [clojure.string :as str]))
(defn read-file [filename]
"Reads the content of a file and splits it into sentences."
(try
(let [content (slurp filename)
sentences (filter not-empty
(map str/trim
(str/split content #"[.!?]")))]
sentences)
(catch Exception e
(throw (Exception. (str "Error reading file: " (.getMessage e)))))))
(defn find-sentences-with-substring [sentences substring]
"Finds all sentences containing the substring."
(filter #(str/includes? (str/lower-case %) (str/lower-case substring))
sentences))
(defn get-next-word [sentence substring]
"Finds the next word after the substring
in the sentence, or '.' if none."
(let [index (str/index-of (str/lower-case sentence)
(str/lower-case substring))]
(if index
(let [post-substring (subs sentence (+ index (count substring)))
words (str/split (str/trim post-substring) #"\s+")]
(if (seq words)
(first words)
"."))
".")))
(defn update-substring [substring next-word]
"Updates the substring by adding the next word
and trimming if longer than 3 words."
(let [words (str/split substring #"\s+")
new-words (conj (vec words) next-word)
trimmed-words (if (> (count new-words) 3)
(subvec new-words 1)
new-words)]
(str/join " " trimmed-words)))
(defn sentence-walk [filename initial-substring]
"Main function to perform the sentence walking process."
(let [sentences (read-file filename)]
(loop [substring initial-substring
accumulated-words []
iterations 0]
(if (or (= (last (str/split substring #"\s+")) ".")
(> iterations 50))
(str/join " " (concat (str/split initial-substring #"\s+")
accumulated-words))
(let [matching-sentences (find-sentences-with-substring
sentences substring)]
(if (empty? matching-sentences)
(str/join " " (concat (str/split initial-substring #"\s+")
accumulated-words))
(let [random-sentence (rand-nth matching-sentences)
next-word (get-next-word random-sentence substring)]
(recur (update-substring substring next-word)
(conj accumulated-words next-word)
(inc iterations)))))))))
(defn -main [& args]
(if (< (count args) 2)
(println "Usage: program <filename> <initial-substring>")
(let [filename (first args)
initial-substring (second args)]
(try
(println (sentence-walk filename initial-substring))
(catch Exception e
(println (.getMessage e)))))))
มันง่ายมากที่จะรู้สึกประทับใจกับสิ่งนี้ ตัวผมเองก็ประทับใจแน่นอน แต่ในขณะเดียวกันมันก็ทำให้ผมกลัวด้วย ผมไม่ได้กลัวพลังของ AI ในการสร้างโค้ด ผมกลัวโครงสร้างของ prompt ที่แย่มากตัวนั้นต่างหาก
ลองดู prompt นั้นให้ดีๆ คำว่า sentence นิยามว่าอย่างไร คำว่า word นิยามว่าอย่างไร วลี the found word หมายความว่าอย่างไร ตัวอักษรจุดพวกนั้นเป็นเครื่องหมายวรรคตอนหรือเป็นคำนาม การ accumulate words หมายความว่าอย่างไร
ใช่ครับ Grok คิดหาคำตอบให้กับสิ่งเหล่านี้ได้ดีพอสำหรับการทดลองเล็กๆ นี้ แต่โปรแกรมที่ได้ออกมานั้นยังห่างไกล จากความพร้อมใช้งานจริงมาก มันทำผิดพลาดโง่ๆ อยู่หลายจุด ซึ่งส่วนใหญ่ก็เกี่ยวข้องกับคำถามที่ผมยกขึ้นมาในย่อหน้าก่อนหน้านี้
ตัวอย่างเช่น นี่คือผลการรันตัวอย่างหนึ่ง:
>clj -M -m markov
~/Documents/Books/Clean\ Code\ 2ded\ 20250311.txt "The Single"
The Single Responsibility Principle, and the Dependency
relationships and wind up back at that first solution, the one with
the project you were working on it every day business and personal
life
สังเกตช่องว่างสองครั้งในบรรทัดที่สามและสี่ไหมครับ ทำไมมันถึงทำแบบนั้น
ผมสามารถแก้ไขข้อผิดพลาดพวกนี้ได้โดยเพิ่มคำอธิบายให้ชัดเจนขึ้นใน prompt เช่น ผมอาจจะเพิ่มข้อความนี้เข้าไป:
Make sure all accumulated words are separated by just one space.
แต่วิธีนี้ก็มีปัญหาอยู่หลายอย่าง อย่างแรก ผมยังไม่รู้เลยว่าทำไมช่องว่างสองครั้งพวกนั้นถึงเกิดขึ้น การแก้ prompt แบบนี้จึงเป็นแค่การแปะแผ่นปะแก้อาการเท่านั้น ไม่ได้แก้ที่ต้นเหตุ อย่างที่สอง ผมมั่นใจได้แค่ไหนว่าวลี all accumulated words จะอ้างอิงถึงค่าที่ส่งกลับได้อย่างถูกต้อง
ผมสามารถลองทำเพื่อหาคำตอบได้ แต่นั่นก็ทำให้เกิดปัญหาอีกอย่างหนึ่ง ทุกครั้งที่มีการเปลี่ยนแปลง prompt ใหม่ Grok สร้างขึ้นใหม่ทั้งหมด (regenerates) จากศูนย์ แทนที่จะแก้ไขโปรแกรมเดิมที่มีอยู่ ด้วยความคลุมเครือทั้งหมดใน prompt แล้วผมจะมั่นใจได้อย่างไรว่า โปรแกรมที่สร้างขึ้นใหม่จะไม่ทำลายสิ่งที่เคยทำงานได้ในเวอร์ชันก่อนหน้า มันจะใช้นิยามของคำว่า word และ sentence แบบเดิมหรือเปล่า มันจะตีความ the found word เหมือนเดิมหรือเปล่า เมื่อพิจารณาจากธรรมชาติเชิงสถิติและความคลุมเครือของอัลกอริทึม LLM แล้ว มีโอกาสสูงมากที่มันจะไม่เหมือนเดิม!
นี่คือสิ่งที่ Grok สร้างขึ้นมาสำหรับ prompt ที่แก้ไขแล้วนั้น สังเกตดูว่าโปรแกรมทั้งสองตัวดูเหมือนถูกเขียนโดย โปรแกรมเมอร์คนละคนกันเลย
(ns sentence-walker
(:require [clojure.string :as str]
[clojure.java.io :as io]))
(defn get-sentences [filename]
"Read file and split into sentences"
(-> (slurp filename)
(str/replace #"[.!?]\s+" "$0|")
(str/split #"\|")
(->> (map str/trim)
(filter not-empty))))
(defn find-matching-sentences [sentences substring]
"Find all sentences containing the substring"
(filter #(str/includes? (str/lower-case %)
(str/lower-case substring))
sentences))
(defn get-next-word [sentence substring]
"Find word following substring in sentence, or . if at end"
(let [idx (str/index-of (str/lower-case sentence)
(str/lower-case substring))
substr-end (+ idx (count substring))
remainder (subs sentence substr-end)]
(if (empty? (str/trim remainder))
"."
(let [next-word (first (str/split (str/trim remainder) #"\s+"))]
(if (or (nil? next-word) (empty? next-word))
"."
next-word)))))
(defn update-substring [substring next-word]
"Add next word and trim if longer than 3 words"
(let [words (str/split substring #"\s+")
new-words (conj (vec words) next-word)]
(if (> (count new-words) 3)
(str/join " " (subvec new-words (- (count new-words) 3)))
(str/join " " new-words))))
(defn walk-sentences [filename initial-substring]
"Main function to walk through sentences"
(let [sentences (get-sentences filename)]
(loop [current-substr initial-substring
accumulated-words [initial-substring]
iterations 0]
(if (or (str/ends-with? current-substr ".")
(> iterations 50))
(str/join " " accumulated-words)
(let [matching-sentences (find-matching-sentences
sentences current-substr)]
(if (empty? matching-sentences)
(str/join " " accumulated-words)
(let [random-sentence (rand-nth matching-sentences)
next-word (get-next-word random-sentence current-substr)
new-substr (update-substring current-substr next-word)]
(recur new-substr
(conj accumulated-words next-word)
(inc iterations)))))))))
;; Example usage:
;; (walk-sentences "sample.txt" "the")
โปรแกรมใหม่ตัวนี้แก้ปัญหาช่องว่างสองครั้งได้ แต่กลับสร้างปัญหาใหม่ขึ้นที่ท้ายประโยค แล้วมันก็ยังจัดการเรื่อง ตัวพิมพ์ใหญ่พิมพ์เล็กได้ไม่ดีนักด้วย
clean code is hard Writing a system that handles one user at a time .
Infancy (ช่วงเริ่มต้น)
ปัญหาเหล่านี้เกี่ยวข้องกับความที่แนวทางนี้ยังอยู่ในช่วงเริ่มต้นเท่านั้น หากจะเขียนโปรแกรมระบบขนาดใหญ่ด้วย prompt ได้อย่างมีประสิทธิภาพจริงๆ เราจะต้องมีเครื่องมือ AI ที่รองรับการเปลี่ยนแปลงแบบค่อยเป็นค่อยไป และ เรายังต้องหาวิธีกำจัดความคลุมเครือใน prompt ของเราด้วย
พูดสั้นๆ คือเราต้องการรูปแบบที่เป็นทางการ (formalism) นั่นคือภาษา prompt ที่ไม่คลุมเครือ แม่นยำ และ ไม่สามารถถูกตีความผิดได้จากธรรมชาติเชิงสถิติและความคลุมเครือของอัลกอริทึม LLM เราต้องหาวิธีพูดในสิ่งที่เรา หมายถึงให้แม่นยำที่สุด
นอกจากนี้เรายังต้องหาวิธีที่จะหมายถึงในสิ่งที่เราพูดด้วย หรือพูดอีกอย่างคือ ต่อให้เรามีภาษาที่แม่นยำ สมบูรณ์แบบ มันก็ยังง่ายมากที่จะพูดอะไรบางอย่างที่แม่นยำแต่ผิดพลาดได้ เพื่อให้แน่ใจว่าข้อความที่แม่นยำของเรา สื่อถึงความตั้งใจได้อย่างถูกต้อง เราควรบอกความตั้งใจของเราด้วยวิธีที่แตกต่างกันอย่างน้อยสองวิธี (เหมือนที่ผมทำในย่อหน้านี้ไงครับ!)
เมื่อคุณพูดอะไรบางอย่างด้วยสองวิธีที่ต่างกัน นั่นคือคุณกำลัง overload แนวคิดที่คุณพยายามจะสื่อสาร การ overload แบบนี้เป็นวิธีที่ทำให้แน่ใจว่าความผิดพลาดในข้อความใดข้อความหนึ่งจะถูกตรวจพบได้จากอีกข้อความหนึ่ง พวกเรามนุษย์ที่มีโอกาสผิดพลาดได้ตลอดเวลาจำเป็นต้องมีการ overload แบบนี้อย่างยิ่ง
การ overload คือกลยุทธ์ที่อยู่เบื้องหลังการทำบัญชีแบบบันทึกคู่ (double-entry bookkeeping) และวินัยด้านการ ทดสอบที่กล่าวถึงในบทก่อนๆ การไว้ใจข้อความที่แม่นยำเพียงชุดเดียวที่เขียนโดยมนุษย์ โดยที่ข้อความนั้นไม่มีการ overload เลย ถือเป็นความไม่รับผิดชอบอย่างยิ่ง สิ่งที่เราต้องการคือวิธีสร้าง prompt ที่แม่นยำและมีการ overload พร้อมกับวิธีตรวจสอบความสอดคล้องของมันได้อย่างรวดเร็ว
A SWAG (การเดาแบบมีหลักวิทยาศาสตร์)
ฉะนั้นให้ผมลองเดาแบบ Scientific Wild-Axx Guess (การเดาแบบมีหลักวิทยาศาสตร์) ดูสักครั้ง
ลองจินตนาการถึงภาษา prompt ที่มีนิยามและข้อจำกัดที่เป็นทางการซึ่งถูก overload ด้วยชุดสถานการณ์แบบ BDD 3 Given - When - Then
3 . [ RSpec ].
ลองเอามาใช้กับปัญหาที่ง่ายกว่าเดิมนิดหน่อยดูครับ สังเกตดูว่ามันดูคล้าย COBOL แค่ไหน ความคล้ายคลึงนี้อาจทำให้เราต้องหยุดคิดสักนิด
Language: Clojure
Name: Markov
Command line arguments:
f a text file.
s a text string.
Definitions:
Word: A string of alphabetic characters.
Sentence: One or more words separated by spaces and ending with
a period, question mark, or exclamation mark.
Constraints:
Ignore case in all comparisons.
Description:
Find all sentences in f that contain s.
Choose a sentence c at random with equal probability.
Return the word in c that immediately follows s;
if none return a period.
Scenarios to be tested with speclj:
Given f contains the following lines:
Oh, Hello there Bob, how are you today? I'm fine.
Hello there Bill, I trust you are well. How's the wife?
Ah yes, hello there John, great day! Kowabunga!
When s is "not in any sentence"
Return "."
When s is "hello"
Then return "there"
When s is "are well"
Then return "."
When s is "hello there"
Then return "Bob", "Bill", or "John".
Assert a uniform distribution after 100 iterations.
Grok ยินดีสร้างโค้ดต่อไปนี้ขึ้นมาให้อย่างไม่มีปัญหา
(ns markov.core
(:require [clojure.string :as str]
[clojure.java.io :as io]))
(defn get-sentences [file]
(->> (slurp file)
(re-seq #"[A-Za-z\s]+[.!?]")
(map str/trim)))
(defn words [sentence]
(str/split (str/lower-case sentence) #"\s+"))
(defn find-following-word [sentences search-str]
(let [search-str (str/lower-case search-str)
matching-sentences (filter
#(str/includes? (str/lower-case %)
search-str)
sentences)]
(if (empty? matching-sentences)
"."
(let [chosen (rand-nth matching-sentences)
word-seq (words chosen)
search-words (str/split search-str #"\s+")
idx (.indexOf word-seq (last search-words))]
(if (or (< idx -1) (>= (+ idx 1) (count word-seq)))
"."
(nth word-seq (+ idx 1)))))))
(defn -main [f s]
(let [sentences (get-sentences f)
result (find-following-word sentences s)]
(println result)))
และนี่คือชุดทดสอบที่ Grok สร้างขึ้นจากสถานการณ์เหล่านั้น
;; speclj tests
(ns markov.core-spec
(:require [speclj.core :refer :all]
[markov.core :refer :all]
[clojure.java.io :as io]))
(def test-content
"Oh, Hello there Bob, how are you today? I'm fine.
Hello there Bill, I trust you are well. How's the wife?
Ah yes, hello there John, great day! Kowabunga!")
(describe "Markov"
(before-all
(spit "test.txt" test-content))
(after-all
(.delete (io/file "test.txt")))
(it "returns period when string not found"
(should= "." (find-following-word (get-sentences "test.txt")
"not in any sentence")))
(it "returns 'there' when searching for 'hello'"
(should= "there" (find-following-word (get-sentences "test.txt")
"hello")))
(it "returns period when searching for 'are well'"
(should= "." (find-following-word (get-sentences "test.txt")
"are well")))
(it "returns Bob, Bill, or John for 'hello there'
with uniform distribution"
(let [results (repeatedly 100
#(find-following-word
(get-sentences "test.txt") "hello there"))
freqs (frequencies results)
expected ["bob" "bill" "john"]]
(should= 3 (count freqs))
(should (every? #(contains? freqs %) expected))
(doseq [name expected]
(let [count (get freqs name)]
;; Check if count is reasonably close to 33 (100/3)
;; with some tolerance
(should (> count 20))
(should (< count 50)))))))
แม้ Grok จะพยายามอย่างเต็มที่ แต่ชุดทดสอบเหล่านี้ก็ไม่ผ่าน สิ่งนี้ควรตอกย้ำให้เห็นว่าการ overload prompt ด้วยสถานการณ์ทดสอบนั้นสำคัญแค่ไหน!
Failures:
1) Markov returns 'there' when searching for 'hello'
Expected: "there"
got: "." (using =)
2) Markov returns period when searching for 'are well'
Expected: "."
got: "i" (using =)
3) Markov returns Bob, Bill, or John for
'hello there' with uniform distribution
Expected: 3
got: 1 (using =)
การ debug โค้ดนั้นค่อนข้างง่าย แต่คุณจะ debug prompt ได้อย่างไร และแม้ว่าคุณจะ “แก้ไข” prompt ได้ คุณมั่นใจได้แค่ไหนว่ามันจะสร้างโค้ดและชุดทดสอบที่ผ่านได้ทุกครั้งที่คุณใช้ prompt นั้นซ้ำ
การที่ Grok เขียนชุดทดสอบขึ้นมาเองแต่กลับไม่สามารถใช้มันอนุมาน (infer) พฤติกรรมที่ต้องการได้นั้น เผยให้เห็นข้อจำกัดของแนวทาง LLM อย่างชัดเจน LLM มีธรรมชาติเชิงสถิติ มันสร้างคำตอบขึ้นมาจากความน่าจะเป็น ไม่ใช่จากการอนุมาน จริงอยู่ที่การอนุมานจำนวนมากถูกฝึกฝนเข้าไปอยู่ในโมเดลเชิงสถิตินั้นแล้ว แต่การประมวลผล prompt ไม่ได้ทำการอนุมานแบบเรียลไทม์ การอนุมานคือการทำงานหลักของเหตุผล ถ้า LLM ไม่ได้อนุมานอย่างจริงจัง นั่นก็แปลว่า LLM ไม่ได้ใช้เหตุผลเลย!
ถ้าคุณลอง debug โค้ดที่ Grok สร้างขึ้นมา คุณจะพบว่า Grok ดูเหมือนจะมองข้ามนิยามของคำว่า “word” ที่ให้ไว้ และตีความข้อจำกัดเรื่องการไม่สนใจตัวพิมพ์ใหญ่พิมพ์เล็กผิดไป การที่ Grok สับสนกับ prompt ที่เป็นทางการและ overload ด้วยสถานการณ์ทดสอบแบบนี้ไม่ใช่เรื่องน่าดีใจเลย อย่างน้อยสำหรับผมแล้ว มันดูชัดเจนว่าถ้าปราศจาก ความเป็นทางการที่ overload ไว้ โอกาสที่จะใช้ AI มาเขียนระบบของเราก็มีแต่จะล้มเหลวอย่างยับเยิน เพราะสุดท้าย แล้วการเขียนโปรแกรมคือสิ่งที่เป็นทางการ และมนุษย์ก็จำเป็นต้องมีการ overload