สร้างตัววิเคราะห์การทําความเข้าใจเนื้อหา
Tip
ดูแท็บ ข้อความและรูปภาพ สําหรับรายละเอียดเพิ่มเติม!
ในสถานการณ์ส่วนใหญ่ คุณควรพิจารณาสร้างและทดสอบตัววิเคราะห์โดยใช้ส่วนติดต่อแบบภาพใน Content Understanding Studio อย่างไรก็ตาม ในบางกรณี คุณอาจต้องการสร้างตัววิเคราะห์โดยส่งคําจํากัดความ JSON ของ Schema สําหรับฟิลด์เนื้อหาที่คุณต้องการไปยัง API
การกําหนดสคีมาสําหรับตัววิเคราะห์
ตัววิเคราะห์จะขึ้นอยู่กับ schema ที่กําหนดเขตข้อมูลที่คุณต้องการแยกหรือสร้างจากไฟล์เนื้อหา ที่เรียบง่ายที่สุด สคีมาคือชุดของเขตข้อมูล ซึ่งสามารถระบุได้ในเอกสาร 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"
}
}
ตัวอย่างของ Schema ของตัววิเคราะห์แบบกําหนดเองนี้ยึดตามตัววิเคราะห์เอกสารที่สร้างไว้ล่วงหน้า และอธิบายเขตข้อมูลสองเขตที่คุณคาดว่าจะพบในนามบัตร: ContactName และ EmailAddress ทั้งสองเขตข้อมูลถูกกําหนดเป็นชนิดข้อมูลสตริง และคาดว่าจะ แยก ออกจากเอกสาร (กล่าวอีกนัยหนึ่งค่าสตริงที่คาดว่าจะมีอยู่ในเอกสาร เพื่อให้สามารถเป็น "อ่าน"; แทนที่จะเป็นเขตข้อมูลที่สามารถ สร้างขึ้น ได้โดยการอนุมานข้อมูลเกี่ยวกับเอกสาร) ออบเจ็ก models ต์ระบุโมเดลกําเนิดที่ตัววิเคราะห์ใช้สําหรับการประมวลผล
หมายเหตุ
ตัวอย่างนี้ง่ายโดยเจตนะ โดยมีข้อมูลน้อยที่สุดที่จําเป็นในการสร้างตัววิเคราะห์การทํางาน ในความเป็นจริง สคีมาจะมีเขตข้อมูลชนิดต่าง ๆ มากขึ้น และข้อกําหนดของตัววิเคราะห์จะรวมการตั้งค่าการกําหนดค่าเพิ่มเติม JSON อาจรวมเอกสารตัวอย่างไว้ด้วย ดูเอกสารประกอบ Azure Content Understanding API สําหรับรายละเอียดเพิ่มเติม
การใช้ Python SDK เพื่อสร้างตัววิเคราะห์
เมื่อมีคําจํากัดความของตัววิเคราะห์แล้ว คุณสามารถใช้ Python SDK เพื่อสร้างตัววิเคราะห์ได้ คลาสมีContentUnderstandingClientbegin_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 เพื่อสร้างตัววิเคราะห์
หรือคุณสามารถใช้ REST API ได้โดยตรง ข้อมูล JSON จะถูกส่งเป็น PUT คําขอไปยังจุดสิ้นสุดด้วยคีย์ API ในส่วนหัวของคําขอเพื่อเริ่มการดําเนินการสร้างตัววิเคราะห์
การตอบสนองจาก PUT คําขอรวมถึง Operation-Location ในส่วนหัว ซึ่งมี URL การเรียกกลับ ที่คุณสามารถใช้เพื่อตรวจสอบสถานะของคําขอโดยการส่ง GET คําขอ
โค้ด Python ต่อไปนี้ส่งคําขอสร้างตัววิเคราะห์ตามเนื้อหาของไฟล์ชื่อ 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!")