1-introduction/2-environment_setup.ipynb (241 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"id": "ff53b1e0",
"metadata": {},
"source": [
"# Environment Setup for Azure AI Foundry Workshop\n",
"\n",
"This notebook will guide you through setting up your environment for the Azure AI Foundry workshop.\n",
"\n",
"## Prerequisites\n",
"- Python 3.8 or later\n",
"- Azure subscription with AI services access\n",
"- Basic Python knowledge"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32f4ec2a",
"metadata": {},
"outputs": [],
"source": [
"# Install required packages (if not already installed):\n",
"!pip install azure-identity azure-ai-projects"
]
},
{
"cell_type": "markdown",
"id": "5a108d90",
"metadata": {},
"source": [
"## Azure Authentication Setup\n",
"First, we'll verify our Azure credentials and setup."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "714b5791",
"metadata": {},
"outputs": [],
"source": [
"from azure.identity import DefaultAzureCredential\n",
"from azure.ai.projects import AIProjectClient\n",
"from azure.ai.projects.models import ConnectionType\n",
"import os\n",
"\n",
"# Initialize Azure credentials\n",
"try:\n",
" credential = DefaultAzureCredential()\n",
" print(\"✓ Successfully initialized DefaultAzureCredential\")\n",
"except Exception as e:\n",
" print(f\"× Error initializing credentials: {str(e)}\")"
]
},
{
"cell_type": "markdown",
"id": "87e22718",
"metadata": {},
"source": [
"## Initialize AI Project Client\n",
"\n",
"> **Note:** Before proceeding, ensure you:\n",
"> 1. Copy your `.env.local` file to `.env`\n",
"> 2. Update the project connection string in your `.env` file\n",
"> 3. Have a Hub and Project already provisioned in Azure AI Foundry\n",
"\n",
"You can find your project connection string in [Azure AI Foundry](https://ai.azure.com) under your project's settings:\n",
"\n",
"<img src=\"proj-conn-string.png\" alt=\"Project Connection String Location\" width=\"600\"/>\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "ad3e2dbc",
"metadata": {},
"source": [
"## Understanding AIProjectClient\n",
"\n",
"The AIProjectClient is a key component for interacting with Azure AI services that:\n",
"\n",
"- **Manages Connections**: Lists and accesses Azure AI resources like OpenAI models\n",
"- **Handles Authentication**: Securely connects using Azure credentials \n",
"- **Enables Model Access**: Provides interfaces to use AI models and deployments\n",
"- **Manages Project Settings**: Controls configurations for your Azure AI project\n",
"\n",
"The client requires:\n",
"- A project connection string (from Azure AI project settings)\n",
"- Valid Azure credentials\n",
"\n",
"You can find your project connection string in Azure AI Studio under Project Settings:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ac8b2fd",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"from pathlib import Path\n",
"\n",
"# Load environment variables\n",
"notebook_path = Path().absolute()\n",
"parent_dir = notebook_path.parent\n",
"load_dotenv(parent_dir / '.env')\n",
"\n",
"# Initialize AIProjectClient with connection string and credentials\n",
"try:\n",
" client = AIProjectClient.from_connection_string(\n",
" conn_str=os.getenv(\"PROJECT_CONNECTION_STRING\"),\n",
" credential=credential\n",
" )\n",
" print(\"✓ Successfully initialized AIProjectClient\")\n",
"except Exception as e:\n",
" print(f\"× Error initializing client: {str(e)}\")"
]
},
{
"cell_type": "markdown",
"id": "10962ac9",
"metadata": {},
"source": [
"## Verify Access to Models and Connections\n",
"Let's verify we can access all the required models and connections specified in the [prerequisites](../README.md#-prerequisites).\n",
"\n",
"We need to validate:\n",
"- GPT models (gpt-4o, gpt-4o-mini) for chat/completion\n",
"- Embedding model for vector search \n",
"- [Grounding with Bing](https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/bing-grounding?view=azure-python-preview&tabs=python&pivots=overview)\n",
"- Azure AI Search connection\n",
"\n",
"This validation ensures we have all the components needed to build our AI applications."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fff9d11",
"metadata": {},
"outputs": [],
"source": [
"# List the properties of all connections\n",
"connections = client.connections.list()\n",
"print(f\"====> Listing of all connections (found {len(connections)}):\")\n",
"for connection in connections:\n",
" print(connection)\n",
"\n",
"# List the properties of all connections of a particular \"type\" (in this sample, Azure OpenAI connections)\n",
"connections = client.connections.list(\n",
" connection_type=ConnectionType.AZURE_OPEN_AI,\n",
")\n",
"print(f\"====> Listing of all Azure Open AI connections (found {len(connections)}):\")\n",
"for connection in connections:\n",
" print(connection)\n",
"\n",
"# Get the properties of the default connection of a particular \"type\", with credentials\n",
"connection = client.connections.get_default(\n",
" connection_type=ConnectionType.AZURE_AI_SERVICES,\n",
" include_credentials=True, # Optional. Defaults to \"False\"\n",
")\n",
"print(\"====> Get default Azure AI Services connection:\")\n",
"print(connection)\n",
"\n",
"print(\"====> Get connection by name:\")\n",
"print(connection)"
]
},
{
"cell_type": "markdown",
"id": "f75c4895",
"metadata": {},
"source": [
"## Validate Model and Search Connections\n",
"The cell below validates that we have properly provisioned and connected to:\n",
"1. Azure OpenAI models through our Azure OpenAI connection\n",
"2. Azure AI Search through our Azure AI Search connection\n",
"\n",
"Both of these services will be essential for building our AI applications. The OpenAI models will provide the core language capabilities, while Azure AI Search will enable efficient information retrieval and knowledge base functionality.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79dd3f5b",
"metadata": {},
"outputs": [],
"source": [
"# List all connections and check for specific types\n",
"conn_list = client.connections.list()\n",
"search_conn_id = \"\"\n",
"openai_conn_id = \"\"\n",
"\n",
"for conn in conn_list:\n",
" conn_type = str(conn.connection_type).split('.')[-1] # Get the part after the dot\n",
" if conn_type == \"AZURE_AI_SEARCH\":\n",
" search_conn_id = conn.id\n",
" elif conn_type == \"AZURE_OPEN_AI\":\n",
" openai_conn_id = conn.id\n",
"\n",
"print(f\"\\n====> Connection IDs found:\")\n",
"if not search_conn_id:\n",
" print(\"Azure AI Search: Not found - Please create an Azure AI Search connection\")\n",
"else:\n",
" print(f\"Azure AI Search: {search_conn_id}\")\n",
" \n",
"if not openai_conn_id:\n",
" print(\"Azure OpenAI: Not found - Please create an Azure OpenAI connection\") \n",
"else:\n",
" print(f\"Azure OpenAI: {openai_conn_id}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}