Overview

This exercise demonstrates how to develop an AI agent using Azure AI Foundry and the Semantic Kernel SDK. The agent processes expense claims by analyzing data and generating structured responses.


Steps & Configuration Details

1. Deploy a Model in Azure AI Foundry

  • Open Azure AI Foundry portal (https://ai.azure.com) and sign in.
  • Search for gpt-4o and select Use this model.
  • Configuration Items:
    • Azure AI Foundry Resource: A valid name.
    • Subscription: Your Azure subscription.
    • Resource Group: Select or create a resource group.
    • Region: Choose any AI Services-supported location.
    • Deployment Name: gpt-4o (default).

2. Clone the Repository

  • Open Azure Portal (https://portal.azure.com).
  • Launch Azure Cloud Shell (PowerShell environment).
  • Clone the repository:
    rm -r ai-agents -f
    git clone https://github.com/MicrosoftLearning/mslearn-ai-agents ai-agents
    
  • Navigate to the correct folder:
    cd ai-agents/Labfiles/04-semantic-kernel/python
    
  • Install dependencies:
    python -m venv labenv
    ./labenv/bin/Activate.ps1
    pip install python-dotenv azure-identity semantic-kernel[azure]
    
  • Open the configuration file:
    code .env
    
  • Update Configuration Values:
    • Project Endpoint (copied from Azure AI Foundry portal).
    • Model Deployment Name (gpt-4o)
  • Save the configuration file.

3. Implement the AI Agent

  • Open the agent code file:
    code semantic-kernel.py
    
  • Add references:
    from dotenv import load_dotenv
    from azure.identity.aio import DefaultAzureCredential
    from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
    from semantic_kernel.functions import kernel_function
    from typing import Annotated
    
  • Define an Email Plugin for expense claim submission:
    class EmailPlugin:
        """A Plugin to simulate email functionality."""
        @kernel_function(description="Sends an email.")
        def send_email(self, to: Annotated[str, "Recipient"], subject: Annotated[str, "Email Subject"], body: Annotated[str, "Email Body"]):
            print(f"\nTo: {to}")
            print(f"Subject: {subject}")
            print(f"{body}\n")
    
  • Load configuration settings:
    load_dotenv()
    ai_agent_settings = AzureAIAgentSettings()
    
  • Connect to Azure AI Foundry:
    async with (
        DefaultAzureCredential(exclude_environment_credential=True, exclude_managed_identity_credential=True) as creds,
        AzureAIAgent.create_client(credential=creds) as project_client,
    ):
    
  • Define the AI agent:
    expenses_agent_def = await project_client.agents.create_agent(
        model=ai_agent_settings.model_deployment_name,
        name="expenses_agent",
        instructions="""You are an AI assistant for expense claim submission. When a user submits expenses data and requests an expense claim, use the plug-in function to send an email to [email protected] with the subject 'Expense Claim' and a body that contains itemized expenses with a total. Then confirm to the user that you've done so."""
    )
    
  • Create a Semantic Kernel Agent:
    expenses_agent = AzureAIAgent(
        client=project_client,
        definition=expenses_agent_def,
        plugins=[EmailPlugin()]
    )
    
  • Process expenses data:
    thread: AzureAIAgentThread = AzureAIAgentThread(client=project_client)
    try:
        prompt_messages = [f"{prompt}: {expenses_data}"]
        response = await expenses_agent.get_response(thread_id=thread.id, messages=prompt_messages)
        print(f"\n# {response.name} :\n {response}")
    except Exception as e:
        print(e)
    finally:
        await thread.delete() if thread else None
        await project_client.agents.delete_agent(expenses_agent.id)
    

4. Run the AI Agent

  • Sign into Azure:
    az login
    
  • Run the application:
    python semantic-kernel.py
    
  • Example prompt:
    Submit an expense claim.
    
  • The agent should generate an email for an expense claim.

5. Clean Up

  • Delete Azure resources to avoid unnecessary costs:

Related Posts