การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง
แปลงข้อความดิบจาก LLM ให้เป็นข้อมูลมีโครงสร้างที่เชื่อถือได้ด้วยตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเองและแบบในตัวของ LangChain
การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Parse Output?
LLMs return free-form text, but applications need structured data: objects, lists, enums. An output parser converts the raw string into something your code can use safely.
The Parser Interface
A LangChain output parser implements two key methods.
parse(text)turns the string into your typeget_format_instructions()tells the model how to format its reply
A Minimal Custom Parser
Subclass BaseOutputParser and implement parse. This one splits a comma-separated reply into a list.
from langchain_core.output_parsers import BaseOutputParser
class CommaListParser(BaseOutputParser):
def parse(self, text):
return [t.strip() for t in text.split(",")]
print(CommaListParser().parse("a, b, c")) # ["a", "b", "c"]Format Instructions
Override get_format_instructions so the prompt nudges the model toward a parseable shape. The instructions are injected into your prompt template.
def get_format_instructions(self):
return "Reply with items separated by commas, no numbering."Pydantic Output Parser
For rich objects, LangChain ships a PydanticOutputParser. You define a schema and it generates instructions plus validation automatically.
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
# parser = PydanticOutputParser(pydantic_object=Person)Wiring a Parser into a Chain
With LCEL you pipe the model output straight into the parser using the | operator.
chain = prompt | llm | CommaListParser()
result = chain.invoke({"topic": "fruits"})Handling Malformed Output
Models sometimes ignore the format. A robust parser validates and raises a clear error, or attempts a best-effort recovery, instead of crashing downstream.
def parse(self, text):
try:
return json.loads(text)
except json.JSONDecodeError:
raise ValueError("Model did not return valid JSON")The OutputFixingParser
Wrap any parser in an OutputFixingParser. When parsing fails, it sends the bad output and the error back to an LLM to repair it.
Streaming and Parsers
Some parsers support incremental parsing of streamed tokens. For structured types this is hard, so streaming parsers often emit partial objects as fields complete.
Validation Beyond Types
You can add business rules inside parse: ranges, allowed values, required combinations. Reject anything that would corrupt your application state.
def parse(self, text):
n = int(text.strip())
if not 1 <= n <= 5:
raise ValueError("Rating must be 1-5")
return nPutting It Together
Define the parser, expose format instructions, inject them into the prompt, and pipe everything in a chain. The result is reliable structured output.
prompt = template.partial(
format=parser.get_format_instructions()
)
chain = prompt | llm | parser
chain.invoke({"input": "..."})Quick Check
Test your understanding of output parsers.
Recap
You built custom output parsing:
- Implement
parseandget_format_instructions - Use
PydanticOutputParserfor rich schemas - Pipe parsers into chains with
| - Wrap with
OutputFixingParserfor resilience
เรียนรู้ LangChain / RAG / Vector DBs ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง”
แปลงข้อความดิบจาก LLM ให้เป็นข้อมูลมีโครงสร้างที่เชื่อถือได้ด้วยตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเองและแบบในตัวของ LangChain คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม
ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การพัฒนาตัวโหลดเอกสารแบบกำหนดเอง
- การผสานรวมโมเดลการฝังแบบกำหนดเอง
- การขยายสายโซ่การค้นคืนด้วยตรรกะแบบกำหนดเอง
- การสร้างตัวแยกวิเคราะห์ผลลัพธ์แบบกำหนดเอง