יצירת מנתח הבנת תוכן

הושלם

ברוב התרחישים, כדאי לשקול ליצור ולבדוק אנלייזרים באמצעות הממשק הוויזואלי ב-Content Understanding Studio. עם זאת, במקרים מסוימים ייתכן שתרצה ליצור אנלייזר על ידי הגשת הגדרת JSON של הסכימה לשדות התוכן הרצויים ל-API.

הגדרת סכימה עבור מנתח

מנתחים מבוססים על סכימות שמגדירות את השדות שברצונך לחלץ או ליצור מקובץ תוכן. בדוגמה הפשוטה ביותר, סכימה היא קבוצת שדות שניתן לציין במסמך 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 API לפרטים נוספים.

שימוש ב-SDK של פייתון ליצירת אנלייזר

עם הגדרת המנתח שלך, תוכל להשתמש ב-SDK של פייתון כדי ליצור את האנלייזר. המחלקה 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 API ליצירת מנתח

לחילופין, אפשר להשתמש ישירות ב-API של REST. נתוני JSON נשלחים כבקשה PUT אל נקודת הקצה עם מפתח ה- API בכותרת הבקשה כדי להפעיל את פעולת יצירת המנתח.

התגובה מהבקשה PUT כוללת מיקום פעולה בכותרת העליונה, המספקת כתובת 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!")