Skip to content

Commit c64e51c

Browse files
moonbox3TaoChenOSU
andauthored
Python: Update the AzureAIAgent packages, types, and APIs based on Azure SDK's new code (#12084)
### Motivation and Context The Azure AI Agent Service is moving to GA soon. As part of that, they've made several changes in the underlying APIs, which require the use of new packages, updated type imports and some new underlying APIs. This PR updates the AzureAIAgent to use the latest Azure SDK's new code. - The client's connection string has been replaced with an endpoint. - The `AzureAIAgentSettings`'s `model_deployment_name` was updated to `deployment_name`. - Tools are now imported from `from azure.ai.agents.models` instead of `from azure.ai.projects.models`. - Declarative spec handling for AzureAIAgent was updated based on the new APIs. - Unit tests were updated. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description Update the AzureAIAgent related code, samples and tests based on the Azure SDK's latest APIs. - Closes #12177 <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Tao Chen <taochen@microsoft.com>
1 parent f976643 commit c64e51c

39 files changed

+813
-782
lines changed

.github/workflows/python-integration-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ env:
3232
AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}
3333
AZURE_OPENAI_AUDIO_TO_TEXT_ENDPOINT: ${{ secrets.AZURE_OPENAI_AUDIO_TO_TEXT_ENDPOINT }}
3434
AZURE_OPENAI_TEXT_TO_AUDIO_ENDPOINT: ${{ secrets.AZURE_OPENAI_TEXT_TO_AUDIO_ENDPOINT }}
35-
AZURE_AI_AGENT_PROJECT_CONNECTION_STRING: ${{ secrets.AZURE_AI_AGENT_PROJECT_CONNECTION_STRING }}
35+
AZURE_AI_AGENT_ENDPOINT: ${{ secrets.AZURE_AI_AGENT_ENDPOINT }}
3636
AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME: ${{ secrets.AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME }}
3737
AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME: ${{ vars.AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME }}
3838
BING_API_KEY: ${{ secrets.BING_API_KEY }}

python/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ autogen = [
6767
aws = [
6868
"boto3>=1.36.4,<1.39.0",
6969
]
70+
# This is temporary to get the PR reviewed, and will be replaced when their new version is public.
7071
azure = [
72+
"azure-ai-projects >= 1.0.0b11",
73+
"azure-ai-agents >= 1.0.0",
7174
"azure-ai-inference >= 1.0.0b6",
72-
"azure-ai-projects >= 1.0.0b7, <1.0.0b11",
7375
"azure-core-tracing-opentelemetry >= 1.0.0b11",
7476
"azure-search-documents >= 11.6.0b4",
7577
"azure-cosmos ~= 4.7"

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_as_kernel_function.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ async def main() -> None:
7171

7272
async with (
7373
DefaultAzureCredential() as creds,
74-
AzureAIAgent.create_client(
75-
credential=creds,
76-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
77-
) as client,
74+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
7875
):
7976
# Create the agent definition
8077
agent_definition = await client.agents.create_agent(

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_azure_ai_search.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import asyncio
44
import logging
55

6-
from azure.ai.projects.models import AzureAISearchTool, ConnectionType
6+
from azure.ai.agents.models import AzureAISearchTool
7+
from azure.ai.projects.models import ConnectionType
78
from azure.identity.aio import DefaultAzureCredential
89

910
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
@@ -39,17 +40,12 @@ async def main() -> None:
3940

4041
async with (
4142
DefaultAzureCredential() as creds,
42-
AzureAIAgent.create_client(
43-
credential=creds,
44-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
45-
) as client,
43+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
4644
):
47-
conn_list = await client.connections.list()
48-
4945
ai_search_conn_id = ""
50-
for conn in conn_list:
51-
if conn.connection_type == ConnectionType.AZURE_AI_SEARCH and conn.authentication_type == "ApiKey":
52-
ai_search_conn_id = conn.id
46+
async for connection in client.connections.list():
47+
if connection.type == ConnectionType.AZURE_AI_SEARCH:
48+
ai_search_conn_id = connection.id
5349
break
5450

5551
ai_search = AzureAISearchTool(index_connection_id=ai_search_conn_id, index_name=AZURE_AI_SEARCH_INDEX_NAME)

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_bing_grounding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44

5-
from azure.ai.projects.models import BingGroundingTool
5+
from azure.ai.agents.models import BingGroundingTool
66
from azure.identity.aio import DefaultAzureCredential
77

88
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_bing_grounding_streaming_with_message_callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from functools import reduce
55

6-
from azure.ai.projects.models import BingGroundingTool
6+
from azure.ai.agents.models import BingGroundingTool
77
from azure.identity.aio import DefaultAzureCredential
88

99
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_code_interpreter_streaming_with_message_callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from functools import reduce
55

6-
from azure.ai.projects.models import CodeInterpreterTool
6+
from azure.ai.agents.models import CodeInterpreterTool
77
from azure.identity.aio import DefaultAzureCredential
88

99
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_code_interpreter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
import os
55

6-
from azure.ai.projects.models import FilePurpose
6+
from azure.ai.agents.models import FilePurpose
77
from azure.identity.aio import DefaultAzureCredential
88

99
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
@@ -28,15 +28,15 @@
2828
model:
2929
id: ${AzureAI:ChatModelId}
3030
connection:
31-
connection_string: ${AzureAI:ConnectionString}
31+
endpoint: ${AzureAI:Endpoint}
3232
tools:
3333
- type: code_interpreter
3434
options:
3535
file_ids:
3636
- ${AzureAI:FileId1}
3737
"""
3838

39-
settings = AzureAIAgentSettings() # ChatModelId & ConnectionString come from env vars
39+
settings = AzureAIAgentSettings() # ChatModelId & Endpoint come from env vars
4040

4141

4242
async def main():
@@ -54,7 +54,7 @@ async def main():
5454

5555
try:
5656
# Upload the CSV file to the agent service
57-
file = await client.agents.upload_file_and_poll(file_path=csv_file_path, purpose=FilePurpose.AGENTS)
57+
file = await client.agents.files.upload_and_poll(file_path=csv_file_path, purpose=FilePurpose.AGENTS)
5858

5959
# Create the AzureAI Agent from the YAML spec
6060
# Note: the extras can be provided in the short-format (shown below) or
@@ -100,7 +100,7 @@ async def main():
100100
finally:
101101
# Cleanup: Delete the thread and agent
102102
await client.agents.delete_agent(agent.id)
103-
await client.agents.delete_file(file.id)
103+
await client.agents.files.delete(file.id)
104104

105105
"""
106106
Sample output:

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_file_search.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
import os
55

6-
from azure.ai.projects.models import OpenAIFile, VectorStore
6+
from azure.ai.agents.models import VectorStore
77
from azure.identity.aio import DefaultAzureCredential
88

99
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings
@@ -28,15 +28,15 @@
2828
model:
2929
id: ${AzureAI:ChatModelId}
3030
connection:
31-
connection_string: ${AzureAI:ConnectionString}
31+
endpoint: ${AzureAI:Endpoint}
3232
tools:
3333
- type: file_search
3434
options:
3535
vector_store_ids:
3636
- ${AzureAI:VectorStoreId}
3737
"""
3838

39-
settings = AzureAIAgentSettings() # ChatModelId & ConnectionString come from .env/env vars
39+
settings = AzureAIAgentSettings() # ChatModelId & Endpoint come from .env/env vars
4040

4141

4242
async def main():
@@ -52,10 +52,8 @@ async def main():
5252
"employees.pdf",
5353
)
5454
# Upload the pdf file to the agent service
55-
file: OpenAIFile = await client.agents.upload_file_and_poll(file_path=pdf_file_path, purpose="assistants")
56-
vector_store: VectorStore = await client.agents.create_vector_store_and_poll(
57-
file_ids=[file.id], name="my_vectorstore"
58-
)
55+
file = await client.agents.files.upload_and_poll(file_path=pdf_file_path, purpose="assistants")
56+
vector_store: VectorStore = await client.agents.vector_stores.create(file_ids=[file.id], name="my_vectorstore")
5957

6058
try:
6159
# Create the AzureAI Agent from the YAML spec
@@ -82,8 +80,8 @@ async def main():
8280
finally:
8381
# Cleanup: Delete the agent, vector store, and file
8482
await client.agents.delete_agent(agent.id)
85-
await client.agents.delete_vector_store(vector_store.id)
86-
await client.agents.delete_file(file.id)
83+
await client.agents.vector_stores.delete(vector_store.id)
84+
await client.agents.files.delete(file.id)
8785

8886
"""
8987
Sample output:

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_function_calling_from_file.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ def get_item_price(
3535
return "$9.99"
3636

3737

38-
# Function spec
39-
40-
settings = AzureAIAgentSettings() # The Spec's ChatModelId & ConnectionString come from .env/env vars
41-
42-
4338
async def main():
4439
async with (
4540
DefaultAzureCredential() as creds,
@@ -59,7 +54,7 @@ async def main():
5954
file_path,
6055
plugins=[MenuPlugin()],
6156
client=client,
62-
settings=settings,
57+
settings=AzureAIAgentSettings(), # The Spec's ChatModelId & Endpoint come from .env/env vars
6358
)
6459

6560
# Create the agent

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_declarative_openapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
model:
2525
id: ${AzureAI:ChatModelId}
2626
connection:
27-
connection_string: ${AzureAI:ConnectionString}
27+
endpoint: ${AzureAI:Endpoint}
2828
options:
2929
temperature: 0.4
3030
tools:
@@ -153,7 +153,7 @@
153153
schemes: {}
154154
"""
155155

156-
settings = AzureAIAgentSettings() # ChatModelId & ConnectionString come from .env/env vars
156+
settings = AzureAIAgentSettings() # ChatModelId & Endpoint come from .env/env vars
157157

158158

159159
async def main():

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_file_manipulation.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
import os
55

6-
from azure.ai.projects.models import CodeInterpreterTool, FilePurpose
6+
from azure.ai.agents.models import CodeInterpreterTool, FilePurpose
77
from azure.identity.aio import DefaultAzureCredential
88

99
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
@@ -22,10 +22,7 @@ async def main() -> None:
2222

2323
async with (
2424
DefaultAzureCredential() as creds,
25-
AzureAIAgent.create_client(
26-
credential=creds,
27-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
28-
) as client,
25+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
2926
):
3027
csv_file_path = os.path.join(
3128
os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))),
@@ -34,7 +31,7 @@ async def main() -> None:
3431
"sales.csv",
3532
)
3633

37-
file = await client.agents.upload_file_and_poll(file_path=csv_file_path, purpose=FilePurpose.AGENTS)
34+
file = await client.agents.files.upload_and_poll(file_path=csv_file_path, purpose=FilePurpose.AGENTS)
3835

3936
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
4037

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_message_callback.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ async def main() -> None:
6060

6161
async with (
6262
DefaultAzureCredential() as creds,
63-
AzureAIAgent.create_client(
64-
credential=creds,
65-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
66-
) as client,
63+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
6764
):
6865
AGENT_NAME = "Host"
6966
AGENT_INSTRUCTIONS = "Answer questions about the menu."

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_message_callback_streaming.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ async def main() -> None:
6161

6262
async with (
6363
DefaultAzureCredential() as creds,
64-
AzureAIAgent.create_client(
65-
credential=creds,
66-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
67-
) as client,
64+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
6865
):
6966
# Create agent definition
7067
agent_definition = await client.agents.create_agent(

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_prompt_templating.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ async def invoke_agent_with_template(template_str: str, template_format: str, de
5656

5757
async with (
5858
DefaultAzureCredential() as creds,
59-
AzureAIAgent.create_client(
60-
credential=creds,
61-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
62-
) as client,
59+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
6360
):
6461
# Create agent definition
6562
agent_definition = await client.agents.create_agent(

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_streaming.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ async def main() -> None:
4040

4141
async with (
4242
DefaultAzureCredential() as creds,
43-
AzureAIAgent.create_client(
44-
credential=creds,
45-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
46-
) as client,
43+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
4744
):
4845
AGENT_NAME = "Host"
4946
AGENT_INSTRUCTIONS = "Answer questions about the menu."

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_structured_outputs.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from enum import Enum
55

6-
from azure.ai.projects.models import (
6+
from azure.ai.agents.models import (
77
ResponseFormatJsonSchema,
88
ResponseFormatJsonSchemaType,
99
)
@@ -38,10 +38,7 @@ async def main():
3838
ai_agent_settings = AzureAIAgentSettings()
3939
async with (
4040
DefaultAzureCredential() as creds,
41-
AzureAIAgent.create_client(
42-
credential=creds,
43-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
44-
) as client,
41+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
4542
):
4643
# Create the agent definition
4744
agent_definition = await client.agents.create_agent(

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_truncation_strategy.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44

5-
from azure.ai.projects.models import TruncationObject
5+
from azure.ai.agents.models import TruncationObject
66
from azure.identity.aio import DefaultAzureCredential
77

88
from semantic_kernel.agents import (
@@ -28,10 +28,7 @@ async def main() -> None:
2828

2929
async with (
3030
DefaultAzureCredential() as creds,
31-
AzureAIAgent.create_client(
32-
credential=creds,
33-
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
34-
) as client,
31+
AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
3532
):
3633
# Create the agent definition
3734
agent_definition = await client.agents.create_agent(

python/samples/concepts/resources/declarative_spec/spec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ instructions: Use the provided functions to answer questions about the menu.
55
model:
66
id: ${AzureAI:ChatModelId}
77
connection:
8-
connection_string: ${AzureAI:ConnectionString}
8+
endpoint: ${AzureAI:Endpoint}
99
options:
1010
temperature: 0.4
1111
tools:

0 commit comments

Comments
 (0)