Develop an AI app with the Microsoft Foundry SDK

Develop an AI app with the Microsoft Foundry SDK
  • Microsoft Foundry provides a REST API to work with:

    • AI Foundry projects

    • Resources inside those projects

  • It also offers language-specific SDKs to make coding easier.

  • Using the SDK, developers can:

    • Connect to a Foundry project

    • Access project resources and models

    • Perform AI operations (e.g., send prompts to generative AI models and process responses)

Core SDK Library

  • The main library is Azure AI Projects

  • It allows you to:

    • Connect to a Microsoft Foundry project

    • Access all resources defined in the project

Available SDKs:

  • Azure AI Projects for Python

  • Azure AI Projects for .NET

  • Azure AI Projects for JavaScript

Each SDK is developed independently, so features may differ slightly across languages.


Language Used in This Module

  • This module uses Python examples

  • Equivalent examples are available in other SDK documentation

Install the Azure AI SDK :
  • pip install azure-ai-projects


Connecting to a Microsoft Foundry Project

Project Endpoint

  • Each project has a unique endpoint

  • You can find it on the Project Overview page in the Microsoft Foundry portal

Types of Endpoints in a Project:

  1. Project endpoint

    • Access project connections, agents, and models

  2. Azure OpenAI Service endpoint

    • Use OpenAI models

  3. Foundry Tools endpoint

    • Use tools like Azure Vision and Azure Language


Creating a project Client
  • Use the project endpoint to create a project client

from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

project_endpoint = "https://......"

project_client = AIProjectClient(
    credential=DefaultAzureCredential(),
    endpoint=project_endpoint
)

Authentication

  • Uses DefaultAzureCredential

  • You must install the identity package:

  • pip install azure-identity

Important Authentication Requirement

  • Code must run in an authenticated Azure session

  • Example:

    • Run az login using Azure CLI before executing the code

Summary

  • Microsoft Foundry SDK lets developers programmatically work with AI projects

  • Azure AI Projects is the core library

  • Authentication and the correct project endpoint are mandatory


Project Connections

  • Every Microsoft Foundry project includes connected resources

  • These resources are defined at:

    • Parent level (Microsoft Foundry resource / hub)

    • Project level

  • Each connection links to an external service, such as:

    • Azure Storage

    • Azure AI Search

    • Azure OpenAI

    • Another Microsoft Foundry resource


Why Project Connections Matter

  • Connections allow your project to use external services

  • Using the Microsoft Foundry SDK, you can:

    • Connect to a project

    • Retrieve its connections

    • Use those connections to consume services programmatically


Accessing Connections Using the SDK

  • In Python, the AIProjectClient object has a connections property

  • This property lets you list and retrieve resource connections


Important Connection Methods

connections.list()

  • Returns all connection objects in the project

  • Each object represents one connected resource

  • You can filter results using:

    • connection_type parameter

    • Example: ConnectionType.AZURE_OPEN_AI

connections.get(connection_name, include_credentials)

  • Returns a specific connection by name

  • include_credentials:

    • Default: True

    • If True, credentials are included (e.g., API keys for Foundry Tools)


What Connection Objects Contain

  • Connection name

  • Connection type

  • Connection-specific properties

  • Credentials (if included)

  • These details are used to connect to the external resource


Python Example: List All Project Connections

from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient try: # Get project client project_endpoint = "https://....." project_client = AIProjectClient( credential=DefaultAzureCredential(), endpoint=project_endpoint, ) # List all connections in the project connections = project_client.connections print("List all connections:") for connection in connections.list(): print(f"{connection.name} ({connection.type})") except Exception as ex: print(ex)


Summary

  • Project connections link Foundry projects to external Azure services

  • Connections can be accessed using AIProjectClient.connections

  • You can:

    • List all connections

    • Get a specific connection

    • Retrieve credentials if needed


Purpose

  • A common AI app scenario is to:

    • Connect to a generative AI model

    • Use prompts to have a chat-based conversation


Two Ways to Connect to Models

  1. Directly using Azure OpenAI SDK

    • Authentication via:

      • API keys

      • Microsoft Entra ID

  2. Using Microsoft Foundry SDK (Recommended for Foundry projects)

    • Retrieve a project client

    • From it, get an authenticated OpenAI chat client

    • Works for any model deployed in the project


Why Use Microsoft Foundry SDK?

  • Simplifies consuming models deployed in your project

  • Easy to switch between models

    • Just change the model deployment name

  • Automatically handles authentication


Important Tip

  • The OpenAI chat client from Microsoft Foundry can chat with:

    • Azure OpenAI models

    • Non-OpenAI models (e.g., Microsoft Phi models)
      as long as they are deployed in the Foundry resource


Required Python Packages

pip install azure-ai-projects pip install azure-identity pip install openai


Steps to Create a Chat Client

  1. Authenticate using DefaultAzureCredential

  2. Connect to the Microsoft Foundry project

  3. Get an OpenAI chat client using get_openai_client()

  4. Send a prompt and receive a chat completion


Python Example: Create a Chat Client

from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient from openai import AzureOpenAI try: # Connect to the project project_endpoint = "https://......" project_client = AIProjectClient( credential=DefaultAzureCredential(), endpoint=project_endpoint, ) # Get an OpenAI chat client chat_client = project_client.get_openai_client( api_version="2024-10-21" ) # Get user input user_prompt = input("Enter a question:") # Create chat completion response = chat_client.chat.completions.create( model=your_model_deployment_name, messages=[ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": user_prompt} ] ) print(response.choices[0].message.content) except Exception as ex: print(ex)


Key Components

  • project_endpoint → Foundry project endpoint

  • get_openai_client() → Returns authenticated chat client

  • model → Deployment name of the model in Foundry

  • messages → Conversation history (system + user roles)


Summary

  • Microsoft Foundry SDK provides an easy way to chat with deployed models

  • Supports multiple models, including non-OpenAI models

  • Authentication is handled using Azure credentials

  • Ideal for building chat-based generative AI apps

Comments

Popular posts from this blog

AI-900-3,4

AI-900 12,13

AI-900 10,11