LangChain / RAG / Vector DBs · درس

إنشاء محللات مخرجات مخصّصة

حوّل نص LLM الخام إلى بيانات منظّمة موثوقة باستخدام محللات مخرجات LangChain المخصّصة والمدمجة.

الدرس 4 من 413 خطوة

إنشاء محللات مخرجات مخصّصة درس مجاني في LangChain / RAG / Vector DBs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 type
  • get_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 n

Putting 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 parse and get_format_instructions
  • Use PydanticOutputParser for rich schemas
  • Pipe parsers into chains with |
  • Wrap with OutputFixingParser for resilience
البدء مجانًا

تعلم LangChain / RAG / Vector DBs مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «إنشاء محللات مخرجات مخصّصة» مجاني؟

نعم — نص درس «إنشاء محللات مخرجات مخصّصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة LangChain / RAG / Vector DBs، انتقل إلى CoddyKit PRO. تتضمن دورة LangChain / RAG / Vector DBs 4 دروس في المجموع.

ماذا ستتعلم في «إنشاء محللات مخرجات مخصّصة»؟

حوّل نص LLM الخام إلى بيانات منظّمة موثوقة باستخدام محللات مخرجات LangChain المخصّصة والمدمجة. تتمرن على LangChain / RAG / Vector DBs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ LangChain / RAG / Vector DBs؟

لا تُشترط خبرة سابقة. LangChain / RAG / Vector DBs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «إنشاء محللات مخرجات مخصّصة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس LangChain / RAG / Vector DBs هذا؟

نعم. كل درس في LangChain / RAG / Vector DBs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تطوير محمّلات مستندات مخصّصة
  2. دمج نماذج التضمين المخصّصة
  3. توسيع سلاسل الاسترجاع بمنطق مخصّص
  4. إنشاء محللات مخرجات مخصّصة
← العودة إلى LangChain / RAG / Vector DBs