Skip to content

Azure AI Engineer Associate - Hands-on Labs โ€‹

๐Ÿงช Lab Environment Setup โ€‹

Prerequisites โ€‹

  • Azure subscription (free tier or pay-as-you-go)
  • Visual Studio Code with Azure extensions
  • Azure CLI installed and configured
  • PowerShell (Windows) or Bash (Linux/Mac)
  • Git for version control

Lab Environment Configuration โ€‹

bash
# Install Azure CLI
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

# Login to Azure
az login

# Install Azure AI CLI extension
az extension add --name ai

# Create resource group for labs
az group create --name rg-ai-labs --location eastus

๐Ÿ“š Module 1: Azure AI Services Foundation โ€‹

Lab 1.1: Create and Configure Azure AI Services โ€‹

Objective: Create and configure multiple Azure AI services

Steps:

  1. Create Computer Vision Service
bash
az cognitiveservices account create \
  --name cv-service-lab1 \
  --resource-group rg-ai-labs \
  --kind ComputerVision \
  --sku S0 \
  --location eastus
  1. Create Language Service
bash
az cognitiveservices account create \
  --name language-service-lab1 \
  --resource-group rg-ai-labs \
  --kind Language \
  --sku S0 \
  --location eastus
  1. Create Speech Service
bash
az cognitiveservices account create \
  --name speech-service-lab1 \
  --resource-group rg-ai-labs \
  --kind SpeechServices \
  --sku S0 \
  --location eastus
  1. Retrieve Keys and Endpoints
bash
# Get Computer Vision keys
az cognitiveservices account keys list \
  --name cv-service-lab1 \
  --resource-group rg-ai-labs

# Get Language Service keys
az cognitiveservices account keys list \
  --name language-service-lab1 \
  --resource-group rg-ai-labs

Expected Outcome: Three Azure AI services created with keys and endpoints retrieved


Lab 1.2: Implement Authentication Patterns โ€‹

Objective: Implement both API key and Azure AD authentication

API Key Authentication (Python):

python
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials

# API Key authentication
subscription_key = "YOUR_API_KEY"
endpoint = "YOUR_ENDPOINT"

client = ComputerVisionClient(
    endpoint=endpoint,
    credentials=CognitiveServicesCredentials(subscription_key)
)

# Test the connection
image_url = "https://example.com/image.jpg"
result = client.analyze_image(image_url, visual_features=[VisualFeatureTypes.tags])
print("API Key authentication successful!")

Azure AD Authentication (Python):

python
from azure.identity import DefaultAzureCredential
from azure.cognitiveservices.vision.computervision import ComputerVisionClient

# Azure AD authentication
credential = DefaultAzureCredential()
client = ComputerVisionClient(
    endpoint="YOUR_ENDPOINT",
    credential=credential
)

# Test the connection
image_url = "https://example.com/image.jpg"
result = client.analyze_image(image_url, visual_features=[VisualFeatureTypes.tags])
print("Azure AD authentication successful!")

Expected Outcome: Both authentication methods working successfully


Lab 1.3: Set up Monitoring and Logging โ€‹

Objective: Configure Application Insights for Azure AI services

Steps:

  1. Create Application Insights
bash
az monitor app-insights component create \
  --app ai-monitoring-lab1 \
  --location eastus \
  --resource-group rg-ai-labs
  1. Configure Custom Metrics (Python)
python
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.identity import DefaultAzureCredential
import logging

# Configure Application Insights
configure_azure_monitor(
    connection_string="YOUR_CONNECTION_STRING"
)

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Create client with monitoring
credential = DefaultAzureCredential()
client = ComputerVisionClient(
    endpoint="YOUR_ENDPOINT",
    credential=credential
)

# Log custom events
logger.info("Starting image analysis")
result = client.analyze_image("https://example.com/image.jpg")
logger.info(f"Analysis completed: {len(result.tags)} tags found")

Expected Outcome: Application Insights configured with custom metrics and logging


๐Ÿ“š Module 2: Computer Vision Solutions โ€‹

Lab 2.1: Image Analysis with Azure AI Vision โ€‹

Objective: Implement comprehensive image analysis

Complete Image Analysis (Python):

python
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from azure.identity import DefaultAzureCredential
import requests
from PIL import Image
import io

def analyze_image_comprehensive(image_url):
    # Initialize client
    credential = DefaultAzureCredential()
    client = ComputerVisionClient(
        endpoint="YOUR_ENDPOINT",
        credential=credential
    )
    
    # Define visual features to analyze
    visual_features = [
        VisualFeatureTypes.tags,
        VisualFeatureTypes.objects,
        VisualFeatureTypes.faces,
        VisualFeatureTypes.adult,
        VisualFeatureTypes.color,
        VisualFeatureTypes.image_type
    ]
    
    # Analyze image
    result = client.analyze_image(image_url, visual_features=visual_features)
    
    # Print results
    print("=== IMAGE ANALYSIS RESULTS ===")
    print(f"Tags: {[tag.name for tag in result.tags]}")
    print(f"Objects: {[obj.object_property for obj in result.objects]}")
    print(f"Faces: {len(result.faces)} detected")
    print(f"Adult content: {result.adult.is_adult_content}")
    print(f"Dominant colors: {result.color.dominant_colors}")
    print(f"Image type: {result.image_type.clip_art_type}")
    
    return result

# Test with sample image
image_url = "https://example.com/sample-image.jpg"
result = analyze_image_comprehensive(image_url)

Expected Outcome: Comprehensive image analysis with all visual features


Lab 2.2: Custom Vision Model Training โ€‹

Objective: Train and deploy a custom image classification model

Custom Vision Training (Python):

python
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateEntry
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.identity import DefaultAzureCredential
import os

def train_custom_vision_model():
    # Initialize training client
    credential = DefaultAzureCredential()
    trainer = CustomVisionTrainingClient(
        endpoint="YOUR_TRAINING_ENDPOINT",
        credential=credential
    )
    
    # Create project
    project = trainer.create_project("Lab2-CustomVision")
    print(f"Created project: {project.id}")
    
    # Create tags
    tag1 = trainer.create_tag(project.id, "Cat")
    tag2 = trainer.create_tag(project.id, "Dog")
    
    # Upload images (assuming you have image files)
    image_dir = "path/to/your/images"
    for filename in os.listdir(image_dir):
        if filename.endswith(('.jpg', '.jpeg', '.png')):
            with open(os.path.join(image_dir, filename), 'rb') as image_file:
                trainer.create_images_from_data(
                    project.id,
                    image_file.read(),
                    [tag1.id if 'cat' in filename.lower() else tag2.id]
                )
    
    # Train the model
    print("Training model...")
    iteration = trainer.train_project(project.id)
    while iteration.status != "Completed":
        iteration = trainer.get_iteration(project.id, iteration.id)
        print(f"Training status: {iteration.status}")
    
    # Deploy the model
    prediction_resource_id = "YOUR_PREDICTION_RESOURCE_ID"
    published_name = "Lab2-Model"
    publish_iteration = trainer.publish_iteration(
        project.id,
        iteration.id,
        published_name,
        prediction_resource_id
    )
    
    print(f"Model published: {published_name}")
    return project.id, published_name

# Test the trained model
def test_custom_vision_model(project_id, published_name):
    credential = DefaultAzureCredential()
    predictor = CustomVisionPredictionClient(
        endpoint="YOUR_PREDICTION_ENDPOINT",
        credential=credential
    )
    
    # Test with sample image
    with open("test_image.jpg", "rb") as image_file:
        results = predictor.classify_image(
            project_id,
            published_name,
            image_file.read()
        )
    
    for prediction in results.predictions:
        print(f"{prediction.tag_name}: {prediction.probability:.2%}")

# Run the training and testing
project_id, published_name = train_custom_vision_model()
test_custom_vision_model(project_id, published_name)

Expected Outcome: Custom vision model trained and deployed successfully


Lab 2.3: Face Detection and Recognition โ€‹

Objective: Implement face detection and recognition capabilities

Face Detection and Recognition (Python):

python
from azure.cognitiveservices.vision.face import FaceClient
from azure.cognitiveservices.vision.face.models import FaceAttributeType
from azure.identity import DefaultAzureCredential
import requests
from PIL import Image, ImageDraw
import io

def detect_and_analyze_faces(image_url):
    # Initialize face client
    credential = DefaultAzureCredential()
    face_client = FaceClient(
        endpoint="YOUR_ENDPOINT",
        credential=credential
    )
    
    # Detect faces with attributes
    detected_faces = face_client.face.detect_with_url(
        image_url,
        return_face_attributes=[
            FaceAttributeType.age,
            FaceAttributeType.gender,
            FaceAttributeType.emotion,
            FaceAttributeType.smile,
            FaceAttributeType.glasses,
            FaceAttributeType.hair
        ]
    )
    
    print(f"Detected {len(detected_faces)} faces")
    
    for i, face in enumerate(detected_faces):
        print(f"\nFace {i+1}:")
        print(f"  Age: {face.face_attributes.age}")
        print(f"  Gender: {face.face_attributes.gender}")
        print(f"  Emotion: {face.face_attributes.emotion}")
        print(f"  Smile: {face.face_attributes.smile}")
        print(f"  Glasses: {face.face_attributes.glasses}")
        print(f"  Hair: {face.face_attributes.hair}")
    
    return detected_faces

def create_face_group_and_identify():
    # Initialize face client
    credential = DefaultAzureCredential()
    face_client = FaceClient(
        endpoint="YOUR_ENDPOINT",
        credential=credential
    )
    
    # Create a person group
    person_group_id = "lab2-person-group"
    face_client.person_group.create(
        person_group_id=person_group_id,
        name="Lab2 Person Group"
    )
    
    # Create a person
    person = face_client.person_group_person.create(
        person_group_id=person_group_id,
        name="John Doe"
    )
    
    # Add face to person
    face_client.person_group_person.add_face_from_url(
        person_group_id=person_group_id,
        person_id=person.person_id,
        url="https://example.com/john-face.jpg"
    )
    
    # Train the person group
    face_client.person_group.train(person_group_id)
    
    # Identify faces
    test_image_url = "https://example.com/test-image.jpg"
    detected_faces = face_client.face.detect_with_url(test_image_url)
    
    if detected_faces:
        face_ids = [face.face_id for face in detected_faces]
        identify_result = face_client.face.identify(
            face_ids=face_ids,
            person_group_id=person_group_id
        )
        
        for result in identify_result:
            print(f"Face {result.face_id}:")
            for candidate in result.candidates:
                print(f"  Person: {candidate.person_id}, Confidence: {candidate.confidence}")

# Run face detection and recognition
detect_and_analyze_faces("https://example.com/group-photo.jpg")
create_face_group_and_identify()

Expected Outcome: Face detection, analysis, and recognition working successfully


๐Ÿ“š Module 3: Natural Language Processing Solutions โ€‹

Lab 3.1: Text Analytics Implementation โ€‹

Objective: Implement comprehensive text analytics

Text Analytics (Python):

python
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import DefaultAzureCredential
import json

def analyze_text_comprehensive(text):
    # Initialize client
    credential = DefaultAzureCredential()
    client = TextAnalyticsClient(
        endpoint="YOUR_ENDPOINT",
        credential=credential
    )
    
    # Sentiment analysis
    sentiment_result = client.analyze_sentiment([text])
    sentiment = sentiment_result[0]
    
    # Key phrase extraction
    key_phrases_result = client.extract_key_phrases([text])
    key_phrases = key_phrases_result[0]
    
    # Named entity recognition
    entities_result = client.recognize_entities([text])
    entities = entities_result[0]
    
    # Language detection
    language_result = client.detect_language([text])
    language = language_result[0]
    
    # PII detection
    pii_result = client.recognize_pii_entities([text])
    pii_entities = pii_result[0]
    
    # Print results
    print("=== TEXT ANALYTICS RESULTS ===")
    print(f"Sentiment: {sentiment.sentiment} (Confidence: {sentiment.confidence_scores.positive:.2f})")
    print(f"Key Phrases: {key_phrases.key_phrases}")
    print(f"Entities: {[entity.text for entity in entities.entities]}")
    print(f"Language: {language.primary_language.name}")
    print(f"PII Entities: {[entity.text for entity in pii_entities.entities]}")
    
    return {
        'sentiment': sentiment,
        'key_phrases': key_phrases,
        'entities': entities,
        'language': language,
        'pii_entities': pii_entities
    }

# Test with sample text
sample_text = """
Microsoft Azure is a cloud computing platform and service created by Microsoft. 
It provides a range of cloud services, including those for compute, analytics, 
storage and networking. Users can pick and choose from these services to develop 
and scale new applications, or run existing applications in the public cloud.
"""

result = analyze_text_comprehensive(sample_text)

Expected Outcome: Comprehensive text analytics with all features


Lab 3.2: Custom Text Classification โ€‹

Objective: Build and deploy a custom text classification model

Custom Text Classification (Python):

python
from azure.ai.language import ConversationAnalysisClient
from azure.identity import DefaultAzureCredential
import json
import time

def create_custom_text_classification_project():
    # Initialize client
    credential = DefaultAzureCredential()
    client = ConversationAnalysisClient(
        endpoint="YOUR_ENDPOINT",
        credential=credential
    )
    
    # Create project
    project_name = "lab3-text-classification"
    project_definition = {
        "projectName": project_name,
        "projectKind": "CustomSingleLabelClassification",
        "language": "en",
        "description": "Lab 3 Custom Text Classification Project"
    }
    
    # Create the project
    project = client.create_project(
        project_name=project_name,
        project_definition=project_definition
    )
    
    print(f"Created project: {project_name}")
    
    # Define classes
    classes = [
        {"category": "Positive"},
        {"category": "Negative"},
        {"category": "Neutral"}
    ]
    
    # Add classes to project
    for class_def in classes:
        client.create_class(
            project_name=project_name,
            class_definition=class_def
        )
    
    # Prepare training data
    training_data = [
        {"text": "I love this product!", "label": "Positive"},
        {"text": "This is terrible quality.", "label": "Negative"},
        {"text": "The product is okay.", "label": "Neutral"},
        {"text": "Amazing experience!", "label": "Positive"},
        {"text": "Worst purchase ever.", "label": "Negative"},
        {"text": "It's fine, nothing special.", "label": "Neutral"}
    ]
    
    # Upload training data
    for item in training_data:
        client.create_training_data(
            project_name=project_name,
            text=item["text"],
            label=item["label"]
        )
    
    # Train the model
    print("Training model...")
    training_job = client.begin_training(
        project_name=project_name,
        model_name="lab3-model"
    )
    
    # Wait for training to complete
    while not training_job.done():
        time.sleep(30)
        print("Training in progress...")
    
    training_result = training_job.result()
    print(f"Training completed: {training_result.status}")
    
    # Deploy the model
    deployment_job = client.begin_deploy_project(
        project_name=project_name,
        deployment_name="lab3-deployment",
        model_name="lab3-model"
    )
    
    while not deployment_job.done():
        time.sleep(30)
        print("Deployment in progress...")
    
    deployment_result = deployment_job.result()
    print(f"Deployment completed: {deployment_result.status}")
    
    return project_name, "lab3-deployment"

def test_custom_classification(project_name, deployment_name):
    # Initialize client
    credential = DefaultAzureCredential()
    client = ConversationAnalysisClient(
        endpoint="YOUR_ENDPOINT",
        credential=credential
    )
    
    # Test the model
    test_texts = [
        "This is absolutely fantastic!",
        "I hate this so much.",
        "It's okay, I guess."
    ]
    
    for text in test_texts:
        result = client.classify_text(
            project_name=project_name,
            deployment_name=deployment_name,
            text=text
        )
        
        print(f"Text: {text}")
        print(f"Classification: {result.classifications[0].category}")
        print(f"Confidence: {result.classifications[0].confidence:.2f}")
        print()

# Run the custom classification
project_name, deployment_name = create_custom_text_classification_project()
test_custom_classification(project_name, deployment_name)

Expected Outcome: Custom text classification model trained and deployed


Lab 3.3: LUIS Application Development โ€‹

Objective: Build a Language Understanding (LUIS) application

LUIS Application (Python):

python
from azure.cognitiveservices.language.luis.authoring import LUISAuthoringClient
from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient
from azure.identity import DefaultAzureCredential
import json

def create_luis_application():
    # Initialize authoring client
    credential = DefaultAzureCredential()
    authoring_client = LUISAuthoringClient(
        endpoint="YOUR_AUTHORING_ENDPOINT",
        credential=credential
    )
    
    # Create LUIS app
    app_definition = {
        "name": "Lab3-LUIS-App",
        "description": "Lab 3 LUIS Application",
        "culture": "en-us",
        "version": "0.1"
    }
    
    app = authoring_client.apps.add(app_definition)
    app_id = app.id
    print(f"Created LUIS app: {app_id}")
    
    # Create intents
    intents = [
        {"name": "BookFlight"},
        {"name": "GetWeather"},
        {"name": "None"}
    ]
    
    for intent in intents:
        authoring_client.model.add_intent(app_id, "0.1", intent["name"])
    
    # Create entities
    entities = [
        {"name": "Location", "type": "simple"},
        {"name": "Date", "type": "datetimeV2"},
        {"name": "Airline", "type": "simple"}
    ]
    
    for entity in entities:
        authoring_client.model.add_entity(app_id, "0.1", entity)
    
    # Add example utterances
    utterances = [
        {"text": "Book a flight to Paris", "intent": "BookFlight", "entities": [{"entity": "Location", "startPos": 20, "endPos": 24}]},
        {"text": "What's the weather in London?", "intent": "GetWeather", "entities": [{"entity": "Location", "startPos": 25, "endPos": 30}]},
        {"text": "Book a flight to New York on Monday", "intent": "BookFlight", "entities": [{"entity": "Location", "startPos": 20, "endPos": 27}, {"entity": "Date", "startPos": 31, "endPos": 37}]}
    ]
    
    for utterance in utterances:
        authoring_client.examples.add(app_id, "0.1", utterance)
    
    # Train the model
    print("Training LUIS model...")
    authoring_client.train.train_version(app_id, "0.1")
    
    # Wait for training to complete
    import time
    while True:
        status = authoring_client.train.get_status(app_id, "0.1")
        if all(status.details[0].status == "Success" for status in status.details):
            break
        time.sleep(10)
    
    # Publish the model
    authoring_client.apps.publish(app_id, {"versionId": "0.1", "isStaging": False})
    print("Model published successfully")
    
    return app_id

def test_luis_application(app_id):
    # Initialize runtime client
    credential = DefaultAzureCredential()
    runtime_client = LUISRuntimeClient(
        endpoint="YOUR_RUNTIME_ENDPOINT",
        credential=credential
    )
    
    # Test utterances
    test_utterances = [
        "Book a flight to Tokyo",
        "What's the weather in Seattle?",
        "I want to book a flight to London on Friday"
    ]
    
    for utterance in test_utterances:
        result = runtime_client.prediction.get_slot_prediction(
            app_id=app_id,
            slot_name="Production",
            query=utterance
        )
        
        print(f"Utterance: {utterance}")
        print(f"Top Intent: {result.prediction.top_intent}")
        print(f"Confidence: {result.prediction.intents[result.prediction.top_intent].score:.2f}")
        print(f"Entities: {[entity.entity for entity in result.prediction.entities]}")
        print()

# Run LUIS application
app_id = create_luis_application()
test_luis_application(app_id)

Expected Outcome: LUIS application created and tested successfully


๐Ÿ“š Module 4: Knowledge Mining & Document Intelligence โ€‹

Lab 4.1: Azure AI Search Index Creation โ€‹

Objective: Create and configure Azure AI Search indexes

Search Index Creation (Python):

python
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SearchIndex, SimpleField, SearchableField, ComplexField
from azure.search.documents import SearchClient
from azure.identity import DefaultAzureCredential
import json

def create_search_index():
    # Initialize search client
    credential = DefaultAzureCredential()
    search_client = SearchIndexClient(
        endpoint="YOUR_SEARCH_ENDPOINT",
        credential=credential
    )
    
    # Define index schema
    index_name = "lab4-documents"
    index_definition = SearchIndex(
        name=index_name,
        fields=[
            SimpleField(name="id", type="Edm.String", key=True),
            SearchableField(name="title", type="Edm.String", analyzer_name="en.microsoft"),
            SearchableField(name="content", type="Edm.String", analyzer_name="en.microsoft"),
            SearchableField(name="tags", type="Collection(Edm.String)"),
            SimpleField(name="created_date", type="Edm.DateTimeOffset"),
            SimpleField(name="file_size", type="Edm.Int32"),
            ComplexField(name="metadata", fields=[
                SimpleField(name="author", type="Edm.String"),
                SimpleField(name="category", type="Edm.String"),
                SimpleField(name="language", type="Edm.String")
            ])
        ],
        scoring_profiles=[
            {
                "name": "boosted_title",
                "text": {
                    "weights": {
                        "title": 2.0,
                        "content": 1.0
                    }
                }
            }
        ]
    )
    
    # Create the index
    search_client.create_index(index_definition)
    print(f"Created search index: {index_name}")
    
    return index_name

def upload_documents_to_index(index_name):
    # Initialize search client
    credential = DefaultAzureCredential()
    search_client = SearchClient(
        endpoint="YOUR_SEARCH_ENDPOINT",
        index_name=index_name,
        credential=credential
    )
    
    # Sample documents
    documents = [
        {
            "id": "1",
            "title": "Azure AI Services Overview",
            "content": "Azure AI services provide pre-built machine learning capabilities for common AI tasks.",
            "tags": ["AI", "Azure", "Machine Learning"],
            "created_date": "2024-01-15T10:00:00Z",
            "file_size": 1024,
            "metadata": {
                "author": "Microsoft",
                "category": "Documentation",
                "language": "en"
            }
        },
        {
            "id": "2",
            "title": "Computer Vision Best Practices",
            "content": "Best practices for implementing computer vision solutions with Azure AI Vision.",
            "tags": ["Computer Vision", "Best Practices", "Azure"],
            "created_date": "2024-01-16T14:30:00Z",
            "file_size": 2048,
            "metadata": {
                "author": "Azure Team",
                "category": "Guide",
                "language": "en"
            }
        },
        {
            "id": "3",
            "title": "Natural Language Processing Tutorial",
            "content": "Step-by-step tutorial for building NLP applications with Azure AI Language.",
            "tags": ["NLP", "Tutorial", "Azure AI Language"],
            "created_date": "2024-01-17T09:15:00Z",
            "file_size": 3072,
            "metadata": {
                "author": "AI Team",
                "category": "Tutorial",
                "language": "en"
            }
        }
    ]
    
    # Upload documents
    search_client.upload_documents(documents)
    print(f"Uploaded {len(documents)} documents to index")
    
    return index_name

def search_documents(index_name):
    # Initialize search client
    credential = DefaultAzureCredential()
    search_client = SearchClient(
        endpoint="YOUR_SEARCH_ENDPOINT",
        index_name=index_name,
        credential=credential
    )
    
    # Perform searches
    search_queries = [
        "Azure AI services",
        "computer vision",
        "natural language processing"
    ]
    
    for query in search_queries:
        print(f"\nSearching for: {query}")
        results = search_client.search(
            search_text=query,
            select=["id", "title", "content", "tags"],
            top=5
        )
        
        for result in results:
            print(f"  Title: {result['title']}")
            print(f"  Content: {result['content'][:100]}...")
            print(f"  Tags: {result['tags']}")
            print(f"  Score: {result['@search.score']}")
            print()

# Run search index creation and testing
index_name = create_search_index()
upload_documents_to_index(index_name)
search_documents(index_name)

Expected Outcome: Search index created with documents and search functionality


Lab 4.2: Cognitive Search Skillset Development โ€‹

Objective: Create cognitive search skillsets with built-in and custom skills

Cognitive Search Skillset (Python):

python
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SearchIndexer, SearchIndexerDataSourceConnection, SearchIndexerSkillset
from azure.identity import DefaultAzureCredential
import json

def create_cognitive_search_skillset():
    # Initialize search client
    credential = DefaultAzureCredential()
    search_client = SearchIndexClient(
        endpoint="YOUR_SEARCH_ENDPOINT",
        credential=credential
    )
    
    # Create data source
    data_source = SearchIndexerDataSourceConnection(
        name="lab4-datasource",
        type="azureblob",
        connection_string="YOUR_STORAGE_CONNECTION_STRING",
        container={"name": "documents"}
    )
    
    search_client.create_data_source_connection(data_source)
    print("Created data source connection")
    
    # Create skillset with built-in skills
    skillset_definition = {
        "name": "lab4-skillset",
        "description": "Lab 4 Cognitive Search Skillset",
        "skills": [
            {
                "@odata.type": "#Microsoft.Skills.Text.SplitSkill",
                "name": "text-split-skill",
                "description": "Split text into chunks",
                "context": "/document/content",
                "textSplitMode": "pages",
                "maximumPageLength": 4000,
                "inputs": [
                    {
                        "name": "text",
                        "source": "/document/content"
                    }
                ],
                "outputs": [
                    {
                        "name": "textItems",
                        "targetName": "pages"
                    }
                ]
            },
            {
                "@odata.type": "#Microsoft.Skills.Text.LanguageDetectionSkill",
                "name": "language-detection-skill",
                "description": "Detect language of text",
                "context": "/document",
                "inputs": [
                    {
                        "name": "text",
                        "source": "/document/content"
                    }
                ],
                "outputs": [
                    {
                        "name": "languageCode",
                        "targetName": "language"
                    }
                ]
            },
            {
                "@odata.type": "#Microsoft.Skills.Text.KeyPhraseExtractionSkill",
                "name": "key-phrase-extraction-skill",
                "description": "Extract key phrases from text",
                "context": "/document/pages/*",
                "inputs": [
                    {
                        "name": "text",
                        "source": "/document/pages/*"
                    },
                    {
                        "name": "languageCode",
                        "source": "/document/language"
                    }
                ],
                "outputs": [
                    {
                        "name": "keyPhrases",
                        "targetName": "keyPhrases"
                    }
                ]
            },
            {
                "@odata.type": "#Microsoft.Skills.Text.EntityRecognitionSkill",
                "name": "entity-recognition-skill",
                "description": "Recognize entities in text",
                "context": "/document/pages/*",
                "inputs": [
                    {
                        "name": "text",
                        "source": "/document/pages/*"
                    },
                    {
                        "name": "languageCode",
                        "source": "/document/language"
                    }
                ],
                "outputs": [
                    {
                        "name": "persons",
                        "targetName": "persons"
                    },
                    {
                        "name": "organizations",
                        "targetName": "organizations"
                    },
                    {
                        "name": "locations",
                        "targetName": "locations"
                    }
                ]
            }
        ]
    }
    
    skillset = SearchIndexerSkillset(**skillset_definition)
    search_client.create_skillset(skillset)
    print("Created cognitive search skillset")
    
    return "lab4-skillset"

def create_indexer_with_skillset(skillset_name):
    # Initialize search client
    credential = DefaultAzureCredential()
    search_client = SearchIndexClient(
        endpoint="YOUR_SEARCH_ENDPOINT",
        credential=credential
    )
    
    # Create indexer
    indexer_definition = {
        "name": "lab4-indexer",
        "description": "Lab 4 Cognitive Search Indexer",
        "data_source_name": "lab4-datasource",
        "target_index_name": "lab4-documents",
        "skillset_name": skillset_name,
        "field_mappings": [
            {
                "source_field_name": "metadata_storage_path",
                "target_field_name": "id"
            },
            {
                "source_field_name": "content",
                "target_field_name": "content"
            }
        ],
        "output_field_mappings": [
            {
                "source_field_name": "/document/pages/*/keyPhrases/*",
                "target_field_name": "tags"
            },
            {
                "source_field_name": "/document/language",
                "target_field_name": "language"
            }
        ]
    }
    
    indexer = SearchIndexer(**indexer_definition)
    search_client.create_indexer(indexer)
    print("Created indexer with skillset")
    
    return "lab4-indexer"

# Run cognitive search skillset creation
skillset_name = create_cognitive_search_skillset()
indexer_name = create_indexer_with_skillset(skillset_name)

Expected Outcome: Cognitive search skillset created with built-in skills


๐Ÿ“š Module 5: Generative AI Solutions โ€‹

Lab 5.1: Azure OpenAI Integration โ€‹

Objective: Integrate Azure OpenAI services for text generation

Azure OpenAI Integration (Python):

python
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential
import json

def setup_azure_openai():
    # Initialize Azure OpenAI client
    client = AzureOpenAI(
        api_version="2024-02-15-preview",
        azure_endpoint="YOUR_AZURE_OPENAI_ENDPOINT",
        azure_ad_token_provider=DefaultAzureCredential().get_token
    )
    
    return client

def text_generation_examples(client):
    # Text completion
    print("=== TEXT COMPLETION ===")
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a helpful AI assistant."},
            {"role": "user", "content": "Explain Azure AI services in simple terms."}
        ],
        max_tokens=150,
        temperature=0.7
    )
    
    print(f"Response: {response.choices[0].message.content}")
    
    # Code generation
    print("\n=== CODE GENERATION ===")
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a Python programming expert."},
            {"role": "user", "content": "Write a Python function to calculate the factorial of a number."}
        ],
        max_tokens=200,
        temperature=0.3
    )
    
    print(f"Code: {response.choices[0].message.content}")
    
    # Creative writing
    print("\n=== CREATIVE WRITING ===")
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a creative writer."},
            {"role": "user", "content": "Write a short story about a robot learning to paint."}
        ],
        max_tokens=300,
        temperature=0.9
    )
    
    print(f"Story: {response.choices[0].message.content}")

def embeddings_example(client):
    # Generate embeddings
    print("\n=== EMBEDDINGS ===")
    response = client.embeddings.create(
        model="text-embedding-ada-002",
        input="Azure AI services provide powerful machine learning capabilities."
    )
    
    embedding = response.data[0].embedding
    print(f"Embedding dimension: {len(embedding)}")
    print(f"First 10 values: {embedding[:10]}")

def function_calling_example(client):
    # Function calling
    print("\n=== FUNCTION CALLING ===")
    
    functions = [
        {
            "name": "get_weather",
            "description": "Get the current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    }
                },
                "required": ["location"]
            }
        }
    ]
    
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "user", "content": "What's the weather like in Seattle?"}
        ],
        functions=functions,
        function_call="auto"
    )
    
    print(f"Function call: {response.choices[0].message.function_call}")

# Run Azure OpenAI examples
client = setup_azure_openai()
text_generation_examples(client)
embeddings_example(client)
function_calling_example(client)

Expected Outcome: Azure OpenAI integration working with various capabilities


Lab 5.2: RAG Implementation โ€‹

Objective: Implement Retrieval Augmented Generation (RAG) pattern

RAG Implementation (Python):

python
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
import json
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

class RAGSystem:
    def __init__(self, openai_endpoint, search_endpoint, search_index):
        self.openai_client = AzureOpenAI(
            api_version="2024-02-15-preview",
            azure_endpoint=openai_endpoint,
            azure_ad_token_provider=DefaultAzureCredential().get_token
        )
        
        self.search_client = SearchClient(
            endpoint=search_endpoint,
            index_name=search_index,
            credential=DefaultAzureCredential()
        )
    
    def generate_embedding(self, text):
        """Generate embedding for text"""
        response = self.openai_client.embeddings.create(
            model="text-embedding-ada-002",
            input=text
        )
        return response.data[0].embedding
    
    def search_documents(self, query, top_k=3):
        """Search for relevant documents"""
        # Generate query embedding
        query_embedding = self.generate_embedding(query)
        
        # Search documents
        results = self.search_client.search(
            search_text=query,
            select=["id", "title", "content", "tags"],
            top=top_k
        )
        
        return list(results)
    
    def generate_answer(self, query, context_documents):
        """Generate answer using retrieved context"""
        # Prepare context
        context = "\n\n".join([
            f"Title: {doc['title']}\nContent: {doc['content']}"
            for doc in context_documents
        ])
        
        # Generate answer
        response = self.openai_client.chat.completions.create(
            model="gpt-4",
            messages=[
                {
                    "role": "system",
                    "content": "You are a helpful assistant. Answer questions based on the provided context. If the answer is not in the context, say so."
                },
                {
                    "role": "user",
                    "content": f"Context:\n{context}\n\nQuestion: {query}"
                }
            ],
            max_tokens=300,
            temperature=0.7
        )
        
        return response.choices[0].message.content
    
    def ask_question(self, query):
        """Main RAG pipeline"""
        print(f"Question: {query}")
        
        # Search for relevant documents
        relevant_docs = self.search_documents(query)
        print(f"\nFound {len(relevant_docs)} relevant documents:")
        for i, doc in enumerate(relevant_docs):
            print(f"  {i+1}. {doc['title']} (Score: {doc['@search.score']:.2f})")
        
        # Generate answer
        answer = self.generate_answer(query, relevant_docs)
        print(f"\nAnswer: {answer}")
        
        return answer

# Initialize RAG system
rag = RAGSystem(
    openai_endpoint="YOUR_AZURE_OPENAI_ENDPOINT",
    search_endpoint="YOUR_SEARCH_ENDPOINT",
    search_index="lab4-documents"
)

# Test RAG system
questions = [
    "What are Azure AI services?",
    "How do I implement computer vision?",
    "What is natural language processing?"
]

for question in questions:
    rag.ask_question(question)
    print("\n" + "="*50 + "\n")

Expected Outcome: RAG system working with document retrieval and answer generation


๐ŸŽฏ Lab Completion Checklist โ€‹

Module 1: Foundation โ€‹

  • [ ] Lab 1.1: Azure AI services created and configured
  • [ ] Lab 1.2: Authentication patterns implemented
  • [ ] Lab 1.3: Monitoring and logging configured

Module 2: Computer Vision โ€‹

  • [ ] Lab 2.1: Image analysis implemented
  • [ ] Lab 2.2: Custom vision model trained
  • [ ] Lab 2.3: Face detection and recognition working

Module 3: Natural Language Processing โ€‹

  • [ ] Lab 3.1: Text analytics implemented
  • [ ] Lab 3.2: Custom text classification working
  • [ ] Lab 3.3: LUIS application created

Module 4: Knowledge Mining โ€‹

  • [ ] Lab 4.1: Search index created and populated
  • [ ] Lab 4.2: Cognitive search skillset developed

Module 5: Generative AI โ€‹

  • [ ] Lab 5.1: Azure OpenAI integration working
  • [ ] Lab 5.2: RAG system implemented

๐Ÿ“ Lab Documentation Requirements โ€‹

For each lab, document:

  1. Objective: What the lab aims to achieve
  2. Prerequisites: Required setup and resources
  3. Steps: Detailed implementation steps
  4. Code: Complete working code
  5. Results: Expected and actual outcomes
  6. Troubleshooting: Common issues and solutions
  7. Extensions: Additional exercises for practice

๐Ÿš€ Next Steps โ€‹

After completing all labs:

  1. Review: Go through all lab documentation
  2. Practice: Implement additional scenarios
  3. Integrate: Combine multiple services in projects
  4. Optimize: Improve performance and efficiency
  5. Deploy: Move solutions to production environments

These hands-on labs provide practical experience with Azure AI services and prepare you for real-world AI engineering challenges.

Released under the MIT License.