إنشاء محلل فهم المحتوى

مكتمل

تلميح

راجع علامة التبويب النص والصور لمزيد من التفاصيل!

في معظم الحالات، يجب أن تفكر في إنشاء واختبار محللات باستخدام الواجهة البصرية في Content Understanding Studio. ومع ذلك، في بعض الحالات قد ترغب في إنشاء محلل عن طريق تقديم تعريف JSON للمخطط الخاص بحقول المحتوى التي ترغب بها إلى واجهة برمجة التطبيقات.

تعريف مخطط محلل

تستند أدوات التحليل إلى المخططات التي تحدد الحقول التي تريد استخراجها أو إنشاؤها من ملف محتوى. في أبسط صوره، المخطط هو مجموعة من الحقول، والتي يمكن تحديدها في مستند JSON، كما هو موضح في هذا المثال لتعريف محلل:

{
    "description": "Simple business card",
    "baseAnalyzerId": "prebuilt-document",
    "config": {
        "returnDetails": true
    },
    "fieldSchema": {
        "fields": {
            "ContactName": {
                "type": "string",
                "method": "extract",
                "description": "Name on business card"
            },
            "EmailAddress": {
                "type": "string",
                "method": "extract",
                "description": "Email address on business card"
            }
        }
    },
    "models": {
        "completion": "gpt-4.1",
        "embedding": "text-embedding-3-large"
    }
}

يستند هذا المثال لمخطط محلل مخصص إلى محلل المستندات الذي تم إنشاؤه مسبقا، ويصف حقلين تتوقع العثور علىهما على بطاقة عمل: ContactName و EmailAddress. يتم تعريف كلا الحقلين على أنهما نوعان من بيانات السلسلة، ومن المتوقع استخراجهما من مستند (بمعنى آخر، من المتوقع أن تكون قيم السلسلة موجودة في المستند بحيث يمكن "قراءتها"؛ بدلا من أن تكون حقولا يمكن إنشاؤها عن طريق الاستدلال على معلومات حول المستند). يحدد الكائن models النماذج التوليدية التي يستخدمها المحلل للمعالجة.

إشعار

هذا المثال بسيط عمدا، مع الحد الأدنى من المعلومات اللازمة لإنشاء محلل عمل. في الواقع، من المحتمل أن يتضمن المخطط المزيد من الحقول من أنواع مختلفة، وسيتضمن تعريف المحلل المزيد من إعدادات التكوين. قد يتضمن JSON مستند عينة. راجع توثيق واجهة برمجة تطبيقات Azure Content Understanding لمزيد من التفاصيل.

استخدام حزمة تطوير البرمجيات لبايثون لإنشاء محلل

مع وجود تعريف المحلل الخاص بك، يمكنك استخدام حزمة تطوير Python لإنشاء المحلل. توفر الفئة ContentUnderstandingClient طريقة begin_create_analyzer تتعامل مع عملية الإنشاء غير المتزامنة نيابة عنك.

from azure.ai.contentunderstanding import ContentUnderstandingClient
from azure.core.credentials import AzureKeyCredential

# Authenticate the client
endpoint = "<YOUR_ENDPOINT>"
credential = AzureKeyCredential("<YOUR_API_KEY>")
client = ContentUnderstandingClient(endpoint=endpoint, credential=credential)

# Define the analyzer
analyzer_name = "business_card_analyser"
analyzer_definition = {
    "description": "Simple business card",
    "baseAnalyzerId": "prebuilt-document",
    "config": {"returnDetails": True},
    "fieldSchema": {
        "fields": {
            "ContactName": {
                "type": "string",
                "method": "extract",
                "description": "Name on business card"
            },
            "EmailAddress": {
                "type": "string",
                "method": "extract",
                "description": "Email address on business card"
            }
        }
    },
    "models": {
        "completion": "gpt-4.1",
        "embedding": "text-embedding-3-large"
    }
}

# Create the analyzer and wait for completion
poller = client.begin_create_analyzer(analyzer_name, body=analyzer_definition)
result = poller.result()
print(f"Analyzer created: {result.analyzer_id}")

استخدام واجهة برمجة تطبيقات REST لإنشاء محلل

بدلا من ذلك، يمكنك استخدام واجهة برمجة تطبيقات REST مباشرة. يتم إرسال بيانات JSON كطلب PUT إلى نقطة النهاية باستخدام مفتاح API في عنوان الطلب لبدء عملية إنشاء المحلل.

تتضمن الاستجابة PUT من الطلب Operation-Location في العنوان، والذي يوفر عنوان URL لرد الاتصال يمكنك استخدامه للتحقق من حالة الطلب عن طريق إرسال GET طلب.

الكود التالي في بايثون يقدم طلبا لإنشاء محلل بناء على محتويات ملف يدعى card.json (والذي يفترض أنه يحتوي على تعريف JSON المذكور سابقا):

import json
import requests

# Get the business card schema
with open("card.json", "r") as file:
    schema_json = json.load(file)

# Use a PUT request to submit the schema for a new analyzer
analyzer_name = "business_card_analyser"

headers = {
    "Ocp-Apim-Subscription-Key": "<YOUR_API_KEY>",
    "Content-Type": "application/json"}

url = f"{<YOUR_ENDPOINT>}/contentunderstanding/analyzers/{analyzer_name}?api-version=2025-11-01"

response = requests.put(url, headers=headers, data=json.dumps(schema_json))

# Get the response and extract the ID assigned to the operation
callback_url = response.headers["Operation-Location"]

# Use a GET request to check the status of the operation
result_response = requests.get(callback_url, headers=headers)

# Keep polling until the operation is complete
status = result_response.json().get("status")
while status == "Running":
    result_response = requests.get(callback_url, headers=headers)
    status = result_response.json().get("status")

print("Done!")