{ "cells": [ { "cell_type": "markdown", "id": "19ecd90d", "metadata": {}, "source": [ "# 🔍 Exploring Agent Framework with Azure OpenAI (Python)\n", "\n", "## 📋 Advanced Integration Tutorial\n", "\n", "This notebook demonstrates how to integrate the Microsoft Agent Framework with **Azure OpenAI Service** using enterprise-grade authentication and configuration patterns. Learn how to build production-ready AI agents that leverage Azure's managed OpenAI infrastructure.\n", "\n", "## 🎯 Learning Objectives\n", "\n", "### 🏢 **Enterprise Integration Patterns**\n", "- **Azure CLI Authentication**: Secure, credential-free authentication using Azure identity\n", "- **Azure OpenAI Service**: Leveraging Microsoft's managed OpenAI infrastructure \n", "- **Production Configuration**: Enterprise-ready setup and deployment patterns\n", "- **Streaming Responses**: Real-time agent interactions for enhanced user experience\n", "\n", "### 🔐 **Security & Identity Management**\n", "- **Azure Identity Integration**: Using Azure CLI credentials for seamless authentication\n", "- **RBAC Compliance**: Role-based access control for production deployments\n", "- **Credential Management**: Avoiding hard-coded API keys in enterprise environments\n", "- **Audit & Compliance**: Leveraging Azure's built-in security and monitoring\n", "\n", "### 🚀 **Scalability & Performance**\n", "- **Azure Infrastructure**: Global scale and high availability\n", "- **Streaming Operations**: Real-time response generation\n", "- **Load Distribution**: Azure's automatic scaling and load balancing\n", "- **Monitoring Integration**: Built-in Azure monitoring and analytics\n", "\n", "## ⚙️ Prerequisites & Setup\n", "\n", "### 📦 **Required Dependencies**\n", "\n", "Install the Agent Framework with Azure integration:\n", "\n", "```bash\n", "# Remove any conflicting installations\n", "pip install agent-framework-core -U\n", "```\n", "\n", "### 🔑 **Azure Configuration Requirements**\n", "\n", "**Azure OpenAI Resource Setup:**\n", "1. **Create Azure OpenAI Resource**: Deploy an OpenAI resource in your Azure subscription\n", "2. **Model Deployment**: Deploy required models (e.g., GPT-4, GPT-3.5-turbo) \n", "3. **Configure Access**: Ensure your Azure identity has appropriate RBAC permissions\n", "4. **Azure CLI Authentication**: Run `az login` to authenticate with Azure\n", "\n", "**Required Azure Permissions:**\n", "- `Cognitive Services User` or `Cognitive Services OpenAI User` role\n", "- Access to the specific Azure OpenAI resource\n", "- Network access to the Azure OpenAI endpoint\n", "\n", "### 🏗️ **Architecture Overview**\n", "\n", "```mermaid\n", "graph LR\n", " A[Python Agent] --> B[Azure Identity]\n", " B --> C[Azure OpenAI Service]\n", " C --> D[GPT Models]\n", " A --> E[Streaming Response]\n", " F[Azure CLI] --> B\n", "```\n", "\n", "**Key Components:**\n", "- **AzureCliCredential**: Secure authentication using Azure CLI session\n", "- **AzureOpenAIChatClient**: Azure-native OpenAI client with enterprise features\n", "- **Streaming Agent**: Real-time response processing and display\n", "\n", "## 🎨 Enterprise Use Cases\n", "\n", "### 📊 **Production Scenarios**\n", "- **Corporate Chatbots**: Internal knowledge assistants with Azure security\n", "- **Customer Support**: AI-powered support agents with compliance requirements\n", "- **Content Generation**: Enterprise content creation with audit trails\n", "- **Process Automation**: Intelligent workflow automation with Azure integration\n", "\n", "### 🔒 **Security Benefits**\n", "- **No API Key Management**: Azure identity eliminates credential storage\n", "- **Audit Trails**: Complete logging and monitoring through Azure\n", "- **Network Security**: Private endpoints and VNet integration support\n", "- **Compliance**: Meets enterprise security and regulatory requirements\n", "\n", "## 🚀 Getting Started\n", "\n", "Execute the cells below to build a production-ready AI agent with Azure OpenAI integration!" ] }, { "cell_type": "code", "execution_count": 1, "id": "a8811e71", "metadata": {}, "outputs": [], "source": [ "# ! pip install -r ../../../Installation/requirements.txt -U" ] }, { "cell_type": "code", "execution_count": 2, "id": "e3ec121a", "metadata": {}, "outputs": [], "source": [ "# 📦 Import Essential Libraries\n", "# Core system utilities and environment configuration\n", "\n", "import os # Environment variable access\n", "from dotenv import load_dotenv # Secure environment configuration loading" ] }, { "cell_type": "code", "execution_count": 3, "id": "40cf69e1", "metadata": {}, "outputs": [], "source": [ "# 🔐 Import Azure Identity and Agent Framework Components\n", "# Enterprise-grade authentication and Azure OpenAI integration \n", "from agent_framework import Agent\n", "from azure.identity import AzureCliCredential\n", "from agent_framework.openai import OpenAIChatCompletionClient" ] }, { "cell_type": "code", "execution_count": 4, "id": "fbb440e3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load environment variables from .env file\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 6, "id": "c16b8adc", "metadata": {}, "outputs": [], "source": [ "# 🤖 Create Enterprise AI Agent with Azure OpenAI\n", "# Uses Azure CLI credentials for secure, production-ready authentication\n", "# No API keys required - leverages your existing Azure identity\n", "\n", "# agent = OpenAIChatCompletionClient(credential=AzureCliCredential()).as_agent(\n", "# instructions=\"You are a helpful weather agent.\" # 📝 Define agent personality and role\n", "# )\n", "\n", "\n", "agent = Agent(\n", " client=OpenAIChatCompletionClient(\n", " model=os.getenv(\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"),\n", " azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n", " credential=AzureCliCredential(),\n", " ),\n", " name=\"DemoAgent\",\n", " instructions=\"You are a helpful weather agent.\"\n", " )" ] }, { "cell_type": "code", "execution_count": 7, "id": "1cfc93c1", "metadata": {}, "outputs": [], "source": [ "# 🎯 Define User Query for the AI Agent\n", "# Creative writing task to demonstrate agent capabilities\n", "query = \"Write a haiku about Agent Framework.\"" ] }, { "cell_type": "code", "execution_count": 8, "id": "9e0697b9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "In code's whispers clear, \n", "Agents dance through logic's depths, \n", "Framework holds them near." ] } ], "source": [ "# 🌊 Experience Real-time Streaming Response\n", "# Stream the agent's response in real-time for better user experience\n", "# Demonstrates Azure OpenAI's streaming capabilities\n", "\n", "async for chunk in agent.run(query, stream=True):\n", " if chunk.text:\n", " print(chunk.text, end=\"\", flush=True) # 📝 Display response as it generates" ] } ], "metadata": { "kernelspec": { "display_name": "agentdev", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.10" } }, "nbformat": 4, "nbformat_minor": 5 }