in tools/doc_intelligence.py [0:0]
def get_figure(self, model_id: str, result_id: str, figure_id: str) -> bytes:
"""
Retrieves the binary image data for a specific figure from the Document Intelligence service.
Args:
model_id (str): The ID of the document model used for analysis.
result_id (str): The ID of the analysis result.
figure_id (str): The ID of the figure to retrieve.
Returns:
bytes: The binary image data of the figure.
Raises:
Exception: If the request fails or the response is invalid.
"""
endpoint = f"https://{self.service_name}.cognitiveservices.azure.com/documentintelligence/documentModels/{model_id}/analyzeResults/{result_id}/figures/{figure_id}"
url = f"{endpoint}?api-version={self.api_version}"
try:
token = self.credential.get_token("https://cognitiveservices.azure.com/.default")
headers = {
"Authorization": f"Bearer {token.token}",
"x-ms-useragent": "gpt-rag/1.0.0"
}
logging.debug(f"[docintelligence] Fetching figure {figure_id} from result {result_id} using model {model_id}.")
except ClientAuthenticationError as e:
error_message = f"Authentication failed while fetching figure: {e}"
logging.error(f"[docintelligence] {error_message}")
raise Exception(error_message)
except Exception as e:
error_message = f"Unexpected error during authentication while fetching figure: {e}"
logging.error(f"[docintelligence] {error_message}")
raise Exception(error_message)
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
logging.debug(f"[docintelligence] Successfully retrieved figure {figure_id}.")
return response.content # Returns binary data
else:
error_message = f"Failed to retrieve figure {figure_id}, status code {response.status_code}: {response.text}"
logging.error(f"[docintelligence] {error_message}")
raise Exception(error_message)
except Exception as e:
error_message = f"Error when sending GET request for figure {figure_id}: {e}"
logging.error(f"[docintelligence] {error_message}")
raise Exception(error_message)