🌊
【Azure Document intelligence】- Key value出力について
執筆日
2024/11/26
やること
Azure Document intelligenceのGeneral documentsモデルを使い、Key-value形式で出力する方法を検証します。
コード
main.py
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient
def main():
key = <"Key">
endpoint = <"endpoint">
credential = AzureKeyCredential(key)
client = DocumentAnalysisClient(endpoint=endpoint, credential=credential)
# Replace the fileUri with the actual URI of the document you want to analyze
file_uri = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
poller = client.begin_analyze_document_from_url("prebuilt-document", file_uri)
result = poller.result()
print("Detected key-value pairs:")
for kvp in result.key_value_pairs:
if kvp.value is None:
print(f" Found key with no value: '{kvp.key.content}'")
else:
print(
f" Found key-value pair: '{kvp.key.content}' and '{kvp.value.content}'"
)
# If running in a script, you can use the following to execute the main function
if __name__ == "__main__":
main()
出力結果
Detected key-value pairs:
Found key-value pair: 'QUARTERLY REPORT PURSUANT TO SECTION 13 OR 15(d) OF THE SECURITIES EXCHANGE ACT OF 1934' and ':selected:'
Found key-value pair: 'For the Quarterly Period Ended' and 'March 31, 2020'
Found key-value pair: 'TRANSITION REPORT PURSUANT TO SECTION 13 OR 15(d) OF THE SECURITIES EXCHANGE ACT OF 1934' and ':unselected:'
Found key with no value: 'Transition Period From'
Found key-value pair: 'Commission File Number' and '001-37845'
Found key-value pair: '(STATE' and 'WASHINGTON'
Found key-value pair: '(I.R.S. ID)' and '91-1144442'
Found key-value pair: 'Securities registered pursuant to Section 12(g) of the Act:' and 'NONE'
Found key-value pair: 'Yes' and ':selected:'
Found key-value pair: 'No' and ':unselected:'
Found key-value pair: 'Yes' and ':selected:'
Found key-value pair: 'No' and ':unselected:'
Found key-value pair: 'Large accelerated filer' and ':selected:'
Found key-value pair: 'Accelerated filer' and ':unselected:'
Found key-value pair: 'Non-accelerated filer' and ':unselected:'
Found key-value pair: 'Emerging growth company' and ':unselected:'
Found key-value pair: 'If an emerging growth company, indicate by check mark if the registrant has elected not to use the extended transition period for complying with any new or revised financial accounting standards provided pursuant to Section 13(a) of the Exchange Act.' and ':unselected:'
Found key-value pair: 'Yes' and ':unselected:'
Found key-value pair: 'No' and ':selected:'
コード(local file)
main.py
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient
def main():
key = <"key">
endpoint = <"endpoint">
credential = AzureKeyCredential(key)
client = DocumentAnalysisClient(endpoint=endpoint, credential=credential)
file_path = <"local file path">
with open(file_path, "rb") as f:
poller = client.begin_analyze_document("prebuilt-document", document=f)
result = poller.result()
print("Detected key-value pairs:")
for kvp in result.key_value_pairs:
if kvp.value is None:
print(f" Found key with no value: '{kvp.key.content}'")
else:
print(
f" Found key-value pair: '{kvp.key.content}' and '{kvp.value.content}'"
)
if __name__ == "__main__":
main()
まとめ
色々なドキュメントで検証したが、すべての項目がKey-value形式で出力されるわけではありません。そのため、Markdownで出力し、LLMで必要な項目をStructured outputsで出力する方法が良さそうかなーと。
Discussion