1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: sample_ai.py
DESCRIPTION:
This sample demonstrates using the AzureApp and AzureInfrastructure components
to build and run various AI scenarios.
This sample includes provisioning the following Azure resources which may incur charges:
- Azure Resource Group
- Azure User-Assigned Managed Identity
- Azure App Configuration (SKU: Free)
- Azure Foundry
- Azure AI Project
- Azure AI Chat model (SKU:)
- Azure AI Embeddings model (SKU:)
See pricing: https://azure.microsoft.com/pricing/.
USAGE:
python sample_ai.py
Running the samples requires that Azure Developer CLI be installed and authenticated:
For more information: https://learn.microsoft.com/azure/developer/azure-developer-cli/
"""
import asyncio
import time
from azure.projects import deprovision
unique_suffix = int(time.time())
async def main():
# Creating a chat client and provisioning the model behind it can be achieved in a few lines.
from azure.projects import AzureApp
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import AssistantMessage, UserMessage
class ChatApp(AzureApp):
chat: ChatCompletionsClient
with ChatApp.provision() as app:
response = app.chat.complete(
messages=[ # type: ignore[arg-type]
UserMessage("How many feet are in a mile?"),
],
)
print(response.choices[0].message.content)
print(f"\nToken usage: {response.usage}")
# The Chat model resource is already parameterized with the following parameters that you
# can use to modify the deployment:
# - aiChatModel, default value is 'o1-mini'
# - aiChatModelFormat, default value is 'OpenAI'
# - aiChatModelVersion, default value is '2024-09-12'
# - aiChatModelSku, default value is 'GlobalStandard'
# - aiChatModelCapacity, default value is 1
parameters = {
"aiChatModel": "DeepSeek-V3",
"aiChatModelFormat": "DeepSeek",
"aiChatModelVersion": "1",
}
with ChatApp.provision(parameters=parameters) as app:
response = app.chat.complete(
messages=[ # type: ignore[arg-type]
UserMessage("How many feet are in a mile?"),
]
)
print(response.choices[0].message.content)
print(f"\nToken usage: {response.usage}")
# If you want to change the default model deployment, including defining your own parameters,
# you can update the AIChat resource
from azure.projects import Parameter
from azure.projects.resources.ai.deployment import AIChat
AIChat.DEFAULTS = {
"name": "Phi-4",
"properties": {
"model": {
"name": "Phi-4",
"version": Parameter("Phi4Version"),
"format": "Microsoft",
},
"raiPolicyName": "Microsoft.DefaultV2",
},
"sku": {"name": "GlobalStandard", "capacity": 1},
}
with ChatApp.provision(parameters={"Phi4Version": "7"}) as app:
response = app.chat.complete(
messages=[ # type: ignore[arg-type]
UserMessage("How many feet are in a mile?"),
]
)
print(response.choices[0].message.content)
print(f"\nToken usage: {response.usage}")
deprovision(app, purge=True)
# If we want to have more control over the deployed resources, including adding other resources or
# multiple models, we can create an AzureInfrastructure definition.
from azure.projects import AzureInfrastructure
from azure.projects.resources.storage.blobs.container import BlobContainer
class ChatInfra(AzureInfrastructure):
phi_4: AIChat = AIChat(model="Phi-4", version="7", format="Microsoft")
open_ai: AIChat = AIChat(model="o1-mini", version="2024-09-12", format="OpenAI")
deepseek: AIChat = AIChat(model="DeepSeek-V3", version="1", format="DeepSeek")
data: BlobContainer = BlobContainer()
infra = ChatInfra()
from openai import AzureOpenAI
from azure.storage.blob import ContainerClient
class MultiChatApp(AzureApp):
chat_a: AzureOpenAI # We can also use the OpenAI SDK
chat_b: ChatCompletionsClient
outputs: ContainerClient
# Our custom infra definition has multiple chat models defined, so we will need to provide
# a mapping to specify which resources should be used with which clients.
resource_map = {"chat_a": "open_ai", "chat_b": "phi_4"}
with MultiChatApp.provision(infra, attr_map=resource_map) as multi_chat_app:
question = "How many feet are in a mile?"
print(f"Question: {question}")
openai_response = multi_chat_app.chat_a.chat.completions.create(
messages=[
{
"role": "user",
"content": question,
},
],
model="o1-mini",
)
print(f"OpenAI says: {openai_response.choices[0].message.content}")
phi_response = multi_chat_app.chat_b.complete(
messages=[
UserMessage(question),
AssistantMessage(openai_response.choices[0].message.content),
UserMessage("Do you agree?"),
]
)
print(f"Phi says: {phi_response.choices[0].message.content}")
deprovision(multi_chat_app, purge=True)
# You can also provision models as part of complete Azure Foundry deployment, which could
# use a combination of new or existing resources
from azure.projects.resources.foundry import AIHub, AIProject
from azure.projects.resources.keyvault import KeyVault
from azure.projects.resources.storage.blobs import BlobStorage
from azure.projects.resources.search import SearchService
from azure.ai.projects import AIProjectClient
# When using the AIProject and AIHub resources, Connections will automatically be created
# with other applicable resources within the Infrastructure definition.
class FoundryInfra(AzureInfrastructure):
phi_4: AIChat = AIChat(model="Phi-4", version="7", format="Microsoft")
open_ai: AIChat = AIChat(model="o1-mini", version="2024-09-12", format="OpenAI")
deepseek: AIChat = AIChat(model="DeepSeek-V3", version="1", format="DeepSeek")
vault: KeyVault = KeyVault()
datastore: BlobStorage = BlobStorage()
search: SearchService = SearchService()
hub: AIHub = AIHub()
project: AIProject = AIProject()
my_foundry = FoundryInfra()
class AIProjectApp(AzureApp):
admin: AIProjectClient
with AIProjectApp.provision(my_foundry) as foundry_app:
for connection in foundry_app.admin.connections.list():
print(f"Connection: {connection.name}, {connection.connection_type}, {connection.authentication_type}")
deprovision(foundry_app, purge=True)
if __name__ == "__main__":
asyncio.run(main())
|