วิศวกรรมพรอมต์และหน้าต่างบริบท
ทำความเข้าใจหน้าต่างบริบทที่กำหนดขอบเขตของการเรียกใช้ LLM ทุกครั้ง และเรียนรู้การเขียนพรอมต์ให้บริบทที่ดึงมา คำสั่ง และคำถามอยู่ร่วมกันได้ เพื่อให้คำตอบในระบบจริงเชื่อถือได้
วิศวกรรมพรอมต์และหน้าต่างบริบท เป็นบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LLM Apps in Production (RAG + Vector DB + Caching) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Context Window
Every LLM has a fixed context window — the max tokens it reads and writes per call. System prompt, retrieved docs, history, and your question all have to fit.
What Lives in the Window
A production RAG prompt packs in system instructions, retrieved context, prior history, and the current question. Exceed the window and something gets cut.
Anatomy of a Prompt
A clear prompt structure helps the model tell instructions apart from data. Here's a clean layout separating context from the question.
prompt = (
'You are a support assistant. '
'Answer ONLY from the context.\n\n'
'Context:\n{context}\n\n'
'Question: {question}'
)Grounding Instructions
To cut hallucination, add grounding instructions: tell the model to answer only from the provided context and to admit when it doesn't know.
rule = 'If the answer is not in the context, say you do not know.'Using a Prompt Template
A prompt template makes prompts reusable and safe to fill with variables. LangChain's ChatPromptTemplate does exactly this.
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
('system', 'Answer only from context: {context}'),
('human', '{question}')
])Counting Tokens
Before sending, count tokens so you don't overflow the window. Rough rule for English: about 4 characters per token.
import tiktoken
enc = tiktoken.get_encoding('cl100k_base')
print(len(enc.encode(filled_prompt)))When Context Is Too Big
When context is too big, shrink it: fewer chunks, smaller chunk sizes, or summarize. Never silently truncate the middle — you might drop the answer.
The Lost-in-the-Middle Effect
Watch the lost-in-the-middle effect: models attend best to the start and end of context, worst to the middle. Put your most relevant chunks first or last.
Reserving Output Space
Input and output share the window. Fill it all with input and there's no room to generate — so reserve output space for the expected answer length.
max_output = 800
budget_for_input = WINDOW - max_outputFew-Shot Examples
A few few-shot examples can steer format and tone, but they eat tokens. Weigh their value against the space they consume.
Iterating on Prompts
Prompt engineering is empirical: change one thing at a time, test on real questions, and measure. Small wording tweaks can shift answer quality a lot.
Quick Check
Test your understanding of context windows.
Recap
Recap: the context window holds system, context, history, question, and output. Structure prompts, ground them, count tokens, beat lost-in-the-middle, and reserve output room.
คำถามที่พบบ่อย
บทเรียน “วิศวกรรมพรอมต์และหน้าต่างบริบท” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “วิศวกรรมพรอมต์และหน้าต่างบริบท” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LLM Apps in Production (RAG + Vector DB + Caching) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “วิศวกรรมพรอมต์และหน้าต่างบริบท”
ทำความเข้าใจหน้าต่างบริบทที่กำหนดขอบเขตของการเรียกใช้ LLM ทุกครั้ง และเรียนรู้การเขียนพรอมต์ให้บริบทที่ดึงมา คำสั่ง และคำถามอยู่ร่วมกันได้ เพื่อให้คำตอบในระบบจริงเชื่อถือได้ คุณปฏิบัติ LLM Apps in Production (RAG + Vector DB + Caching) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LLM Apps in Production (RAG + Vector DB + Caching) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LLM Apps in Production (RAG + Vector DB + Caching) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “วิศวกรรมพรอมต์และหน้าต่างบริบท” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) นี้ได้ไหม
ได้ บทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจแอป LLM ในสภาพแวดล้อมจริง
- พื้นฐานของการสร้างแบบเสริมด้วยการค้นคืน
- ภาพรวมสถาปัตยกรรมระบบ RAG เบื้องต้น
- วิศวกรรมพรอมต์และหน้าต่างบริบท