أنماط الاستدلال متعدد الوسائط
إسناد الادعاءات النصية إلى الصور وتركيب سياق متعدد الوسائط من مصادر متعددة.
أنماط الاستدلال متعدد الوسائط درس مجاني في AI Agents على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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)استراتيجيات دمج الإشارات
عندما تتفق الإشارات الواردة من وسائط متعددة، زِد مستوى الثقة. وعندما تختلف، طبّق استراتيجية لحل التعارض: ثق بالمصدر الأكثر تنظيمًا (فالمخططات تتفوق على النصوص التفسيرية في الأرقام)، أو أبلغ عن الأمر للمراجعة البشرية، أو اطلب من نموذج اللغة الكبير الاستدلال على المصدر الأكثر موثوقية.
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
}التحقق من المعرفة
عندما تتعارض إشارات النص والمخطط، ما الذي ينبغي للوكيل متعدد الوسائط فعله وفقًا لأفضل الممارسات؟
مراجعة: أنماط الاستدلال عبر الوسائط
لقد أكملت هذا الدرس. إليك النقاط الأساسية:
- الربط بين النص والصورة: تحقّق من الادعاءات النصية مقابل الأدلة المرئية
- دمج الإشارات: ثقة موزونة (المخططات > النصوص التفسيرية للأرقام)، فالمصادر المنظمة تتفوق على غير المنظمة
- اكتشاف التناقضات: صعّد الأمر دائمًا إلى مراجع بشري — ولا تحل التناقضات تلقائيًا أبدًا
- التوجيه وفق الثقة: موافقة تلقائية (مرتفعة) → مراجعة اختيارية (متوسطة) → تصعيد (منخفضة أو عند وجود تناقض)
- الأدلة غير الكافية: امتنع عن إصدار استنتاج عبر الوسائط بدلًا من اختلاقه
الدورة التالية: وكلاء إنترنت الأشياء المعتمدون على المستشعرات — معالجة تدفقات البيانات من العالم الواقعي باستخدام MQTT.
الأسئلة الشائعة
هل درس «أنماط الاستدلال متعدد الوسائط» مجاني؟
نعم — نص درس «أنماط الاستدلال متعدد الوسائط» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Agents، انتقل إلى CoddyKit PRO. تتضمن دورة AI Agents 4 دروس في المجموع.
ماذا ستتعلم في «أنماط الاستدلال متعدد الوسائط»؟
إسناد الادعاءات النصية إلى الصور وتركيب سياق متعدد الوسائط من مصادر متعددة. تتمرن على AI Agents مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Agents؟
لا تُشترط خبرة سابقة. AI Agents على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «أنماط الاستدلال متعدد الوسائط»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Agents هذا؟
نعم. كل درس في AI Agents يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- وكلاء الصور + النصوص باستخدام Claude Vision وGPT-4V
- سير عمل الوكلاء للصوت + النص
- فهم الفيديو لدى الوكلاء
- أنماط الاستدلال متعدد الوسائط