0Pricing
Prompt Engineering & LLM Optimization for Developers · บทเรียน

การแยกวิเคราะห์และตรวจสอบผลลัพธ์

นำกลไกการแยกวิเคราะห์และตรวจสอบที่รัดกุมไปใช้งาน เพื่อให้แน่ใจว่าผลลัพธ์จาก LLM อยู่ในรูปแบบที่ต้องการและผ่านมาตรฐานคุณภาพที่กำหนด

การแยกวิเคราะห์และตรวจสอบผลลัพธ์ เป็นบทเรียน Prompt Engineering & LLM Optimization for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Prompt Engineering & LLM Optimization for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Parse LLM Output?

Large Language Models (LLMs) are powerful, but their raw text outputs can be unpredictable. For applications, we often need structured, reliable data.

Output parsing is the process of converting an LLM's free-form text response into a structured format your application can easily use, like JSON or a specific data type.

The Need for Validation

Even after parsing, the extracted data might not be valid. An LLM might hallucinate a number, provide an incorrect type, or miss a required field.

Output validation ensures the parsed data adheres to predefined rules, data types, ranges, or custom business logic, preventing errors downstream in your application.

Challenges with Raw LLM Output

LLMs can sometimes include conversational filler, extra explanations, or slightly deviate from the requested format. Consider an LLM asked to return a user's ID and name:

  • "Here is the user: ID:123, Name:Alice."
  • "User info -> {id: 456, name: Bob}"
  • "ID is 789, Name is Charlie. Hope this helps!"

Each needs a different approach to extract the data.

Basic String Manipulation

For very simple and highly constrained outputs, basic string methods can work. This is suitable when you have strong control over the prompt and expect minimal deviation.

Common methods include trim(), substring(), indexOf(), and split() to isolate and extract parts of the string.

String Manipulation Example

Here's how to extract data from a simple "ID:123,Name:Alice" string using basic Java string methods:

public class Main {
  public static void main(String[] args) {
    String llmOutput = "ID:123,Name:Alice";
    
    String[] parts = llmOutput.split(",");
    String idStr = parts[0].replace("ID:", "").trim();
    String nameStr = parts[1].replace("Name:", "").trim();
    
    System.out.println("ID: " + idStr);
    System.out.println("Name: " + nameStr);
  }
}

Regular Expressions (Regex)

When output patterns are more complex, or you need to match specific formats with variations, Regular Expressions (Regex) are incredibly powerful. They define search patterns for strings.

Regex can extract data even if there's extra text, inconsistent spacing, or different ordering of elements.

Regex Parsing Example

Let's use regex to extract a number from a string that might have various prefixes or suffixes. This Java example uses java.util.regex.Pattern and Matcher.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    String llmOutput = "The magic number is 42! Please use it.";
    Pattern pattern = Pattern.compile("\\d+"); // Matches one or more digits
    Matcher matcher = pattern.matcher(llmOutput);
    
    if (matcher.find()) {
      System.out.println("Found number: " + matcher.group());
    } else {
      System.out.println("No number found.");
    }
  }
}

Parsing JSON Outputs

For structured data, JSON (JavaScript Object Notation) is the preferred format. LLMs can be prompted to output JSON directly. You'll need a JSON parsing library to convert the string into an object.

This allows you to access fields by name (e.g., data.get("id")) instead of relying on string positions.

JSON Parsing in Java

Using a library like org.json (or Jackson/Gson for more complex cases) simplifies parsing JSON. Here's how to parse a simple JSON string:

import org.json.JSONObject;

public class Main {
  public static void main(String[] args) {
    String jsonString = "{"id":123, "name":"Alice"}";
    try {
      JSONObject json = new JSONObject(jsonString);
      int id = json.getInt("id");
      String name = json.getString("name");
      
      System.out.println("User ID: " + id);
      System.out.println("User Name: " + name);
    } catch (Exception e) {
      System.err.println("Error parsing JSON: " + e.getMessage());
    }
  }
}

Implementing Data Validation

After parsing, validate the data. This involves checking data types, ranges, and business rules. For JSON, you might check if required fields exist, if numbers are within expected bounds, or if strings match certain patterns.

Example checks: age > 0, email.contains("@"), list.size() > 0.

Quick Check: Output Handling

When working with LLM outputs, what are effective strategies to ensure the data is usable and correct in your application?

Recap & Next Steps

In this lesson, you learned that robust LLM integration requires more than just prompting. You need to implement solid output parsing to extract data from raw text and output validation to ensure that data meets your application's requirements.

  • Basic string methods for simple cases.
  • Regular Expressions for pattern matching.
  • JSON parsing libraries for structured data.
  • Validation logic to check data types, ranges, and rules.

Mastering these techniques will significantly improve the reliability and stability of your LLM-powered applications. Next, explore advanced techniques like Retrieval Augmented Generation (RAG) to ground LLM responses in external knowledge!

คำถามที่พบบ่อย

บทเรียน “การแยกวิเคราะห์และตรวจสอบผลลัพธ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแยกวิเคราะห์และตรวจสอบผลลัพธ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแยกวิเคราะห์และตรวจสอบผลลัพธ์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Prompt Engineering & LLM Optimization for Developers นี้ได้ไหม

ได้ บทเรียน Prompt Engineering & LLM Optimization for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ประสิทธิภาพการใช้โทเคนและการจัดการบริบท
  2. เทคนิคลดความหน่วง
  3. การแยกวิเคราะห์และตรวจสอบผลลัพธ์
  4. การแคชและการรวมคำขอเพื่อลดต้นทุน LLM
← กลับไปที่ Prompt Engineering & LLM Optimization for Developers