Make it easier to use AzureKeyCredentials with Azure Doc Intelligence

This commit is contained in:
Adam Fourney 2025-03-24 11:44:18 -07:00
parent 2ffe6ea591
commit bc0959de58

View file

@ -1,5 +1,6 @@
import sys import sys
import re import re
import os
from typing import BinaryIO, Any, List from typing import BinaryIO, Any, List
@ -18,6 +19,7 @@ try:
AnalyzeResult, AnalyzeResult,
DocumentAnalysisFeature, DocumentAnalysisFeature,
) )
from azure.core.credentials import AzureKeyCredential, TokenCredential
from azure.identity import DefaultAzureCredential from azure.identity import DefaultAzureCredential
except ImportError: except ImportError:
# Preserve the error and stack trace for later # Preserve the error and stack trace for later
@ -71,6 +73,7 @@ class DocumentIntelligenceConverter(DocumentConverter):
*, *,
endpoint: str, endpoint: str,
api_version: str = "2024-07-31-preview", api_version: str = "2024-07-31-preview",
credential: AzureKeyCredential | TokenCredential = None,
): ):
super().__init__() super().__init__()
@ -86,12 +89,18 @@ class DocumentIntelligenceConverter(DocumentConverter):
_dependency_exc_info[2] _dependency_exc_info[2]
) )
if credential is None:
if os.environ.get("AZURE_API_KEY") is None:
credential = DefaultAzureCredential()
else:
credential = AzureKeyCredential(os.environ["AZURE_API_KEY"])
self.endpoint = endpoint self.endpoint = endpoint
self.api_version = api_version self.api_version = api_version
self.doc_intel_client = DocumentIntelligenceClient( self.doc_intel_client = DocumentIntelligenceClient(
endpoint=self.endpoint, endpoint=self.endpoint,
api_version=self.api_version, api_version=self.api_version,
credential=DefaultAzureCredential(), credential=credential,
) )
def accepts( def accepts(