-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathsample_update_analyzer.py
More file actions
129 lines (106 loc) · 4.66 KB
/
sample_update_analyzer.py
File metadata and controls
129 lines (106 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# pylint: disable=line-too-long,useless-suppression
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------
"""
FILE: sample_update_analyzer.py
DESCRIPTION:
This sample demonstrates how to update an existing custom analyzer, including updating
its description and tags.
The update_analyzer method allows you to modify only these properties of an existing analyzer:
- Description: Update the analyzer's description
- Tags: Add or update tags
To change create-only properties such as models, field_schema, config, base_analyzer_id,
dynamic_field_schema, processing_location, or knowledge_sources, use begin_create_analyzer
with allow_replace=True.
USAGE:
python sample_update_analyzer.py
Set the environment variables with your own values before running the sample:
1) CONTENTUNDERSTANDING_ENDPOINT - the endpoint to your Content Understanding resource.
2) CONTENTUNDERSTANDING_KEY - your Content Understanding API key (optional if using DefaultAzureCredential).
"""
import os
import time
from dotenv import load_dotenv
from azure.ai.contentunderstanding import ContentUnderstandingClient
from azure.ai.contentunderstanding.models import (
ContentAnalyzer,
ContentAnalyzerConfig,
ContentFieldSchema,
ContentFieldDefinition,
ContentFieldType,
GenerationMethod,
)
from azure.core.credentials import AzureKeyCredential
from azure.identity import DefaultAzureCredential
load_dotenv()
def main() -> None:
endpoint = os.environ["CONTENTUNDERSTANDING_ENDPOINT"]
key = os.getenv("CONTENTUNDERSTANDING_KEY")
credential = AzureKeyCredential(key) if key else DefaultAzureCredential()
client = ContentUnderstandingClient(endpoint=endpoint, credential=credential)
# Create initial analyzer
analyzer_id = f"my_analyzer_for_update_{int(time.time())}"
print(f"Creating initial analyzer '{analyzer_id}'...")
analyzer = ContentAnalyzer(
base_analyzer_id="prebuilt-document",
description="Initial description",
config=ContentAnalyzerConfig(return_details=True),
field_schema=ContentFieldSchema(
name="demo_schema",
description="Schema for update demo",
fields={
"company_name": ContentFieldDefinition(
type=ContentFieldType.STRING,
method=GenerationMethod.EXTRACT,
description="Name of the company",
),
},
),
models={"completion": "gpt-4.1"},
tags={"tag1": "tag1_initial_value", "tag2": "tag2_initial_value"},
)
poller = client.begin_create_analyzer(
analyzer_id=analyzer_id,
resource=analyzer,
)
poller.result()
print(f"Analyzer '{analyzer_id}' created successfully!")
# [START update_analyzer]
# First, get the current analyzer to preserve base analyzer ID
current_analyzer = client.get_analyzer(analyzer_id=analyzer_id)
# Display current analyzer information
print("\nCurrent analyzer information:")
print(f" Description: {current_analyzer.description}")
if current_analyzer.tags:
tags_str = ", ".join(f"{k}={v}" for k, v in current_analyzer.tags.items())
print(f" Tags: {tags_str}")
# Create an updated analyzer with the properties supported by update_analyzer.
# Other analyzer properties are create-only and require begin_create_analyzer(..., allow_replace=True).
updated_analyzer = ContentAnalyzer(
base_analyzer_id=current_analyzer.base_analyzer_id,
description="Updated description",
tags={
"tag1": "tag1_updated_value", # Update existing tag
"tag3": "tag3_value", # Add new tag
},
)
# Update the analyzer
print(f"\nUpdating analyzer '{analyzer_id}'...")
client.update_analyzer(analyzer_id=analyzer_id, resource=updated_analyzer)
# Verify the update
updated = client.get_analyzer(analyzer_id=analyzer_id)
print("\nUpdated analyzer information:")
print(f" Description: {updated.description}")
if updated.tags:
tags_str = ", ".join(f"{k}={v}" for k, v in updated.tags.items())
print(f" Tags: {tags_str}")
# [END update_analyzer]
# Clean up - delete the analyzer
print(f"\nCleaning up: deleting analyzer '{analyzer_id}'...")
client.delete_analyzer(analyzer_id=analyzer_id)
print(f"Analyzer '{analyzer_id}' deleted successfully.")
if __name__ == "__main__":
main()