การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง
บังคับให้ LLM ส่งคืนข้อมูลแบบมีโครงสร้างที่เชื่อถือได้ด้วยตัวแยกวิเคราะห์ผลลัพธ์และแบบแผนข้อมูล พร้อมตรวจสอบหรือลองใหม่เมื่อโมเดลสร้างผลลัพธ์ที่รูปแบบไม่ถูกต้อง
การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents with LangChain & Autonomous Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Problem with Free Text
LLMs return prose by default, but your code needs structured data: JSON, a list, a typed object. Parsing free text with regex is fragile.
This lesson covers getting reliable structured output from models.
Asking for a Format
The first step is simply instructing the model to produce a specific format. But instruction alone is not enough; models drift, add prose, or wrap output in markdown.
prompt = 'Extract name and age as JSON: "Lena is 30"'
# model might reply: 'Sure! {"name":"Lena","age":30}'Output Parsers
LangChain output parsers do two jobs: they generate format instructions to inject into the prompt, and they parse the model's response back into a structured object.
from langchain.output_parsers import CommaSeparatedListOutputParser
parser = CommaSeparatedListOutputParser()
print(parser.get_format_instructions())Schema-Based Parsing
Define the shape you want with a schema (e.g. a Pydantic model). The parser turns it into instructions and validates the result against the fields and types.
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: intInjecting Format Instructions
Add the parser's instructions into your prompt template so the model knows exactly what structure to emit.
template = 'Extract info.\n{format_instructions}\nText: {text}'
prompt = template.format(
format_instructions=parser.get_format_instructions(),
text='Lena is 30')Parsing the Response
After the model replies, the parser converts the text into your typed object, raising an error if it does not match the schema.
result = parser.parse(model_output)
print(result.name, result.age)Handling Malformed Output
Models occasionally produce invalid JSON. A retry/fixing parser detects the failure and asks the model to correct its own output, turning a hard crash into a recoverable step.
from langchain.output_parsers import RetryOutputParser
robust = RetryOutputParser.from_llm(parser=parser, llm=llm)Native JSON / Tool Modes
Many modern models support a JSON mode or function/tool calling that constrains output to valid structured data at the API level. When available, this is far more reliable than prompt instructions alone.
Validation Beyond Types
A value can be the right type but still wrong: a negative age, an empty required field. Add validators so business rules are enforced, not just the data shape.
if result.age < 0 or result.age > 130:
raise ValueError('age out of range')Why It Matters for Agents
Agents chain steps together, feeding one output into the next. If a step emits malformed data, the whole chain breaks. Structured, validated output is what makes multi-step agents dependable.
A Reliable Output Workflow
Putting it together:
- Define a schema for the data you need
- Inject format instructions into the prompt
- Prefer native JSON/tool mode when available
- Parse and validate, with a retry parser as a safety net
Quick Check
Test your understanding of structured output.
Recap
You learned to get reliable structured data from LLMs.
- Output parsers generate instructions and parse responses
- Schemas validate shape and types
- Retry parsers recover from malformed output
- Native JSON/tool modes are most reliable when available
เรียนรู้ AI Agents with LangChain & Autonomous Workflows ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 50
คำถามที่พบบ่อย
บทเรียน “การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง”
บังคับให้ LLM ส่งคืนข้อมูลแบบมีโครงสร้างที่เชื่อถือได้ด้วยตัวแยกวิเคราะห์ผลลัพธ์และแบบแผนข้อมูล พร้อมตรวจสอบหรือลองใหม่เมื่อโมเดลสร้างผลลัพธ์ที่รูปแบบไม่ถูกต้อง คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม
ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เทคนิคการออกแบบพรอมต์อย่างมีประสิทธิภาพ
- การผสานรวม LLM กับ LangChain
- การจัดการพารามิเตอร์และค่าใช้จ่ายของโมเดล
- การแยกวิเคราะห์และตรวจสอบผลลัพธ์แบบมีโครงสร้าง