AI Agents · บทเรียน

รูปแบบการให้เหตุผลข้ามสื่อ

ยึดโยงข้อกล่าวอ้างในข้อความกับภาพและสังเคราะห์บริบทหลายสื่อจากหลายแหล่ง

บทเรียน 4 จาก 413 ขั้นตอน

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

การให้เหตุผลข้ามรูปแบบ

การให้เหตุผลข้ามรูปแบบเกิดขึ้นเมื่อเอเจนต์ต้องประสานข้อมูลจาก รูปแบบข้อมูลสองรูปแบบขึ้นไป ได้แก่ ข้อความ รูปภาพ แผนภูมิ และตาราง ซึ่งอาจสอดคล้อง ขัดแย้ง หรือเสริมซึ่งกันและกัน ตัวอย่างเช่น ข้อความในรายงานระบุว่ารายได้เติบโต 20% แต่แผนภูมิที่แนบมากลับแสดงเส้นที่ทรงตัว

เอเจนต์ต้องตัดสินใจว่าแหล่งข้อมูลใดถูกต้อง หรือแจ้งความไม่สอดคล้องให้มนุษย์ตรวจสอบ

การเชื่อมโยงข้อความกับภาพ

การเชื่อมโยงข้อความกับภาพ หมายถึงการตรวจสอบว่าข้อกล่าวอ้างในข้อความสามารถยืนยันได้ด้วยภาพประกอบหรือไม่ ตัวอย่างเช่น คำอธิบายผลิตภัณฑ์ตรงกับภาพถ่ายผลิตภัณฑ์หรือไม่ หรือเอกสารกล่าวถึงตารางที่ปรากฏอยู่ในภาพจริงหรือไม่

import anthropic
import base64

def ground_text_in_image(
    text_claim: str,
    image_path: str
) -> dict:
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    with open(image_path, 'rb') as f:
        b64 = base64.standard_b64encode(f.read()).decode('utf-8')

    prompt = (
        f'Text claim: "{text_claim}"\n\n'
        'Does the image above support, contradict, or partially support this claim?\n'
        'Return JSON: {"verdict": "support|contradict|partial|insufficient_evidence", '
        '"confidence": 0.0, "evidence": "..."}'
    )
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=256,
        messages=[{'role': 'user', 'content': [
            {'type': 'image', 'source': {'type': 'base64',
              'media_type': 'image/jpeg', 'data': b64}},
            {'type': 'text', 'text': prompt}
        ]}]
    )
    import json
    return json.loads(response.content[0].text)

การตรวจจับความขัดแย้งระหว่างแผนภูมิกับข้อความ

สถานการณ์ที่พบได้บ่อยในโลกจริงคือ เนื้อความของรายงานทางการเงินกล่าวอย่างหนึ่ง แต่แผนภูมิที่ฝังอยู่ในเอกสารกลับบอกเรื่องที่แตกต่างกัน เอเจนต์สามารถแยกข้อมูลแผนภูมิจากภาพและเปรียบเทียบกับข้อกล่าวอ้างเชิงตัวเลขในข้อความได้

def compare_chart_to_text(
    chart_image_path: str,
    text_with_claims: str
) -> dict:
    import anthropic, base64, json
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    with open(chart_image_path, 'rb') as f:
        b64 = base64.standard_b64encode(f.read()).decode('utf-8')

    prompt = (
        'The following text makes claims about data.\n'
        f'TEXT: {text_with_claims}\n\n'
        'Compare the chart image to the text claims. '
        'List any contradictions and agreements. '
        'Return JSON: {"agreements": [str], "contradictions": [str], '
        '"verdict": "consistent|inconsistent|partial"}'
    )
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=512,
        messages=[{'role': 'user', 'content': [
            {'type': 'image', 'source': {'type': 'base64',
              'media_type': 'image/jpeg', 'data': b64}},
            {'type': 'text', 'text': prompt}
        ]}]
    )
    return json.loads(response.content[0].text)

กลยุทธ์การรวมสัญญาณ

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

SIGNAL_SOURCES = {
    'chart': 0.9,     # high trust for quantitative data
    'table': 0.85,
    'photo': 0.8,
    'prose': 0.6,     # lower trust — may be imprecise or outdated
    'caption': 0.7
}

def combine_signals(signals: list) -> dict:
    """
    signals: list of {'source': str, 'claim': str, 'supports': bool}
    Returns weighted verdict.
    """
    support_weight = 0.0
    total_weight = 0.0
    for sig in signals:
        w = SIGNAL_SOURCES.get(sig['source'], 0.5)
        total_weight += w
        if sig['supports']:
            support_weight += w

    confidence = support_weight / total_weight if total_weight > 0 else 0.0
    return {
        'confidence': round(confidence, 3),
        'verdict': 'supported' if confidence >= 0.6 else 'contested',
        'needs_review': 0.4 <= confidence < 0.6
    }

if __name__ == '__main__':
    signals = [
        {'source': 'chart', 'claim': 'Revenue grew 20%', 'supports': True},
        {'source': 'prose', 'claim': 'Revenue grew 20%', 'supports': False},
    ]
    print(combine_signals(signals))

ไปป์ไลน์ตรวจสอบข้อความกับภาพ

ไปป์ไลน์ตรวจสอบผลิตภัณฑ์ทำงานดังนี้: เมื่อได้รับคำอธิบายผลิตภัณฑ์และภาพถ่ายผลิตภัณฑ์ ให้ตรวจสอบว่าคุณลักษณะสำคัญทุกข้อที่กล่าวถึงในข้อความปรากฏให้เห็นในภาพหรือไม่ จากนั้นส่งคืนรายงานแบบมีโครงสร้างที่ระบุรายการตรงกันและไม่ตรงกัน

def verify_product_description(
    description: str,
    product_image_path: str
) -> dict:
    import anthropic, base64, json
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    with open(product_image_path, 'rb') as f:
        b64 = base64.standard_b64encode(f.read()).decode('utf-8')

    prompt = (
        f'Product description:\n{description}\n\n'
        'For each attribute mentioned in the description (color, shape, material, '
        'size, features), check whether it is visible in the product photo.\n'
        'Return JSON: {"verified": [{"attribute": str, "status": str}], '
        '"unverified": [str], "overall_match": float}\n'
        'status: confirmed / contradicted / not_visible\n'
        'overall_match: 0.0-1.0'
    )
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=512,
        messages=[{'role': 'user', 'content': [
            {'type': 'image', 'source': {'type': 'base64',
              'media_type': 'image/jpeg', 'data': b64}},
            {'type': 'text', 'text': prompt}
        ]}]
    )
    return json.loads(response.content[0].text)

การอ้างอิงไขว้ระหว่างเอกสารกับภาพ

เมื่อวิเคราะห์เอกสารที่สแกน จะมีทั้งข้อความ OCR และภาพต้นฉบับ ให้ตรวจสอบอ้างอิงไขว้ระหว่างสองส่วนนี้ว่า ข้อความที่แยกออกมาตรงกับสิ่งที่ปรากฏในภาพหรือไม่ ความไม่ตรงกันอาจบ่งชี้ถึงข้อผิดพลาดของ OCR ที่ต้องแก้ไข

def crossref_ocr_with_image(
    ocr_text: str,
    document_image_path: str
) -> dict:
    import anthropic, base64, json
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    with open(document_image_path, 'rb') as f:
        b64 = base64.standard_b64encode(f.read()).decode('utf-8')

    prompt = (
        f'The OCR system produced this text from the document image:\n\n'
        f'{ocr_text}\n\n'
        'Compare the OCR text to what you actually see in the image. '
        'Identify any OCR errors, missed text, or hallucinated characters.\n'
        'Return JSON: {"ocr_errors": [{"incorrect": str, "correct": str}], '
        '"missed_sections": [str], "accuracy_estimate": float}'
    )
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=512,
        messages=[{'role': 'user', 'content': [
            {'type': 'image', 'source': {'type': 'base64',
              'media_type': 'image/jpeg', 'data': b64}},
            {'type': 'text', 'text': prompt}
        ]}]
    )
    return json.loads(response.content[0].text)

เอเจนต์ให้เหตุผลจากหลายแหล่ง

เอเจนต์ให้เหตุผลจากหลายแหล่งจะรวบรวมหลักฐานจากแต่ละรูปแบบอย่างเป็นระบบ รวมสัญญาณต่าง ๆ และสร้างข้อสรุปสุดท้ายพร้อมคะแนนความเชื่อมั่น เอเจนต์จะระบุว่าแหล่งข้อมูลใดสนับสนุนข้อสรุป และแหล่งใดขัดแย้งกับข้อสรุป

def multi_source_reasoning(
    claim: str,
    evidence_sources: list  # list of {'type': 'text'|'image', 'content': str|path}
) -> dict:
    import anthropic, base64, json
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    content_blocks = [
        {'type': 'text',
         'text': f'Evaluate this claim using ALL sources below:\nClaim: "{claim}"\n'}
    ]
    for i, src in enumerate(evidence_sources):
        if src['type'] == 'text':
            content_blocks.append(
                {'type': 'text', 'text': f'Source {i+1} (text): {src["content"]}'}
            )
        elif src['type'] == 'image':
            with open(src['content'], 'rb') as f:
                b64 = base64.standard_b64encode(f.read()).decode('utf-8')
            content_blocks.append(
                {'type': 'text', 'text': f'Source {i+1} (image):'}
            )
            content_blocks.append(
                {'type': 'image', 'source': {'type': 'base64',
                  'media_type': 'image/jpeg', 'data': b64}}
            )
    content_blocks.append({'type': 'text', 'text':
        'Return JSON: {"verdict": str, "confidence": float, '
        '"supporting_sources": [int], "contradicting_sources": [int]}'
    })
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=512,
        messages=[{'role': 'user', 'content': content_blocks}]
    )
    return json.loads(response.content[0].text)

การสร้างข้อเท็จจริงจากภาพเป็นข้อความ

บางครั้งคุณมีภาพและต้องสร้างข้อเท็จจริงแบบมีโครงสร้างจากภาพนั้น เพื่อใช้ในการให้เหตุผลที่อาศัยข้อความเป็นขั้นตอนถัดไป ให้แยกข้อเท็จจริงเชิงปริมาณจากแผนภูมิและตารางเป็น JSON เพื่อให้สามารถตรวจสอบทางคณิตศาสตร์เทียบกับข้อกล่าวอ้างในข้อความได้

def extract_facts_from_chart(chart_path: str) -> dict:
    import anthropic, base64, json
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    with open(chart_path, 'rb') as f:
        b64 = base64.standard_b64encode(f.read()).decode('utf-8')

    prompt = (
        'Extract all quantitative data from this chart. '
        'Return JSON with: chart_type, x_axis_label, y_axis_label, '
        'and data_points as [{label: str, value: float}].\n'
        'Also extract any trend: increasing/decreasing/stable/volatile.'
    )
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=512,
        messages=[{'role': 'user', 'content': [
            {'type': 'image', 'source': {'type': 'base64',
              'media_type': 'image/jpeg', 'data': b64}},
            {'type': 'text', 'text': prompt}
        ]}]
    )
    return json.loads(response.content[0].text)

การกำหนดเส้นทางตามเกณฑ์ความเชื่อมั่น

หลังการให้เหตุผลข้ามรูปแบบ ให้กำหนดเส้นทางของผลลัพธ์ตามความเชื่อมั่น: ความเชื่อมั่นสูง → อนุมัติอัตโนมัติ ความเชื่อมั่นปานกลาง → แจ้งเพื่อให้เลือกตรวจสอบ และความเชื่อมั่นต่ำ → ส่งต่อให้มนุษย์ ห้ามอนุมัติอัตโนมัติเมื่อสัญญาณขัดแย้งกัน ไม่ว่าคะแนนความเชื่อมั่นจะเป็นเท่าใด

def route_cross_modal_result(result: dict) -> str:
    confidence = result.get('confidence', 0.0)
    has_contradiction = bool(result.get('contradicting_sources'))

    if has_contradiction:
        return 'ESCALATE'
    if confidence >= 0.85:
        return 'AUTO_APPROVE'
    if confidence >= 0.6:
        return 'OPTIONAL_REVIEW'
    return 'ESCALATE'

# Example routing table:
# confidence >= 0.85, no contradiction -> AUTO_APPROVE
# confidence 0.6-0.85, no contradiction -> OPTIONAL_REVIEW
# any contradiction, or confidence < 0.6 -> ESCALATE

def handle_routing(claim: str, evidence_sources: list):
    result = multi_source_reasoning(claim, evidence_sources)
    action = route_cross_modal_result(result)
    print(f'Claim: "{claim}"')
    print(f'Verdict: {result["verdict"]} (confidence={result["confidence"]:.2f})')
    print(f'Action: {action}')
    return action, result

การจัดการหลักฐานภาพที่ไม่เพียงพอ

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

def assess_image_evidence_quality(
    image_path: str,
    claim: str
) -> dict:
    import anthropic, base64, json
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    with open(image_path, 'rb') as f:
        b64 = base64.standard_b64encode(f.read()).decode('utf-8')

    prompt = (
        f'Claim to verify: "{claim}"\n\n'
        'Assess whether this image provides sufficient evidence to evaluate the claim.\n'
        'Consider: image quality, relevance, completeness, and readability.\n'
        'Return JSON: {"sufficient": bool, "quality_issues": [str], '
        '"usable_evidence": str}'
    )
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=256,
        messages=[{'role': 'user', 'content': [
            {'type': 'image', 'source': {'type': 'base64',
              'media_type': 'image/jpeg', 'data': b64}},
            {'type': 'text', 'text': prompt}
        ]}]
    )
    return json.loads(response.content[0].text)

การสร้างเครื่องมือตรวจสอบข้อเท็จจริงข้ามรูปแบบ

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

def fact_check_report(
    report_text: str,
    image_paths: list
) -> dict:
    import re
    # Extract numeric claims from text (simple regex pattern)
    claim_pattern = r'[A-Z][^.!?]*[0-9][%$][^.!?]*[.!?]'
    claims = re.findall(claim_pattern, report_text)

    results = []
    for claim in claims:
        sources = [
            {'type': 'text', 'content': report_text},
        ] + [{'type': 'image', 'content': p} for p in image_paths]

        reasoning = multi_source_reasoning(claim, sources)
        action = route_cross_modal_result(reasoning)
        results.append({
            'claim': claim,
            'verdict': reasoning['verdict'],
            'confidence': reasoning['confidence'],
            'action': action
        })

    total = len(results)
    auto_approved = sum(1 for r in results if r['action'] == 'AUTO_APPROVE')
    return {
        'total_claims': total,
        'auto_approved': auto_approved,
        'escalated': total - auto_approved,
        'details': results
    }

ตรวจสอบความรู้

เมื่อสัญญาณจากข้อความและแผนภูมิ ขัดแย้ง กัน เอเจนต์ข้ามรูปแบบควรทำอย่างไรตามแนวปฏิบัติที่ดีที่สุด

สรุป: รูปแบบการให้เหตุผลข้ามรูปแบบ

คุณเรียนบทนี้จบแล้ว ประเด็นสำคัญมีดังนี้:

  • การเชื่อมโยงข้อความกับภาพ: ตรวจสอบข้อกล่าวอ้างในข้อความกับหลักฐานจากภาพ
  • การรวมสัญญาณ: ให้น้ำหนักความน่าเชื่อถือ (แผนภูมิ > ร้อยแก้วสำหรับข้อมูลตัวเลข) โดยแหล่งข้อมูลที่มีโครงสร้างมีความน่าเชื่อถือกว่าแหล่งที่ไม่มีโครงสร้าง
  • การตรวจจับความขัดแย้ง: ส่งต่อให้มนุษย์ตรวจสอบเสมอ — ห้ามแก้ไขความขัดแย้งโดยอัตโนมัติ
  • การกำหนดเส้นทางตามความเชื่อมั่น: อนุมัติอัตโนมัติ (สูง) → ตรวจสอบเพิ่มเติมได้ (ปานกลาง) → ส่งต่อให้มนุษย์ (ต่ำหรือมีความขัดแย้ง)
  • หลักฐานไม่เพียงพอ: งดสรุปผลแทนการสร้างข้อสรุปข้ามรูปแบบที่ไม่มีมูล

หลักสูตรถัดไป: เอเจนต์ IoT และเซนเซอร์ — ประมวลผลกระแสข้อมูลจากโลกจริงด้วย MQTT

เริ่มต้นได้ฟรี

เรียนรู้ AI Agents ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
60
บทเรียน
239

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

บทเรียน “รูปแบบการให้เหตุผลข้ามสื่อ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “รูปแบบการให้เหตุผลข้ามสื่อ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบการให้เหตุผลข้ามสื่อ”

ยึดโยงข้อกล่าวอ้างในข้อความกับภาพและสังเคราะห์บริบทหลายสื่อจากหลายแหล่ง คุณปฏิบัติ AI Agents ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “รูปแบบการให้เหตุผลข้ามสื่อ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน AI Agents นี้ได้ไหม

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

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

  1. เอเจนต์ภาพ + ข้อความด้วย Claude Vision และ GPT-4V
  2. เวิร์กโฟลว์เอเจนต์เสียง + ข้อความ
  3. การทำความเข้าใจวิดีโอในเอเจนต์
  4. รูปแบบการให้เหตุผลข้ามสื่อ
← กลับไปที่ AI Agents