การสตรีมคำตอบ LLM ไปยังผู้ใช้
ส่งโทเค็นให้ผู้ใช้แบบเรียลไทม์ เรียนรู้ว่าการสตรีมทำงานอย่างไร เหตุใดจึงช่วยลดความหน่วงที่ผู้ใช้รับรู้ และวิธีรับผลลัพธ์ที่สตรีมในโค้ด
การสตรีมคำตอบ LLM ไปยังผู้ใช้ เป็นบทเรียน Prompt Engineering & LLM Optimization for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Prompt Engineering & LLM Optimization for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Stream?
By default an LLM call returns the entire response only after generation finishes. For long answers this feels slow.
Streaming sends tokens as they are produced, so the user sees text appear word-by-word — drastically improving perceived responsiveness.
Time to First Token
Two latency numbers matter:
- TTFT (time to first token): how long until the first word appears
- Total time: until the full answer is ready
Streaming barely changes total time but makes TTFT the number your users actually feel.
Server-Sent Events
Most LLM APIs stream over Server-Sent Events (SSE): a long-lived HTTP response where each chunk is a small JSON event prefixed with data:.
The stream ends with a special [DONE] marker.
Enabling Streaming
You opt in by setting a flag on the request. The API then returns an iterable stream instead of a single object.
const stream = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: messages,
stream: true
});Reading Chunks
Each chunk carries a delta — the new piece of text. You concatenate deltas to rebuild the full message.
let full = "";
for await (const chunk of stream) {
const piece = chunk.choices[0].delta.content || "";
full += piece;
process.stdout.write(piece);
}Updating the UI
On the frontend you append each delta to the visible message. A simple approach: keep state and re-render on every token.
function onToken(token, setText) {
setText(prev => prev + token);
}Handling the Done Signal
When the stream closes, finalize: stop the typing indicator, persist the full message, and re-enable the input box.
stream.on("end", () => {
saveMessage(full);
hideTypingIndicator();
});Errors Mid-Stream
A stream can fail halfway. Always wrap consumption in try/catch and show whatever partial text you already received rather than discarding it.
try {
for await (const c of stream) { /* ... */ }
} catch (e) {
showPartial(full);
reportError(e);
}Cancellation
Users may want to stop a long answer. Pass an AbortController signal so you can cancel the request and free server resources.
const controller = new AbortController();
client.chat.completions.create({ ...opts, signal: controller.signal });
// later
controller.abort();Streaming with Tools
When the model is calling a function, tool-call arguments also arrive as deltas. Buffer them until the call is complete before executing the tool.
Cost & Token Counting
Streaming does not change cost — you still pay per token. To count usage, sum the tokens you received, or request a final usage event if the API supports it.
Quick Check
Test your streaming knowledge.
Recap
You learned to stream LLM responses: opt in with a stream flag, read incremental delta chunks over SSE, update the UI per token, handle the done signal, manage errors and cancellation, and remember streaming improves perceived latency without changing cost.
คำถามที่พบบ่อย
บทเรียน “การสตรีมคำตอบ LLM ไปยังผู้ใช้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสตรีมคำตอบ LLM ไปยังผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Prompt Engineering & LLM Optimization for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสตรีมคำตอบ LLM ไปยังผู้ใช้”
ส่งโทเค็นให้ผู้ใช้แบบเรียลไทม์ เรียนรู้ว่าการสตรีมทำงานอย่างไร เหตุใดจึงช่วยลดความหน่วงที่ผู้ใช้รับรู้ และวิธีรับผลลัพธ์ที่สตรีมในโค้ด คุณปฏิบัติ Prompt Engineering & LLM Optimization for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Prompt Engineering & LLM Optimization for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Prompt Engineering & LLM Optimization for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การสตรีมคำตอบ LLM ไปยังผู้ใช้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Prompt Engineering & LLM Optimization for Developers นี้ได้ไหม
ได้ บทเรียน Prompt Engineering & LLM Optimization for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างเสริมด้วยการค้นคืน (RAG)
- การเรียกใช้ฟังก์ชันและการใช้เครื่องมือ
- การสร้างเอเจนต์ LLM อย่างง่าย
- การสตรีมคำตอบ LLM ไปยังผู้ใช้